From 3f865aab1d42e47a9f9ac931f002e8127599632f Mon Sep 17 00:00:00 2001 From: Relintai Date: Thu, 28 Dec 2023 11:17:24 +0100 Subject: [PATCH] Cleanups to MLPPMANN. --- mlpp/mann/mann.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mlpp/mann/mann.cpp b/mlpp/mann/mann.cpp index 976f364..b9331c6 100644 --- a/mlpp/mann/mann.cpp +++ b/mlpp/mann/mann.cpp @@ -101,21 +101,23 @@ void MLPPMANN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { if (_output_layer->get_activation() == MLPPActivation::ACTIVATION_FUNCTION_SOFTMAX) { _output_layer->set_delta(_y_hat->subn(_output_set)); } else { - _output_layer->set_delta( - mlpp_cost.run_cost_deriv_matrix(_output_layer->get_cost(), _y_hat, _output_set)->hadamard_productn(avn.run_activation_deriv_matrix(_output_layer->get_activation(), _output_layer->get_z()))); + Ref r1 = mlpp_cost.run_cost_deriv_matrix(_output_layer->get_cost(), _y_hat, _output_set); + Ref r2 = avn.run_activation_deriv_matrix(_output_layer->get_activation(), _output_layer->get_z()); + + _output_layer->set_delta(r1->hadamard_productn(r2)); } Ref output_w_grad = _output_layer->get_input()->transposen()->multn(_output_layer->get_delta()); _output_layer->set_weights(_output_layer->get_weights()->subn(output_w_grad->scalar_multiplyn(learning_rate / _n))); - _output_layer->set_weights(regularization.reg_weightsm(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg())); + _output_layer->set_weights(regularization.reg_weightsm(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), + _output_layer->get_reg())); + _output_layer->set_bias(_output_layer->get_bias()->subtract_matrix_rowsn(_output_layer->get_delta()->scalar_multiplyn(learning_rate / _n))); if (!_network.empty()) { Ref layer = _network[_network.size() - 1]; - //auto hiddenLayerAvn = layer.activation_map[layer.activation]; - layer->set_delta(_output_layer->get_delta()->multn(_output_layer->get_weights()->transposen())->hadamard_productn(avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z()))); Ref hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta()); @@ -129,8 +131,6 @@ void MLPPMANN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { layer = _network[i]; Ref next_layer = _network[i + 1]; - //hiddenLayerAvn = layer.activation_map[layer.activation]; - layer->set_delta(next_layer->get_delta()->multn(next_layer->get_weights())->hadamard_productn(avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z()))); hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());