From: Jihoon Lee Date: Wed, 2 Jun 2021 07:19:59 +0000 (+0900) Subject: [SplitLayer] Support flexible dimension X-Git-Tag: accepted/tizen/unified/20210829.234903~305 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ef08fe67d7c64276863ed17227ade413b0c313de;p=platform%2Fcore%2Fml%2Fnntrainer.git [SplitLayer] Support flexible dimension This patch adds support for the flexible dimension in split layer. **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Cc: Parichay Kapoor Signed-off-by: Jihoon Lee --- diff --git a/Applications/ProductRatings/jni/main.cpp b/Applications/ProductRatings/jni/main.cpp index b7b6787..9ad8395 100644 --- a/Applications/ProductRatings/jni/main.cpp +++ b/Applications/ProductRatings/jni/main.cpp @@ -212,8 +212,8 @@ int main(int argc, char *argv[]) { std::cout << "Input dimension: " << NN.getInputDimension()[0]; - } catch (...) { - std::cerr << "Unexpected Error during init" << std::endl; + } catch (std::exception &e) { + std::cerr << "Unexpected Error during init " << e.what() << std::endl; return 1; } diff --git a/Applications/ProductRatings/jni/meson.build b/Applications/ProductRatings/jni/meson.build index f5029fe..91fc573 100644 --- a/Applications/ProductRatings/jni/meson.build +++ b/Applications/ProductRatings/jni/meson.build @@ -9,7 +9,7 @@ e = executable('nntrainer_product_ratings', ) # test split example -test('nntrainer_product_ratings', e, args: ['train', +test('app_product_ratings', e, args: ['train', nntr_app_resdir / 'ProductRatings' / 'product_ratings_model.ini', nntr_app_resdir / 'ProductRatings' / 'sample_product_ratings.txt'] ) diff --git a/Applications/ProductRatings/res/product_ratings_model.ini b/Applications/ProductRatings/res/product_ratings_model.ini index 3616451..499f99e 100644 --- a/Applications/ProductRatings/res/product_ratings_model.ini +++ b/Applications/ProductRatings/res/product_ratings_model.ini @@ -5,7 +5,6 @@ Epochs = 100 # Epochs Loss = mse # Loss function : mse (mean squared error) # cross ( cross entropy ) # Save_Path = "product_ratings_model.bin" # enable this to save result -batch_size = 20 # batch size [Optimizer] Type = adam diff --git a/nntrainer/layers/split_layer.cpp b/nntrainer/layers/split_layer.cpp index 72710ce..97a0b39 100644 --- a/nntrainer/layers/split_layer.cpp +++ b/nntrainer/layers/split_layer.cpp @@ -72,10 +72,12 @@ int SplitLayer::initialize(Manager &manager) { * together and all the dimensions after the split_dimension to faciliate * easier splitting of the data. */ - input_reshape_helper = {1, 1, 1, 1}; - for (unsigned int idx = 0; idx < split_dimension; ++idx) { - input_reshape_helper.batch(input_reshape_helper.batch() * - in_dim.getTensorDim(idx)); + leading_helper_dim = 1; + input_reshape_helper.channel(1); + input_reshape_helper.height(1); + input_reshape_helper.width(1); + for (unsigned int idx = 1; idx < split_dimension; ++idx) { + leading_helper_dim *= in_dim.getTensorDim(idx); } input_reshape_helper.height(in_dim.getTensorDim(split_dimension)); @@ -92,9 +94,17 @@ int SplitLayer::initialize(Manager &manager) { output_reshape_helper = input_reshape_helper; output_reshape_helper.height(1); + setBatch(in_dim.batch()); + return status; } +void SplitLayer::setBatch(unsigned int batch) { + Layer::setBatch(batch); + input_reshape_helper.batch(batch * leading_helper_dim); + output_reshape_helper.batch(batch * leading_helper_dim); +} + void SplitLayer::forwarding(bool training) { Tensor &input_ = net_input[0]->getVariableRef(); @@ -153,6 +163,8 @@ void SplitLayer::setProperty(const PropertyType type, case PropertyType::split_dimension: { if (!value.empty()) { status = setUint(split_dimension, value); + NNTR_THROW_IF(split_dimension == 0, std::invalid_argument) + << "[Split] Batch dimension cannot be split dimension"; throw_status(status); } } break; diff --git a/nntrainer/layers/split_layer.h b/nntrainer/layers/split_layer.h index ff91798..a18d3ad 100644 --- a/nntrainer/layers/split_layer.h +++ b/nntrainer/layers/split_layer.h @@ -83,6 +83,11 @@ public: */ void calcDerivative() override; + /** + * @copydoc Layer::setBatch(unsigned int batch) + */ + void setBatch(unsigned int batch) override; + using Layer::setProperty; /** @@ -101,6 +106,8 @@ public: private: unsigned int split_dimension; /** dimension along which to split the input */ + unsigned int leading_helper_dim; /**< batch dimension of helper dimension not + containing the actual batch */ TensorDim input_reshape_helper; /** helper dimension to reshape input */ TensorDim output_reshape_helper; /** helper dimension to reshape outputs */ }; diff --git a/nntrainer/tensor/tensor.cpp b/nntrainer/tensor/tensor.cpp index 9254763..a3dcca6 100644 --- a/nntrainer/tensor/tensor.cpp +++ b/nntrainer/tensor/tensor.cpp @@ -991,9 +991,10 @@ Tensor Tensor::clone() const { } void Tensor::reshape(const TensorDim &d) { - if (d.getDataLen() != dim.getDataLen()) { - throw std::invalid_argument("Error: reshape cannot change the tensor size"); - } + NNTR_THROW_IF(d.getDataLen() != dim.getDataLen(), std::invalid_argument) + << "[Tensor]: reshape cannot change the buffer size, trying reshaping " + "\nfrom " + << getDim() << " to " << d; dim = d; strides = d.computeStrides();