mlpack
softmax_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_SOFTMAX_IMPL_HPP
14 #define MLPACK_METHODS_ANN_LAYER_SOFTMAX_IMPL_HPP
15 
16 // In case it hasn't yet been included.
17 #include "softmax.hpp"
18 
19 namespace mlpack {
20 namespace ann {
21 
22 template<typename InputDataType, typename OutputDataType>
24 {
25  // Nothing to do here.
26 }
27 
28 template<typename InputDataType, typename OutputDataType>
29 template<typename InputType, typename OutputType>
31  const InputType& input,
32  OutputType& output)
33 {
34  InputType softmaxInput = arma::exp(input.each_row() -
35  arma::max(input, 0));
36  output = softmaxInput.each_row() / sum(softmaxInput, 0);
37 }
38 
39 template<typename InputDataType, typename OutputDataType>
40 template<typename eT>
42  const arma::Mat<eT>& input,
43  const arma::Mat<eT>& gy,
44  arma::Mat<eT>& g)
45 {
46  g = input % (gy - arma::repmat(arma::sum(gy % input), input.n_rows, 1));
47 }
48 
49 template<typename InputDataType, typename OutputDataType>
50 template<typename Archive>
52  Archive& /* ar */,
53  const uint32_t /* version */)
54 {
55  // Nothing to do here.
56 }
57 
58 } // namespace ann
59 } // namespace mlpack
60 
61 #endif
void Backward(const arma::Mat< eT > &input, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
Definition: softmax_impl.hpp:41
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void serialize(Archive &, const uint32_t)
Serialize the layer.
Definition: softmax_impl.hpp:51
Softmax()
Create the Softmax object.
Definition: softmax_impl.hpp:23
void Forward(const InputType &input, OutputType &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
Definition: softmax_impl.hpp:30