add param random initializers

This commit is contained in:
Roman Grundkiewicz 2016-09-14 18:18:58 +02:00
parent 74626d347f
commit c28ba2e67f
3 changed files with 36 additions and 0 deletions

View File

@ -5,4 +5,5 @@
#include "graph_operators.h"
#include "expressions.h"
#include "expression_operators.h"
#include "param_initializers.h"

34
src/param_initializers.h Normal file
View File

@ -0,0 +1,34 @@
#pragma once
#include <random>
#include <algorithm>
#include <iterator>
#include <functional>
#include "tensor.h"
namespace marian {
void zeros(Tensor t) {
std::vector<float> vals(t.size(), 0.0f);
thrust::copy(vals.begin(), vals.end(), t.begin());
}
void ones(Tensor t) {
std::vector<float> 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<float> vals(t.size());
std::generate(begin(vals), end(vals), gen);
thrust::copy(vals.begin(), vals.end(), t.begin());
}
} // namespace marian

View File

@ -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;