From c05c9e9593ee54fea83c44a06026cc4dfdee8778 Mon Sep 17 00:00:00 2001 From: Az-r-ow Date: Mon, 8 Jul 2024 11:40:35 +0200 Subject: [PATCH] fix: adam not considering Flatten layer --- src/NeuralNet/Network.cpp | 11 ++++++----- src/NeuralNet/optimizers/Adam.hpp | 3 +++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/NeuralNet/Network.cpp b/src/NeuralNet/Network.cpp index 0af39e3..cba3558 100644 --- a/src/NeuralNet/Network.cpp +++ b/src/NeuralNet/Network.cpp @@ -351,11 +351,12 @@ void Network::updateOptimizerSetup(size_t numLayers) { if (std::dynamic_pointer_cast(this->optimizer)) { // Get the number of dense layers - nLayers = - std::count_if(this->layers.begin(), this->layers.end(), - [](const std::shared_ptr &ptr) { - return std::dynamic_pointer_cast(ptr) != nullptr; - }); + nLayers = std::count_if( + this->layers.begin(), this->layers.end(), + [](const std::shared_ptr &ptr) { + return std::dynamic_pointer_cast(ptr) != nullptr || + std::dynamic_pointer_cast(ptr) != nullptr; + }); } this->optimizer->insiderInit(nLayers); diff --git a/src/NeuralNet/optimizers/Adam.hpp b/src/NeuralNet/optimizers/Adam.hpp index bd472f0..7afa1e6 100644 --- a/src/NeuralNet/optimizers/Adam.hpp +++ b/src/NeuralNet/optimizers/Adam.hpp @@ -58,6 +58,9 @@ class Adam : public Optimizer { v = Eigen::MatrixBase::Zero(param.rows(), param.cols()); } + assert(gradients.rows() == m.rows() && gradients.cols() == m.cols()); + assert(gradients.rows() == v.rows() && gradients.cols() == v.cols()); + // update biased first moment estimate m = (beta1 * m).array() + ((1 - beta2) * gradients.array()).array();