[unittest] Added unittest for train without optimizer
authorParichay Kapoor <pk.kapoor@samsung.com>
Fri, 26 Mar 2021 05:41:39 +0000 (14:41 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 1 Apr 2021 11:13:09 +0000 (20:13 +0900)
Added unittest for training without optimizer and some
corresponding fixes.

**Self evaluation:**
1. Build test: [x]Passed [ ]Failed [ ]Skipped
2. Run test: [x]Passed [ ]Failed [ ]Skipped

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
nntrainer/models/neuralnet.cpp
test/ccapi/unittest_ccapi.cpp

index 81f93b8..1831ce6 100644 (file)
@@ -166,8 +166,6 @@ int NeuralNetwork::initialize() {
 
   ml_logd("initializing neural network, layer size: %d", n_layers);
 
-  opt->initialize();
-
   setBatchSize();
 
   for (unsigned int idx = 0; idx < n_layers; ++idx) {
@@ -212,7 +210,6 @@ int NeuralNetwork::initialize() {
     NN_RETURN_STATUS();
 
     REGISTER_EVENT(l.getName(), lnode.event_key)
-    opt->addOptimizerVariable(l.getWeightsRef());
 
     auto &in_out = manager->trackLayerOutputs(
       l.getType(), l.getName(), l.getOutputDimension(), l.getInputDimension());
@@ -243,6 +240,15 @@ int NeuralNetwork::initialize() {
     }
   }
 
+  // initialize optimizer and related variables
+  if (opt) {
+    opt->initialize();
+    for (unsigned int idx = 0; idx < n_layers; ++idx) {
+      auto &lnode = model_graph.getSortedLayerNode(idx);
+      opt->addOptimizerVariable(lnode.layer->getWeightsRef());
+    }
+  }
+
   // Allocate and initialize weights
   manager->initializeWeights();
   manager->allocateWeights();
@@ -575,6 +581,11 @@ int NeuralNetwork::train(std::vector<std::string> values) {
     return ML_ERROR_INVALID_PARAMETER;
   }
 
+  if (!opt) {
+    ml_loge("Cannot train network without optimizer.");
+    return ML_ERROR_INVALID_PARAMETER;
+  }
+
   status = setTrainConfig(values);
   NN_RETURN_STATUS();
 
index 646b30a..8cfe7b4 100644 (file)
@@ -24,6 +24,8 @@ static const std::string getTestResPath(const std::string &file) {
   return getResPath(file, {"test"});
 }
 
+// Add unittest for train fail without optimizer, but inference pass
+
 /**
  * @brief Neural Network Model Construct Test
  */
@@ -150,43 +152,42 @@ TEST(ccapi_dataset, construct_02_p) {
   EXPECT_NO_THROW(ml::train::createDataset(ml::train::DatasetType::FILE));
 }
 
+static IniSection model_base("Model", "Type = NeuralNetwork"
+                                      " | Epochs = 1"
+                                      " | Loss = cross"
+                                      " | Save_Path = 'model.bin'"
+                                      " | batch_size = 32");
+
+static IniSection optimizer("Optimizer", "Type = adam"
+                                         " | Learning_rate = 0.0001"
+                                         " | Decay_rate = 0.96"
+                                         " | Decay_steps = 1000"
+                                         " | beta1 = 0.9"
+                                         " | beta2 = 0.9999"
+                                         " | epsilon = 1e-7");
+
+static IniSection dataset("Dataset", "BufferSize=100"
+                                     " | TrainData = trainingSet.dat"
+                                     " | ValidData = valSet.dat"
+                                     " | LabelData = label.dat");
+
+static IniSection inputlayer("inputlayer", "Type = input"
+                                           "| Input_Shape = 1:1:62720"
+                                           "| bias_initializer = zeros"
+                                           "| Normalization = true"
+                                           "| Activation = sigmoid");
+
+static IniSection outputlayer("outputlayer", "Type = fully_connected"
+                                             "| input_layers = inputlayer"
+                                             "| Unit = 10"
+                                             "| bias_initializer = zeros"
+                                             "| Activation = softmax");
+
 /**
  * @brief Neural Network Model Training
  */
 TEST(nntrainer_ccapi, train_with_config_01_p) {
   std::unique_ptr<ml::train::Model> model;
-
-  static IniSection model_base("Model", "Type = NeuralNetwork"
-                                        " | Epochs = 1"
-                                        " | Loss = cross"
-                                        " | Save_Path = 'model.bin'"
-                                        " | batch_size = 32");
-
-  static IniSection optimizer("Optimizer", "Type = adam"
-                                           " | Learning_rate = 0.0001"
-                                           " | Decay_rate = 0.96"
-                                           " | Decay_steps = 1000"
-                                           " | beta1 = 0.9"
-                                           " | beta2 = 0.9999"
-                                           " | epsilon = 1e-7");
-
-  static IniSection dataset("Dataset", "BufferSize=100"
-                                       " | TrainData = trainingSet.dat"
-                                       " | ValidData = valSet.dat"
-                                       " | LabelData = label.dat");
-
-  static IniSection inputlayer("inputlayer", "Type = input"
-                                             "| Input_Shape = 1:1:62720"
-                                             "| bias_initializer = zeros"
-                                             "| Normalization = true"
-                                             "| Activation = sigmoid");
-
-  static IniSection outputlayer("outputlayer", "Type = fully_connected"
-                                               "| input_layers = inputlayer"
-                                               "| Unit = 10"
-                                               "| bias_initializer = zeros"
-                                               "| Activation = softmax");
-
   ScopedIni s("test_train_01_p",
               {model_base + "batch_size = 16", optimizer,
                dataset + "-BufferSize", inputlayer, outputlayer});
@@ -376,6 +377,24 @@ TEST(nntrainer_ccapi, train_batch_size_update_after) {
 }
 
 /**
+ * @brief Neural Network Model Training
+ */
+TEST(nntrainer_ccapi, train_with_config_02_n) {
+  std::unique_ptr<ml::train::Model> model;
+  ScopedIni s("test_train_01_p",
+              {model_base + "batch_size = 16", dataset + "-BufferSize",
+               inputlayer, outputlayer});
+
+  EXPECT_NO_THROW(model =
+                    ml::train::createModel(ml::train::ModelType::NEURAL_NET));
+
+  EXPECT_EQ(model->loadFromConfig(s.getIniName()), ML_ERROR_NONE);
+  EXPECT_EQ(model->compile(), ML_ERROR_NONE);
+  EXPECT_EQ(model->initialize(), ML_ERROR_NONE);
+  EXPECT_EQ(model->train(), ML_ERROR_INVALID_PARAMETER);
+}
+
+/**
  * @brief Main gtest
  */
 int main(int argc, char **argv) {