Cleanups to MLPPMANN.

This commit is contained in:
Relintai 2023-12-28 11:17:24 +01:00
parent 92c1bb0523
commit 3f865aab1d

View File

@ -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) { if (_output_layer->get_activation() == MLPPActivation::ACTIVATION_FUNCTION_SOFTMAX) {
_output_layer->set_delta(_y_hat->subn(_output_set)); _output_layer->set_delta(_y_hat->subn(_output_set));
} else { } else {
_output_layer->set_delta( Ref<MLPPMatrix> r1 = mlpp_cost.run_cost_deriv_matrix(_output_layer->get_cost(), _y_hat, _output_set);
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<MLPPMatrix> r2 = avn.run_activation_deriv_matrix(_output_layer->get_activation(), _output_layer->get_z());
_output_layer->set_delta(r1->hadamard_productn(r2));
} }
Ref<MLPPMatrix> output_w_grad = _output_layer->get_input()->transposen()->multn(_output_layer->get_delta()); Ref<MLPPMatrix> 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(_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))); _output_layer->set_bias(_output_layer->get_bias()->subtract_matrix_rowsn(_output_layer->get_delta()->scalar_multiplyn(learning_rate / _n)));
if (!_network.empty()) { if (!_network.empty()) {
Ref<MLPPHiddenLayer> layer = _network[_network.size() - 1]; Ref<MLPPHiddenLayer> 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()))); 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<MLPPMatrix> hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta()); Ref<MLPPMatrix> 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]; layer = _network[i];
Ref<MLPPHiddenLayer> next_layer = _network[i + 1]; Ref<MLPPHiddenLayer> 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()))); 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()); hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());