[Layer] Add trainable prop
authorJihoon Lee <jhoon.it.lee@samsung.com>
Thu, 17 Jun 2021 01:30:01 +0000 (10:30 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Wed, 23 Jun 2021 07:42:19 +0000 (16:42 +0900)
This patch add trainable property to layer

**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/layers/common_properties.h
nntrainer/layers/layer_context.h
nntrainer/layers/layer_impl.cpp
nntrainer/layers/layer_impl.h

index 7195725..09dbc03 100644 (file)
@@ -67,6 +67,22 @@ public:
 };
 
 /**
+ * @brief trainable property, use this to set and check how if certain layer is
+ * trainable
+ *
+ */
+class Trainable : public nntrainer::Property<bool> {
+public:
+  /**
+   * @brief Construct a new Trainable object
+   *
+   */
+  Trainable(bool val = true) : nntrainer::Property<bool>(val) {}
+  static constexpr const char *key = "trainable";
+  using prop_tag = bool_prop_tag;
+};
+
+/**
  * @brief RAII class to define the connection spec
  *
  */
index 2742a33..d466993 100644 (file)
@@ -207,9 +207,6 @@ public:
     if (!weights[idx]->getTrainable())
       throw std::invalid_argument(
         "Requesting gradient for a non-trainable weight.");
-    if (!trainable)
-      throw std::invalid_argument(
-        "Requesting gradient for a non-trainable layer.");
     return weights[idx]->getGradientRef();
   }
 
@@ -297,16 +294,8 @@ public:
    */
   unsigned int getNumWeights() const { return weights.size(); }
 
-  /**
-   * @brief Get the if the layer is trainable
-   *
-   * @return bool true if trainable, else false
-   */
-  bool getTrainable() { return trainable; }
-
 private:
   std::tuple<props::Name> props; /**< props of the layer */
-  bool trainable;                /**< if the layer is trainable */
 
   std::vector<Weight *> weights;   /**< weights of the layer */
   std::vector<Var_Grad *> inputs;  /**< inputs of the layer */
index 96fcd3b..5585a53 100644 (file)
@@ -15,6 +15,7 @@
 #include <string>
 #include <vector>
 
+#include <common_properties.h>
 #include <nntrainer_error.h>
 #include <nntrainer_log.h>
 #include <node_exporter.h>
@@ -23,7 +24,7 @@ namespace nntrainer {
 
 LayerImpl::LayerImpl() :
   finalized(false),
-  layer_impl_props(std::make_unique<std::tuple<>>()) {}
+  layer_impl_props(std::make_unique<std::tuple<props::Trainable>>()) {}
 
 void LayerImpl::finalize(InitContext &context) {
   NNTR_THROW_IF(finalized, nntrainer::exception::not_supported)
@@ -32,7 +33,9 @@ void LayerImpl::finalize(InitContext &context) {
   finalized = true;
 }
 
-void LayerImpl::setProperty(const std::vector<std::string> &values) {}
+void LayerImpl::setProperty(const std::vector<std::string> &values) {
+  loadProperties(values, *layer_impl_props);
+}
 
 void LayerImpl::exportTo(Exporter &exporter,
                          const ExportMethods &method) const {}
index 6998289..f3b9101 100644 (file)
@@ -26,6 +26,11 @@ class RunContext;
 class Exporter;
 
 enum class ExportMethods;
+
+namespace props {
+class Trainable;
+}
+
 /**
  * @class   An abstract class to ease developing a layer
  * @brief   An abstract class for all layers
@@ -63,8 +68,9 @@ public:
                         const ExportMethods &method) const override;
 
 private:
-  bool finalized;                                 /**< check if finalized */
-  std::unique_ptr<std::tuple<>> layer_impl_props; /**< layer_impl_props */
+  bool finalized; /**< check if finalized */
+  std::unique_ptr<std::tuple<props::Trainable>>
+    layer_impl_props; /**< layer_impl_props */
 };
 
 } // namespace nntrainer