[IntegratedTest] Add methods and types for tests
authorJihoon Lee <jhoon.it.lee@samsung.com>
Mon, 12 Oct 2020 08:18:09 +0000 (17:18 +0900)
committerJihoon Lee <jhoon.it.lee@samsung.com>
Mon, 19 Oct 2020 03:04:39 +0000 (12:04 +0900)
**Changes proposed in this PR:**
- Open up getter for layer::num_weights and neuralnet flatGraph
- Use getOutputDimension for some codes
- Add types for future competibility

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

Signed-off-by: Jihoon Lee <jhoon.it.lee@samsung.com>
nntrainer/include/layer.h
nntrainer/include/neuralnet.h
nntrainer/src/neuralnet.cpp

index 8345b1b..a93267e 100644 (file)
@@ -453,6 +453,13 @@ protected:
   }
 
   /**
+   * @brief Get the number of weights
+   *
+   * @return unsigned int number of weights
+   */
+  unsigned int getNumWeights() { return num_weights; }
+
+  /**
    * @brief     weight_list in this layer. This contains trainable weights of
    * layers.
    */
index eb0c894..5d0b1e1 100644 (file)
@@ -71,6 +71,11 @@ class NeuralNetwork {
   friend class ModelLoader; /** access private members of ModelLoader */
 
 public:
+  using NodeType = std::shared_ptr<Layer>; /** Type of a Node */
+  using GraphType = std::vector<NodeType>; /** actual graph type */
+  using FlatGraphType =
+    std::vector<NodeType>; /** topological sorted, iterable 1-D list of nodes */
+
   /**
    * @brief     Constructor of NeuralNetwork Class
    */
@@ -220,10 +225,11 @@ public:
 
   /**
    * @brief     add layer into neural network model
+   * @param[in] layer layer to add
    * @retval #ML_ERROR_NONE Successful.
    * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
    */
-  int addLayer(std::shared_ptr<Layer> layer);
+  int addLayer(NodeType layer);
 
   /**
    * @brief     set optimizer for the neural network model
@@ -239,7 +245,7 @@ public:
    * @retval #ML_ERROR_NONE Successful.
    * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
    */
-  int getLayer(const char *name, std::shared_ptr<Layer> *layer);
+  int getLayer(const char *name, NodeType *layer);
 
   /*
    * @brief     get input dimension of neural network
@@ -254,6 +260,14 @@ public:
   TensorDim getOutputDimension() { return layers.back()->getOutputDimension(); }
 
   /**
+   * @brief get FlatGraph of current graph
+   * @note flat graph contains pointer to the actual nodes, which is not deeply
+   * copied.
+   * @retval flatGraph of the current graph
+   */
+  FlatGraphType getFlatGraph() { return layers; }
+
+  /**
    * @brief     Set loss type for the neural network.
    * @param[in] loss Type of the loss.
    * @retval #ML_ERROR_NONE Successful.
@@ -310,8 +324,7 @@ private:
 
   NetType net_type; /**< Network Type */
 
-  std::vector<std::shared_ptr<Layer>>
-    layers; /**< vector for store layer pointers */
+  GraphType layers; /**< vector for store layer pointers */
 
   std::shared_ptr<DataBuffer> data_buffer; /**< Data Buffer to get Input */
 
@@ -401,7 +414,7 @@ private:
   /**
    * @brief     Ensure that layer has a name
    */
-  void ensureName(std::shared_ptr<Layer> layer, std::string prefix = "");
+  void ensureName(NodeType layer, const std::string &prefix = "");
 
   /**
    * @brief     Swap function for the class
index 1a4a27a..3c5b50b 100644 (file)
@@ -79,7 +79,7 @@ int NeuralNetwork::initLossLayer() {
       return ML_ERROR_NOT_SUPPORTED;
     }
 
-    std::shared_ptr<Layer> act_layer = layers.back();
+    NodeType act_layer = layers.back();
     layers.pop_back();
 
     switch (act_layer->getActivationType()) {
@@ -98,7 +98,7 @@ int NeuralNetwork::initLossLayer() {
   std::shared_ptr<LossLayer> loss_layer = std::make_shared<LossLayer>();
   ensureName(loss_layer);
 
-  loss_layer->setInputDimension(layers.back()->getOutputDimension());
+  loss_layer->setInputDimension(getOutputDimension());
   status = loss_layer->initialize();
   NN_RETURN_STATUS();
 
@@ -411,8 +411,7 @@ int NeuralNetwork::train(std::vector<std::string> values) {
   setBatchSize(batch_size);
 
   /** Setup data buffer properties */
-  status =
-    data_buffer->setClassNum(layers.back()->getOutputDimension().width());
+  status = data_buffer->setClassNum(getOutputDimension().width());
   NN_RETURN_STATUS();
 
   status = data_buffer->setFeatureSize(layers[0]->getInputDimension());
@@ -449,8 +448,7 @@ int NeuralNetwork::train_run() {
     int count = 0;
 
     sharedTensor in = MAKE_SHARED_TENSOR(getInputDimension());
-    sharedTensor label =
-      MAKE_SHARED_TENSOR(layers.back()->getOutputDimension());
+    sharedTensor label = MAKE_SHARED_TENSOR(getOutputDimension());
 
     while (true) {
       if (data_buffer->getDataFromBuffer(nntrainer::BUF_TRAIN, in->getData(),
@@ -558,7 +556,7 @@ int NeuralNetwork::isInitializable() {
   return ML_ERROR_NONE;
 }
 
-int NeuralNetwork::addLayer(std::shared_ptr<Layer> layer) {
+int NeuralNetwork::addLayer(NodeType layer) {
   int status = ML_ERROR_NONE;
 
   if (initialized) {
@@ -608,8 +606,7 @@ int NeuralNetwork::setDataBuffer(std::shared_ptr<DataBuffer> data_buffer) {
   return ML_ERROR_NONE;
 }
 
-void NeuralNetwork::ensureName(std::shared_ptr<Layer> layer,
-                               std::string prefix) {
+void NeuralNetwork::ensureName(NodeType layer, const std::string &prefix) {
   if (layer->getName().empty()) {
     std::set<std::string>::iterator iter;
     std::string name;
@@ -623,7 +620,7 @@ void NeuralNetwork::ensureName(std::shared_ptr<Layer> layer,
   }
 }
 
-int NeuralNetwork::getLayer(const char *name, std::shared_ptr<Layer> *layer) {
+int NeuralNetwork::getLayer(const char *name, NodeType *layer) {
   int status = ML_ERROR_INVALID_PARAMETER;
   std::string name_str(name);