[mlpack-git] master: Refactor softmax layer for new network API. (5e395bd)

gitdub at big.cc.gt.atl.ga.us gitdub at big.cc.gt.atl.ga.us
Tue Sep 1 03:58:12 EDT 2015


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

On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/7b68ca3376ccf081e82146ef02710eedcd4f3aa8...236d5bcda2680cf2c5cae3ce6db7c342cad2842b

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

commit 5e395bd1eb7426d0df8a24107ac0e85d354a05b4
Author: Marcus Edel <marcus.edel at fu-berlin.de>
Date:   Sun Aug 30 17:24:38 2015 +0200

    Refactor softmax layer for new network API.


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

5e395bd1eb7426d0df8a24107ac0e85d354a05b4
 src/mlpack/methods/ann/layer/softmax_layer.hpp | 172 +++++++------------------
 1 file changed, 44 insertions(+), 128 deletions(-)

diff --git a/src/mlpack/methods/ann/layer/softmax_layer.hpp b/src/mlpack/methods/ann/layer/softmax_layer.hpp
index b47ec05..d2396c4 100644
--- a/src/mlpack/methods/ann/layer/softmax_layer.hpp
+++ b/src/mlpack/methods/ann/layer/softmax_layer.hpp
@@ -2,8 +2,7 @@
  * @file softmax_layer.hpp
  * @author Marcus Edel
  *
- * Definition of the SoftmaxLayer class, which implements a standard softmax
- * network layer.
+ * Definition of the SoftmaxLayer class.
  */
 #ifndef __MLPACK_METHODS_ANN_LAYER_SOFTMAX_LAYER_HPP
 #define __MLPACK_METHODS_ANN_LAYER_SOFTMAX_LAYER_HPP
@@ -14,70 +13,25 @@ namespace mlpack {
 namespace ann /** Artificial Neural Network. */ {
 
 /**
- * An implementation of a standard softmax layer.
+ * Implementation of the softmax layer. The softmax loss layer computes the
+ * multinomial logistic loss of the softmax of its inputs.
  *
- * @tparam DataType Type of data (arma::colvec, arma::mat arma::sp_mat or
- * arma::cube).
+ * @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 DataType = arma::colvec>
+template <
+    typename InputDataType = arma::mat,
+    typename OutputDataType = arma::mat
+>
 class SoftmaxLayer
 {
  public:
   /**
-   * Create the SoftmaxLayer object using the specified number of neurons.
-   *
-   * @param layerSize The number of neurons.
-   */
-  SoftmaxLayer(const size_t layerSize) :
-      inputActivations(arma::zeros<DataType>(layerSize)),
-      delta(arma::zeros<DataType>(layerSize)),
-      layerRows(layerSize),
-      layerCols(1),
-      layerSlices(1),
-      outputMaps(1)
-  {
-    // Nothing to do here.
-  }
-
-  /**
-   * Create 2-dimensional SoftmaxLayer object using the specified rows and
-   * columns. In this case, DataType must be arma::mat or arma::sp_mat.
-   *
-   * @param layerRows The number of rows of neurons.
-   * @param layerCols The number of columns of neurons.
-   */
-  SoftmaxLayer(const size_t layerRows, const size_t layerCols) :
-      inputActivations(arma::zeros<DataType>(layerRows, layerCols)),
-      delta(arma::zeros<DataType>(layerRows, layerCols)),
-      layerRows(layerRows),
-      layerCols(layerCols),
-      layerSlices(1),
-      outputMaps(1)
-  {
-    // Nothing to do here.
-  }
-
-  /**
-   * Create n-dimensional SoftmaxLayer object using the specified rows and
-   * columns and number of slices. In this case, DataType must be arma::cube.
-   *
-   * @param layerRows The number of rows of neurons.
-   * @param layerCols The number of columns of neurons.
-   * @param layerCols The number of slices of neurons.
-   * @param layerCols The number of output maps.
+   * Create the SoftmaxLayer object.
    */
-  SoftmaxLayer(const size_t layerRows,
-               const size_t layerCols,
-               const size_t layerSlices,
-               const size_t outputMaps = 1) :
-      inputActivations(arma::zeros<DataType>(layerRows, layerCols,
-          layerSlices * outputMaps)),
-      delta(arma::zeros<DataType>(layerRows, layerCols,
-          layerSlices * outputMaps)),
-      layerRows(layerRows),
-      layerCols(layerCols),
-      layerSlices(layerSlices),
-      outputMaps(outputMaps)
+  SoftmaxLayer()
   {
     // Nothing to do here.
   }
@@ -86,15 +40,15 @@ class SoftmaxLayer
    * Ordinary feed forward pass of a neural network, evaluating the function
    * f(x) by propagating the activity forward through f.
    *
-   * @param inputActivation Input data used for evaluating the specified
-   * activity function.
-   * @param outputActivation Data to store the resulting output activation.
+   * @param input Input data used for evaluating the specified function.
+   * @param output Resulting output activation.
    */
-  void FeedForward(const DataType& inputActivation, DataType& outputActivation)
+  template<typename eT>
+  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output)
   {
-    outputActivation = arma::trunc_exp(inputActivation -
-        arma::repmat(arma::max(inputActivation), inputActivation.n_rows, 1));
-    outputActivation /= arma::accu(outputActivation);
+    output = arma::trunc_exp(input -
+        arma::repmat(arma::max(input), input.n_rows, 1));
+    output /= arma::accu(output);
   }
 
   /**
@@ -102,80 +56,42 @@ class SoftmaxLayer
    * f(x) by propagating x backwards trough f. Using the results from the feed
    * forward pass.
    *
-   * @param inputActivation Input data used for calculating the function f(x).
-   * @param error The backpropagated error.
-   * @param delta The calculating delta using the partial derivative of the
-   * error with respect to a weight.
+   * @param input The propagated input activation.
+   * @param gy The backpropagated error.
+   * @param g The calculated gradient.
    */
-  void FeedBackward(const DataType& /* unused */,
-                    const DataType& error,
-                    DataType& delta)
+  template<typename eT>
+  void Backward(const arma::Mat<eT>& /* unused */,
+                const arma::Mat<eT>& gy,
+                arma::Mat<eT>& g)
   {
-    delta = error;
+    g = gy;
   }
 
-  //! Get the input activations.
-  DataType& InputActivation() const { return inputActivations; }
-  //! Modify the input activations.
-  DataType& InputActivation() { return inputActivations; }
+  //! Get the input parameter.
+  InputDataType& InputParameter() const {return inputParameter; }
+  //! Modify the input parameter.
+  InputDataType& InputParameter() { return inputParameter; }
 
-  //! Get the detla.
-  DataType& Delta() const { return delta; }
-  //! Modify the delta.
-  DataType& Delta() { return delta; }
+  //! Get the output parameter.
+  OutputDataType& OutputParameter() const {return outputParameter; }
+  //! Modify the output parameter.
+  OutputDataType& OutputParameter() { return outputParameter; }
 
-  //! Get input size.
-  size_t InputSize() const { return layerRows; }
+  //! Get the delta.
+  InputDataType& Delta() const {return delta; }
   //! Modify the delta.
-  size_t& InputSize() { return layerRows; }
-
-  //! Get output size.
-  size_t OutputSize() const { return layerRows; }
-  //! Modify the output size.
-  size_t& OutputSize() { return layerRows; }
-
-  //! Get the number of layer rows.
-  size_t LayerRows() const { return layerRows; }
-  //! Modify the number of layer rows.
-  size_t& LayerRows() { return layerRows; }
-
-  //! Get the number of layer columns.
-  size_t LayerCols() const { return layerCols; }
-  //! Modify the number of layer columns.
-  size_t& LayerCols() { return layerCols; }
-
-  //! Get the number of layer slices.
-  size_t LayerSlices() const { return layerSlices; }
-
-  //! Get the number of output maps.
-  size_t OutputMaps() const { return outputMaps; }
-
-  //! The the value of the deterministic parameter.
-  bool Deterministic() const {return deterministic; }
-  //! Modify the value of the deterministic parameter.
-  bool& Deterministic() {return deterministic; }
+  InputDataType& Delta() { return delta; }
 
  private:
-  //! Locally-stored input activation object.
-  DataType inputActivations;
-
   //! Locally-stored delta object.
-  DataType delta;
-
-  //! Locally-stored number of layer rows.
-  size_t layerRows;
-
-  //! Locally-stored number of layer cols.
-  size_t layerCols;
-
-  //! Locally-stored number of layer slices.
-  size_t layerSlices;
+  OutputDataType delta;
 
-  //! Locally-stored number of output maps.
-  size_t outputMaps;
+  //! Locally-stored input parameter object.
+  InputDataType inputParameter;
 
-  //! Locally-stored deterministic parameter.
-  bool deterministic;
+  //! Locally-stored output parameter object.
+  OutputDataType outputParameter;
 }; // class SoftmaxLayer
 
 }; // namespace ann



More information about the mlpack-git mailing list