diff --git a/src/mlpack/methods/ann/layer/base_layer.hpp b/src/mlpack/methods/ann/layer/base_layer.hpp index 1b9f0d8b87..876d87e6f2 100644 --- a/src/mlpack/methods/ann/layer/base_layer.hpp +++ b/src/mlpack/methods/ann/layer/base_layer.hpp @@ -120,12 +120,12 @@ class BaseLayer } //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType const& InputParameter() const { return inputParameter; } //! Modify the input parameter. InputDataType& InputParameter() { return inputParameter; } //! Get the output parameter. - OutputDataType& OutputParameter() const { return outputParameter; } + OutputDataType const& OutputParameter() const { return outputParameter; } //! Modify the output parameter. OutputDataType& OutputParameter() { return outputParameter; } @@ -133,6 +133,15 @@ class BaseLayer OutputDataType const& Delta() const { return delta; } //! Modify the delta. OutputDataType& Delta() { return delta; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& /* ar */, const unsigned int /* version */) + { + /* Nothing to do here */ + } private: //! Locally-stored delta object. diff --git a/src/mlpack/methods/ann/layer/bias_layer.hpp b/src/mlpack/methods/ann/layer/bias_layer.hpp index 11d293d2a7..10b8b90436 100644 --- a/src/mlpack/methods/ann/layer/bias_layer.hpp +++ b/src/mlpack/methods/ann/layer/bias_layer.hpp @@ -123,29 +123,39 @@ class BiasLayer } //! Get the weights. - InputDataType& Weights() const { return weights; } + InputDataType const& Weights() const { return weights; } //! Modify the weights. InputDataType& Weights() { return weights; } //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType const& InputParameter() const { return inputParameter; } //! Modify the input parameter. InputDataType& InputParameter() { return inputParameter; } //! Get the output parameter. - OutputDataType& OutputParameter() const { return outputParameter; } + OutputDataType const& OutputParameter() const { return outputParameter; } //! Modify the output parameter. OutputDataType& OutputParameter() { return outputParameter; } //! Get the delta. - OutputDataType& Delta() const { return delta; } + OutputDataType const& Delta() const { return delta; } //! Modify the delta. OutputDataType& Delta() { return delta; } //! Get the gradient. - InputDataType& Gradient() const { return gradient; } + InputDataType const& Gradient() const { return gradient; } //! Modify the gradient. InputDataType& Gradient() { return gradient; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& ar, const unsigned int /* version */) + { + ar & data::CreateNVP(weights, "weights"); + ar & CreateNVP(bias, "bias"); + } private: //! Locally-stored number of output units. diff --git a/src/mlpack/methods/ann/layer/binary_classification_layer.hpp b/src/mlpack/methods/ann/layer/binary_classification_layer.hpp index ae5fb46aee..368c1ecda9 100644 --- a/src/mlpack/methods/ann/layer/binary_classification_layer.hpp +++ b/src/mlpack/methods/ann/layer/binary_classification_layer.hpp @@ -60,6 +60,15 @@ class BinaryClassificationLayer for (size_t i = 0; i < output.n_elem; i++) output(i) = output(i) > 0.5 ? 1 : 0; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& /* ar */, const unsigned int /* version */) + { + /* Nothing to do here */ + } }; // class BinaryClassificationLayer //! Layer traits for the binary class classification layer. diff --git a/src/mlpack/methods/ann/layer/conv_layer.hpp b/src/mlpack/methods/ann/layer/conv_layer.hpp index 91c090315d..aeb6c07093 100644 --- a/src/mlpack/methods/ann/layer/conv_layer.hpp +++ b/src/mlpack/methods/ann/layer/conv_layer.hpp @@ -160,29 +160,46 @@ class ConvLayer } //! Get the weights. - OutputDataType& Weights() const { return weights; } + OutputDataType const& Weights() const { return weights; } //! Modify the weights. OutputDataType& Weights() { return weights; } //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType const& InputParameter() const { return inputParameter; } //! Modify the input parameter. InputDataType& InputParameter() { return inputParameter; } //! Get the output parameter. - OutputDataType& OutputParameter() const { return outputParameter; } + OutputDataType const& OutputParameter() const { return outputParameter; } //! Modify the output parameter. OutputDataType& OutputParameter() { return outputParameter; } //! Get the delta. - OutputDataType& Delta() const { return delta; } + OutputDataType const& Delta() const { return delta; } //! Modify the delta. OutputDataType& Delta() { return delta; } //! Get the gradient. - OutputDataType& Gradient() const { return gradient; } + OutputDataType const& Gradient() const { return gradient; } //! Modify the gradient. OutputDataType& Gradient() { return gradient; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& ar, const unsigned int /* version */) + { + ar & data::CreateNVP(weights, "weights"); + ar & data::CreateNVP(wfilter, "wfilter"); + ar & data::CreateNVP(hfilter, "hfilter"); + ar & data::CreateNVP(inMaps, "inMaps"); + ar & data::CreateNVP(outMaps, "outMaps"); + ar & data::CreateNVP(xStride, "xStride"); + ar & data::CreateNVP(yStride, "yStride"); + ar & data::CreateNVP(wPad, "wPad"); + ar & data::CreateNVP(hPad, "hPad"); + } private: /* diff --git a/src/mlpack/methods/ann/layer/dropout_layer.hpp b/src/mlpack/methods/ann/layer/dropout_layer.hpp index c9da7216a0..05d38c6915 100644 --- a/src/mlpack/methods/ann/layer/dropout_layer.hpp +++ b/src/mlpack/methods/ann/layer/dropout_layer.hpp @@ -147,17 +147,17 @@ class DropoutLayer } //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType const& InputParameter() const { return inputParameter; } //! Modify the input parameter. InputDataType& InputParameter() { return inputParameter; } //! Get the output parameter. - OutputDataType& OutputParameter() const { return outputParameter; } + OutputDataType const& OutputParameter() const { return outputParameter; } //! Modify the output parameter. OutputDataType& OutputParameter() { return outputParameter; } //! Get the detla. - OutputDataType& Delta() const { return delta; } + OutputDataType const& Delta() const { return delta; } //! Modify the delta. OutputDataType& Delta() { return delta; } @@ -180,6 +180,16 @@ class DropoutLayer bool Rescale() const {return rescale; } //! Modify the value of the rescale parameter. bool& Rescale() {return rescale; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& ar, const unsigned int /* version */) + { + ar & data::CreateNVP(ratio, "ratio"); + ar & data::CreateNVP(rescale, "rescale"); + } private: //! Locally-stored delta object. diff --git a/src/mlpack/methods/ann/layer/linear_layer.hpp b/src/mlpack/methods/ann/layer/linear_layer.hpp index 1c3a1fa60c..b34e978c36 100644 --- a/src/mlpack/methods/ann/layer/linear_layer.hpp +++ b/src/mlpack/methods/ann/layer/linear_layer.hpp @@ -111,29 +111,38 @@ class LinearLayer } //! Get the weights. - OutputDataType& Weights() const { return weights; } + OutputDataType const& Weights() const { return weights; } //! Modify the weights. OutputDataType& Weights() { return weights; } //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType const& InputParameter() const { return inputParameter; } //! Modify the input parameter. InputDataType& InputParameter() { return inputParameter; } //! Get the output parameter. - OutputDataType& OutputParameter() const { return outputParameter; } + OutputDataType const& OutputParameter() const { return outputParameter; } //! Modify the output parameter. OutputDataType& OutputParameter() { return outputParameter; } //! Get the delta. - OutputDataType& Delta() const { return delta; } + OutputDataType const& Delta() const { return delta; } //! Modify the delta. OutputDataType& Delta() { return delta; } //! Get the gradient. - OutputDataType& Gradient() const { return gradient; } + OutputDataType const& Gradient() const { return gradient; } //! Modify the gradient. OutputDataType& Gradient() { return gradient; } + + /** + * Serialize the layer + */ + template + void Serialize(Archive& ar, const unsigned int /* version */) + { + ar & data::CreateNVP(weights, "weights"); + } private: /* diff --git a/src/mlpack/methods/ann/layer/lstm_layer.hpp b/src/mlpack/methods/ann/layer/lstm_layer.hpp index ee57456346..59c103c15a 100644 --- a/src/mlpack/methods/ann/layer/lstm_layer.hpp +++ b/src/mlpack/methods/ann/layer/lstm_layer.hpp @@ -248,27 +248,27 @@ class LSTMLayer } //! Get the peephole weights. - OutputDataType& Weights() const { return peepholeWeights; } + OutputDataType const& Weights() const { return peepholeWeights; } //! Modify the peephole weights. OutputDataType& Weights() { return peepholeWeights; } //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType const& InputParameter() const { return inputParameter; } //! Modify the input parameter. InputDataType& InputParameter() { return inputParameter; } //! Get the output parameter. - OutputDataType& OutputParameter() const { return outputParameter; } + OutputDataType const& OutputParameter() const { return outputParameter; } //! Modify the output parameter. OutputDataType& OutputParameter() { return outputParameter; } //! Get the delta. - OutputDataType& Delta() const { return delta; } + OutputDataType const& Delta() const { return delta; } //! Modify the delta. OutputDataType& Delta() { return delta; } //! Get the peephole gradient. - OutputDataType& Gradient() const { return peepholeGradient; } + OutputDataType const& Gradient() const { return peepholeGradient; } //! Modify the peephole gradient. OutputDataType& Gradient() { return peepholeGradient; } @@ -276,6 +276,26 @@ class LSTMLayer size_t SeqLen() const { return seqLen; } //! Modify the sequence length. size_t& SeqLen() { return seqLen; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& ar, const unsigned int /* version */) + { + ar & data::CreateNVP(peepholes, "peepholes"); + + if (peepholes) + { + ar & data::CreateNVP(peepholeWeights, "peepholeWeights"); + + if (Archive::is_loading::value) + { + peepholeDerivatives = arma::zeros( + peepholeWeights.n_rows, 3); + } + } + } private: //! Locally-stored number of output units. diff --git a/src/mlpack/methods/ann/layer/one_hot_layer.hpp b/src/mlpack/methods/ann/layer/one_hot_layer.hpp index a4dc6f4360..34458c3b2d 100644 --- a/src/mlpack/methods/ann/layer/one_hot_layer.hpp +++ b/src/mlpack/methods/ann/layer/one_hot_layer.hpp @@ -64,11 +64,12 @@ class OneHotLayer } /** - * Serialize the layer + * Serialize the layer. */ template - void Serialize(Archive& ar, const unsigned int /* version */) - { + void Serialize(Archive& /* ar */, const unsigned int /* version */) + { + /* Nothing to do here */ } }; // class OneHotLayer diff --git a/src/mlpack/methods/ann/layer/pooling_layer.hpp b/src/mlpack/methods/ann/layer/pooling_layer.hpp index 64add9b1d3..e19ddceff2 100644 --- a/src/mlpack/methods/ann/layer/pooling_layer.hpp +++ b/src/mlpack/methods/ann/layer/pooling_layer.hpp @@ -133,26 +133,28 @@ class PoolingLayer } //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType const& InputParameter() const { return inputParameter; } //! Modify the input parameter. InputDataType& InputParameter() { return inputParameter; } //! Get the output parameter. - InputDataType& OutputParameter() const { return outputParameter; } + InputDataType const& OutputParameter() const { return outputParameter; } //! Modify the output parameter. InputDataType& OutputParameter() { return outputParameter; } //! Get the delta. - OutputDataType& Delta() const { return delta; } + OutputDataType const& Delta() const { return delta; } //! Modify the delta. OutputDataType& Delta() { return delta; } /** - * Serialize the layer + * Serialize the layer. */ template void Serialize(Archive& ar, const unsigned int /* version */) - { + { + ar & data::CreateNVP(kSize, "kSize"); + ar & data::CreateNVP(pooling, "pooling"); } private: diff --git a/src/mlpack/methods/ann/layer/recurrent_layer.hpp b/src/mlpack/methods/ann/layer/recurrent_layer.hpp index 729179f7ed..639852eedc 100644 --- a/src/mlpack/methods/ann/layer/recurrent_layer.hpp +++ b/src/mlpack/methods/ann/layer/recurrent_layer.hpp @@ -100,34 +100,44 @@ class RecurrentLayer } //! Get the weights. - OutputDataType& Weights() const { return weights; } + OutputDataType const& Weights() const { return weights; } //! Modify the weights. OutputDataType& Weights() { return weights; } //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType const& InputParameter() const { return inputParameter; } //! Modify the input parameter. InputDataType& InputParameter() { return inputParameter; } //! Get the input parameter. - InputDataType& RecurrentParameter() const { return recurrentParameter; } + InputDataType const& RecurrentParameter() const { return recurrentParameter; } //! Modify the input parameter. InputDataType& RecurrentParameter() { return recurrentParameter; } //! Get the output parameter. - OutputDataType& OutputParameter() const { return outputParameter; } + OutputDataType const& OutputParameter() const { return outputParameter; } //! Modify the output parameter. OutputDataType& OutputParameter() { return outputParameter; } //! Get the delta. - OutputDataType& Delta() const { return delta; } + OutputDataType const& Delta() const { return delta; } //! Modify the delta. OutputDataType& Delta() { return delta; } //! Get the gradient. - OutputDataType& Gradient() const { return gradient; } + OutputDataType const& Gradient() const { return gradient; } //! Modify the gradient. OutputDataType& Gradient() { return gradient; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& ar, const unsigned int /* version */) + { + ar & data::CreateNVP(recurrentParameter, "recurrentParameter"); + ar & data::CreateNVP(weights, "weights"); + } private: //! Locally-stored number of input units. diff --git a/src/mlpack/methods/ann/layer/softmax_layer.hpp b/src/mlpack/methods/ann/layer/softmax_layer.hpp index 4bfd27d64e..151ebfef41 100644 --- a/src/mlpack/methods/ann/layer/softmax_layer.hpp +++ b/src/mlpack/methods/ann/layer/softmax_layer.hpp @@ -69,19 +69,28 @@ class SoftmaxLayer } //! Get the input parameter. - InputDataType& InputParameter() const { return inputParameter; } + InputDataType const& InputParameter() const { return inputParameter; } //! Modify the input parameter. InputDataType& InputParameter() { return inputParameter; } //! Get the output parameter. - OutputDataType& OutputParameter() const { return outputParameter; } + OutputDataType const& OutputParameter() const { return outputParameter; } //! Modify the output parameter. OutputDataType& OutputParameter() { return outputParameter; } //! Get the delta. - InputDataType& Delta() const { return delta; } + InputDataType const& Delta() const { return delta; } //! Modify the delta. InputDataType& Delta() { return delta; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& /* ar */, const unsigned int /* version */) + { + /* Nothing to do here */ + } private: //! Locally-stored delta object. diff --git a/src/mlpack/methods/ann/layer/sparse_bias_layer.hpp b/src/mlpack/methods/ann/layer/sparse_bias_layer.hpp index 8fc33c378e..9b79536e5c 100644 --- a/src/mlpack/methods/ann/layer/sparse_bias_layer.hpp +++ b/src/mlpack/methods/ann/layer/sparse_bias_layer.hpp @@ -116,6 +116,16 @@ class SparseBiasLayer InputDataType const& Gradient() const { return gradient; } //! Modify the gradient. InputDataType& Gradient() { return gradient; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& ar, const unsigned int /* version */) + { + ar & data::CreateNVP(weights, "weights"); + ar & data::CreateNVP(batchSize, "batchSize"); + } private: //! Locally-stored number of output units. diff --git a/src/mlpack/methods/ann/layer/sparse_input_layer.hpp b/src/mlpack/methods/ann/layer/sparse_input_layer.hpp index 9b5f27474a..ce5ce7dc8e 100644 --- a/src/mlpack/methods/ann/layer/sparse_input_layer.hpp +++ b/src/mlpack/methods/ann/layer/sparse_input_layer.hpp @@ -114,9 +114,19 @@ class SparseInputLayer OutputDataType& Delta() { return delta; } //! Get the gradient. - OutputDataType& Gradient() const { return gradient; } + OutputDataType const& Gradient() const { return gradient; } //! Modify the gradient. OutputDataType& Gradient() { return gradient; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& ar, const unsigned int /* version */) + { + ar & data::CreateNVP(weights, "weights"); + ar & data::CreateNVP(lambda, "lambda"); + } private: //! Locally-stored number of input units. diff --git a/src/mlpack/methods/ann/layer/sparse_output_layer.hpp b/src/mlpack/methods/ann/layer/sparse_output_layer.hpp index ae1597fafb..3022e2a17a 100644 --- a/src/mlpack/methods/ann/layer/sparse_output_layer.hpp +++ b/src/mlpack/methods/ann/layer/sparse_output_layer.hpp @@ -155,6 +155,18 @@ class SparseOutputLayer OutputDataType const& Gradient() const { return gradient; } //! Modify the gradient. OutputDataType& Gradient() { return gradient; } + + /** + * Serialize the layer. + */ + template + void Serialize(Archive& ar, const unsigned int /* version */) + { + ar & data::CreateNVP(weights, "weights"); + ar & data::CreateNVP(lambda, "lambda"); + ar & data::CreateNVP(beta, "beta"); + ar & data::CreateNVP(rho, "rho"); + } private: //! Locally-stored number of input units.