diff --git a/src/mlpack/methods/ann/layer/add_impl.hpp b/src/mlpack/methods/ann/layer/add_impl.hpp index e9903981f6..a268956fe8 100644 --- a/src/mlpack/methods/ann/layer/add_impl.hpp +++ b/src/mlpack/methods/ann/layer/add_impl.hpp @@ -23,7 +23,7 @@ template Add::Add(const size_t outSize) : outSize(outSize) { - weights.set_size(outSize, 1); + weights.set_size(WeightSize(), 1); } template diff --git a/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp b/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp index e71bb2cf5e..cfb200e3ec 100644 --- a/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp +++ b/src/mlpack/methods/ann/layer/atrous_convolution_impl.hpp @@ -122,8 +122,7 @@ AtrousConvolution< dilationWidth(dilationWidth), dilationHeight(dilationHeight) { - weights.set_size((outSize * inSize * kernelWidth * kernelHeight) + outSize, - 1); + weights.set_size(WeightSize(), 1); // Transform paddingType to lowercase. std::string paddingTypeLow = paddingType; diff --git a/src/mlpack/methods/ann/layer/concat.hpp b/src/mlpack/methods/ann/layer/concat.hpp index 81df70a1b2..13d29458cb 100644 --- a/src/mlpack/methods/ann/layer/concat.hpp +++ b/src/mlpack/methods/ann/layer/concat.hpp @@ -165,9 +165,9 @@ class Concat } //! Return the initial point for the optimization. - const arma::mat& Parameters() const { return parameters; } + const arma::mat& Parameters() const { return weights; } //! Modify the initial point for the optimization. - arma::mat& Parameters() { return parameters; } + arma::mat& Parameters() { return weights; } //! Get the value of run parameter. bool Run() const { return run; } @@ -196,6 +196,9 @@ class Concat //! Get the axis of concatenation. size_t const& ConcatAxis() const { return axis; } + //! Get the size of the weight matrix. + size_t WeightSize() const { return 0; } + /** * Serialize the layer */ @@ -225,8 +228,8 @@ class Concat //! Locally-stored network modules. std::vector > network; - //! Locally-stored model parameters. - arma::mat parameters; + //! Locally-stored model weights. + OutputDataType weights; //! Locally-stored delta visitor. DeltaVisitor deltaVisitor; diff --git a/src/mlpack/methods/ann/layer/concat_impl.hpp b/src/mlpack/methods/ann/layer/concat_impl.hpp index 694784c7a5..b410361295 100644 --- a/src/mlpack/methods/ann/layer/concat_impl.hpp +++ b/src/mlpack/methods/ann/layer/concat_impl.hpp @@ -33,7 +33,7 @@ Concat::Concat( run(run), channels(1) { - parameters.set_size(0, 0); + weights.set_size(0, 0); } template::Concat( model(model), run(run) { - parameters.set_size(0, 0); + weights.set_size(0, 0); // Parameters to help calculate the number of channels. size_t oldColSize = 1, newColSize = 1; diff --git a/src/mlpack/methods/ann/layer/dropconnect.hpp b/src/mlpack/methods/ann/layer/dropconnect.hpp index 7451705974..db8d76aa4b 100644 --- a/src/mlpack/methods/ann/layer/dropconnect.hpp +++ b/src/mlpack/methods/ann/layer/dropconnect.hpp @@ -115,9 +115,9 @@ class DropConnect std::vector >& Model() { return network; } //! Get the parameters. - OutputDataType const& Parameters() const { return parameters; } + OutputDataType const& Parameters() const { return weights; } //! Modify the parameters. - OutputDataType& Parameters() { return parameters; } + OutputDataType& Parameters() { return weights; } //! Get the output parameter. OutputDataType const& OutputParameter() const { return outputParameter; } @@ -150,6 +150,9 @@ class DropConnect scale = 1.0 / (1.0 - ratio); } + //! Return the size of the weight matrix. + size_t WeightSize() const { return 0; } + /** * Serialize the layer. */ @@ -164,7 +167,7 @@ class DropConnect double scale; //! Locally-stored weight object. - OutputDataType parameters; + OutputDataType weights; //! Locally-stored delta object. OutputDataType delta; diff --git a/src/mlpack/methods/ann/layer/fast_lstm.hpp b/src/mlpack/methods/ann/layer/fast_lstm.hpp index a0400772fd..957c64e670 100644 --- a/src/mlpack/methods/ann/layer/fast_lstm.hpp +++ b/src/mlpack/methods/ann/layer/fast_lstm.hpp @@ -164,6 +164,12 @@ class FastLSTM //! Get the number of output units. size_t OutSize() const { return outSize; } + //! Get the size of the weight matrix. + size_t WeightSize() const + { + return 4 * outSize * inSize + 4 * outSize + 4 * outSize * outSize; + } + /** * Serialize the layer */ diff --git a/src/mlpack/methods/ann/layer/fast_lstm_impl.hpp b/src/mlpack/methods/ann/layer/fast_lstm_impl.hpp index b283dbc5ad..5f5502cf9a 100644 --- a/src/mlpack/methods/ann/layer/fast_lstm_impl.hpp +++ b/src/mlpack/methods/ann/layer/fast_lstm_impl.hpp @@ -42,8 +42,7 @@ FastLSTM::FastLSTM( { // Weights for: input to gate layer (4 * outsize * inSize + 4 * outsize) // and output to gate (4 * outSize). - weights.set_size( - 4 * outSize * inSize + 4 * outSize + 4 * outSize * outSize, 1); + weights.set_size(WeightSize(), 1); } template diff --git a/src/mlpack/methods/ann/layer/linear_impl.hpp b/src/mlpack/methods/ann/layer/linear_impl.hpp index 83a7b1f157..183597d4a7 100644 --- a/src/mlpack/methods/ann/layer/linear_impl.hpp +++ b/src/mlpack/methods/ann/layer/linear_impl.hpp @@ -38,7 +38,7 @@ Linear::Linear( outSize(outSize), regularizer(regularizer) { - weights.set_size(outSize * inSize + outSize, 1); + weights.set_size(WeightSize(), 1); } template linear = new Linear<>(randomSize, randomSize); + LayerTypes<> linearLayer = new Linear<>(randomInSize, randomOutSize); - CheckCorrectnessOfWeightSize(linear); + size_t weightSize = boost::apply_visitor(WeightSizeVisitor(), linearLayer); + + CheckCorrectnessOfWeightSize(linearLayer); +} + +/** + * Test that WeightSizeVisitor works properly for concat layer. + */ +TEST_CASE("WeightSizeVisitorTestForConcatLayer", "[ANNVisitorTest]") +{ + LayerTypes<> concatLayer = new Concat<>(); + + size_t weightSize = boost::apply_visitor(WeightSizeVisitor(), concatLayer); + + CheckCorrectnessOfWeightSize(concatLayer); +} + +/** + * Test that WeightSizeVisitor works properly for fast lstm layer. + */ +TEST_CASE("WeightSizeVisitorTestForFastLSTMLayer", "[ANNVisitorTest]") +{ + size_t randomInSize = arma::randi(arma::distr_param(1, 100)); + size_t randomOutSize = arma::randi(arma::distr_param(1, 100)); + + LayerTypes<> fastLSTMLayer = new FastLSTM<>(randomInSize, randomOutSize); + + size_t weightSize = boost::apply_visitor(WeightSizeVisitor(), fastLSTMLayer); + + CheckCorrectnessOfWeightSize(fastLSTMLayer); +} + +/** + * Test that WeightSizeVisitor works properly for Add layer. + */ +TEST_CASE("WeightSizeVisitorTestForAddLayer", "[ANNVisitorTest]") +{ + size_t randomOutSize = arma::randi(arma::distr_param(1, 100)); + + LayerTypes<> addLayer = new Add<>(randomOutSize); + + size_t weightSize = boost::apply_visitor(WeightSizeVisitor(), addLayer); + + CheckCorrectnessOfWeightSize(addLayer); +} + +/** + * Test that WeightSizeVisitor works properly for Atrous Convolution Layer. + */ +TEST_CASE("WeightSizeVisitorTestForAtrousConvolutionLayer", "[ANNVisitorTest]") +{ + size_t randomInSize = arma::randi(arma::distr_param(1, 100)); + size_t randomOutSize = arma::randi(arma::distr_param(1, 100)); + size_t randomKernelWidth = arma::randi(arma::distr_param(1, 100)); + size_t randomKernelHeight = arma::randi(arma::distr_param(1, 100)); + + LayerTypes<> atrousConvLayer = new AtrousConvolution<>(randomInSize, randomOutSize, + randomKernelWidth, randomKernelHeight); + + size_t weightSize = boost::apply_visitor(WeightSizeVisitor(), + atrousConvLayer); + + CheckCorrectnessOfWeightSize(atrousConvLayer); }