[layer] Remove extra property from embedding
authorParichay Kapoor <kparichay@gmail.com>
Sun, 11 Jul 2021 19:15:12 +0000 (04:15 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 22 Jul 2021 11:47:24 +0000 (20:47 +0900)
Remove extra property "in_length" from embedding layer.
in_length was used to set and provide the number of inputs to be
provided to the embedding layer. However, this must be fixed
based on the input provided from the previous layer.
This patch removes this property and let this value be inferred
from the given input dimensions.

Signed-off-by: Parichay Kapoor <kparichay@gmail.com>
Applications/ProductRatings/res/product_ratings_model.ini
api/ccapi/include/layer.h
nntrainer/layers/embedding.cpp
nntrainer/layers/embedding.h
nntrainer/layers/layer_internal.h
nntrainer/utils/parse_util.cpp
test/unittest/layers/unittest_layers_concat.cpp
test/unittest/layers/unittest_layers_embedding.cpp
test/unittest/layers/unittest_layers_split.cpp
test/unittest/unittest_nntrainer_layers.cpp

index 499f99e..866119c 100644 (file)
@@ -26,14 +26,12 @@ input_layers = split
 Type = embedding
 in_dim = 6          # in dim must be more than len(set(user ids)) + 1
 out_dim = 5
-in_length = 1
 
 [product_embed]
 input_layers = split
 Type = embedding
 in_dim = 6          # in dim must be more than len(set(product ids)) + 1
 out_dim = 5
-in_length = 1
 
 [concat]
 input_layers = user_embed, product_embed
index 61e0be6..3fadda7 100644 (file)
@@ -115,7 +115,6 @@ public:
    * - random_translate
    * - in_dim : int ( input dimension for embedding layer )
    * - out_dim : int ( output dimesion for embedding layer )
-   * - in_length : int ( input length for embedding layer )
    * - recurrent_activation : string (type) - used only in lstm
    * - return_sequences : bool (type) - used only in lstm
    * - distribute : bool
index a6cf5ce..96f87db 100644 (file)
@@ -38,7 +38,7 @@ void EmbeddingLayer::finalize(InitLayerContext &context) {
 
   TensorDim output_dim = input_dim;
 
-  output_dim.height(in_length);
+  output_dim.height(input_dim.width());
   output_dim.width(out_dim);
   context.setOutputDimensions({output_dim});
 
@@ -89,10 +89,6 @@ void EmbeddingLayer::setProperty(const std::string &type_str,
     status = setUint(out_dim, value);
     throw_status(status);
   } break;
-  case PropertyType::in_length: {
-    status = setUint(in_length, value);
-    throw_status(status);
-  } break;
   default:
     LayerImpl::setProperty(type_str, value);
     break;
@@ -107,7 +103,7 @@ void EmbeddingLayer::forwarding(RunLayerContext &context, bool training) {
   for (unsigned int b = 0; b < input_.batch(); ++b) {
     float *in_data = input_.getAddress(b * input_.getDim().getFeatureLen());
 
-    for (unsigned int i = 0; i < in_length; ++i) {
+    for (unsigned int i = 0; i < input_.width(); ++i) {
       if (in_data[i] > in_dim) {
         throw std::invalid_argument("input word index is greater than in_dim");
       }
@@ -149,7 +145,7 @@ void EmbeddingLayer::calcGradient(RunLayerContext &context) {
   for (unsigned int b = 0; b < input_.batch(); ++b) {
     float *in_data = input_.getAddress(b * input_.getDim().getFeatureLen());
 
-    for (unsigned int i = 0; i < in_length; ++i) {
+    for (unsigned int i = 0; i < input_.width(); ++i) {
       // Assume padding is 0 and index always start from 1.
       // If in_data[i] - 1 < 0, then it skips.
       if (in_data[i] - 1 < 0)
index 826cc80..27f0a95 100644 (file)
@@ -28,12 +28,10 @@ public:
   /**
    * @brief     Constructor of Embedding Layer
    */
-  EmbeddingLayer(unsigned int in_dim_ = 0, unsigned int out_dim_ = 0,
-                 unsigned int in_length_ = 0) :
+  EmbeddingLayer(unsigned int in_dim_ = 0, unsigned int out_dim_ = 0) :
     LayerImpl(),
     in_dim(in_dim_),
     out_dim(out_dim_),
-    in_length(in_length_),
     weight_idx(0) {}
 
   /**
@@ -104,7 +102,6 @@ public:
 private:
   unsigned int in_dim;
   unsigned int out_dim;
-  unsigned int in_length;
   unsigned int weight_idx;
 
   /**
index 32abbed..981cee5 100644 (file)
@@ -239,13 +239,12 @@ public:
    *            28. random_translate
    *            29. in_dim : int ( input dimension for embedding layer )
    *            30. out_dim : int ( output dimesion for embedding layer )
-   *            31. in_length : int ( input length for embedding layer )
-   *            32. recurrent_activation :  string (type) - lstm
-   *            33. distribute : bool
-   *            34. split_dimension : string (type)
-   *            35. return_sequences :  bool (type) - lstm
-   *            36. hidden_state_activation :  string (type) - lstm
-   *            37. dropout :  float (type) - drop out rate
+   *            31. recurrent_activation :  string (type) - lstm
+   *            32. distribute : bool
+   *            33. split_dimension : string (type)
+   *            34. return_sequences :  bool (type) - lstm
+   *            35. hidden_state_activation :  string (type) - lstm
+   *            36. dropout :  float (type) - drop out rate
    */
   enum class PropertyType {
     input_shape = 0,
@@ -279,13 +278,12 @@ public:
     random_translate = 28,
     in_dim = 29,
     out_dim = 30,
-    in_length = 31,
-    recurrent_activation = 32,
-    distribute = 33,
-    split_dimension = 34,
-    return_sequences = 35,
-    hidden_state_activation = 36,
-    dropout = 37,
+    recurrent_activation = 31,
+    distribute = 32,
+    split_dimension = 33,
+    return_sequences = 34,
+    hidden_state_activation = 35,
+    dropout = 36,
     unknown
   };
 
index 5c0825d..c1ff734 100644 (file)
@@ -214,13 +214,12 @@ unsigned int parseType(std::string ll, InputType t) {
  * random_translate = 28
  * in_dim = 29
  * out_dim = 30
- * in_length = 31
- * recurrent_activation = 32
- * distribute = 33
- * split_dimension = 34
- * return_sequences = 35
- * hidden_state_activation = 36
- * dropout = 37
+ * recurrent_activation = 31
+ * distribute = 32
+ * split_dimension = 33
+ * return_sequences = 34
+ * hidden_state_activation = 35
+ * dropout = 36
  *
  * InputLayer has 0, 1, 2, 3 properties.
  * FullyConnectedLayer has 1, 4, 6, 7, 8, 9 properties.
@@ -228,7 +227,7 @@ unsigned int parseType(std::string ll, InputType t) {
  * Pooling2DLayer has 12, 13, 14, 15 properties.
  * BatchNormalizationLayer has 0, 1, 5, 6, 7 properties.
  */
-static std::array<std::string, 39> property_string = {
+static std::array<std::string, 38> property_string = {
   "input_shape",
   "normalization",
   "standardization",
@@ -260,7 +259,6 @@ static std::array<std::string, 39> property_string = {
   "random_translate",
   "in_dim",
   "out_dim",
-  "in_length",
   "recurrent_activation",
   "distribute",
   "split_dimension",
index 6f7cfff..8430498 100644 (file)
@@ -18,7 +18,7 @@
 
 auto semantic_concat =
   LayerSemanticsParamType(nntrainer::createLayer<nntrainer::ConcatLayer>,
-                          nntrainer::ConcatLayer::type, {}, {}, 0, false);
+                          nntrainer::ConcatLayer::type, {}, 0, false);
 
 INSTANTIATE_TEST_CASE_P(Concat, LayerSemantics,
                         ::testing::Values(semantic_concat));
index 5e54feb..39d6a60 100644 (file)
@@ -18,8 +18,7 @@
 
 auto semantic_embedding = LayerSemanticsParamType(
   nntrainer::createLayer<nntrainer::EmbeddingLayer>,
-  nntrainer::EmbeddingLayer::type, {"in_length=1", "out_dim=1", "in_dim=1"}, {},
-  0, false);
+  nntrainer::EmbeddingLayer::type, {"out_dim=1", "in_dim=1"}, 0, false);
 
 INSTANTIATE_TEST_CASE_P(Embedding, LayerSemantics,
                         ::testing::Values(semantic_embedding));
index 4647ee4..d603788 100644 (file)
@@ -18,7 +18,7 @@
 
 auto semantic_split =
   LayerSemanticsParamType(nntrainer::createLayer<nntrainer::SplitLayer>,
-                          nntrainer::SplitLayer::type, {}, {}, 0, false);
+                          nntrainer::SplitLayer::type, {}, 0, false);
 
 INSTANTIATE_TEST_CASE_P(Split, LayerSemantics,
                         ::testing::Values(semantic_split));
index b264982..dc27d0d 100644 (file)
@@ -2251,8 +2251,7 @@ protected:
 
   virtual void prepareLayer() {
     int status = setProperty("in_dim=50 |"
-                             "out_dim=8 |"
-                             "in_length=12");
+                             "out_dim=8");
     EXPECT_EQ(status, ML_ERROR_NONE);
     setBatch(3);
   }