singa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wan...@apache.org
Subject [5/5] incubator-singa git commit: SINGA-120 - Implemented GRU and BPTT
Date Wed, 06 Jan 2016 14:56:18 GMT
SINGA-120 - Implemented GRU and BPTT

check with cpplint


Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/24a1be9a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/24a1be9a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/24a1be9a

Branch: refs/heads/master
Commit: 24a1be9aa19064491710895a36fd93617c2a57da
Parents: 9705f39
Author: WANG Sheng <wangsheng1001@gmail.com>
Authored: Wed Jan 6 22:54:24 2016 +0800
Committer: WANG Sheng <wangsheng1001@gmail.com>
Committed: Wed Jan 6 22:54:24 2016 +0800

----------------------------------------------------------------------
 include/singa/neuralnet/neuron_layer.h |  19 +-
 src/neuralnet/neuron_layer/gru.cc      |   6 +-
 src/test/test_gru_layer.cc             | 460 ++++++++++++++--------------
 3 files changed, 241 insertions(+), 244 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/24a1be9a/include/singa/neuralnet/neuron_layer.h
----------------------------------------------------------------------
diff --git a/include/singa/neuralnet/neuron_layer.h b/include/singa/neuralnet/neuron_layer.h
index 3fe08f4..f03e91b 100644
--- a/include/singa/neuralnet/neuron_layer.h
+++ b/include/singa/neuralnet/neuron_layer.h
@@ -170,7 +170,7 @@ class GRULayer : public NeuronLayer {
   void Setup(const LayerProto& proto, const vector<Layer*>& srclayers) override;
   void ComputeFeature(int flag, const vector<Layer*>& srclayers) override;
   void ComputeGradient(int flag, const vector<Layer*>& srclayers) override;
-  ConnectionType dst_layer_connection() const override{
+  ConnectionType dst_layer_connection() const override {
     return kOneToMany;
   }
   Blob<float>* mutable_grad(const Layer* from) override {
@@ -179,15 +179,14 @@ class GRULayer : public NeuronLayer {
     else
       return gradvec_[0];
   }
-  const Blob<float>& grad(const Layer* from) override{
+  const Blob<float>& grad(const Layer* from) override {
     if (typeid(*from) == typeid(GRULayer))
       return *gradvec_[1];
     else
       return *gradvec_[0];
   }
-
   const std::vector<Param*> GetParams() const override {
-    std::vector<Param*> params{weight_z_hx_, weight_r_hx_,weight_c_hx_,
+    std::vector<Param*> params{weight_z_hx_, weight_r_hx_, weight_c_hx_,
       weight_z_hh_, weight_r_hh_, weight_c_hh_};
 
     if (bias_z_ != nullptr && bias_r_ != nullptr && bias_c_ != nullptr) {
@@ -199,14 +198,12 @@ class GRULayer : public NeuronLayer {
   }
 
  private:
-  int batchsize_; // batch size
-  int vdim_, hdim_; // dimensions
-
+  int batchsize_;  // batch size
+  int vdim_, hdim_;  // dimensions
   Blob<float> *update_gate_, *reset_gate_, *new_memory_;
-
-  Param *weight_z_hx_, *weight_z_hh_, *bias_z_; // update gate
-  Param *weight_r_hx_, *weight_r_hh_, *bias_r_; // reset gate
-  Param *weight_c_hx_, *weight_c_hh_, *bias_c_; // new memory
+  Param *weight_z_hx_, *weight_z_hh_, *bias_z_;  // update gate
+  Param *weight_r_hx_, *weight_r_hh_, *bias_r_;  // reset gate
+  Param *weight_c_hx_, *weight_c_hh_, *bias_c_;  // new memory
 };
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/24a1be9a/src/neuralnet/neuron_layer/gru.cc
----------------------------------------------------------------------
diff --git a/src/neuralnet/neuron_layer/gru.cc b/src/neuralnet/neuron_layer/gru.cc
index d04c11b..440da91 100644
--- a/src/neuralnet/neuron_layer/gru.cc
+++ b/src/neuralnet/neuron_layer/gru.cc
@@ -47,7 +47,7 @@ GRULayer::~GRULayer() {
   delete update_gate_;
   delete reset_gate_;
   delete new_memory_;
-  //delete reset_context_;
+  // delete reset_context_;
 }
 
 void GRULayer::Setup(const LayerProto& conf,
@@ -127,14 +127,14 @@ void GRULayer::ComputeFeature(int flag,
     MVAddRow(1.0f, 1.0f, bias_z_->data(), update_gate_);
   GEMM(1.0f, 1.0f, *context, *w_z_hh_t, update_gate_);
   Map<op::Sigmoid<float>, float>(*update_gate_, update_gate_);
-  //LOG(ERROR) << "Update Gate: " << update_gate_->cpu_data()[0];
+  // LOG(ERROR) << "Update Gate: " << update_gate_->cpu_data()[0];
   // Compute the reset gate
   GEMM(1.0f, 0.0f, src, *w_r_hx_t, reset_gate_);
   if (bias_r_ != nullptr)
     MVAddRow(1.0f, 1.0f, bias_r_->data(), reset_gate_);
   GEMM(1.0f, 1.0f, *context, *w_r_hh_t, reset_gate_);
   Map<op::Sigmoid<float>, float>(*reset_gate_, reset_gate_);
-  //LOG(ERROR) << "Reset Gate: " << reset_gate_->cpu_data()[0];
+  // LOG(ERROR) << "Reset Gate: " << reset_gate_->cpu_data()[0];
   // Compute the new memory
   GEMM(1.0f, 0.0f, *context, *w_c_hh_t, new_memory_);
   Mult<float>(*reset_gate_, *new_memory_, new_memory_);

http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/24a1be9a/src/test/test_gru_layer.cc
----------------------------------------------------------------------
diff --git a/src/test/test_gru_layer.cc b/src/test/test_gru_layer.cc
index 955cc8a..d7c8fe8 100644
--- a/src/test/test_gru_layer.cc
+++ b/src/test/test_gru_layer.cc
@@ -34,254 +34,254 @@ using namespace std;
 using namespace singa;
 
 class GRULayerTest: public ::testing::Test {
-protected:
-	virtual void SetUp() {
-		// Initialize the settings for the first input-layer
-		std::string path1 = "src/test/gru-in-1.csv"; // path of a csv file
-		std::ofstream ofs1(path1, std::ofstream::out);
-		ASSERT_TRUE(ofs1.is_open());
-		ofs1 << "0,0,0,1\n";
-		ofs1 << "0,0,1,0\n";
-		ofs1.close();
-		auto conf1 = in1_conf.mutable_store_conf();
-		conf1->set_path(path1);
-		conf1->set_batchsize(2);
-		conf1->add_shape(4);
-		conf1->set_backend("textfile");
-		conf1->set_has_label(false);
-
-
-		// Initialize the settings for the second input-layer
-		std::string path2 = "src/test/gru-in-2.csv"; // path of a csv file
-		std::ofstream ofs2(path2, std::ofstream::out);
-		ASSERT_TRUE(ofs2.is_open());
-		ofs2 << "0,1,0,0\n";
-		ofs2 << "1,0,0,0\n";
-		ofs2.close();
-		auto conf2 = in2_conf.mutable_store_conf();
-		conf2->set_path(path2);
-
-		conf2->set_batchsize(2);
-		conf2->add_shape(4);
-		conf2->set_backend("textfile");
-		conf2->set_has_label(false);
-
-
-		gru1_conf.mutable_gru_conf() -> set_dim_hidden(2);
-		gru1_conf.mutable_gru_conf() -> set_bias_term(true);
-		for (int i = 0; i < 9; i ++) {
-			gru1_conf.add_param();
-		}
-
-
-		gru1_conf.mutable_param(0)->set_name("wzhx1");
-		gru1_conf.mutable_param(0)->set_type(kParam);
-		gru1_conf.mutable_param(0)->mutable_init()->set_type(kConstant);
-		gru1_conf.mutable_param(0)->mutable_init()->set_value(0.5f);
-
-		gru1_conf.mutable_param(1)->set_name("wrhx1");
-		gru1_conf.mutable_param(1)->set_type(kParam);
-		gru1_conf.mutable_param(1)->mutable_init()->set_type(kConstant);
-		gru1_conf.mutable_param(1)->mutable_init()->set_value(0.5f);
-
-		gru1_conf.mutable_param(2)->set_name("wchx1");
-		gru1_conf.mutable_param(2)->set_type(kParam);
-		gru1_conf.mutable_param(2)->mutable_init()->set_type(kConstant);
-		gru1_conf.mutable_param(2)->mutable_init()->set_value(0.5f);
-
-		gru1_conf.mutable_param(3)->set_name("wzhh1");
-		gru1_conf.mutable_param(3)->set_type(kParam);
-		gru1_conf.mutable_param(3)->mutable_init()->set_type(kConstant);
-		gru1_conf.mutable_param(3)->mutable_init()->set_value(0.5f);
-
-		gru1_conf.mutable_param(4)->set_name("wrhh1");
-		gru1_conf.mutable_param(4)->set_type(kParam);
-		gru1_conf.mutable_param(4)->mutable_init()->set_type(kConstant);
-		gru1_conf.mutable_param(4)->mutable_init()->set_value(0.5f);
-
-		gru1_conf.mutable_param(5)->set_name("wchh1");
-		gru1_conf.mutable_param(5)->set_type(kParam);
-		gru1_conf.mutable_param(5)->mutable_init()->set_type(kConstant);
-		gru1_conf.mutable_param(5)->mutable_init()->set_value(0.5f);
-
-		gru1_conf.mutable_param(6)->set_name("bz1");
-		gru1_conf.mutable_param(6)->set_type(kParam);
-		gru1_conf.mutable_param(6)->mutable_init()->set_type(kConstant);
-		gru1_conf.mutable_param(6)->mutable_init()->set_value(0.5f);
-
-		gru1_conf.mutable_param(7)->set_name("br1");
-		gru1_conf.mutable_param(7)->set_type(kParam);
-		gru1_conf.mutable_param(7)->mutable_init()->set_type(kConstant);
-		gru1_conf.mutable_param(7)->mutable_init()->set_value(0.5f);
-
-		gru1_conf.mutable_param(8)->set_name("bc1");
-		gru1_conf.mutable_param(8)->set_type(kParam);
-		gru1_conf.mutable_param(8)->mutable_init()->set_type(kConstant);
-		gru1_conf.mutable_param(8)->mutable_init()->set_value(0.5f);
-
-		gru2_conf.mutable_gru_conf() -> set_dim_hidden(2);
-		gru2_conf.mutable_gru_conf() -> set_bias_term(true);
-		for (int i = 0; i < 9; i ++) {
-			gru2_conf.add_param();
-		}
-
-		gru2_conf.mutable_param(0)->set_name("wzhx2");
-		gru2_conf.mutable_param(0)->set_type(kParam);
-		gru2_conf.mutable_param(0)->mutable_init()->set_type(kConstant);
-		gru2_conf.mutable_param(0)->mutable_init()->set_value(0.5f);
-
-		gru2_conf.mutable_param(1)->set_name("wrhx2");
-		gru2_conf.mutable_param(1)->set_type(kParam);
-		gru2_conf.mutable_param(1)->mutable_init()->set_type(kConstant);
-		gru2_conf.mutable_param(1)->mutable_init()->set_value(0.5f);
-
-		gru2_conf.mutable_param(2)->set_name("wchx2");
-		gru2_conf.mutable_param(2)->set_type(kParam);
-		gru2_conf.mutable_param(2)->mutable_init()->set_type(kConstant);
-		gru2_conf.mutable_param(2)->mutable_init()->set_value(0.5f);
-
-		gru2_conf.mutable_param(3)->set_name("wzhh2");
-		gru2_conf.mutable_param(3)->set_type(kParam);
-		gru2_conf.mutable_param(3)->mutable_init()->set_type(kConstant);
-		gru2_conf.mutable_param(3)->mutable_init()->set_value(0.5f);
-
-		gru2_conf.mutable_param(4)->set_name("wrhh2");
-		gru2_conf.mutable_param(4)->set_type(kParam);
-		gru2_conf.mutable_param(4)->mutable_init()->set_type(kConstant);
-		gru2_conf.mutable_param(4)->mutable_init()->set_value(0.5f);
-
-		gru2_conf.mutable_param(5)->set_name("wchh2");
-		gru2_conf.mutable_param(5)->set_type(kParam);
-		gru2_conf.mutable_param(5)->mutable_init()->set_type(kConstant);
-		gru2_conf.mutable_param(5)->mutable_init()->set_value(0.5f);
-
-		gru2_conf.mutable_param(6)->set_name("bz2");
-		gru2_conf.mutable_param(6)->set_type(kParam);
-		gru2_conf.mutable_param(6)->mutable_init()->set_type(kConstant);
-		gru2_conf.mutable_param(6)->mutable_init()->set_value(0.5f);
-
-		gru2_conf.mutable_param(7)->set_name("br2");
-		gru2_conf.mutable_param(7)->set_type(kParam);
-		gru2_conf.mutable_param(7)->mutable_init()->set_type(kConstant);
-		gru2_conf.mutable_param(7)->mutable_init()->set_value(0.5f);
-
-		gru2_conf.mutable_param(8)->set_name("bc2");
-		gru2_conf.mutable_param(8)->set_type(kParam);
-		gru2_conf.mutable_param(8)->mutable_init()->set_type(kConstant);
-		gru2_conf.mutable_param(8)->mutable_init()->set_value(0.5f);
-
-	}
-	singa::LayerProto in1_conf;
-	singa::LayerProto in2_conf;
-	singa::LayerProto gru1_conf;
-	singa::LayerProto gru2_conf;
+ protected:
+  virtual void SetUp() {
+    // Initialize the settings for the first input-layer
+    std::string path1 = "src/test/gru-in-1.csv";  // path of a csv file
+    std::ofstream ofs1(path1, std::ofstream::out);
+    ASSERT_TRUE(ofs1.is_open());
+    ofs1 << "0,0,0,1\n";
+    ofs1 << "0,0,1,0\n";
+    ofs1.close();
+    auto conf1 = in1_conf.mutable_store_conf();
+    conf1->set_path(path1);
+    conf1->set_batchsize(2);
+    conf1->add_shape(4);
+    conf1->set_backend("textfile");
+    conf1->set_has_label(false);
+
+
+    // Initialize the settings for the second input-layer
+    std::string path2 = "src/test/gru-in-2.csv";  // path of a csv file
+    std::ofstream ofs2(path2, std::ofstream::out);
+    ASSERT_TRUE(ofs2.is_open());
+    ofs2 << "0,1,0,0\n";
+    ofs2 << "1,0,0,0\n";
+    ofs2.close();
+    auto conf2 = in2_conf.mutable_store_conf();
+    conf2->set_path(path2);
+
+    conf2->set_batchsize(2);
+    conf2->add_shape(4);
+    conf2->set_backend("textfile");
+    conf2->set_has_label(false);
+
+
+    gru1_conf.mutable_gru_conf() -> set_dim_hidden(2);
+    gru1_conf.mutable_gru_conf() -> set_bias_term(true);
+    for (int i = 0; i < 9; i ++) {
+      gru1_conf.add_param();
+    }
+
+
+    gru1_conf.mutable_param(0)->set_name("wzhx1");
+    gru1_conf.mutable_param(0)->set_type(kParam);
+    gru1_conf.mutable_param(0)->mutable_init()->set_type(kConstant);
+    gru1_conf.mutable_param(0)->mutable_init()->set_value(0.5f);
+
+    gru1_conf.mutable_param(1)->set_name("wrhx1");
+    gru1_conf.mutable_param(1)->set_type(kParam);
+    gru1_conf.mutable_param(1)->mutable_init()->set_type(kConstant);
+    gru1_conf.mutable_param(1)->mutable_init()->set_value(0.5f);
+
+    gru1_conf.mutable_param(2)->set_name("wchx1");
+    gru1_conf.mutable_param(2)->set_type(kParam);
+    gru1_conf.mutable_param(2)->mutable_init()->set_type(kConstant);
+    gru1_conf.mutable_param(2)->mutable_init()->set_value(0.5f);
+
+    gru1_conf.mutable_param(3)->set_name("wzhh1");
+    gru1_conf.mutable_param(3)->set_type(kParam);
+    gru1_conf.mutable_param(3)->mutable_init()->set_type(kConstant);
+    gru1_conf.mutable_param(3)->mutable_init()->set_value(0.5f);
+
+    gru1_conf.mutable_param(4)->set_name("wrhh1");
+    gru1_conf.mutable_param(4)->set_type(kParam);
+    gru1_conf.mutable_param(4)->mutable_init()->set_type(kConstant);
+    gru1_conf.mutable_param(4)->mutable_init()->set_value(0.5f);
+
+    gru1_conf.mutable_param(5)->set_name("wchh1");
+    gru1_conf.mutable_param(5)->set_type(kParam);
+    gru1_conf.mutable_param(5)->mutable_init()->set_type(kConstant);
+    gru1_conf.mutable_param(5)->mutable_init()->set_value(0.5f);
+
+    gru1_conf.mutable_param(6)->set_name("bz1");
+    gru1_conf.mutable_param(6)->set_type(kParam);
+    gru1_conf.mutable_param(6)->mutable_init()->set_type(kConstant);
+    gru1_conf.mutable_param(6)->mutable_init()->set_value(0.5f);
+
+    gru1_conf.mutable_param(7)->set_name("br1");
+    gru1_conf.mutable_param(7)->set_type(kParam);
+    gru1_conf.mutable_param(7)->mutable_init()->set_type(kConstant);
+    gru1_conf.mutable_param(7)->mutable_init()->set_value(0.5f);
+
+    gru1_conf.mutable_param(8)->set_name("bc1");
+    gru1_conf.mutable_param(8)->set_type(kParam);
+    gru1_conf.mutable_param(8)->mutable_init()->set_type(kConstant);
+    gru1_conf.mutable_param(8)->mutable_init()->set_value(0.5f);
+
+    gru2_conf.mutable_gru_conf() -> set_dim_hidden(2);
+    gru2_conf.mutable_gru_conf() -> set_bias_term(true);
+    for (int i = 0; i < 9; i ++) {
+      gru2_conf.add_param();
+    }
+
+    gru2_conf.mutable_param(0)->set_name("wzhx2");
+    gru2_conf.mutable_param(0)->set_type(kParam);
+    gru2_conf.mutable_param(0)->mutable_init()->set_type(kConstant);
+    gru2_conf.mutable_param(0)->mutable_init()->set_value(0.5f);
+
+    gru2_conf.mutable_param(1)->set_name("wrhx2");
+    gru2_conf.mutable_param(1)->set_type(kParam);
+    gru2_conf.mutable_param(1)->mutable_init()->set_type(kConstant);
+    gru2_conf.mutable_param(1)->mutable_init()->set_value(0.5f);
+
+    gru2_conf.mutable_param(2)->set_name("wchx2");
+    gru2_conf.mutable_param(2)->set_type(kParam);
+    gru2_conf.mutable_param(2)->mutable_init()->set_type(kConstant);
+    gru2_conf.mutable_param(2)->mutable_init()->set_value(0.5f);
+
+    gru2_conf.mutable_param(3)->set_name("wzhh2");
+    gru2_conf.mutable_param(3)->set_type(kParam);
+    gru2_conf.mutable_param(3)->mutable_init()->set_type(kConstant);
+    gru2_conf.mutable_param(3)->mutable_init()->set_value(0.5f);
+
+    gru2_conf.mutable_param(4)->set_name("wrhh2");
+    gru2_conf.mutable_param(4)->set_type(kParam);
+    gru2_conf.mutable_param(4)->mutable_init()->set_type(kConstant);
+    gru2_conf.mutable_param(4)->mutable_init()->set_value(0.5f);
+
+    gru2_conf.mutable_param(5)->set_name("wchh2");
+    gru2_conf.mutable_param(5)->set_type(kParam);
+    gru2_conf.mutable_param(5)->mutable_init()->set_type(kConstant);
+    gru2_conf.mutable_param(5)->mutable_init()->set_value(0.5f);
+
+    gru2_conf.mutable_param(6)->set_name("bz2");
+    gru2_conf.mutable_param(6)->set_type(kParam);
+    gru2_conf.mutable_param(6)->mutable_init()->set_type(kConstant);
+    gru2_conf.mutable_param(6)->mutable_init()->set_value(0.5f);
+
+    gru2_conf.mutable_param(7)->set_name("br2");
+    gru2_conf.mutable_param(7)->set_type(kParam);
+    gru2_conf.mutable_param(7)->mutable_init()->set_type(kConstant);
+    gru2_conf.mutable_param(7)->mutable_init()->set_value(0.5f);
+
+    gru2_conf.mutable_param(8)->set_name("bc2");
+    gru2_conf.mutable_param(8)->set_type(kParam);
+    gru2_conf.mutable_param(8)->mutable_init()->set_type(kConstant);
+    gru2_conf.mutable_param(8)->mutable_init()->set_value(0.5f);
+  }
+  singa::LayerProto in1_conf;
+  singa::LayerProto in2_conf;
+  singa::LayerProto gru1_conf;
+  singa::LayerProto gru2_conf;
 };
 
 TEST_F(GRULayerTest, Setup) {
-	singa::Driver driver;
-	//driver.RegisterLayer<GRULayer, int> (kGRU);
-	driver.RegisterParam<Param>(0);
-	driver.RegisterParamGenerator<UniformGen>(kUniform);
-	driver.RegisterParamGenerator<ParamGenerator>(kConstant);
-
-	singa::CSVInputLayer in_layer_1;
-	singa::CSVInputLayer in_layer_2;
-
-	in_layer_1.Setup(in1_conf, std::vector<singa::Layer*> { });
-	EXPECT_EQ(2, static_cast<int>(in_layer_1.aux_data().size()));
-	EXPECT_EQ(8, in_layer_1.data(nullptr).count());
-
-	in_layer_2.Setup(in2_conf, std::vector<singa::Layer*>{ });
-	EXPECT_EQ(2, static_cast<int>(in_layer_2.aux_data().size()));
-	EXPECT_EQ(8, in_layer_2.data(nullptr).count());
-
-	singa::GRULayer gru_layer_1;
-	gru_layer_1.Setup(gru1_conf, std::vector<singa::Layer*>{&in_layer_1});
-	//EXPECT_EQ(2, gru_layer_1.hdim());
-	//EXPECT_EQ(4, gru_layer_1.vdim());
-
-	for (unsigned int i = 0; i < gru_layer_1.GetParams().size(); i ++) {
-		gru_layer_1.GetParams()[i]->InitValues();
-	}
-	EXPECT_EQ (0.5, gru_layer_1.GetParams()[0]->data().cpu_data()[0]);
-	//cout << "gru_layer_1: " << gru_layer_1.GetParams()[0]->data().cpu_data()[0]
<< endl;
-
-	singa::GRULayer gru_layer_2;
-	gru_layer_2.Setup(gru2_conf, std::vector<singa::Layer*>{&in_layer_2, &gru_layer_1});
-	//EXPECT_EQ(2, gru_layer_2.hdim());
-	//EXPECT_EQ(4, gru_layer_2.vdim());
-	for (unsigned int i = 0; i < gru_layer_2.GetParams().size(); i ++) {
-		gru_layer_2.GetParams()[i]->InitValues();
-	}
-	EXPECT_EQ (0.5, gru_layer_2.GetParams()[0]->data().cpu_data()[0]);
+  singa::Driver driver;
+  // driver.RegisterLayer<GRULayer, int> (kGRU);
+  driver.RegisterParam<Param>(0);
+  driver.RegisterParamGenerator<UniformGen>(kUniform);
+  driver.RegisterParamGenerator<ParamGenerator>(kConstant);
+
+  singa::CSVInputLayer in_layer_1;
+  singa::CSVInputLayer in_layer_2;
+
+  in_layer_1.Setup(in1_conf, std::vector<singa::Layer*> { });
+  EXPECT_EQ(2, static_cast<int>(in_layer_1.aux_data().size()));
+  EXPECT_EQ(8, in_layer_1.data(nullptr).count());
+
+  in_layer_2.Setup(in2_conf, std::vector<singa::Layer*>{ });
+  EXPECT_EQ(2, static_cast<int>(in_layer_2.aux_data().size()));
+  EXPECT_EQ(8, in_layer_2.data(nullptr).count());
+
+  singa::GRULayer gru_layer_1;
+  gru_layer_1.Setup(gru1_conf, std::vector<singa::Layer*>{&in_layer_1});
+  // EXPECT_EQ(2, gru_layer_1.hdim());
+  // EXPECT_EQ(4, gru_layer_1.vdim());
+
+  for (unsigned int i = 0; i < gru_layer_1.GetParams().size(); i ++) {
+    gru_layer_1.GetParams()[i]->InitValues();
+  }
+  EXPECT_EQ (0.5, gru_layer_1.GetParams()[0]->data().cpu_data()[0]);
+  // cout << "gru_layer_1: " << gru_layer_1.GetParams()[0]->data().cpu_data()[0]
+  // << endl;
+
+  singa::GRULayer gru_layer_2;
+  gru_layer_2.Setup(gru2_conf,
+                    std::vector<singa::Layer*>{&in_layer_2, &gru_layer_1});
+  // EXPECT_EQ(2, gru_layer_2.hdim());
+  // EXPECT_EQ(4, gru_layer_2.vdim());
+  for (unsigned int i = 0; i < gru_layer_2.GetParams().size(); i ++) {
+    gru_layer_2.GetParams()[i]->InitValues();
+  }
+  EXPECT_EQ (0.5, gru_layer_2.GetParams()[0]->data().cpu_data()[0]);
 }
 
 
 /*
 TEST_F(GRULayerTest, ComputeFeature) {
-	singa::CSVInputLayer in_layer_1;
-	singa::CSVInputLayer in_layer_2;
-
-	in_layer_1.Setup(in1_conf, std::vector<singa::Layer*> { });
-	in_layer_1.ComputeFeature(singa::kTrain, std::vector<singa::Layer*> { });
-	in_layer_2.Setup(in2_conf, std::vector<singa::Layer*>{ });
-	in_layer_2.ComputeFeature(singa::kTrain, std::vector<singa::Layer*> { });
-
-
-	singa::GRULayer gru_layer_1;
-	gru_layer_1.Setup(gru1_conf, std::vector<singa::Layer*>{&in_layer_1});
-	for (unsigned int i = 0; i < gru_layer_1.GetParams().size(); i ++) {
-		gru_layer_1.GetParams()[i]->InitValues();
-	}
-	gru_layer_1.ComputeFeature(singa::kTrain, std::vector<singa::Layer*>{&in_layer_1});
-	for (int i = 0; i < gru_layer_1.data(nullptr).count(); i ++) {
-		EXPECT_GT(0.000001,abs(0.204824-gru_layer_1.data(nullptr).cpu_data()[i]));
-	}
-
-	singa::GRULayer gru_layer_2;
-	gru_layer_2.Setup(gru2_conf, std::vector<singa::Layer*>{&in_layer_2, &gru_layer_1});
-	for (unsigned int i = 0; i < gru_layer_2.GetParams().size(); i ++) {
-		gru_layer_2.GetParams()[i]->InitValues();
-	}
-	gru_layer_2.ComputeFeature(singa::kTrain, std::vector<singa::Layer*>{&in_layer_2,
&gru_layer_1});
-	for (int i = 0; i < gru_layer_2.data(nullptr).count(); i ++) {
-		EXPECT_GT(0.000001,abs(0.346753-gru_layer_2.data(nullptr).cpu_data()[i]));
-	}
+  singa::CSVInputLayer in_layer_1;
+  singa::CSVInputLayer in_layer_2;
+
+  in_layer_1.Setup(in1_conf, std::vector<singa::Layer*> { });
+  in_layer_1.ComputeFeature(singa::kTrain, std::vector<singa::Layer*> { });
+  in_layer_2.Setup(in2_conf, std::vector<singa::Layer*>{ });
+  in_layer_2.ComputeFeature(singa::kTrain, std::vector<singa::Layer*> { });
+
+
+  singa::GRULayer gru_layer_1;
+  gru_layer_1.Setup(gru1_conf, std::vector<singa::Layer*>{&in_layer_1});
+  for (unsigned int i = 0; i < gru_layer_1.GetParams().size(); i ++) {
+    gru_layer_1.GetParams()[i]->InitValues();
+  }
+  gru_layer_1.ComputeFeature(singa::kTrain, std::vector<singa::Layer*>{&in_layer_1});
+  for (int i = 0; i < gru_layer_1.data(nullptr).count(); i ++) {
+    EXPECT_GT(0.000001,abs(0.204824-gru_layer_1.data(nullptr).cpu_data()[i]));
+  }
+
+  singa::GRULayer gru_layer_2;
+  gru_layer_2.Setup(gru2_conf, std::vector<singa::Layer*>{&in_layer_2, &gru_layer_1});
+  for (unsigned int i = 0; i < gru_layer_2.GetParams().size(); i ++) {
+    gru_layer_2.GetParams()[i]->InitValues();
+  }
+  gru_layer_2.ComputeFeature(singa::kTrain, std::vector<singa::Layer*>{&in_layer_2,
&gru_layer_1});
+  for (int i = 0; i < gru_layer_2.data(nullptr).count(); i ++) {
+    EXPECT_GT(0.000001,abs(0.346753-gru_layer_2.data(nullptr).cpu_data()[i]));
+  }
 }
 
-/*
 TEST_F(GRULayerTest, ComputeGradient) {
-	singa::CSVInputLayer in_layer_1;
-	singa::CSVInputLayer in_layer_2;
+  singa::CSVInputLayer in_layer_1;
+  singa::CSVInputLayer in_layer_2;
 
-	in_layer_1.Setup(in1_conf, std::vector<singa::Layer*> { });
-	in_layer_1.ComputeFeature(singa::kTrain, std::vector<singa::Layer*> { });
-	in_layer_2.Setup(in2_conf, std::vector<singa::Layer*>{ });
-	in_layer_2.ComputeFeature(singa::kTrain, std::vector<singa::Layer*> { });
+  in_layer_1.Setup(in1_conf, std::vector<singa::Layer*> { });
+  in_layer_1.ComputeFeature(singa::kTrain, std::vector<singa::Layer*> { });
+  in_layer_2.Setup(in2_conf, std::vector<singa::Layer*>{ });
+  in_layer_2.ComputeFeature(singa::kTrain, std::vector<singa::Layer*> { });
 
 
-	singa::GRULayer gru_layer_1;
-	gru_layer_1.Setup(gru1_conf, std::vector<singa::Layer*>{&in_layer_1});
-	for (unsigned int i = 0; i < gru_layer_1.GetParams().size(); i ++) {
-		gru_layer_1.GetParams()[i]->InitValues();
-	}
-	gru_layer_1.ComputeFeature(singa::kTrain, std::vector<singa::Layer*>{&in_layer_1});
+  singa::GRULayer gru_layer_1;
+  gru_layer_1.Setup(gru1_conf, std::vector<singa::Layer*>{&in_layer_1});
+  for (unsigned int i = 0; i < gru_layer_1.GetParams().size(); i ++) {
+    gru_layer_1.GetParams()[i]->InitValues();
+  }
+  gru_layer_1.ComputeFeature(singa::kTrain, std::vector<singa::Layer*>{&in_layer_1});
 
 
-	singa::GRULayer gru_layer_2;
-	gru_layer_2.Setup(gru2_conf, std::vector<singa::Layer*>{&in_layer_2, &gru_layer_1});
-	for (unsigned int i = 0; i < gru_layer_2.GetParams().size(); i ++) {
-		gru_layer_2.GetParams()[i]->InitValues();
-	}
-	gru_layer_2.ComputeFeature(singa::kTrain, std::vector<singa::Layer*>{&in_layer_2,
&gru_layer_1});
+  singa::GRULayer gru_layer_2;
+  gru_layer_2.Setup(gru2_conf, std::vector<singa::Layer*>{&in_layer_2, &gru_layer_1});
+  for (unsigned int i = 0; i < gru_layer_2.GetParams().size(); i ++) {
+    gru_layer_2.GetParams()[i]->InitValues();
+  }
+  gru_layer_2.ComputeFeature(singa::kTrain, std::vector<singa::Layer*>{&in_layer_2,
&gru_layer_1});
 
-	// For test purpose, we set dummy values for gru_layer_2.grad_
-	for (int i = 0; i < gru_layer_2.grad(nullptr).count(); i ++) {
-		gru_layer_2.mutable_grad(nullptr)->mutable_cpu_data()[i] = 1.0f;
-	}
-	gru_layer_2.ComputeGradient(singa::kTrain, std::vector<singa::Layer*>{&in_layer_2,
&gru_layer_1});
+  // For test purpose, we set dummy values for gru_layer_2.grad_
+  for (int i = 0; i < gru_layer_2.grad(nullptr).count(); i ++) {
+    gru_layer_2.mutable_grad(nullptr)->mutable_cpu_data()[i] = 1.0f;
+  }
+  gru_layer_2.ComputeGradient(singa::kTrain, std::vector<singa::Layer*>{&in_layer_2,
&gru_layer_1});
 
-	gru_layer_1.ComputeGradient(singa::kTrain, std::vector<singa::Layer*>{&in_layer_1});
+  gru_layer_1.ComputeGradient(singa::kTrain, std::vector<singa::Layer*>{&in_layer_1});
 
 }
 */


Mime
View raw message