12 #ifndef MLPACK_METHODS_ANN_LAYER_NOISYLINEAR_IMPL_HPP 13 #define MLPACK_METHODS_ANN_LAYER_NOISYLINEAR_IMPL_HPP 21 template<
typename InputDataType,
typename OutputDataType>
29 template<
typename InputDataType,
typename OutputDataType>
33 outSize(layer.outSize),
34 weights(layer.weights)
39 template<
typename InputDataType,
typename OutputDataType>
42 const size_t outSize) :
47 weightEpsilon.set_size(outSize, inSize);
48 biasEpsilon.set_size(outSize, 1);
51 template<
typename InputDataType,
typename OutputDataType>
54 inSize(
std::move(layer.inSize)),
55 outSize(
std::move(layer.outSize)),
56 weights(
std::move(layer.weights))
60 layer.weights =
nullptr;
64 template<
typename InputDataType,
typename OutputDataType>
70 inSize = layer.inSize;
71 outSize = layer.outSize;
72 weights = layer.weights;
78 template<
typename InputDataType,
typename OutputDataType>
84 inSize = std::move(layer.inSize);
86 outSize = std::move(layer.outSize);
88 weights = std::move(layer.weights);
89 layer.weights =
nullptr;
95 template<
typename InputDataType,
typename OutputDataType>
98 weightMu = arma::mat(weights.memptr(),
99 outSize, inSize,
false,
false);
100 biasMu = arma::mat(weights.memptr() + weightMu.n_elem,
101 outSize, 1,
false,
false);
102 weightSigma = arma::mat(weights.memptr() + weightMu.n_elem + biasMu.n_elem,
103 outSize, inSize,
false,
false);
104 biasSigma = arma::mat(weights.memptr() + weightMu.n_elem * 2 + biasMu.n_elem,
105 outSize, 1,
false,
false);
109 template<
typename InputDataType,
typename OutputDataType>
112 arma::mat epsilonIn = arma::randn<arma::mat>(inSize, 1);
113 epsilonIn = arma::sign(epsilonIn) % arma::sqrt(arma::abs(epsilonIn));
114 arma::mat epsilonOut = arma::randn<arma::mat>(outSize, 1);
115 epsilonOut = arma::sign(epsilonOut) % arma::sqrt(arma::abs(epsilonOut));
116 weightEpsilon = epsilonOut * epsilonIn.t();
117 biasEpsilon = epsilonOut;
120 template<
typename InputDataType,
typename OutputDataType>
123 const double muRange = 1 / std::sqrt(inSize);
125 weightMu = muRange * (weightMu * 2 - 1);
127 biasMu = muRange * (biasMu * 2 - 1);
128 weightSigma.fill(0.5 / std::sqrt(inSize));
129 biasSigma.fill(0.5 / std::sqrt(outSize));
132 template<
typename InputDataType,
typename OutputDataType>
133 template<
typename eT>
135 const arma::Mat<eT>& input, arma::Mat<eT>& output)
137 weight = weightMu + weightSigma % weightEpsilon;
138 bias = biasMu + biasSigma % biasEpsilon;
139 output = weight * input;
140 output.each_col() += bias;
143 template<
typename InputDataType,
typename OutputDataType>
144 template<
typename eT>
146 const arma::Mat<eT>& ,
const arma::Mat<eT>& gy, arma::Mat<eT>& g)
151 template<
typename InputDataType,
typename OutputDataType>
152 template<
typename eT>
154 const arma::Mat<eT>& input,
155 const arma::Mat<eT>& error,
156 arma::Mat<eT>& gradient)
159 arma::mat weightGrad = error * input.t();
162 gradient.rows(0, weight.n_elem - 1) = arma::vectorise(weightGrad);
163 gradient.rows(weight.n_elem, weight.n_elem + bias.n_elem - 1)
164 = arma::sum(error, 1);
167 gradient.rows(weight.n_elem + bias.n_elem, gradient.n_elem - bias.n_elem - 1)
168 = arma::vectorise(weightGrad % weightEpsilon);
169 gradient.rows(gradient.n_elem - bias.n_elem, gradient.n_elem - 1)
170 = arma::sum(error, 1) % biasEpsilon;
173 template<
typename InputDataType,
typename OutputDataType>
174 template<
typename Archive>
176 Archive& ar,
const uint32_t )
178 ar(CEREAL_NVP(inSize));
179 ar(CEREAL_NVP(outSize));
183 if (cereal::is_loading<Archive>())
184 weights.set_size((outSize * inSize + outSize) * 2, 1);
OutputDataType const & Gradient() const
Get the gradient.
Definition: noisylinear.hpp:138
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
Definition: noisylinear_impl.hpp:134
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
size_t WeightSize() const
Get size of weights.
Definition: noisylinear.hpp:152
Definition: pointer_wrapper.hpp:23
NoisyLinear & operator=(const NoisyLinear &layer)
Operator= copy constructor.
Definition: noisylinear_impl.hpp:66
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: noisylinear_impl.hpp:175
Implementation of the NoisyLinear layer class.
Definition: layer_types.hpp:107
void Backward(const arma::Mat< eT > &, 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: noisylinear_impl.hpp:145
NoisyLinear()
Create the NoisyLinear object.
Definition: noisylinear_impl.hpp:22