[Bug/Act] Fix setActivation call properly
authorJihoon Lee <jhoon.it.lee@samsung.com>
Mon, 19 Oct 2020 11:00:22 +0000 (20:00 +0900)
committerJihoon Lee <jhoon.it.lee@samsung.com>
Tue, 20 Oct 2020 10:41:24 +0000 (19:41 +0900)
`Act::setActivation` should be called for activationLayer.
However, because `virtual Layer::setActivation` had diffrent signature
`Act::setActivation`, There was no way this function can be called.

This patch fixes the issue.

**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/src/activation_layer.cpp
nntrainer/src/layer.cpp

index a93267e..1207bfd 100644 (file)
@@ -240,6 +240,13 @@ public:
   int setOptimizer(std::shared_ptr<Optimizer> opt);
 
   /**
+   * @brief Get the Optimizer object
+   *
+   * @return std::shared_ptr<Optimizer> optimizer
+   */
+  std::shared_ptr<Optimizer> getOptimizer() { return opt; }
+
+  /**
    * @brief     Activation Type Getter
    * @retval    Activation Type.
    */
@@ -331,6 +338,13 @@ public:
     return weight_list.get()[position];
   }
 
+  /**
+   * @brief Get the number of weights
+   *
+   * @return unsigned int number of weights
+   */
+  unsigned int getNumWeights() { return num_weights; }
+
 #if defined(ENABLE_TEST)
   /**
    * @brief Set the batch for the layer
@@ -453,13 +467,6 @@ 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.
    */
@@ -487,6 +494,13 @@ protected:
    */
   void setType(LayerType type) { this->type = type; }
 
+  /**
+   * @brief     Activation Setter
+   * @param[in] activation activation type
+   * @throw std::invalid_argument when ActivationType is unknown
+   */
+  virtual void setActivation(ActivationType activation);
+
 private:
   /**
    * @brief     Set containing all the names of layers
@@ -533,14 +547,6 @@ private:
   virtual void printMetric(std::ostream &out);
 
   /**
-   * @brief     Activation Setter
-   * @param[in] activation activation type
-   * @retval #ML_ERROR_NONE Successful.
-   * @retval #ML_ERROR_INVALID_PARAMETER invalid parameter.
-   */
-  int setActivation(ActivationType activation);
-
-  /**
    * @brief     set weight decay parameters
    * @param[in] w struct for weight decay
    */
index c796fb1..4bca0cb 100644 (file)
@@ -104,6 +104,8 @@ int ActivationLayer::setActivation(
  * @param[in] ActivationType ActivationType ActivationType to be set
  */
 void ActivationLayer::setActivation(ActivationType acti_type) {
+  Layer::setActivation(acti_type);
+
   switch (acti_type) {
   case ActivationType::ACT_TANH:
     this->setActivation(tanhFloat, tanhPrime);
@@ -124,7 +126,6 @@ void ActivationLayer::setActivation(ActivationType acti_type) {
   default:
     throw std::runtime_error("Error: Not Supported Activation Type");
   }
-  this->activation_type = acti_type;
 }
 
 Tensor ActivationLayer::softmax(Tensor const &t) {
index daccd27..5d3be2c 100644 (file)
 
 namespace nntrainer {
 
-int Layer::setActivation(ActivationType acti) {
-  int status = ML_ERROR_NONE;
+void Layer::setActivation(ActivationType acti) {
   if (acti == ActivationType::ACT_UNKNOWN) {
-    ml_loge("Error:have to specify activation function");
-    return ML_ERROR_INVALID_PARAMETER;
+    throw std::invalid_argument("Error:have to specify activation function");
   }
   activation_type = acti;
-
-  return status;
 }
 
 int Layer::setOptimizer(std::shared_ptr<Optimizer> opt) {
@@ -159,8 +155,7 @@ void Layer::setProperty(const PropertyType type, const std::string &value) {
     break;
   case PropertyType::activation:
     if (!value.empty()) {
-      status = setActivation((ActivationType)parseType(value, TOKEN_ACTI));
-      throw_status(status);
+      setActivation((ActivationType)parseType(value, TOKEN_ACTI));
     }
     break;
   case PropertyType::flatten: