From 2d29242db85bb2c942a66d854ffef458ed04b0ba Mon Sep 17 00:00:00 2001 From: Aakash Kaushik Date: Tue, 15 Sep 2020 01:34:29 +0530 Subject: [PATCH 01/53] softmin activation function added --- src/mlpack/methods/ann/layer/CMakeLists.txt | 2 + src/mlpack/methods/ann/layer/softmin.hpp | 97 +++++++++++++++++++ src/mlpack/methods/ann/layer/softmin_impl.hpp | 60 ++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 src/mlpack/methods/ann/layer/softmin.hpp create mode 100644 src/mlpack/methods/ann/layer/softmin_impl.hpp diff --git a/src/mlpack/methods/ann/layer/CMakeLists.txt b/src/mlpack/methods/ann/layer/CMakeLists.txt index 34ea03c6a7..b4726b0c6f 100644 --- a/src/mlpack/methods/ann/layer/CMakeLists.txt +++ b/src/mlpack/methods/ann/layer/CMakeLists.txt @@ -116,6 +116,8 @@ set(SOURCES celu_impl.hpp softshrink.hpp softshrink_impl.hpp + softmin.hpp + softmin_impl.hpp ) # Add directory name to sources. diff --git a/src/mlpack/methods/ann/layer/softmin.hpp b/src/mlpack/methods/ann/layer/softmin.hpp new file mode 100644 index 0000000000..3b832e3eae --- /dev/null +++ b/src/mlpack/methods/ann/layer/softmin.hpp @@ -0,0 +1,97 @@ +/** + * @file methods/ann/layer/softmin.hpp + * @author Aakash Kaushik + * + * Definition of the Softmin class. + * + * mlpack is free software; you may redistribute it and/or modify it under the + * terms of the 3-clause BSD license. You should have received a copy of the + * 3-clause BSD license along with mlpack. If not, see + * http://www.opensource.org/licenses/BSD-3-Clause for more information. + */ + +#ifndef MLPACK_METHODS_ANN_LAYER_SOFTMIN_HPP +#define MLPACK_METHODS_ANN_LAYER_SOFTMIN_HPP + +#include + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +/** +* Implementation of the Softmin layer. The Softmin function takes as a input +* a vector of K real numbers, rescaling them so that the elements of the +* K-dimensional output vector lie in the range [0, 1] and sum to 1. +* +* @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 Softmin +{ + public: + /** + * Create the Softmin object. + */ + Softmin(); + + /** + * 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 + void Forward(const InputType& input, OutputType& output); + + /** + * Ordinary feed backward pass of a neural network, calculating the function + * f(x) by propagating x backwards through 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 + void Backward(const arma::mat& input, + const arma::mat& gy, + arma::mat& g); + + //! Get the output parameter. + OutputDataType& OutputParameter() const { return outputParameter }; + //! Modify the output parameter. + OutputDataType& OutputParameter() { return outputParameter }; + + //! Get the delta. + InputDataType& Delta() const { return delta; } + //! Modify the delta. + InputDataType& Delta() { return delta; } + + /** + * Serialize the layer. + */ + template + void serialize(Archive& /* ar */, const unsigned int /* version */); + + private: + //! Locally-stored delta object. + OutputDataType delta; + + //! Locally stored output parameter object. + OutputDataType outputParameter; +}; // class Softmin + +} // namespace ann +} // namespace mlpack + +// Include implementation. +#include "softmin_impl.hpp" + +#endif diff --git a/src/mlpack/methods/ann/layer/softmin_impl.hpp b/src/mlpack/methods/ann/layer/softmin_impl.hpp new file mode 100644 index 0000000000..4e85837438 --- /dev/null +++ b/src/mlpack/methods/ann/layer/softmin_impl.hpp @@ -0,0 +1,60 @@ +/** + * @file methods/ann/layer/softmin_impl.hpp + * @author Aakash Kaushik + * + * Implementation of the Softmin class. + * + * mlpack is free software; you may redistribute it and/or modify it under the + * terms of the 3-clause BSD license. You should have received a copy of the + * 3-clause BSD license along with mlpack. If not, see + * http://www.opensource.org/licenses/BSD-3-Clause for more information. + */ +#ifndef MLPACK_METHODS_ANN_LAYER_SOFTMIN_IMPL_HPP +#define MLPACK_METHODS_ANN_LAYER_SOFTMIN_IMPL_HPP + +// In case it hasn't yet been included. +#include "softmin.hpp" + +namespace mlpack { +namespace ann /** Artificial Neural Network. */ { + +template +Softmin::Softmin() +{ + // Nothing to do here. +} + +template +template +void softmax::Forward( + const InputType& input + OutputType& output) +{ + output = arma::repmat(arma::log(arma::sum( + arma::exp(-input),1)), 1, input.n_cols); + output = arma::exp(- input - output); +} + +template +template +void Softmax::Backward( + const arma::Mat& input, + const arma::Mat& gy, + arma::Mat& g) +{ + //g = Need to figure out. +} + +template +template