diff --git a/src/mlpack/methods/ann/layer/layer_types.hpp b/src/mlpack/methods/ann/layer/layer_types.hpp index f8d6ef9e3c..74fe05610f 100644 --- a/src/mlpack/methods/ann/layer/layer_types.hpp +++ b/src/mlpack/methods/ann/layer/layer_types.hpp @@ -60,6 +60,11 @@ template class FastLSTM; template class Reparametrization; template class VRClassReward; +template +class Reparametrization; + template::Forward( stdDeviation); gaussianSample = arma::randn>(latentSize, input.n_cols); - output = mean + std::move(stdDeviation) % gaussianSample; + output = mean + stdDeviation % gaussianSample; } template template void Reparametrization::Backward( - const arma::Mat&& input, arma::Mat&& gy, arma::Mat&& g) + const arma::Mat&& /* input */, arma::Mat&& gy, arma::Mat&& g) { arma::Mat softplusDer; - SoftplusFunction::Deriv((input - std::move(mean)) / gaussianSample, - softplusDer); + SoftplusFunction::Deriv(std::move(stdDeviation), softplusDer); g = join_cols(gy % std::move(gaussianSample) % std::move(softplusDer), gy); } @@ -71,9 +70,10 @@ double Reparametrization::klForward( const InputType&& input) { stdDeviation = input.submat(0, 0, latentSize - 1, input.n_cols); + mean = input.submat(latentSize, 0, 2 * latentSize - 1, input.n_cols); - return -0.5 * arma::accu(arma::log(stdDeviation) - stdDeviation - arma::pow( - input.submat(latentSize, 0, 2 * latentSize - 1, input.n_cols), 2) + 1); + return -0.5 * arma::accu(2 * arma::log(stdDeviation) - + arma::pov(stdDeviation, 2) - arma::pow(mean, 2) + 1); } template @@ -82,8 +82,7 @@ void Reparametrization::klBackward( const InputType&& input, OutputType&& output) { - output = join_cols(-1 / input.submat(0, 0, latentSize - 1, input.n_cols) - 1, - input.submat(latentSize, 0, 2 * latentSize - 1, input.n_cols)); + output = join_cols(-1 / stdDeviation + stdDeviation, mean); } template