pmlpp/wgan/wgan.cpp

481 lines
19 KiB
C++
Raw Normal View History

2023-12-30 00:41:59 +01:00
/*************************************************************************/
/* wgan.cpp */
/*************************************************************************/
/* This file is part of: */
/* PMLPP Machine Learning Library */
/* https://github.com/Relintai/pmlpp */
/*************************************************************************/
2023-12-30 00:43:39 +01:00
/* Copyright (c) 2023-present Péter Magyar. */
2023-12-30 00:41:59 +01:00
/* Copyright (c) 2022-2023 Marc Melikyan */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2023-01-24 18:12:23 +01:00
#include "wgan.h"
2023-02-06 15:05:00 +01:00
#include "core/log/logger.h"
2023-01-24 18:12:23 +01:00
#include "../activation/activation.h"
2023-01-24 19:00:54 +01:00
#include "../cost/cost.h"
2023-01-24 18:12:23 +01:00
#include "../regularization/reg.h"
#include "../utilities/utilities.h"
2023-02-06 02:36:22 +01:00
#include "core/object/method_bind_ext.gen.inc"
2023-02-06 12:20:52 +01:00
Ref<MLPPMatrix> MLPPWGAN::get_output_set() {
2023-02-13 00:19:16 +01:00
return _output_set;
2023-02-06 12:20:52 +01:00
}
void MLPPWGAN::set_output_set(const Ref<MLPPMatrix> &val) {
2023-02-13 00:19:16 +01:00
_output_set = val;
2023-02-06 12:20:52 +01:00
}
int MLPPWGAN::get_k() const {
2023-02-13 00:19:16 +01:00
return _k;
2023-02-06 12:20:52 +01:00
}
void MLPPWGAN::set_k(const int val) {
2023-02-13 00:19:16 +01:00
_k = val;
2023-02-06 12:20:52 +01:00
}
2023-02-06 02:36:22 +01:00
Ref<MLPPMatrix> MLPPWGAN::generate_example(int n) {
2023-05-01 10:37:28 +02:00
return model_set_test_generator(MLPPMatrix::create_gaussian_noise(n, _k));
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
void MLPPWGAN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
2023-02-05 18:46:12 +01:00
//MLPPCost mlpp_cost;
2023-02-05 18:16:34 +01:00
real_t cost_prev = 0;
int epoch = 1;
2023-05-01 10:44:40 +02:00
int n = _output_set->size().y;
2023-02-05 18:46:12 +01:00
forward_pass();
2023-02-05 18:16:34 +01:00
const int CRITIC_INTERATIONS = 5; // Wasserstein GAN specific parameter.
while (true) {
2023-05-01 10:44:40 +02:00
cost_prev = cost(_y_hat, MLPPVector::create_vec_one(n));
2023-02-05 18:16:34 +01:00
2023-02-06 12:20:52 +01:00
Ref<MLPPMatrix> generator_input_set;
Ref<MLPPMatrix> discriminator_input_set;
2023-02-06 14:24:56 +01:00
discriminator_input_set.instance();
2023-02-05 18:16:34 +01:00
2023-02-06 12:20:52 +01:00
Ref<MLPPVector> ly_hat;
Ref<MLPPVector> loutput_set;
2023-02-05 18:16:34 +01:00
// Training of the discriminator.
for (int i = 0; i < CRITIC_INTERATIONS; i++) {
2023-05-01 10:44:40 +02:00
generator_input_set = MLPPMatrix::create_gaussian_noise(n, _k);
2023-02-06 12:20:52 +01:00
discriminator_input_set->set_from_mlpp_matrix(model_set_test_generator(generator_input_set));
2023-04-29 15:07:30 +02:00
discriminator_input_set->rows_add_mlpp_matrix(_output_set); // Fake + real inputs.
2023-02-06 12:20:52 +01:00
ly_hat = model_set_test_discriminator(discriminator_input_set);
2023-05-01 10:44:40 +02:00
loutput_set = MLPPVector::create_vec_one(n)->scalar_multiplyn(-1); // WGAN changes y_i = 1 and y_i = 0 to y_i = 1 and y_i = -1
Ref<MLPPVector> output_set_real = MLPPVector::create_vec_one(n);
2023-04-29 14:37:58 +02:00
loutput_set->append_mlpp_vector(output_set_real); // Fake + real output scores.
2023-02-06 12:20:52 +01:00
DiscriminatorGradientResult discriminator_gradient_results = compute_discriminator_gradients(ly_hat, loutput_set);
2023-12-27 23:23:50 +01:00
Vector<Ref<MLPPMatrix>> cumulative_discriminator_hidden_layer_w_grad = discriminator_gradient_results.cumulative_hidden_layer_w_grad;
2023-02-06 12:20:52 +01:00
Ref<MLPPVector> output_discriminator_w_grad = discriminator_gradient_results.output_w_grad;
2023-12-27 23:23:50 +01:00
real_t lrpn = learning_rate / n;
for (int j = 0; j < cumulative_discriminator_hidden_layer_w_grad.size(); ++j) {
cumulative_discriminator_hidden_layer_w_grad.write[j]->scalar_multiply(lrpn);
}
2023-05-01 10:44:40 +02:00
output_discriminator_w_grad->scalar_multiply(learning_rate / n);
2023-02-06 12:20:52 +01:00
update_discriminator_parameters(cumulative_discriminator_hidden_layer_w_grad, output_discriminator_w_grad, learning_rate);
2023-02-05 18:16:34 +01:00
}
// Training of the generator.
2023-05-01 10:44:40 +02:00
generator_input_set = MLPPMatrix::create_gaussian_noise(n, _k);
2023-02-06 14:24:56 +01:00
discriminator_input_set->set_from_mlpp_matrix(model_set_test_generator(generator_input_set));
2023-02-06 12:20:52 +01:00
ly_hat = model_set_test_discriminator(discriminator_input_set);
2023-05-01 10:44:40 +02:00
loutput_set = MLPPVector::create_vec_one(n);
2023-02-05 18:16:34 +01:00
2023-12-27 23:23:50 +01:00
Vector<Ref<MLPPMatrix>> cumulative_generator_hidden_layer_w_grad = compute_generator_gradients(_y_hat, loutput_set);
real_t lrpn = learning_rate / n;
for (int i = 0; i < cumulative_generator_hidden_layer_w_grad.size(); ++i) {
cumulative_generator_hidden_layer_w_grad.write[i]->scalar_multiply(lrpn);
}
2023-02-06 12:20:52 +01:00
update_generator_parameters(cumulative_generator_hidden_layer_w_grad, learning_rate);
2023-02-05 18:46:12 +01:00
forward_pass();
2023-02-05 18:16:34 +01:00
2023-02-06 02:36:22 +01:00
if (ui) {
2023-05-01 10:44:40 +02:00
handle_ui(epoch, cost_prev, _y_hat, MLPPVector::create_vec_one(n));
2023-02-05 18:16:34 +01:00
}
epoch++;
if (epoch > max_epoch) {
break;
}
}
}
real_t MLPPWGAN::score() {
MLPPUtilities util;
2023-02-05 18:46:12 +01:00
forward_pass();
2023-05-01 10:44:40 +02:00
int n = _output_set->size().y;
return util.performance_vec(_y_hat, MLPPVector::create_vec_one(n));
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
void MLPPWGAN::save(const String &file_name) {
2023-02-05 18:16:34 +01:00
MLPPUtilities util;
2023-02-06 02:36:22 +01:00
/*
2023-02-05 18:16:34 +01:00
if (!network.empty()) {
2023-02-06 02:36:22 +01:00
util.saveParameters(file_name, network[0].weights, network[0].bias, 0, 1);
2023-02-05 18:16:34 +01:00
for (uint32_t i = 1; i < network.size(); i++) {
util.saveParameters(fileName, network[i].weights, network[i].bias, 1, i + 1);
}
2023-02-06 02:36:22 +01:00
util.saveParameters(file_name, outputLayer->weights, outputLayer->bias, 1, network.size() + 1);
2023-02-05 18:16:34 +01:00
} else {
2023-02-06 02:36:22 +01:00
util.saveParameters(file_name, outputLayer->weights, outputLayer->bias, 0, network.size() + 1);
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
*/
2023-02-05 18:16:34 +01:00
}
2023-05-01 10:37:28 +02:00
void MLPPWGAN::create_layer(int n_hidden, MLPPActivation::ActivationFunction activation, MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) {
2023-02-06 02:36:22 +01:00
Ref<MLPPHiddenLayer> layer;
layer.instance();
layer->set_n_hidden(n_hidden);
layer->set_activation(activation);
layer->set_weight_init(weight_init);
layer->set_reg(reg);
layer->set_lambda(lambda);
layer->set_alpha(alpha);
2023-05-01 10:44:40 +02:00
int n = _output_set->size().y;
2023-02-13 00:19:16 +01:00
if (_network.empty()) {
2023-05-01 10:44:40 +02:00
layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k));
2023-05-01 10:37:28 +02:00
} else {
layer->set_input(_network.write[_network.size() - 1]->get_a());
}
_network.push_back(layer);
layer->forward_pass();
}
void MLPPWGAN::add_layer(Ref<MLPPHiddenLayer> layer) {
if (!layer.is_valid()) {
return;
}
if (_network.empty()) {
2023-05-01 10:44:40 +02:00
int n = _output_set->size().y;
layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k));
2023-02-05 18:16:34 +01:00
} else {
2023-02-13 00:19:16 +01:00
layer->set_input(_network.write[_network.size() - 1]->get_a());
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
_network.push_back(layer);
2023-02-06 02:36:22 +01:00
layer->forward_pass();
2023-02-05 18:16:34 +01:00
}
2023-05-01 10:37:28 +02:00
Ref<MLPPHiddenLayer> MLPPWGAN::get_layer(const int index) {
ERR_FAIL_INDEX_V(index, _network.size(), Ref<MLPPHiddenLayer>());
return _network[index];
}
void MLPPWGAN::remove_layer(const int index) {
ERR_FAIL_INDEX(index, _network.size());
_network.remove(index);
}
int MLPPWGAN::get_layer_count() const {
return _network.size();
}
2023-02-05 18:16:34 +01:00
2023-02-06 02:36:22 +01:00
void MLPPWGAN::add_output_layer(MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) {
2023-02-13 00:19:16 +01:00
ERR_FAIL_COND(_network.empty());
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
if (!_output_layer.is_valid()) {
_output_layer.instance();
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
_output_layer->set_n_hidden(_network[_network.size() - 1]->get_n_hidden());
_output_layer->set_activation(MLPPActivation::ACTIVATION_FUNCTION_LINEAR);
_output_layer->set_cost(MLPPCost::COST_TYPE_WASSERSTEIN_LOSS);
_output_layer->set_input(_network.write[_network.size() - 1]->get_a());
_output_layer->set_weight_init(weight_init);
_output_layer->set_lambda(lambda);
_output_layer->set_alpha(alpha);
2023-02-05 18:16:34 +01:00
}
2023-05-01 10:44:40 +02:00
MLPPWGAN::MLPPWGAN(int p_k, const Ref<MLPPMatrix> &p_output_set) {
2023-02-13 00:19:16 +01:00
_output_set = p_output_set;
_k = p_k;
2023-02-06 14:24:56 +01:00
2023-02-13 00:19:16 +01:00
_y_hat.instance();
2023-02-05 18:46:12 +01:00
}
MLPPWGAN::MLPPWGAN() {
2023-02-13 00:19:16 +01:00
_k = 0;
2023-02-06 14:24:56 +01:00
2023-02-13 00:19:16 +01:00
_y_hat.instance();
2023-02-05 18:46:12 +01:00
}
MLPPWGAN::~MLPPWGAN() {
}
2023-02-06 02:36:22 +01:00
Ref<MLPPMatrix> MLPPWGAN::model_set_test_generator(const Ref<MLPPMatrix> &X) {
2023-02-13 00:19:16 +01:00
if (!_network.empty()) {
_network.write[0]->set_input(X);
_network.write[0]->forward_pass();
2023-02-05 18:16:34 +01:00
2023-02-13 00:19:16 +01:00
for (int i = 1; i <= _network.size() / 2; ++i) {
_network.write[i]->set_input(_network.write[i - 1]->get_a());
_network.write[i]->forward_pass();
2023-02-05 18:16:34 +01:00
}
}
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
return _network.write[_network.size() / 2]->get_a();
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
Ref<MLPPVector> MLPPWGAN::model_set_test_discriminator(const Ref<MLPPMatrix> &X) {
2023-02-13 00:19:16 +01:00
if (!_network.empty()) {
for (int i = _network.size() / 2 + 1; i < _network.size(); i++) {
if (i == _network.size() / 2 + 1) {
_network.write[i]->set_input(X);
2023-02-05 18:16:34 +01:00
} else {
2023-02-13 00:19:16 +01:00
_network.write[i]->set_input(_network.write[i - 1]->get_a());
2023-02-05 18:16:34 +01:00
}
2023-02-13 00:19:16 +01:00
_network.write[i]->forward_pass();
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
_output_layer->set_input(_network.write[_network.size() - 1]->get_a());
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
_output_layer->forward_pass();
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
return _output_layer->get_a();
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
real_t MLPPWGAN::cost(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
2023-02-05 18:16:34 +01:00
MLPPReg regularization;
2023-02-06 02:36:22 +01:00
MLPPCost mlpp_cost;
2023-02-05 18:16:34 +01:00
2023-02-06 02:36:22 +01:00
real_t total_reg_term = 0;
2023-02-13 00:19:16 +01:00
for (int i = 0; i < _network.size() - 1; ++i) {
Ref<MLPPHiddenLayer> layer = _network[i];
2023-02-06 02:36:22 +01:00
total_reg_term += regularization.reg_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg());
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
total_reg_term += regularization.reg_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg());
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
return mlpp_cost.run_cost_norm_vector(_output_layer->get_cost(), y_hat, y) + total_reg_term;
2023-02-05 18:16:34 +01:00
}
2023-02-05 18:46:12 +01:00
void MLPPWGAN::forward_pass() {
2023-05-01 10:44:40 +02:00
int n = _output_set->size().y;
2023-02-13 00:19:16 +01:00
if (!_network.empty()) {
Ref<MLPPHiddenLayer> layer = _network[0];
2023-02-05 18:16:34 +01:00
2023-05-01 10:44:40 +02:00
layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k));
2023-02-06 02:36:22 +01:00
layer->forward_pass();
2023-02-13 00:19:16 +01:00
for (int i = 1; i < _network.size(); i++) {
layer = _network[i];
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
layer->set_input(_network.write[i - 1]->get_a());
2023-02-06 02:36:22 +01:00
layer->forward_pass();
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
_output_layer->set_input(_network.write[_network.size() - 1]->get_a());
2023-02-05 18:16:34 +01:00
} else { // Should never happen, though.
2023-05-01 10:44:40 +02:00
_output_layer->set_input(MLPPMatrix::create_gaussian_noise(n, _k));
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
_output_layer->forward_pass();
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
_y_hat->set_from_mlpp_vector(_output_layer->get_a());
2023-02-05 18:16:34 +01:00
}
2023-12-27 23:23:50 +01:00
void MLPPWGAN::update_discriminator_parameters(const Vector<Ref<MLPPMatrix>> &hidden_layer_updations, const Ref<MLPPVector> &output_layer_updation, real_t learning_rate) {
2023-05-01 10:44:40 +02:00
int n = _output_set->size().y;
2023-05-01 10:37:28 +02:00
_output_layer->set_weights(_output_layer->get_weights()->subn(output_layer_updation));
2023-05-01 10:44:40 +02:00
_output_layer->set_bias(_output_layer->get_bias() - learning_rate * _output_layer->get_delta()->sum_elements() / n);
2023-02-05 18:16:34 +01:00
2023-02-13 00:19:16 +01:00
if (!_network.empty()) {
Ref<MLPPHiddenLayer> layer = _network[_network.size() - 1];
2023-02-05 18:16:34 +01:00
2023-12-27 23:23:50 +01:00
Ref<MLPPMatrix> slice = hidden_layer_updations[0];
2023-05-01 10:37:28 +02:00
layer->set_weights(layer->get_weights()->subn(slice));
2023-05-01 10:44:40 +02:00
layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / n)));
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
for (int i = _network.size() - 2; i > _network.size() / 2; i--) {
layer = _network[i];
2023-02-06 02:36:22 +01:00
2023-12-27 23:23:50 +01:00
slice = hidden_layer_updations[(_network.size() - 2) - i + 1];
2023-05-01 10:37:28 +02:00
layer->set_weights(layer->get_weights()->subn(slice));
2023-05-01 10:44:40 +02:00
layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / n)));
2023-02-05 18:16:34 +01:00
}
}
}
2023-12-27 23:23:50 +01:00
void MLPPWGAN::update_generator_parameters(const Vector<Ref<MLPPMatrix>> &hidden_layer_updations, real_t learning_rate) {
2023-02-13 00:19:16 +01:00
if (!_network.empty()) {
2023-05-01 10:44:40 +02:00
int n = _output_set->size().y;
2023-05-01 10:37:28 +02:00
Ref<MLPPMatrix> slice;
2023-02-13 00:19:16 +01:00
for (int i = _network.size() / 2; i >= 0; i--) {
Ref<MLPPHiddenLayer> layer = _network[i];
2023-02-06 02:36:22 +01:00
2023-12-27 23:23:50 +01:00
slice = hidden_layer_updations[(_network.size() - 2) - i + 1];
2023-05-01 10:37:28 +02:00
2023-02-05 18:16:34 +01:00
//std::cout << network[i].weights.size() << "x" << network[i].weights[0].size() << std::endl;
//std::cout << hiddenLayerUpdations[(network.size() - 2) - i + 1].size() << "x" << hiddenLayerUpdations[(network.size() - 2) - i + 1][0].size() << std::endl;
2023-05-01 10:37:28 +02:00
layer->set_weights(layer->get_weights()->subn(slice));
2023-05-01 10:44:40 +02:00
layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / n)));
2023-02-05 18:16:34 +01:00
}
}
}
2023-02-06 02:36:22 +01:00
MLPPWGAN::DiscriminatorGradientResult MLPPWGAN::compute_discriminator_gradients(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set) {
MLPPCost mlpp_cost;
2023-02-05 18:16:34 +01:00
MLPPActivation avn;
MLPPReg regularization;
2023-02-06 02:36:22 +01:00
DiscriminatorGradientResult data;
2023-05-01 10:37:28 +02:00
_output_layer->set_delta(mlpp_cost.run_cost_deriv_vector(_output_layer->get_cost(), y_hat, output_set)->hadamard_productn(avn.run_activation_deriv_vector(_output_layer->get_activation(), _output_layer->get_z())));
2023-02-05 18:16:34 +01:00
2023-05-01 10:37:28 +02:00
data.output_w_grad = _output_layer->get_input()->transposen()->mult_vec(_output_layer->get_delta());
data.output_w_grad->add(regularization.reg_deriv_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg()));
2023-02-05 18:16:34 +01:00
2023-02-13 00:19:16 +01:00
if (!_network.empty()) {
Ref<MLPPHiddenLayer> layer = _network[_network.size() - 1];
2023-02-05 18:16:34 +01:00
2023-05-01 10:37:28 +02:00
layer->set_delta(_output_layer->get_delta()->outer_product(_output_layer->get_weights())->hadamard_productn(avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z())));
2023-02-06 15:05:00 +01:00
2023-05-01 10:37:28 +02:00
Ref<MLPPMatrix> hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
2023-02-05 18:16:34 +01:00
2023-12-27 23:23:50 +01:00
data.cumulative_hidden_layer_w_grad.push_back(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well.
2023-02-05 18:16:34 +01:00
//std::cout << "HIDDENLAYER FIRST:" << hiddenLayerWGrad.size() << "x" << hiddenLayerWGrad[0].size() << std::endl;
2023-02-06 02:36:22 +01:00
//std::cout << "WEIGHTS SECOND:" << layer.weights.size() << "x" << layer.weights[0].size() << std::endl;
2023-02-05 18:16:34 +01:00
2023-02-13 00:19:16 +01:00
for (int i = _network.size() - 2; i > _network.size() / 2; i--) {
layer = _network[i];
Ref<MLPPHiddenLayer> next_layer = _network[i + 1];
2023-02-05 18:16:34 +01:00
2023-05-01 10:37:28 +02:00
layer->set_delta(next_layer->get_delta()->multn(next_layer->get_weights()->transposen())->hadamard_productn(avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z())));
2023-02-06 02:36:22 +01:00
2023-05-01 10:37:28 +02:00
hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
2023-12-27 23:23:50 +01:00
data.cumulative_hidden_layer_w_grad.push_back(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well.
2023-02-05 18:16:34 +01:00
}
}
2023-02-06 02:36:22 +01:00
return data;
2023-02-05 18:16:34 +01:00
}
2023-12-27 23:23:50 +01:00
Vector<Ref<MLPPMatrix>> MLPPWGAN::compute_generator_gradients(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set) {
2023-02-05 18:16:34 +01:00
class MLPPCost cost;
MLPPActivation avn;
MLPPReg regularization;
2023-12-27 23:23:50 +01:00
Vector<Ref<MLPPMatrix>> cumulative_hidden_layer_w_grad; // Tensor containing ALL hidden grads.
2023-02-05 18:16:34 +01:00
2023-02-13 00:19:16 +01:00
Ref<MLPPVector> cost_deriv_vector = cost.run_cost_deriv_vector(_output_layer->get_cost(), y_hat, output_set);
Ref<MLPPVector> activation_deriv_vector = avn.run_activation_deriv_vector(_output_layer->get_activation(), _output_layer->get_z());
2023-02-06 12:20:52 +01:00
2023-05-01 10:37:28 +02:00
_output_layer->set_delta(cost_deriv_vector->hadamard_productn(activation_deriv_vector));
2023-02-06 12:20:52 +01:00
2023-05-01 10:37:28 +02:00
Ref<MLPPVector> output_w_grad = _output_layer->get_input()->transposen()->mult_vec(_output_layer->get_delta());
output_w_grad->add(regularization.reg_deriv_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg()));
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
if (!_network.empty()) {
Ref<MLPPHiddenLayer> layer = _network[_network.size() - 1];
2023-02-06 12:20:52 +01:00
2023-02-06 14:24:56 +01:00
Ref<MLPPMatrix> activation_deriv_matrix = avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z());
2023-05-01 10:37:28 +02:00
layer->set_delta(_output_layer->get_delta()->outer_product(_output_layer->get_weights())->hadamard_productn(activation_deriv_matrix));
2023-02-06 12:20:52 +01:00
2023-05-01 10:37:28 +02:00
Ref<MLPPMatrix> hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
2023-12-27 23:23:50 +01:00
cumulative_hidden_layer_w_grad.push_back(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well.
2023-02-06 12:20:52 +01:00
2023-02-13 00:19:16 +01:00
for (int i = _network.size() - 2; i >= 0; i--) {
layer = _network[i];
Ref<MLPPHiddenLayer> next_layer = _network[i + 1];
2023-02-06 12:20:52 +01:00
2023-02-06 14:24:56 +01:00
activation_deriv_matrix = avn.run_activation_deriv_matrix(layer->get_activation(), layer->get_z());
2023-02-06 12:20:52 +01:00
2023-05-01 10:37:28 +02:00
layer->set_delta(next_layer->get_delta()->multn(next_layer->get_weights()->transposen())->hadamard_productn(activation_deriv_matrix));
hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
2023-12-27 23:23:50 +01:00
cumulative_hidden_layer_w_grad.push_back(hidden_layer_w_grad->addn(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()))); // Adding to our cumulative hidden layer grads. Maintain reg terms as well.
2023-02-05 18:16:34 +01:00
}
}
2023-02-06 02:36:22 +01:00
2023-02-06 12:20:52 +01:00
return cumulative_hidden_layer_w_grad;
2023-02-05 18:16:34 +01:00
}
2023-02-06 02:36:22 +01:00
void MLPPWGAN::handle_ui(int epoch, real_t cost_prev, const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set) {
MLPPUtilities::cost_info(epoch, cost_prev, cost(y_hat, output_set));
2023-02-06 15:05:00 +01:00
2023-02-13 00:19:16 +01:00
PLOG_MSG("Layer " + itos(_network.size() + 1) + ":");
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
MLPPUtilities::print_ui_vb(_output_layer->get_weights(), _output_layer->get_bias());
2023-02-06 02:36:22 +01:00
2023-02-13 00:19:16 +01:00
if (!_network.empty()) {
for (int i = _network.size() - 1; i >= 0; i--) {
Ref<MLPPHiddenLayer> layer = _network[i];
2023-02-06 02:36:22 +01:00
2023-02-06 15:05:00 +01:00
PLOG_MSG("Layer " + itos(i + 1) + ":");
2023-02-06 02:36:22 +01:00
2023-02-06 15:05:00 +01:00
MLPPUtilities::print_ui_mb(layer->get_weights(), layer->get_bias());
2023-02-05 18:16:34 +01:00
}
}
}
2023-02-05 18:46:12 +01:00
void MLPPWGAN::_bind_methods() {
2023-02-06 12:20:52 +01:00
ClassDB::bind_method(D_METHOD("get_output_set"), &MLPPWGAN::get_output_set);
ClassDB::bind_method(D_METHOD("set_output_set", "val"), &MLPPWGAN::set_output_set);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_output_set", "get_output_set");
ClassDB::bind_method(D_METHOD("get_k"), &MLPPWGAN::get_k);
ClassDB::bind_method(D_METHOD("set_k", "val"), &MLPPWGAN::set_k);
ADD_PROPERTY(PropertyInfo(Variant::INT, "k"), "set_k", "get_k");
ClassDB::bind_method(D_METHOD("generate_example", "n"), &MLPPWGAN::generate_example);
ClassDB::bind_method(D_METHOD("gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPWGAN::gradient_descent, false);
ClassDB::bind_method(D_METHOD("score"), &MLPPWGAN::score);
ClassDB::bind_method(D_METHOD("save", "file_name"), &MLPPWGAN::save);
2023-05-01 10:37:28 +02:00
ClassDB::bind_method(D_METHOD("create_layer", "activation", "weight_init", "reg", "lambda", "alpha"), &MLPPWGAN::create_layer, MLPPUtilities::WEIGHT_DISTRIBUTION_TYPE_DEFAULT, MLPPReg::REGULARIZATION_TYPE_NONE, 0.5, 0.5);
ClassDB::bind_method(D_METHOD("add_layer", "layer"), &MLPPWGAN::add_layer);
ClassDB::bind_method(D_METHOD("get_layer", "index"), &MLPPWGAN::get_layer);
ClassDB::bind_method(D_METHOD("remove_layer", "index"), &MLPPWGAN::remove_layer);
ClassDB::bind_method(D_METHOD("get_layer_count"), &MLPPWGAN::score);
2023-02-06 12:20:52 +01:00
ClassDB::bind_method(D_METHOD("add_output_layer", "weight_init", "reg", "lambda", "alpha"), &MLPPWGAN::add_output_layer, MLPPUtilities::WEIGHT_DISTRIBUTION_TYPE_DEFAULT, MLPPReg::REGULARIZATION_TYPE_NONE, 0.5, 0.5);
2023-02-05 18:46:12 +01:00
}