From c28ba2e67f62aad1d9a3acbe6a15d82ac2bb9792 Mon Sep 17 00:00:00 2001 From: Roman Grundkiewicz Date: Wed, 14 Sep 2016 18:18:58 +0200 Subject: [PATCH] add param random initializers --- src/marian.h | 1 + src/param_initializers.h | 34 ++++++++++++++++++++++++++++++++++ src/test.cu | 1 + 3 files changed, 36 insertions(+) create mode 100644 src/param_initializers.h diff --git a/src/marian.h b/src/marian.h index 8c987ccf..0876d4cd 100644 --- a/src/marian.h +++ b/src/marian.h @@ -5,4 +5,5 @@ #include "graph_operators.h" #include "expressions.h" #include "expression_operators.h" +#include "param_initializers.h" diff --git a/src/param_initializers.h b/src/param_initializers.h new file mode 100644 index 00000000..ab781064 --- /dev/null +++ b/src/param_initializers.h @@ -0,0 +1,34 @@ +#pragma once + +#include +#include +#include +#include + +#include "tensor.h" + +namespace marian { + +void zeros(Tensor t) { + std::vector vals(t.size(), 0.0f); + thrust::copy(vals.begin(), vals.end(), t.begin()); +} + +void ones(Tensor t) { + std::vector vals(t.size(), 1.0f); + thrust::copy(vals.begin(), vals.end(), t.begin()); +} + +void randreal(Tensor t) { + std::random_device device; + std::default_random_engine engine(device()); + std::uniform_real_distribution<> dist(0, 1); + auto gen = std::bind(dist, engine); + + std::vector vals(t.size()); + std::generate(begin(vals), end(vals), gen); + + thrust::copy(vals.begin(), vals.end(), t.begin()); +} + +} // namespace marian diff --git a/src/test.cu b/src/test.cu index 0285e3a5..a86c60ee 100644 --- a/src/test.cu +++ b/src/test.cu @@ -20,6 +20,7 @@ int main(int argc, char** argv) { Expr y = input(shape={whatevs, LABEL_SIZE}, name="Y"); Expr w = param(shape={IMAGE_SIZE, LABEL_SIZE}, name="W0"); + // Expr w = param(shape={IMAGE_SIZE, LABEL_SIZE}, name="W0", init=randreal); Expr b = param(shape={1, LABEL_SIZE}, name="b0"); Expr z = dot(x, w) + b;