[mlpack-git] master: Add ConstantLayer class that outputs a constant value given any input. (c0dbd32)
gitdub at mlpack.org
gitdub at mlpack.org
Mon Apr 11 10:07:46 EDT 2016
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/fb3994c48e59d47b51931d9e7fbfa4777f181ca3...460e326cb2dd1025d45f424bc70967f8d76a4c2f
>---------------------------------------------------------------
commit c0dbd32ac0f5594cfe1949a2ce548604113a9b2e
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date: Mon Apr 11 16:07:46 2016 +0200
Add ConstantLayer class that outputs a constant value given any input.
>---------------------------------------------------------------
c0dbd32ac0f5594cfe1949a2ce548604113a9b2e
src/mlpack/methods/ann/layer/constant_layer.hpp | 116 ++++++++++++++++++++++++
1 file changed, 116 insertions(+)
diff --git a/src/mlpack/methods/ann/layer/constant_layer.hpp b/src/mlpack/methods/ann/layer/constant_layer.hpp
new file mode 100644
index 0000000..a142a67
--- /dev/null
+++ b/src/mlpack/methods/ann/layer/constant_layer.hpp
@@ -0,0 +1,116 @@
+/**
+ * @file constant_layer.hpp
+ * @author Marcus Edel
+ *
+ * Definition of the ConstantLayer class, which outputs a constant value given
+ * any input.
+ */
+#ifndef __MLPACK_METHODS_ANN_LAYER_CONSTANT_LAYER_HPP
+#define __MLPACK_METHODS_ANN_LAYER_CONSTANT_LAYER_HPP
+
+#include <mlpack/core.hpp>
+
+namespace mlpack {
+namespace ann /** Artificial Neural Network. */ {
+
+/**
+ * Implementation of the constant layer. The constant layer outputs a given
+ * constant value given any input value.
+ *
+ * @tparam InputDataType Type of the input data (arma::colvec, arma::mat,
+ * arma::sp_mat or arma::cube).
+ * @tparam OutputDataType Type of the output data (arma::colvec, arma::mat,
+ * arma::sp_mat or arma::cube).
+ */
+template <
+ typename InputDataType = arma::mat,
+ typename OutputDataType = arma::mat
+>
+class ConstantLayer
+{
+ public:
+ /**
+ * Create the ConstantLayer object that outputs a given constant scalar value
+ * given any input value.
+ *
+ * @param outSize The number of output units.
+ * @param scalar The constant value used to create the constant output.
+ */
+ ConstantLayer(const size_t outSize, const double scalar)
+ {
+ constantOutput = OutputDataType(outSize, 1);
+ constantOutput.fill(scalar);
+ }
+
+ /**
+ * Ordinary feed forward pass of a neural network. The forward pass fills the
+ * output with the specified constant parameter.
+ *
+ * @param input Input data used for evaluating the specified function.
+ * @param output Resulting output activation.
+ */
+ template<typename eT>
+ void Forward(const arma::Mat<eT>& /* input */, arma::Mat<eT>& output)
+ {
+ output = constantOutput;
+ }
+
+ /**
+ * Ordinary feed backward pass of a neural network. The backward pass of the
+ * constant layer is returns always a zero output error matrix.
+ *
+ * @param input The propagated input activation.
+ * @param gy The backpropagated error.
+ * @param g The calculated gradient.
+ */
+ template<typename eT>
+ void Backward(const arma::Mat<eT>& /* input */,
+ const arma::Mat<eT>& /* gy */,
+ arma::Mat<eT>& g)
+ {
+ g = arma::zeros<arma::Mat<eT> >(inputParameter.n_rows,
+ inputParameter.n_cols);
+ }
+
+ //! Get the input parameter.
+ InputDataType& InputParameter() const { return inputParameter; }
+ //! Modify the input parameter.
+ InputDataType& InputParameter() { return inputParameter; }
+
+ //! Get the output parameter.
+ OutputDataType& OutputParameter() const { return outputParameter; }
+ //! Modify the output parameter.
+ OutputDataType& OutputParameter() { return outputParameter; }
+
+ //! Get the delta.
+ OutputDataType& Delta() const { return delta; }
+ //! Modify the delta.
+ OutputDataType& Delta() { return delta; }
+
+ /**
+ * Serialize the layer.
+ */
+ template<typename Archive>
+ void Serialize(Archive& ar, const unsigned int /* version */)
+ {
+ ar & data::CreateNVP(constantOutput, "constantOutput");
+ }
+
+ private:
+ //! Locally-stored constant output matrix.
+ OutputDataType constantOutput;
+
+ //! Locally-stored delta object.
+ OutputDataType delta;
+
+ //! Locally-stored input parameter object.
+ InputDataType inputParameter;
+
+ //! Locally-stored output parameter object.
+ OutputDataType outputParameter;
+}; // class ConstantLayer
+
+}; // namespace ann
+}; // namespace mlpack
+
+#endif
More information about the mlpack-git
mailing list