[refactor] Cleanup headers
authorParichay Kapoor <pk.kapoor@samsung.com>
Tue, 22 Sep 2020 05:17:57 +0000 (14:17 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 24 Sep 2020 23:00:04 +0000 (08:00 +0900)
This patch cleans up the headers of the internal codes
so that the headers can directly be exposed for the c++ API

Also removed applyIf from lazy_tensor (after checking with @zhoonit)
as it exposed #define in header and was not used

**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>
42 files changed:
nnstreamer/tensor_filter/tensor_filter_nntrainer.cc
nntrainer/include/activation_layer.h
nntrainer/include/addition_layer.h
nntrainer/include/blas_interface.h
nntrainer/include/bn_layer.h
nntrainer/include/conv2d_layer.h
nntrainer/include/databuffer.h
nntrainer/include/databuffer_file.h
nntrainer/include/databuffer_func.h
nntrainer/include/fc_layer.h
nntrainer/include/flatten_layer.h
nntrainer/include/input_layer.h
nntrainer/include/layer.h
nntrainer/include/lazy_tensor.h
nntrainer/include/loss_layer.h
nntrainer/include/neuralnet.h
nntrainer/include/nntrainer_error.h
nntrainer/include/nntrainer_logger.h
nntrainer/include/optimizer.h
nntrainer/include/pooling2d_layer.h
nntrainer/include/tensor.h
nntrainer/include/tensor_dim.h
nntrainer/include/util_func.h
nntrainer/src/activation_layer.cpp
nntrainer/src/blas_interface.cpp
nntrainer/src/bn_layer.cpp
nntrainer/src/conv2d_layer.cpp
nntrainer/src/databuffer.cpp
nntrainer/src/fc_layer.cpp
nntrainer/src/layer.cpp
nntrainer/src/lazy_tensor.cpp
nntrainer/src/loss_layer.cpp
nntrainer/src/nntrainer_logger.cpp
nntrainer/src/optimizer.cpp
nntrainer/src/pooling2d_layer.cpp
nntrainer/src/tensor.cpp
nntrainer/src/tensor_dim.cpp
nntrainer/src/util_func.cpp
test/nntrainer_test_util.cpp
test/unittest/unittest_nntrainer_layers.cpp
test/unittest/unittest_nntrainer_lazy_tensor.cpp
test/unittest/unittest_nntrainer_tensor.cpp

index 998788b..60e696d 100644 (file)
@@ -17,7 +17,8 @@
  */
 
 #include <algorithm>
-#include <limits.h>
+#include <map>
+#include <limits>
 #include <unistd.h>
 
 #include <nnstreamer_plugin_api.h>
index b3c10d9..340c9b7 100644 (file)
 #define __ACTIVATION_LAYER_H__
 #ifdef __cplusplus
 
-#include <fstream>
-#include <iostream>
 #include <layer.h>
-#include <optimizer.h>
 #include <tensor.h>
-#include <vector>
 
 namespace nntrainer {
 
@@ -78,9 +74,9 @@ public:
   void copy(std::shared_ptr<Layer> l);
 
   /**
-   * @brief setActivation by preset actiType
+   * @brief setActivation by preset ActiType
    *
-   * @param[in] ActiType actiType actiType to be set
+   * @param[in] ActiType ActiType ActiType to be set
    */
   void setActivation(ActiType acti_type);
 
index e871485..bfc362b 100644 (file)
 #define __ADDITION_LAYER_H__
 #ifdef __cplusplus
 
-#include <fstream>
-#include <iostream>
 #include <layer.h>
 #include <tensor.h>
-#include <vector>
 
 namespace nntrainer {
 
index e3310fe..faad008 100644 (file)
 #define __BLAS_INTERFACE_H_
 #ifdef __cplusplus
 
-#include <assert.h>
-#include <iostream>
-#include <math.h>
-#include <stdio.h>
-
 #ifdef USE_BLAS
 extern "C" {
 #include <cblas.h>
index 8bf0703..5920cf4 100644 (file)
 #ifdef __cplusplus
 
 #include <array>
-#include <fstream>
 #include <functional>
-#include <iostream>
 #include <vector>
 
 #include <layer.h>
-#include <optimizer.h>
 #include <tensor.h>
 
 namespace nntrainer {
index 5a56902..6a51382 100644 (file)
 #define __CONV2D_LAYER_H_
 #ifdef __cplusplus
 
-#include <fstream>
-#include <iostream>
 #include <layer.h>
-#include <optimizer.h>
 #include <tensor.h>
-#include <vector>
-
-#define CONV2D_DIM 2
 
 namespace nntrainer {
 
+constexpr const unsigned int CONV2D_DIM = 2;
+
 /**
  * @class   Convolution 2D Layer
  * @brief   Convolution 2D Layer
index e0a1983..9190e95 100644 (file)
 #include <iostream>
 #include <memory>
 #include <random>
-#include <tensor_dim.h>
 #include <thread>
-#include <util_func.h>
 #include <vector>
 
-/**
- * @brief Number of Data Set
- */
-#define NBUFTYPE 4
-
-typedef std::vector<std::vector<std::vector<std::vector<float>>>> vec_4d;
+#include <tensor_dim.h>
 
 #define SET_VALIDATION(val)                                              \
   do {                                                                   \
@@ -72,6 +65,13 @@ typedef std::vector<std::vector<std::vector<std::vector<float>>>> vec_4d;
     }                                                      \
   } while (0)
 
+/**
+ * @brief Number of Data Set
+ */
+constexpr const unsigned int NBUFTYPE = 4;
+
+typedef std::vector<std::vector<std::vector<std::vector<float>>>> vec_4d;
+
 typedef enum {
   DATA_NOT_READY = 0,
   DATA_READY = 1,
@@ -128,38 +128,7 @@ public:
    * @brief     Create Buffer
    * @retval    DataBuffer
    */
-  DataBuffer(DataBufferType type) :
-    train_running(),
-    val_running(),
-    test_running(),
-    train_thread(),
-    val_thread(),
-    test_thread(),
-    data_buffer_type(type),
-    user_data(nullptr) {
-    SET_VALIDATION(false);
-    class_num = 0;
-    cur_train_bufsize = 0;
-    cur_val_bufsize = 0;
-    cur_test_bufsize = 0;
-    train_bufsize = 0;
-    val_bufsize = 0;
-    test_bufsize = 0;
-    max_train = 0;
-    max_val = 0;
-    max_test = 0;
-    rest_train = 0;
-    rest_val = 0;
-    rest_test = 0;
-    batch_size = 0;
-    train_running = false;
-    val_running = false;
-    test_running = false;
-    trainReadyFlag = DATA_NOT_READY;
-    valReadyFlag = DATA_NOT_READY;
-    testReadyFlag = DATA_NOT_READY;
-    rng.seed(getSeed());
-  };
+  DataBuffer(DataBufferType type);
 
   /**
    * @brief     Destructor
index 45a7340..77011c8 100644 (file)
@@ -26,7 +26,6 @@
 #ifdef __cplusplus
 
 #include <atomic>
-#include <databuffer.h>
 #include <fstream>
 #include <functional>
 #include <iostream>
@@ -34,6 +33,8 @@
 #include <thread>
 #include <vector>
 
+#include <databuffer.h>
+
 namespace nntrainer {
 
 /**
index 74d6807..0fe274a 100644 (file)
 #ifdef __cplusplus
 
 #include <atomic>
-#include <databuffer.h>
 #include <fstream>
 #include <functional>
 #include <iostream>
 #include <memory>
-#include <nntrainer-api-common.h>
 #include <thread>
 #include <vector>
 
+#include <databuffer.h>
+#include <nntrainer-api-common.h>
+
 namespace nntrainer {
 
 /**
index 2202109..23e9058 100644 (file)
 #define __FC_LAYER_H__
 #ifdef __cplusplus
 
-#include <fstream>
-#include <iostream>
 #include <layer.h>
-#include <optimizer.h>
 #include <tensor.h>
-#include <vector>
 
 namespace nntrainer {
 
index 4af1db6..8b072b7 100644 (file)
 #define __FLATTEN_LAYER_H__
 #ifdef __cplusplus
 
-#include <fstream>
-#include <iostream>
 #include <layer.h>
 #include <tensor.h>
-#include <vector>
 
 namespace nntrainer {
 
index fa61375..4b7e0a8 100644 (file)
 #define __INPUT_LAYER_H__
 #ifdef __cplusplus
 
-#include <fstream>
-#include <iostream>
 #include <layer.h>
-#include <optimizer.h>
 #include <tensor.h>
-#include <vector>
 
 namespace nntrainer {
 
index 3b88934..17adc0c 100644 (file)
 #define __LAYERS_H__
 #ifdef __cplusplus
 
-#include <fstream>
-#include <iostream>
 #include <memory>
-#include <optimizer.h>
 #include <set>
+#include <vector>
+
+#include <optimizer.h>
 #include <tensor.h>
 #include <tensor_dim.h>
-#include <vector>
 
 namespace nntrainer {
 
 /**
- * @brief     Enumeration of loss function type
- *            0. MSE ( Mean Squared Error )
- *            1. ENTROPY ( Cross Entropy )
- *            2. ENTROPY_SIGMOID (Cross Entropy amalgamated with sigmoid for
- * stability)
- *            3. ENTROPY_SOFTMAX (Cross Entropy amalgamated with softmax for
- * stability)
- *            4. Unknown
- */
-enum class LossType {
-  LOSS_MSE,
-  LOSS_ENTROPY,
-  LOSS_ENTROPY_SIGMOID,
-  LOSS_ENTROPY_SOFTMAX,
-  LOSS_UNKNOWN
-};
-
-/**
  * @brief     Enumeration of activation function type
  *            0. tanh
  *            1. sigmoid
@@ -351,16 +332,6 @@ public:
   void setWeightInit(WeightInitializer wini) { weight_initializer = wini; }
 
   /**
-   * @brief  initialize Weight
-   * @param[in] w_dim TensorDim
-   * @param[in] initializer Weight Initializer
-   * @param[out] status Status
-   * @retval Tensor Initialized Tensor
-   */
-  Tensor initializeWeight(const TensorDim &w_dim, WeightInitializer initializer,
-                          int &status);
-
-  /**
    * @brief Set the input dimension
    * @param[in] d dimension to be set
    */
@@ -622,6 +593,17 @@ std::ostream &operator<<(std::ostream &out, T &l) {
   return out;
 }
 
+/**
+ * @brief  initialize Weight
+ * @param[in] w_dim TensorDim
+ * @param[in] initializer Weight Initializer
+ * @param[out] status Status
+ * @retval Tensor Initialized Tensor
+ */
+// TODO: move out
+Tensor getInitializedTensor(const TensorDim &w_dim,
+                            WeightInitializer initializer);
+
 } // namespace nntrainer
 
 #endif /* __cplusplus */
index 4539e7e..b004e55 100644 (file)
 #define __LAZY_TENSOR_H__
 #ifdef __cplusplus
 
-#include <nntrainer_error.h>
 #include <tensor.h>
 #include <vector>
 
-#define FWD(...) std::forward<decltype(__VA_ARGS__)>(__VA_ARGS__)
-
-#define _LIFT(X)                                            \
-  [](nntrainer::Tensor & t,                                 \
-     auto &&... args) noexcept(noexcept(t.X(FWD(args)...))) \
-    ->decltype(t.X(FWD(args)...)) {                         \
-    return t.X(FWD(args)...);                               \
-  }
-
 namespace nntrainer {
 
 /**
@@ -145,30 +135,6 @@ public:
   LazyTensor &average();
 
   /**
-   * @brief     apply A tensor function when predicate is true
-   * @param[in] bool predicate predicate to check to determine application
-   * @param[in] _Callable&& fn function to be applied
-   *            (Must be wrapped with _LIFT(X) macro to resolve overload set)
-   * @param[in] _Args&&... args args for fn
-   * @retval    LazyTensor *this
-   */
-  template <typename _Callable, typename... _Args>
-  LazyTensor &applyIf(bool predicate, _Callable &&fn, _Args &&... args) {
-    if (predicate) {
-      auto f = [&](Tensor &t) mutable -> int {
-        try {
-          return fn(t, std::forward<_Args>(args)...);
-        } catch (std::runtime_error &e) {
-          return ML_ERROR_INVALID_PARAMETER;
-        }
-      };
-      call_chain.push_back(f);
-    }
-
-    return *this;
-  }
-
-  /**
    * @brief execute the call_chain to get the tensor
    * @retval calculated tensor
    */
index 6e03a52..a0efb97 100644 (file)
 #define __LOSS_LAYER_H__
 #ifdef __cplusplus
 
-#include <fstream>
-#include <iostream>
 #include <layer.h>
-#include <optimizer.h>
 #include <tensor.h>
-#include <vector>
 
 namespace nntrainer {
 
 /**
+ * @brief     Enumeration of loss function type
+ */
+enum class LossType {
+  LOSS_MSE,             /** Mean Squared Error */
+  LOSS_ENTROPY,         /** Cross Entropy */
+  LOSS_ENTROPY_SIGMOID, /** Cross Entropy amalgamated with sigmoid for stability
+                         */
+  LOSS_ENTROPY_SOFTMAX, /** Cross Entropy amalgamated with softmax for stability
+                         */
+  LOSS_UNKNOWN          /** Unknown */
+};
+
+/**
  * @class   LossLayer
  * @brief   loss layer
  */
index c215b42..7823730 100644 (file)
 #define __NEURALNET_H__
 #ifdef __cplusplus
 
+#include <vector>
+
 #include <activation_layer.h>
 #include <bn_layer.h>
 #include <conv2d_layer.h>
 #include <databuffer.h>
 #include <fc_layer.h>
 #include <flatten_layer.h>
-#include <fstream>
 #include <input_layer.h>
-#include <iostream>
 #include <layer.h>
 #include <loss_layer.h>
 #include <ml-api-common.h>
 #include <optimizer.h>
 #include <pooling2d_layer.h>
 #include <tensor.h>
-#include <vector>
 
 namespace nntrainer {
 
index 10c3465..436b45d 100644 (file)
@@ -17,7 +17,7 @@
 #define ML_ERROR_BAD_ADDRESS TIZEN_ERROR_BAD_ADDRESS
 #define ML_ERROR_RESULT_OUT_OF_RANGE TIZEN_ERROR_RESULT_OUT_OF_RANGE
 #else
-#include <errno.h>
+#include <cerrno>
 #define ML_ERROR_BAD_ADDRESS (-EFAULT)
 #define ML_ERROR_RESULT_OUT_OF_RANGE (-ERANGE)
 #endif
index 299e3f0..bcabb56 100644 (file)
 #ifdef __cplusplus
 
 #include <fstream>
-#include <iostream>
 #include <mutex>
 #include <string>
-#include <vector>
 
 /**
  * @brief     Log Level of NNtrainer
index 1d50379..f63ee3f 100644 (file)
@@ -23,7 +23,6 @@
 #define __OPTIMIZER_H__
 #ifdef __cplusplus
 
-#include <iostream>
 #include <memory>
 #include <tensor.h>
 
@@ -32,6 +31,7 @@ namespace nntrainer {
 /**
  * @brief UpdatableParam that could be updated thorugh optimizer
  */
+// TODO: move this out from here
 struct UpdatableParam {
   Tensor weight;         /**< weight to be updated and used */
   Tensor grad;           /**< gradient for the weight */
index 56ca690..c93f1ce 100644 (file)
@@ -15,8 +15,6 @@
 #define __POOLING2D_LAYER_H__
 #ifdef __cplusplus
 
-#include <fstream>
-#include <iostream>
 #include <layer.h>
 #include <tensor.h>
 #include <vector>
index 98e6458..ed149e0 100644 (file)
 #ifdef __cplusplus
 
 #include <array>
-#include <cmath>
-#include <fstream>
-#include <iostream>
+#include <functional>
 #include <memory>
-#include <regex>
+#include <vector>
+
 #include <tensor_dim.h>
 
 #define MAKE_SHARED_TENSOR(x) std::make_shared<nntrainer::Tensor>(x)
@@ -580,14 +579,6 @@ public:
     return strides;
   }
 
-  /**
-   * @brief compute Loop info for broadcasting and vectorization
-   *
-   * @param m target tensor to be calculated against.
-   * @return BroadcastInfo Loopinfo needed to run external loop
-   */
-  BroadcastInfo computeBroadcastInfo(const Tensor &m);
-
 private:
   /**
    * @brief Get linear index given the n-d index
@@ -626,14 +617,20 @@ private:
     Tensor const &m,
     std::function<void(const BroadcastInfo &e, float *, const float *)> v_func);
 
+  /**
+   * @brief compute Loop info for broadcasting and vectorization
+   *
+   * @param m target tensor to be calculated against.
+   * @return BroadcastInfo Loopinfo needed to run external loop
+   */
+  BroadcastInfo computeBroadcastInfo(const Tensor &m);
+
   /**< handle the data as a std::shared_ptr<float> type */
   TensorDim dim;
   std::array<unsigned int, MAXDIM> strides;
   bool is_contiguous;
   std::shared_ptr<float> data;
 
-  static constexpr float min_limits = std::numeric_limits<float>::min();
-  static constexpr float max_limits = std::numeric_limits<float>::max();
   template <typename T> void setDist(T dist);
   static constexpr float epsilon = 1e-5;
 };
index a253c00..f2ba91a 100644 (file)
 
 #include <array>
 #include <iostream>
-#include <memory>
-#include <regex>
-#include <vector>
 
 namespace nntrainer {
 
-#define MAXDIM 4
+constexpr const size_t MAXDIM = 4;
 
 class TensorDim {
 public:
   TensorDim() {
-    for (int i = 0; i < MAXDIM; ++i) {
+    for (size_t i = 0; i < MAXDIM; ++i) {
       dim[i] = 0;
     }
     len = 0;
index cbe4f9a..772d80d 100644 (file)
@@ -24,7 +24,6 @@
 #define __UTIL_FUNC_H__
 #ifdef __cplusplus
 
-#include <layer.h>
 #include <tensor.h>
 
 namespace nntrainer {
index 45c86b6..813b44e 100644 (file)
@@ -11,6 +11,8 @@
  *
  */
 
+#include <algorithm>
+#include <cmath>
 #include <fstream>
 #include <functional>
 #include <iostream>
@@ -117,9 +119,9 @@ int ActivationLayer::setActivation(
 }
 
 /**
- * @brief setActivation by preset actiType
+ * @brief setActivation by preset ActiType
  *
- * @param[in] ActiType actiType actiType to be set
+ * @param[in] ActiType ActiType ActiType to be set
  */
 void ActivationLayer::setActivation(ActiType acti_type) {
   switch (acti_type) {
@@ -231,7 +233,7 @@ Tensor ActivationLayer::softmaxPrime(Tensor const &x,
   return PI;
 }
 
-float ActivationLayer::sigmoid(float x) { return 1.0f / (1.0f + exp(-x)); }
+float ActivationLayer::sigmoid(float x) { return 1.0f / (1.0f + exp_util(-x)); }
 
 float ActivationLayer::sigmoidPrime(float x) {
   float sprime = sigmoid(x);
index bb9cd77..6c03f5d 100644 (file)
@@ -12,6 +12,9 @@
  */
 
 #include <blas_interface.h>
+#include <nntrainer_error.h>
+
+#include <cmath>
 
 #define sgemv_loop(ci, cj, cM, cN)           \
   do {                                       \
index a0b6f0d..dd66746 100644 (file)
@@ -53,18 +53,14 @@ int BatchNormalizationLayer::initialize() {
   }
 
   Tensor mu =
-    initializeWeight(dim, initializers[static_cast<int>(BNParams::mu)], status);
-  Tensor var = initializeWeight(
-    dim, initializers[static_cast<int>(BNParams::var)], status);
-  NN_RETURN_STATUS();
-
-  Tensor gamma = initializeWeight(
-    dim, initializers[static_cast<int>(BNParams::gamma)], status);
-  NN_RETURN_STATUS();
-
-  Tensor beta = initializeWeight(
-    dim, initializers[static_cast<int>(BNParams::beta)], status);
-  NN_RETURN_STATUS();
+    getInitializedTensor(dim, initializers[static_cast<int>(BNParams::mu)]);
+  Tensor var =
+    getInitializedTensor(dim, initializers[static_cast<int>(BNParams::var)]);
+
+  Tensor gamma =
+    getInitializedTensor(dim, initializers[static_cast<int>(BNParams::gamma)]);
+  Tensor beta =
+    getInitializedTensor(dim, initializers[static_cast<int>(BNParams::beta)]);
 
   setParamSize(4);
   paramsAt(0) = {std::move(mu), Tensor(), "BN:moving_mean", false};
index 6b94906..b711e46 100644 (file)
@@ -10,6 +10,8 @@
  * @brief      This is Convolution Layer Class for Neural Network
  *
  */
+#include <cstring>
+#include <string>
 
 #include <blas_interface.h>
 #include <conv2d_layer.h>
@@ -18,7 +20,6 @@
 #include <nntrainer_error.h>
 #include <nntrainer_log.h>
 #include <parse_util.h>
-#include <string>
 #include <util_func.h>
 
 namespace nntrainer {
@@ -39,10 +40,10 @@ int Conv2DLayer::initialize() {
   setParamSize(filter_size * 2);
 
   for (unsigned int i = 0; i < filter_size; ++i) {
-    Tensor Knl = initializeWeight(dim, weight_initializer, status);
+    Tensor Knl = getInitializedTensor(dim, weight_initializer);
     NN_RETURN_STATUS();
 
-    Tensor bias = initializeWeight(bias_dim, bias_initializer, status);
+    Tensor bias = getInitializedTensor(bias_dim, bias_initializer);
     NN_RETURN_STATUS();
 
     Tensor delK(dim);
@@ -353,10 +354,11 @@ sharedConstTensor Conv2DLayer::backwarding(sharedConstTensor derivative,
       Tensor &delK = paramsAt(i).grad;
       Tensor &filters = paramsAt(i).weight;
 
-      delK = delK.chain()
-               .applyIf(this->isWeightRegularizerL2Norm(), _LIFT(add_i),
-                        filters, weight_regularizer.constant)
-               .run();
+      if (isWeightRegularizerL2Norm()) {
+        status = delK.add_i(filters, weight_regularizer.constant);
+        if (status != ML_ERROR_NONE)
+          throw std::runtime_error("Weight regularization failed");
+      }
     }
 
     opt.apply_gradients(params, param_size, iteration);
@@ -385,17 +387,17 @@ int Conv2DLayer::setSize(int *size, PropertyType type) {
   int status = ML_ERROR_NONE;
   switch (type) {
   case PropertyType::kernel_size:
-    for (int i = 0; i < CONV2D_DIM; ++i) {
+    for (unsigned int i = 0; i < CONV2D_DIM; ++i) {
       kernel_size[i] = size[i];
     }
     break;
   case PropertyType::stride:
-    for (int i = 0; i < CONV2D_DIM; ++i) {
+    for (unsigned int i = 0; i < CONV2D_DIM; ++i) {
       stride[i] = size[i];
     }
     break;
   case PropertyType::padding:
-    for (int i = 0; i < CONV2D_DIM; ++i) {
+    for (unsigned int i = 0; i < CONV2D_DIM; ++i) {
       padding[i] = size[i];
     }
     break;
index 3f73bad..0ea67d8 100644 (file)
@@ -37,6 +37,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <thread>
+#include <util_func.h>
 
 std::exception_ptr globalExceptionPtr = nullptr;
 
@@ -54,6 +55,39 @@ std::condition_variable cv_train;
 std::condition_variable cv_val;
 std::condition_variable cv_test;
 
+DataBuffer::DataBuffer(DataBufferType type) :
+  train_running(),
+  val_running(),
+  test_running(),
+  train_thread(),
+  val_thread(),
+  test_thread(),
+  data_buffer_type(type),
+  user_data(nullptr) {
+  SET_VALIDATION(false);
+  class_num = 0;
+  cur_train_bufsize = 0;
+  cur_val_bufsize = 0;
+  cur_test_bufsize = 0;
+  train_bufsize = 0;
+  val_bufsize = 0;
+  test_bufsize = 0;
+  max_train = 0;
+  max_val = 0;
+  max_test = 0;
+  rest_train = 0;
+  rest_val = 0;
+  rest_test = 0;
+  batch_size = 0;
+  train_running = false;
+  val_running = false;
+  test_running = false;
+  trainReadyFlag = DATA_NOT_READY;
+  valReadyFlag = DATA_NOT_READY;
+  testReadyFlag = DATA_NOT_READY;
+  rng.seed(getSeed());
+};
+
 int DataBuffer::rangeRandom(int min, int max) {
   std::uniform_int_distribution<int> dist(min, max);
   return dist(rng);
index 1f3fe58..5e90fe3 100644 (file)
@@ -45,11 +45,9 @@ int FullyConnectedLayer::initialize() {
   TensorDim dim = output_dim;
   dim.height(input_dim.width());
   dim.batch(1);
-  Tensor weight = initializeWeight(dim, weight_initializer, status);
-  NN_RETURN_STATUS();
 
-  Tensor bias = initializeWeight(bias_dim, bias_initializer, status);
-  NN_RETURN_STATUS();
+  Tensor weight = getInitializedTensor(dim, weight_initializer);
+  Tensor bias = getInitializedTensor(bias_dim, bias_initializer);
 
   setParamSize(2);
   paramsAt(0) = {std::move(weight), Tensor(weight.getDim()), "FC:weight"};
index a93313b..ea8bcc8 100644 (file)
@@ -81,9 +81,8 @@ void Layer::save(std::ofstream &file) {
   }
 }
 
-Tensor Layer::initializeWeight(const TensorDim &w_dim,
-                               WeightInitializer initializer, int &status) {
-
+Tensor getInitializedTensor(const TensorDim &w_dim,
+                            WeightInitializer initializer) {
   Tensor w = Tensor(w_dim);
 
   if (initializer == WEIGHT_UNKNOWN) {
@@ -100,29 +99,30 @@ Tensor Layer::initializeWeight(const TensorDim &w_dim,
     w.setValue(1.0f);
     break;
   case WEIGHT_LECUN_NORMAL:
-    w.setRandNormal(0.0f, sqrt(1.0f / w_dim.height()));
+    w.setRandNormal(0.0f, sqrtFloat(1.0f / w_dim.height()));
     break;
   case WEIGHT_XAVIER_NORMAL:
-    w.setRandNormal(0.0f, sqrt(2.0f / (w_dim.width() + w_dim.height())));
+    w.setRandNormal(0.0f, sqrtFloat(2.0f / (w_dim.width() + w_dim.height())));
     break;
   case WEIGHT_HE_NORMAL:
-    w.setRandNormal(0.0f, sqrt(2.0f / (w_dim.height())));
+    w.setRandNormal(0.0f, sqrtFloat(2.0f / (w_dim.height())));
     break;
   case WEIGHT_LECUN_UNIFORM:
-    w.setRandUniform(-1.0f * sqrt(1.0f / w_dim.height()),
-                     sqrt(1.0f / w_dim.height()));
+    w.setRandUniform(-1.0f * sqrtFloat(1.0f / w_dim.height()),
+                     sqrtFloat(1.0f / w_dim.height()));
     break;
   case WEIGHT_XAVIER_UNIFORM:
-    w.setRandUniform(-1.0f * sqrt(6.0f / (w_dim.height() + w_dim.width())),
-                     sqrt(6.0 / (w_dim.height() + w_dim.width())));
+    w.setRandUniform(-1.0f * sqrtFloat(6.0f / (w_dim.height() + w_dim.width())),
+                     sqrtFloat(6.0 / (w_dim.height() + w_dim.width())));
     break;
   case WEIGHT_HE_UNIFORM:
-    w.setRandUniform(-1.0f * sqrt(6.0f / (w_dim.height())),
-                     sqrt(6.0 / (w_dim.height())));
+    w.setRandUniform(-1.0f * sqrtFloat(6.0f / (w_dim.height())),
+                     sqrtFloat(6.0 / (w_dim.height())));
     break;
   default:
     break;
   }
+
   return w;
 }
 
index 10e84ff..c40d5ca 100644 (file)
@@ -12,7 +12,7 @@
  */
 
 #include <lazy_tensor.h>
-#include <tensor.h>
+#include <nntrainer_error.h>
 
 namespace nntrainer {
 
index c85de33..46b0b56 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #include <activation_layer.h>
+#include <cmath>
 #include <layer.h>
 #include <lazy_tensor.h>
 #include <loss_layer.h>
index d6793d3..1427e6c 100644 (file)
@@ -24,6 +24,7 @@
 #include <cstring>
 #include <ctime>
 #include <iomanip>
+#include <mutex>
 #include <nntrainer_logger.h>
 #include <sstream>
 #include <stdarg.h>
index 0b4663e..eec4a5b 100644 (file)
  *
  */
 
+#include <cmath>
+#include <fstream>
 #include <iostream>
+
 #include <lazy_tensor.h>
 #include <nntrainer_error.h>
 #include <nntrainer_log.h>
index 6feaa57..e2406dd 100644 (file)
@@ -11,6 +11,9 @@
  *
  */
 
+#include <cstring>
+#include <limits>
+
 #include <layer.h>
 #include <lazy_tensor.h>
 #include <nntrainer_error.h>
index 9a4dd55..eaffb70 100644 (file)
  */
 
 #include <assert.h>
-#include <blas_interface.h>
+#include <cmath>
 #include <cstring>
+#include <fstream>
 #include <iomanip>
 #include <iterator>
-#include <math.h>
-#include <nntrainer_error.h>
-#include <nntrainer_log.h>
-#include <parse_util.h>
 #include <random>
+#include <regex>
 #include <sstream>
 #include <stdio.h>
-#include <tensor.h>
-#include <util_func.h>
 
+#include <blas_interface.h>
 #include <lazy_tensor.h>
+#include <nntrainer_error.h>
+#include <nntrainer_log.h>
+#include <parse_util.h>
+#include <tensor.h>
+#include <util_func.h>
 
 #define transposeloop(cl, ci, cj, ck, sl, si, sj, sk)                 \
   do {                                                                \
index 3394ce3..04b3d44 100644 (file)
@@ -12,8 +12,8 @@
  *
  */
 
-#include <assert.h>
 #include <cstring>
+#include <regex>
 #include <sstream>
 #include <stdio.h>
 
@@ -64,7 +64,7 @@ void TensorDim::setTensorDim(unsigned int idx, unsigned int value) {
       "[TensorDim] Trying to assign value <=0 to tensor dim");
 
   if (len == 0) {
-    for (int i = 0; i < MAXDIM; ++i) {
+    for (size_t i = 0; i < MAXDIM; ++i) {
       dim[i] = 1;
     }
   }
@@ -80,7 +80,7 @@ int TensorDim::setTensorDim(std::string input_shape) {
     std::sregex_iterator(input_shape.begin(), input_shape.end(), words_regex);
   auto words_end = std::sregex_iterator();
   int cur_dim = std::distance(words_begin, words_end);
-  if (cur_dim <= 0 || cur_dim > MAXDIM) {
+  if (cur_dim <= 0 || (size_t)cur_dim > MAXDIM) {
     ml_loge("Tensor Dimension should be between 1 and 4");
     return ML_ERROR_INVALID_PARAMETER;
   }
@@ -93,7 +93,7 @@ int TensorDim::setTensorDim(std::string input_shape) {
 }
 
 bool TensorDim::operator==(const TensorDim &rhs) const {
-  for (int i = 0; i < MAXDIM; ++i) {
+  for (size_t i = 0; i < MAXDIM; ++i) {
     if (this->dim[i] != rhs.dim[i]) {
       return false;
     }
index 31d57a6..19bd21a 100644 (file)
  *
  */
 
-#include <assert.h>
-#include <math.h>
+#include <cmath>
+#include <fstream>
 #include <random>
-#include <tensor.h>
+
 #include <util_func.h>
 
 namespace nntrainer {
index abec18b..38555a4 100644 (file)
@@ -26,6 +26,7 @@
 #include <iostream>
 #include <nntrainer_error.h>
 #include <random>
+#include <regex>
 #include <tensor.h>
 
 #define num_class 10
index 4e702fa..d67ed05 100644 (file)
@@ -10,6 +10,7 @@
  * @bug         No known bugs
  */
 #include <fstream>
+#include <regex>
 
 #include <activation_layer.h>
 #include <addition_layer.h>
index 0261589..c711e64 100644 (file)
@@ -156,25 +156,6 @@ TEST_F(nntrainer_LazyTensorOpsTest, LazyTensorOps_08_p) {
   EXPECT_TRUE(target.chain().sum(3).run() == expected);
 }
 
-TEST_F(nntrainer_LazyTensorOpsTest, ApplyIf_01_p) {
-
-  EXPECT_TRUE(
-    target.chain().applyIf(true, _LIFT(add_i), constant_(4.0), 0.5).run() ==
-    original.add(2.0));
-
-  EXPECT_TRUE(target.chain().applyIf(true, _LIFT(add_i), 2.0f).run() ==
-              original.add(2.0));
-
-  EXPECT_TRUE(target.chain().applyIf(true, _LIFT(add_i), 2.0).run() ==
-              original.add(2.0));
-}
-
-TEST_F(nntrainer_LazyTensorOpsTest, ApplyIf_01_n) {
-  EXPECT_THROW(
-    target.chain().applyIf(true, _LIFT(add_i), constant(4.0, 9, 9, 9, 9)).run(),
-    std::runtime_error);
-}
-
 /**
  * @brief Main gtest
  */
index 3d5ce59..fddc566 100644 (file)
@@ -1979,87 +1979,6 @@ TEST(nntrainer_Tensor, print_large_size) {
   EXPECT_EQ(ss.str(), expected.str());
 }
 
-TEST(nntrainer_Tensor, DISABLED_broadcast_info_n) {
-  nntrainer::Tensor t = ranged(1, 1, 2, 1);
-  nntrainer::Tensor b = ranged(1, 1, 2, 1);
-
-  auto vector_func = [](float *buf, int stride, int size) {
-    float *cur = buf;
-    std::cerr << "[ ";
-    for (int i = 0; i < size; ++i) {
-      std::cerr << *cur << ' ';
-      cur += stride;
-    }
-    std::cerr << "]\n";
-  };
-
-  nntrainer::BroadcastInfo e;
-  EXPECT_NO_THROW(e = t.computeBroadcastInfo(b));
-
-  float *buf = t.getData();
-  float *mbuf = b.getData();
-
-  auto &t_strides = t.getStrides();
-  unsigned int offset;
-  unsigned int m_offset;
-
-  if (e.buffer_axis == -1) {
-    vector_func(buf, t_strides[3], e.buffer_size);
-    vector_func(mbuf, e.strides[3], e.buffer_size);
-  } else {
-    for (unsigned int b = 0; b < t.batch(); ++b) {
-      if (e.buffer_axis == 0) {
-        offset = b * t_strides[0];
-        m_offset = b * e.strides[0];
-        std::cerr << "=====================\n";
-        vector_func(buf + offset, t_strides[3], e.buffer_size);
-        vector_func(mbuf + m_offset, e.strides[3], e.buffer_size);
-        continue;
-      }
-      for (unsigned int c = 0; c < t.channel(); ++c) {
-        if (e.buffer_axis == 1) {
-          offset = b * t_strides[0] + c * t_strides[1];
-          m_offset = b * e.strides[0] + c * e.strides[1];
-          std::cerr << "=====================\n";
-          vector_func(buf + offset, t_strides[3], e.buffer_size);
-          vector_func(mbuf + m_offset, e.strides[3], e.buffer_size);
-          continue;
-        }
-        for (unsigned int h = 0; h < t.height(); ++h) {
-          if (e.buffer_axis == 2) {
-            offset = b * t_strides[0] + c * t_strides[1] + h * t_strides[2];
-            m_offset = b * e.strides[0] + c * e.strides[1] + h * e.strides[2];
-            std::cerr << "=====================\n";
-            vector_func(buf + offset, t_strides[3], e.buffer_size);
-            vector_func(mbuf + m_offset, e.strides[3], e.buffer_size);
-            continue;
-          }
-          for (unsigned int w = 0; w < t.width(); ++w) {
-            offset = b * t_strides[0] + c * t_strides[1] + h * t_strides[2] +
-                     w * t_strides[3];
-            m_offset = b * e.strides[0] + c * e.strides[1] + h * e.strides[2] +
-                       w * e.strides[3];
-            std::cerr << "after width: " << offset << std::endl;
-            std::cerr << "=====================\n";
-            vector_func(buf + offset, t_strides[3], e.buffer_size);
-            vector_func(mbuf + m_offset, e.strides[3], e.buffer_size);
-          }
-        }
-      }
-    }
-  }
-  std::cerr << "buffer_axis: " << e.buffer_axis << std::endl;
-  std::cerr << "t strides: ";
-  for (auto i : t_strides)
-    std::cerr << i << ' ';
-  std::cerr << std::endl;
-  std::cerr << "strides: ";
-  for (auto i : e.strides)
-    std::cerr << i << ' ';
-  std::cerr << std::endl;
-  std::cerr << "buffer_size: " << e.buffer_size << std::endl;
-}
-
 TEST(nntrainer_Tensor, DISABLED_equation_test_01_p) {
   nntrainer::Tensor a, b, c;
   nntrainer::Tensor ret1, ret2;