singa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wang...@apache.org
Subject [2/5] incubator-singa git commit: SINGA-41 Support single node single GPU training
Date Tue, 29 Sep 2015 03:23:53 GMT
http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/cb30eadc/src/utils/updater.cc
----------------------------------------------------------------------
diff --git a/src/utils/updater.cc b/src/utils/updater.cc
index a1742c4..229d1d2 100644
--- a/src/utils/updater.cc
+++ b/src/utils/updater.cc
@@ -28,15 +28,21 @@
 
 namespace singa {
 
-using mshadow::cpu;
 using mshadow::expr::F;
 using mshadow::op::sqrtop;
 using mshadow::op::square;
-using mshadow::Shape;
-using mshadow::Shape1;
 using mshadow::Tensor;
 using mshadow::TensorContainer;
 
+using mshadow::cpu;
+using mshadow::Shape1;
+
+inline Tensor<cpu, 1> UTensor1(Blob<float>* blob){
+  Tensor<cpu, 1> tensor(blob->mutable_cpu_data(), Shape1(blob->count()));
+  return tensor;
+}
+
+/*******************************************************/
 LRGenerator* LRGenerator::Create(const LRGenProto& proto) {
   auto factory = Singleton<Factory<LRGenerator>>::Instance();
   LRGenerator* gen = nullptr;
@@ -101,9 +107,8 @@ void Updater::Init(const UpdaterProto& proto) {
 }
 
 void SGDUpdater::Update(int step, Param* param, float grad_scale) {
-  Shape<1> s = Shape1(param->size());
-  Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
-  Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
+  auto data = UTensor1(param->mutable_data());
+  auto grad = UTensor1(param->mutable_grad());
   float lr = lr_gen_->Get(step) * param->lr_scale();
   float wd = weight_decay_ * param->wd_scale();
   if (grad_scale != 1.f)
@@ -111,7 +116,7 @@ void SGDUpdater::Update(int step, Param* param, float grad_scale) {
   if (wd > 0)  // L2 regularization, should be done after timing grad_scale
     grad += data * wd;
   if (momentum_ > 0) {
-    Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
+    auto history = UTensor1(param->mutable_history());
     history = history * momentum_ - lr * grad;
     data += history;
   } else {
@@ -120,13 +125,14 @@ void SGDUpdater::Update(int step, Param* param, float grad_scale) {
   }
 }
 
+
+
 /***********************Nesterov******************************/
 void NesterovUpdater::Update(int step, Param* param, float grad_scale) {
-  Shape<1> s = Shape1(param->size());
-  Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
-  Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
-  Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
-  TensorContainer<cpu, 1> tmp(s);
+  auto data = UTensor1(param->mutable_data());
+  auto grad = UTensor1(param->mutable_grad());
+  auto history = UTensor1(param->mutable_history());
+  TensorContainer<cpu, 1> tmp(data.shape);
   float lr = lr_gen_->Get(step)*param->lr_scale();
   float wd = weight_decay_*param->wd_scale();
   if (grad_scale != 1.f)
@@ -140,10 +146,10 @@ void NesterovUpdater::Update(int step, Param* param, float grad_scale)
{
 }
 /***********************AdaGrad******************************/
 void AdaGradUpdater::Update(int step, Param* param, float grad_scale) {
-  Shape<1> s = Shape1(param->size());
-  Tensor<cpu, 1> data(param->mutable_cpu_data(), s);
-  Tensor<cpu, 1> grad(param->mutable_cpu_grad(), s);
-  Tensor<cpu, 1> history(param->mutable_cpu_history(), s);
+  auto data = UTensor1(param->mutable_data());
+  auto grad = UTensor1(param->mutable_grad());
+  auto history = UTensor1(param->mutable_history());
+
   float lr = lr_gen_->Get(step)*param->lr_scale();
   float wd = weight_decay_*param->wd_scale();
   if (grad_scale != 1.f)


Mime
View raw message