[mlpack-git] master: Add base layer. (1ffcc65)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Sat Aug 29 08:23:08 EDT 2015


Repository : https://github.com/mlpack/mlpack

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/ea45ace1ff744390a4c35183528eda881eda5c61...fd336238de224ed72fc23b84e1e2f02ae3c879d6

>---------------------------------------------------------------

commit 1ffcc65bc207267b0d66081cb138b48fd54a59fe
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date:   Mon Aug 17 14:33:01 2015 +0200

    Add base layer.


>---------------------------------------------------------------

1ffcc65bc207267b0d66081cb138b48fd54a59fe
 src/mlpack/methods/ann/layer/base_layer.hpp | 129 ++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)

diff --git a/src/mlpack/methods/ann/layer/base_layer.hpp b/src/mlpack/methods/ann/layer/base_layer.hpp
new file mode 100644
index 0000000..47c26ca
--- /dev/null
+++ b/src/mlpack/methods/ann/layer/base_layer.hpp
@@ -0,0 +1,129 @@
+/**
+ * @file base_layer.hpp
+ * @author Marcus Edel
+ *
+ * Definition of the BaseLayer class, which attaches various functions to the
+ * embedding layer.
+ */
+#ifndef __MLPACK_METHODS_ANN_LAYER_BASE_LAYER_HPP
+#define __MLPACK_METHODS_ANN_LAYER_BASE_LAYER_HPP
+
+#include <mlpack/core.hpp>
+#include <mlpack/methods/ann/activation_functions/logistic_function.hpp>
+#include <mlpack/methods/ann/activation_functions/identity_function.hpp>
+
+namespace mlpack {
+namespace ann /** Artificial Neural Network. */ {
+
+/**
+ * Implementation of the base layer. The base layer works as a metaclass which
+ * attaches various functions to the embedding layer.
+ *
+ * A few convenience typedefs are given:
+ *
+ *  - SigmoidLayer
+ *  - IdentityLayer
+ *
+ * @tparam ActivationFunction Activation function used for the embedding layer.
+ * @tparam DataType Type of data (arma::colvec, arma::mat arma::sp_mat or
+ * arma::cube).
+ */
+template <
+    class ActivationFunction = LogisticFunction,
+    typename DataType = arma::colvec
+>
+class BaseLayer
+{
+ public:
+  /**
+   * Create the BaseLayer object using the specified number of units.
+   *
+   * @param inSize The number of input units.
+   * @param outSize The number of output units.
+   */
+  BaseLayer(const size_t inSize, const size_t outSize) :
+      inSize(inSize),
+      outSize(outSize)
+  {
+    // Nothing to do here.
+  }
+
+  /**
+   * Ordinary feed forward pass of a neural network, evaluating the function
+   * f(x) by propagating the activity forward through f.
+   *
+   * @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)
+  {
+    ActivationFunction::fn(input, output);
+  }
+
+  /**
+   * Ordinary feed backward pass of a neural network, calculating the function
+   * f(x) by propagating x backwards trough f. Using the results from the feed
+   * forward pass.
+   *
+   * @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)
+  {
+    arma::Mat<eT> derivative;
+    ActivationFunction::deriv(input, derivative);
+    g = gy % derivative;
+  }
+
+  //! Get the parameter.
+  DataType& Parameter() const {return parameter; }
+  //! Modify the parameter.
+  DataType& Parameter() { return parameter; }
+
+  //! Get the delta.
+  DataType& Delta() const {return delta; }
+  //! Modify the delta.
+  DataType& Delta() { return delta; }
+
+ private:
+  //! Locally-stored number of input units.
+  const size_t inSize;
+
+  //! Locally-stored number of output units.
+  const size_t outSize;
+
+  //! Locally-stored delta object.
+  DataType delta;
+
+  //! Locally-stored parameter object.
+  DataType parameter;
+}; // class BaseLayer
+
+// Convenience typedefs.
+
+/**
+ * Standard Sigmoid-Layer using the logistic activation function.
+ */
+template <
+    class ActivationFunction = LogisticFunction,
+    typename DataType = arma::colvec
+>
+using SigmoidLayer = BaseLayer<ActivationFunction, DataType>;
+
+/**
+ * Standard Identity-Layer using the identity activation function.
+ */
+template <
+    class ActivationFunction = IdentityFunction,
+    typename DataType = arma::colvec
+>
+using IdentityLayer = BaseLayer<ActivationFunction, DataType>;
+
+
+}; // namespace ann
+}; // namespace mlpack
+
+#endif



More information about the mlpack-git mailing list