mirror of
https://github.com/Relintai/pmlpp.git
synced 2024-11-08 13:12:09 +01:00
More cleanups.
This commit is contained in:
parent
6558d0d2f6
commit
94db644c69
133
mlpp/gan/gan.cpp
133
mlpp/gan/gan.cpp
@ -7,7 +7,6 @@
|
||||
#include "gan.h"
|
||||
#include "../activation/activation.h"
|
||||
#include "../cost/cost.h"
|
||||
#include "../lin_alg/lin_alg.h"
|
||||
#include "../regularization/reg.h"
|
||||
#include "../utilities/utilities.h"
|
||||
|
||||
@ -40,53 +39,53 @@ void MLPPGAN::set_k(const int val) {
|
||||
*/
|
||||
|
||||
Ref<MLPPMatrix> MLPPGAN::generate_example(int n) {
|
||||
MLPPLinAlg alg;
|
||||
|
||||
return model_set_test_generator(alg.gaussian_noise(n, _k));
|
||||
return model_set_test_generator(MLPPMatrix::create_gaussian_noise(n, _k));
|
||||
}
|
||||
|
||||
void MLPPGAN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
|
||||
MLPPCost mlpp_cost;
|
||||
MLPPLinAlg alg;
|
||||
real_t cost_prev = 0;
|
||||
int epoch = 1;
|
||||
|
||||
forward_pass();
|
||||
|
||||
while (true) {
|
||||
cost_prev = cost(_y_hat, alg.onevecnv(_n));
|
||||
cost_prev = cost(_y_hat, MLPPVector::create_vec_one(_n));
|
||||
|
||||
// Training of the discriminator.
|
||||
|
||||
Ref<MLPPMatrix> generator_input_set = alg.gaussian_noise(_n, _k);
|
||||
Ref<MLPPMatrix> generator_input_set = MLPPMatrix::create_gaussian_noise(_n, _k);
|
||||
Ref<MLPPMatrix> discriminator_input_set = model_set_test_generator(generator_input_set);
|
||||
discriminator_input_set->rows_add_mlpp_matrix(_output_set); // Fake + real inputs.
|
||||
|
||||
Ref<MLPPVector> y_hat = model_set_test_discriminator(discriminator_input_set);
|
||||
Ref<MLPPVector> output_set = alg.zerovecnv(_n);
|
||||
Ref<MLPPVector> output_set_real = alg.onevecnv(_n);
|
||||
Ref<MLPPVector> output_set = MLPPVector::create_vec_zero(_n);
|
||||
Ref<MLPPVector> output_set_real = MLPPVector::create_vec_one(_n);
|
||||
output_set->append_mlpp_vector(output_set_real); // Fake + real output scores.
|
||||
|
||||
ComputeDiscriminatorGradientsResult dgrads = compute_discriminator_gradients(y_hat, _output_set);
|
||||
|
||||
dgrads.cumulative_hidden_layer_w_grad = alg.scalar_multiplynvt(learning_rate / _n, dgrads.cumulative_hidden_layer_w_grad);
|
||||
dgrads.output_w_grad = alg.scalar_multiplynv(learning_rate / _n, dgrads.output_w_grad);
|
||||
dgrads.cumulative_hidden_layer_w_grad->scalar_multiply(learning_rate / _n);
|
||||
dgrads.output_w_grad->scalar_multiply(learning_rate / _n);
|
||||
|
||||
update_discriminator_parameters(dgrads.cumulative_hidden_layer_w_grad, dgrads.output_w_grad, learning_rate);
|
||||
|
||||
// Training of the generator.
|
||||
generator_input_set = alg.gaussian_noise(_n, _k);
|
||||
generator_input_set = MLPPMatrix::create_gaussian_noise(_n, _k);
|
||||
discriminator_input_set = model_set_test_generator(generator_input_set);
|
||||
y_hat = model_set_test_discriminator(discriminator_input_set);
|
||||
_output_set = alg.onevecnv(_n);
|
||||
_output_set = MLPPVector::create_vec_one(_n);
|
||||
|
||||
Ref<MLPPTensor3> cumulative_generator_hidden_layer_w_grad = compute_generator_gradients(y_hat, _output_set);
|
||||
|
||||
cumulative_generator_hidden_layer_w_grad->scalar_multiply(learning_rate / _n);
|
||||
|
||||
Vector<Ref<MLPPMatrix>> cumulative_generator_hidden_layer_w_grad = compute_generator_gradients(y_hat, _output_set);
|
||||
cumulative_generator_hidden_layer_w_grad = alg.scalar_multiplynvt(learning_rate / _n, cumulative_generator_hidden_layer_w_grad);
|
||||
update_generator_parameters(cumulative_generator_hidden_layer_w_grad, learning_rate);
|
||||
|
||||
forward_pass();
|
||||
|
||||
if (ui) {
|
||||
print_ui(epoch, cost_prev, _y_hat, alg.onevecnv(_n));
|
||||
print_ui(epoch, cost_prev, _y_hat, MLPPVector::create_vec_one(_n));
|
||||
}
|
||||
|
||||
epoch++;
|
||||
@ -98,12 +97,11 @@ void MLPPGAN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) {
|
||||
}
|
||||
|
||||
real_t MLPPGAN::score() {
|
||||
MLPPLinAlg alg;
|
||||
MLPPUtilities util;
|
||||
|
||||
forward_pass();
|
||||
|
||||
return util.performance_vec(_y_hat, alg.onevecnv(_n));
|
||||
return util.performance_vec(_y_hat, MLPPVector::create_vec_one(_n));
|
||||
}
|
||||
|
||||
void MLPPGAN::save(const String &file_name) {
|
||||
@ -122,9 +120,8 @@ void MLPPGAN::save(const String &file_name) {
|
||||
}
|
||||
|
||||
void MLPPGAN::add_layer(int n_hidden, MLPPActivation::ActivationFunction activation, MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) {
|
||||
MLPPLinAlg alg;
|
||||
if (_network.empty()) {
|
||||
Ref<MLPPHiddenLayer> layer = Ref<MLPPHiddenLayer>(memnew(MLPPHiddenLayer(n_hidden, activation, alg.gaussian_noise(_n, _k), weight_init, reg, lambda, alpha)));
|
||||
Ref<MLPPHiddenLayer> layer = Ref<MLPPHiddenLayer>(memnew(MLPPHiddenLayer(n_hidden, activation, MLPPMatrix::create_gaussian_noise(_n, _k), weight_init, reg, lambda, alpha)));
|
||||
|
||||
_network.push_back(layer);
|
||||
|
||||
@ -139,12 +136,10 @@ void MLPPGAN::add_layer(int n_hidden, MLPPActivation::ActivationFunction activat
|
||||
}
|
||||
|
||||
void MLPPGAN::add_output_layer(MLPPUtilities::WeightDistributionType weight_init, MLPPReg::RegularizationType reg, real_t lambda, real_t alpha) {
|
||||
MLPPLinAlg alg;
|
||||
|
||||
if (!_network.empty()) {
|
||||
_output_layer = Ref<MLPPOutputLayer>(memnew(MLPPOutputLayer(_network.write[_network.size() - 1]->get_n_hidden(), MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, MLPPCost::COST_TYPE_LOGISTIC_LOSS, _network.write[_network.size() - 1]->get_a(), weight_init, reg, lambda, alpha)));
|
||||
} else {
|
||||
_output_layer = Ref<MLPPOutputLayer>(memnew(MLPPOutputLayer(_k, MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, MLPPCost::COST_TYPE_LOGISTIC_LOSS, alg.gaussian_noise(_n, _k), weight_init, reg, lambda, alpha)));
|
||||
_output_layer = Ref<MLPPOutputLayer>(memnew(MLPPOutputLayer(_k, MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, MLPPCost::COST_TYPE_LOGISTIC_LOSS, MLPPMatrix::create_gaussian_noise(_n, _k), weight_init, reg, lambda, alpha)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -209,10 +204,8 @@ real_t MLPPGAN::cost(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
|
||||
}
|
||||
|
||||
void MLPPGAN::forward_pass() {
|
||||
MLPPLinAlg alg;
|
||||
|
||||
if (!_network.empty()) {
|
||||
_network.write[0]->set_input(alg.gaussian_noise(_n, _k));
|
||||
_network.write[0]->set_input(MLPPMatrix::create_gaussian_noise(_n, _k));
|
||||
_network.write[0]->forward_pass();
|
||||
|
||||
for (int i = 1; i < _network.size(); i++) {
|
||||
@ -221,47 +214,55 @@ void MLPPGAN::forward_pass() {
|
||||
}
|
||||
_output_layer->set_input(_network.write[_network.size() - 1]->get_a());
|
||||
} else { // Should never happen, though.
|
||||
_output_layer->set_input(alg.gaussian_noise(_n, _k));
|
||||
_output_layer->set_input(MLPPMatrix::create_gaussian_noise(_n, _k));
|
||||
}
|
||||
|
||||
_output_layer->forward_pass();
|
||||
_y_hat = _output_layer->get_a();
|
||||
}
|
||||
|
||||
void MLPPGAN::update_discriminator_parameters(const Vector<Ref<MLPPMatrix>> &hidden_layer_updations, const Ref<MLPPVector> &output_layer_updation, real_t learning_rate) {
|
||||
MLPPLinAlg alg;
|
||||
|
||||
_output_layer->set_weights(alg.subtractionnv(_output_layer->get_weights(), output_layer_updation));
|
||||
void MLPPGAN::update_discriminator_parameters(const Ref<MLPPTensor3> &hidden_layer_updations, const Ref<MLPPVector> &output_layer_updation, real_t learning_rate) {
|
||||
_output_layer->set_weights(_output_layer->get_weights()->subn(output_layer_updation));
|
||||
real_t output_layer_bias = _output_layer->get_bias();
|
||||
output_layer_bias -= learning_rate * alg.sum_elementsv(_output_layer->get_delta()) / _n;
|
||||
output_layer_bias -= learning_rate * _output_layer->get_delta()->sum_elements() / _n;
|
||||
_output_layer->set_bias(output_layer_bias);
|
||||
|
||||
Ref<MLPPMatrix> slice;
|
||||
slice.instance();
|
||||
|
||||
if (!_network.empty()) {
|
||||
Ref<MLPPHiddenLayer> layer = _network[_network.size() - 1];
|
||||
|
||||
layer->set_weights(alg.subtractionnm(layer->get_weights(), hidden_layer_updations[0]));
|
||||
layer->set_bias(alg.subtract_matrix_rowsnv(layer->get_bias(), alg.scalar_multiplynm(learning_rate / _n, layer->get_delta())));
|
||||
hidden_layer_updations->z_slice_get_into_mlpp_matrix(0, slice);
|
||||
|
||||
layer->set_weights(layer->get_weights()->subn(slice));
|
||||
layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / _n)));
|
||||
|
||||
for (int i = _network.size() - 2; i > _network.size() / 2; i--) {
|
||||
layer = _network[i];
|
||||
|
||||
layer->set_weights(alg.subtractionnm(layer->get_weights(), hidden_layer_updations[(_network.size() - 2) - i + 1]));
|
||||
layer->set_bias(alg.subtract_matrix_rowsnv(layer->get_bias(), alg.scalar_multiplynm(learning_rate / _n, layer->get_delta())));
|
||||
hidden_layer_updations->z_slice_get_into_mlpp_matrix((_network.size() - 2) - i + 1, slice);
|
||||
|
||||
layer->set_weights(layer->get_weights()->subn(slice));
|
||||
layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / _n)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MLPPGAN::update_generator_parameters(const Vector<Ref<MLPPMatrix>> &hidden_layer_updations, real_t learning_rate) {
|
||||
MLPPLinAlg alg;
|
||||
|
||||
void MLPPGAN::update_generator_parameters(const Ref<MLPPTensor3> &hidden_layer_updations, real_t learning_rate) {
|
||||
if (!_network.empty()) {
|
||||
Ref<MLPPMatrix> slice;
|
||||
slice.instance();
|
||||
|
||||
for (int i = _network.size() / 2; i >= 0; i--) {
|
||||
Ref<MLPPHiddenLayer> layer = _network[i];
|
||||
|
||||
hidden_layer_updations->z_slice_get_into_mlpp_matrix((_network.size() - 2) - i + 1, slice);
|
||||
|
||||
//std::cout << network[i].weights.size() << "x" << network[i].weights[0].size() << std::endl;
|
||||
//std::cout << hidden_layer_updations[(network.size() - 2) - i + 1].size() << "x" << hidden_layer_updations[(network.size() - 2) - i + 1][0].size() << std::endl;
|
||||
layer->set_weights(alg.subtractionnm(layer->get_weights(), hidden_layer_updations[(_network.size() - 2) - i + 1]));
|
||||
layer->set_bias(alg.subtract_matrix_rowsnv(layer->get_bias(), alg.scalar_multiplynm(learning_rate / _n, layer->get_delta())));
|
||||
layer->set_weights(layer->get_weights()->subn(slice));
|
||||
layer->set_bias(layer->get_bias()->subtract_matrix_rowsn(layer->get_delta()->scalar_multiplyn(learning_rate / _n)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -269,7 +270,6 @@ void MLPPGAN::update_generator_parameters(const Vector<Ref<MLPPMatrix>> &hidden_
|
||||
MLPPGAN::ComputeDiscriminatorGradientsResult MLPPGAN::compute_discriminator_gradients(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set) {
|
||||
MLPPCost mlpp_cost;
|
||||
MLPPActivation avn;
|
||||
MLPPLinAlg alg;
|
||||
MLPPReg regularization;
|
||||
|
||||
ComputeDiscriminatorGradientsResult res;
|
||||
@ -277,20 +277,22 @@ MLPPGAN::ComputeDiscriminatorGradientsResult MLPPGAN::compute_discriminator_grad
|
||||
Ref<MLPPVector> cost_deriv = mlpp_cost.run_cost_deriv_vector(_output_layer->get_cost(), y_hat, _output_set);
|
||||
Ref<MLPPVector> activ_deriv = avn.run_activation_deriv_vector(_output_layer->get_activation(), _output_layer->get_z());
|
||||
|
||||
_output_layer->set_delta(alg.hadamard_productnv(cost_deriv, activ_deriv));
|
||||
_output_layer->set_delta(cost_deriv->hadamard_productn(activ_deriv));
|
||||
|
||||
res.output_w_grad = alg.mat_vec_multnv(alg.transposenm(_output_layer->get_input()), _output_layer->get_delta());
|
||||
res.output_w_grad = alg.additionnv(res.output_w_grad, regularization.reg_deriv_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg()));
|
||||
res.output_w_grad = _output_layer->get_input()->transposen()->mult_vec(_output_layer->get_delta());
|
||||
res.output_w_grad->add(regularization.reg_deriv_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg()));
|
||||
|
||||
if (!_network.empty()) {
|
||||
Ref<MLPPHiddenLayer> layer = _network[_network.size() - 1];
|
||||
|
||||
Ref<MLPPVector> hidden_layer_activ_deriv = avn.run_activation_deriv_vector(layer->get_activation(), layer->get_z());
|
||||
|
||||
layer->set_delta(alg.hadamard_productnm(alg.outer_product(_output_layer->get_delta(), _output_layer->get_weights()), hidden_layer_activ_deriv));
|
||||
Ref<MLPPMatrix> hidden_layer_w_grad = alg.matmultnm(alg.transposenm(layer->get_input()), layer->get_delta());
|
||||
layer->set_delta(_output_layer->get_delta()->outer_product(_output_layer->get_weights())->hadamard_productn(hidden_layer_activ_deriv));
|
||||
|
||||
res.cumulative_hidden_layer_w_grad.push_back(alg.additionnm(hidden_layer_w_grad, 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.
|
||||
Ref<MLPPMatrix> hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
|
||||
|
||||
hidden_layer_w_grad->add(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()));
|
||||
res.cumulative_hidden_layer_w_grad->z_slice_add_mlpp_matrix(hidden_layer_w_grad); // Adding to our cumulative hidden layer grads. Maintain reg terms as well.
|
||||
|
||||
for (int i = static_cast<int>(_network.size()) - 2; i > static_cast<int>(_network.size()) / 2; i--) {
|
||||
layer = _network[i];
|
||||
@ -298,41 +300,44 @@ MLPPGAN::ComputeDiscriminatorGradientsResult MLPPGAN::compute_discriminator_grad
|
||||
|
||||
hidden_layer_activ_deriv = avn.run_activation_deriv_vector(layer->get_activation(), layer->get_z());
|
||||
|
||||
layer->set_delta(alg.hadamard_productnm(alg.matmultnm(next_layer->get_delta(), alg.transposenm(next_layer->get_weights())), hidden_layer_activ_deriv));
|
||||
hidden_layer_w_grad = alg.matmultnm(alg.transposenm(layer->get_input()), layer->get_delta());
|
||||
layer->set_delta(next_layer->get_delta()->multn(next_layer->get_weights()->transposen())->hadamard_productn(hidden_layer_activ_deriv));
|
||||
|
||||
res.cumulative_hidden_layer_w_grad.push_back(alg.additionnm(hidden_layer_w_grad, 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.
|
||||
hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
|
||||
|
||||
res.cumulative_hidden_layer_w_grad->z_slice_add_mlpp_matrix(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.
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
Vector<Ref<MLPPMatrix>> MLPPGAN::compute_generator_gradients(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set) {
|
||||
Ref<MLPPTensor3> MLPPGAN::compute_generator_gradients(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set) {
|
||||
MLPPCost mlpp_cost;
|
||||
MLPPActivation avn;
|
||||
MLPPLinAlg alg;
|
||||
MLPPReg regularization;
|
||||
|
||||
Vector<Ref<MLPPMatrix>> cumulative_hidden_layer_w_grad; // Tensor containing ALL hidden grads.
|
||||
Ref<MLPPTensor3> cumulative_hidden_layer_w_grad; // Tensor containing ALL hidden grads.
|
||||
|
||||
Ref<MLPPVector> cost_deriv = mlpp_cost.run_cost_deriv_vector(_output_layer->get_cost(), y_hat, _output_set);
|
||||
Ref<MLPPVector> activ_deriv = avn.run_activation_deriv_vector(_output_layer->get_activation(), _output_layer->get_z());
|
||||
|
||||
_output_layer->set_delta(alg.hadamard_productnv(cost_deriv, activ_deriv));
|
||||
_output_layer->set_delta(cost_deriv->hadamard_productn(activ_deriv));
|
||||
|
||||
Ref<MLPPVector> output_w_grad = alg.mat_vec_multnv(alg.transposenm(_output_layer->get_input()), _output_layer->get_delta());
|
||||
output_w_grad = alg.additionnv(output_w_grad, regularization.reg_deriv_termv(_output_layer->get_weights(), _output_layer->get_lambda(), _output_layer->get_alpha(), _output_layer->get_reg()));
|
||||
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()));
|
||||
|
||||
if (!_network.empty()) {
|
||||
Ref<MLPPHiddenLayer> layer = _network[_network.size() - 1];
|
||||
|
||||
Ref<MLPPVector> hidden_layer_activ_deriv = avn.run_activation_deriv_vector(layer->get_activation(), layer->get_z());
|
||||
|
||||
layer->set_delta(alg.hadamard_productnv(alg.outer_product(_output_layer->get_delta(), _output_layer->get_weights()), hidden_layer_activ_deriv));
|
||||
layer->set_delta(_output_layer->get_delta()->outer_product(_output_layer->get_weights())->hadamard_productn(hidden_layer_activ_deriv));
|
||||
|
||||
Ref<MLPPMatrix> hidden_layer_w_grad = alg.matmultnm(alg.transposenm(layer->get_input()), layer->get_delta());
|
||||
cumulative_hidden_layer_w_grad.push_back(alg.additionnm(hidden_layer_w_grad, 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.
|
||||
Ref<MLPPMatrix> hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
|
||||
hidden_layer_w_grad->add(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()));
|
||||
|
||||
cumulative_hidden_layer_w_grad->z_slice_add_mlpp_matrix(hidden_layer_w_grad); // Adding to our cumulative hidden layer grads. Maintain reg terms as well.
|
||||
|
||||
for (int i = _network.size() - 2; i >= 0; i--) {
|
||||
layer = _network[i];
|
||||
@ -340,10 +345,12 @@ Vector<Ref<MLPPMatrix>> MLPPGAN::compute_generator_gradients(const Ref<MLPPVecto
|
||||
|
||||
hidden_layer_activ_deriv = avn.run_activation_deriv_vector(layer->get_activation(), layer->get_z());
|
||||
|
||||
layer->set_delta(alg.hadamard_productnm(alg.matmultnm(next_layer->get_delta(), alg.transposenm(next_layer->get_weights())), hidden_layer_activ_deriv));
|
||||
layer->set_delta(next_layer->get_delta()->multn(next_layer->get_weights()->transposen())->hadamard_productn(hidden_layer_activ_deriv));
|
||||
|
||||
hidden_layer_w_grad = alg.matmultnm(alg.transposenm(layer->get_input()), layer->get_delta());
|
||||
cumulative_hidden_layer_w_grad.push_back(alg.additionnm(hidden_layer_w_grad, 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.
|
||||
hidden_layer_w_grad = layer->get_input()->transposen()->multn(layer->get_delta());
|
||||
hidden_layer_w_grad->add(regularization.reg_deriv_termm(layer->get_weights(), layer->get_lambda(), layer->get_alpha(), layer->get_reg()));
|
||||
|
||||
cumulative_hidden_layer_w_grad->z_slice_add_mlpp_matrix(hidden_layer_w_grad); // Adding to our cumulative hidden layer grads. Maintain reg terms as well.
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,8 @@
|
||||
#include "../hidden_layer/hidden_layer.h"
|
||||
#include "../output_layer/output_layer.h"
|
||||
|
||||
#include "../lin_alg/mlpp_tensor3.h"
|
||||
|
||||
#include "../activation/activation.h"
|
||||
#include "../utilities/utilities.h"
|
||||
|
||||
@ -57,16 +59,21 @@ protected:
|
||||
|
||||
void forward_pass();
|
||||
|
||||
void update_discriminator_parameters(const Vector<Ref<MLPPMatrix>> &hidden_layer_updations, const Ref<MLPPVector> &output_layer_updation, real_t learning_rate);
|
||||
void update_generator_parameters(const Vector<Ref<MLPPMatrix>> &hidden_layer_updations, real_t learning_rate);
|
||||
void update_discriminator_parameters(const Ref<MLPPTensor3> &hidden_layer_updations, const Ref<MLPPVector> &output_layer_updation, real_t learning_rate);
|
||||
void update_generator_parameters(const Ref<MLPPTensor3> &hidden_layer_updations, real_t learning_rate);
|
||||
|
||||
struct ComputeDiscriminatorGradientsResult {
|
||||
Vector<Ref<MLPPMatrix>> cumulative_hidden_layer_w_grad; // Tensor containing ALL hidden grads.
|
||||
Ref<MLPPTensor3> cumulative_hidden_layer_w_grad; // Tensor containing ALL hidden grads.
|
||||
Ref<MLPPVector> output_w_grad;
|
||||
|
||||
ComputeDiscriminatorGradientsResult() {
|
||||
cumulative_hidden_layer_w_grad.instance();
|
||||
output_w_grad.instance();
|
||||
}
|
||||
};
|
||||
|
||||
ComputeDiscriminatorGradientsResult compute_discriminator_gradients(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set);
|
||||
Vector<Ref<MLPPMatrix>> compute_generator_gradients(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set);
|
||||
Ref<MLPPTensor3> compute_generator_gradients(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set);
|
||||
|
||||
void print_ui(int epoch, real_t cost_prev, const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set);
|
||||
|
||||
|
@ -8,7 +8,6 @@
|
||||
|
||||
#include "core/math/math_defs.h"
|
||||
|
||||
#include "../lin_alg/lin_alg.h"
|
||||
#include "../stat/stat.h"
|
||||
#include "../utilities/utilities.h"
|
||||
|
||||
@ -126,7 +125,6 @@ MLPPGaussianNB::~MLPPGaussianNB() {
|
||||
|
||||
void MLPPGaussianNB::evaluate() {
|
||||
MLPPStat stat;
|
||||
MLPPLinAlg alg;
|
||||
|
||||
// Computing mu_k_y and sigma_k_y
|
||||
_mu->resize(_class_num);
|
||||
@ -160,7 +158,7 @@ void MLPPGaussianNB::evaluate() {
|
||||
_priors->element_set(indx, _priors->element_get(indx));
|
||||
}
|
||||
|
||||
_priors = alg.scalar_multiplynv(real_t(1) / real_t(_output_set->size()), _priors);
|
||||
_priors->scalar_multiply(real_t(1) / real_t(_output_set->size()));
|
||||
|
||||
for (int i = 0; i < _output_set->size(); i++) {
|
||||
LocalVector<real_t> score;
|
||||
|
@ -6,7 +6,6 @@
|
||||
|
||||
#include "hidden_layer.h"
|
||||
#include "../activation/activation.h"
|
||||
#include "../lin_alg/lin_alg.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
@ -5,7 +5,6 @@
|
||||
//
|
||||
|
||||
#include "kmeans.h"
|
||||
#include "../lin_alg/lin_alg.h"
|
||||
#include "../utilities/utilities.h"
|
||||
|
||||
#include "core/math/random_pcg.h"
|
||||
@ -54,8 +53,6 @@ Ref<MLPPMatrix> MLPPKMeans::model_set_test(const Ref<MLPPMatrix> &X) {
|
||||
ERR_FAIL_COND_V(!X.is_valid(), Ref<MLPPMatrix>());
|
||||
ERR_FAIL_COND_V(!_initialized, Ref<MLPPMatrix>());
|
||||
|
||||
MLPPLinAlg alg;
|
||||
|
||||
int input_set_size_y = _input_set->size().y;
|
||||
|
||||
Ref<MLPPMatrix> closest_centroids;
|
||||
@ -83,7 +80,7 @@ Ref<MLPPMatrix> MLPPKMeans::model_set_test(const Ref<MLPPMatrix> &X) {
|
||||
for (int j = 0; j < r0_size; ++j) {
|
||||
_mu->row_get_into_mlpp_vector(j, tmp_mujv);
|
||||
|
||||
bool is_centroid_closer = alg.euclidean_distance(tmp_xiv, tmp_mujv) < alg.euclidean_distance(tmp_xiv, closest_centroid);
|
||||
bool is_centroid_closer = tmp_xiv->euclidean_distance(tmp_mujv) < tmp_xiv->euclidean_distance(closest_centroid);
|
||||
|
||||
if (is_centroid_closer) {
|
||||
closest_centroid->set_from_mlpp_vector(tmp_mujv);
|
||||
@ -99,8 +96,6 @@ Ref<MLPPVector> MLPPKMeans::model_test(const Ref<MLPPVector> &x) {
|
||||
ERR_FAIL_COND_V(!x.is_valid(), Ref<MLPPVector>());
|
||||
ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
|
||||
|
||||
MLPPLinAlg alg;
|
||||
|
||||
Ref<MLPPVector> closest_centroid;
|
||||
closest_centroid.instance();
|
||||
closest_centroid->resize(_mu->size().x);
|
||||
@ -116,7 +111,7 @@ Ref<MLPPVector> MLPPKMeans::model_test(const Ref<MLPPVector> &x) {
|
||||
for (int j = 0; j < mu_size_y; ++j) {
|
||||
_mu->row_get_into_mlpp_vector(j, tmp_mujv);
|
||||
|
||||
if (alg.euclidean_distance(x, tmp_mujv) < alg.euclidean_distance(x, closest_centroid)) {
|
||||
if (x->euclidean_distance(tmp_mujv) < x->euclidean_distance(closest_centroid)) {
|
||||
closest_centroid->set_from_mlpp_vector(tmp_mujv);
|
||||
}
|
||||
}
|
||||
@ -168,8 +163,6 @@ real_t MLPPKMeans::score() {
|
||||
Ref<MLPPVector> MLPPKMeans::silhouette_scores() {
|
||||
ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
|
||||
|
||||
MLPPLinAlg alg;
|
||||
|
||||
Ref<MLPPMatrix> closest_centroids = model_set_test(_input_set);
|
||||
|
||||
ERR_FAIL_COND_V(!closest_centroids.is_valid(), Ref<MLPPVector>());
|
||||
@ -233,7 +226,7 @@ Ref<MLPPVector> MLPPKMeans::silhouette_scores() {
|
||||
if (r_i_tempv->is_equal_approx(r_j_tempv)) {
|
||||
_input_set->row_get_into_mlpp_vector(j, input_set_j_tempv);
|
||||
|
||||
a += alg.euclidean_distance(input_set_i_tempv, input_set_j_tempv);
|
||||
a += input_set_i_tempv->euclidean_distance(input_set_j_tempv);
|
||||
}
|
||||
}
|
||||
|
||||
@ -252,7 +245,7 @@ Ref<MLPPVector> MLPPKMeans::silhouette_scores() {
|
||||
for (int k = 0; k < input_set_size_y; ++k) {
|
||||
_input_set->row_get_into_mlpp_vector(k, input_set_k_tempv);
|
||||
|
||||
sum += alg.euclidean_distance(input_set_i_tempv, input_set_k_tempv);
|
||||
sum += input_set_i_tempv->euclidean_distance(input_set_k_tempv);
|
||||
}
|
||||
|
||||
// NORMALIZE b[i]
|
||||
@ -305,8 +298,6 @@ MLPPKMeans::~MLPPKMeans() {
|
||||
void MLPPKMeans::_evaluate() {
|
||||
ERR_FAIL_COND(!_initialized);
|
||||
|
||||
MLPPLinAlg alg;
|
||||
|
||||
if (_r->size() != Size2i(_k, _input_set->size().y)) {
|
||||
_r->resize(Size2i(_k, _input_set->size().y));
|
||||
}
|
||||
@ -335,16 +326,16 @@ void MLPPKMeans::_evaluate() {
|
||||
_mu->row_get_into_mlpp_vector(0, closest_centroid);
|
||||
_input_set->row_get_into_mlpp_vector(i, input_set_i_tempv);
|
||||
|
||||
closest_centroid_current_dist = alg.euclidean_distance(input_set_i_tempv, closest_centroid);
|
||||
closest_centroid_current_dist = input_set_i_tempv->euclidean_distance(closest_centroid);
|
||||
|
||||
for (int j = 0; j < r_size_x; ++j) {
|
||||
_mu->row_get_into_mlpp_vector(j, mu_j_tempv);
|
||||
|
||||
bool is_centroid_closer = alg.euclidean_distance(input_set_i_tempv, mu_j_tempv) < closest_centroid_current_dist;
|
||||
bool is_centroid_closer = input_set_i_tempv->euclidean_distance(mu_j_tempv) < closest_centroid_current_dist;
|
||||
|
||||
if (is_centroid_closer) {
|
||||
_mu->row_get_into_mlpp_vector(j, closest_centroid);
|
||||
closest_centroid_current_dist = alg.euclidean_distance(input_set_i_tempv, closest_centroid);
|
||||
closest_centroid_current_dist = input_set_i_tempv->euclidean_distance(closest_centroid);
|
||||
closest_centroid_index = j;
|
||||
}
|
||||
}
|
||||
@ -355,8 +346,6 @@ void MLPPKMeans::_evaluate() {
|
||||
|
||||
// This simply computes or re-computes mu_k
|
||||
void MLPPKMeans::_compute_mu() {
|
||||
MLPPLinAlg alg;
|
||||
|
||||
int mu_size_y = _mu->size().y;
|
||||
int r_size_y = _r->size().y;
|
||||
|
||||
@ -385,13 +374,13 @@ void MLPPKMeans::_compute_mu() {
|
||||
|
||||
real_t r_j_i = _r->element_get(j, i);
|
||||
|
||||
alg.scalar_multiplyv(_r->element_get(j, i), input_set_j_tempv, mat_tempv);
|
||||
alg.additionv(num, mat_tempv, num);
|
||||
mat_tempv->scalar_multiplyb(_r->element_get(j, i), input_set_j_tempv);
|
||||
num->add(mat_tempv);
|
||||
|
||||
den += r_j_i;
|
||||
}
|
||||
|
||||
alg.scalar_multiplyv(real_t(1) / real_t(den), num, mu_tempv);
|
||||
mu_tempv->scalar_multiplyb(real_t(1) / real_t(den), num);
|
||||
|
||||
_mu->row_set_mlpp_vector(i, mu_tempv);
|
||||
}
|
||||
@ -422,8 +411,6 @@ void MLPPKMeans::_centroid_initialization() {
|
||||
}
|
||||
|
||||
void MLPPKMeans::_kmeanspp_initialization() {
|
||||
MLPPLinAlg alg;
|
||||
|
||||
RandomPCG rand;
|
||||
rand.randomize();
|
||||
|
||||
@ -461,7 +448,7 @@ void MLPPKMeans::_kmeanspp_initialization() {
|
||||
for (int k = 0; k < i; k++) {
|
||||
_mu->row_get_into_mlpp_vector(k, mu_tempv);
|
||||
|
||||
sum += alg.euclidean_distance(input_set_j_tempv, mu_tempv);
|
||||
sum += input_set_j_tempv->euclidean_distance(mu_tempv);
|
||||
}
|
||||
|
||||
if (sum * sum > max_dist) {
|
||||
@ -476,8 +463,6 @@ void MLPPKMeans::_kmeanspp_initialization() {
|
||||
real_t MLPPKMeans::_cost() {
|
||||
ERR_FAIL_COND_V(!_initialized, 0);
|
||||
|
||||
MLPPLinAlg alg;
|
||||
|
||||
Ref<MLPPVector> input_set_i_tempv;
|
||||
input_set_i_tempv.instance();
|
||||
input_set_i_tempv->resize(_input_set->size().x);
|
||||
@ -500,8 +485,8 @@ real_t MLPPKMeans::_cost() {
|
||||
for (int j = 0; j < r_size_x; j++) {
|
||||
_mu->row_get_into_mlpp_vector(j, mu_j_tempv);
|
||||
|
||||
alg.subtractionv(input_set_i_tempv, mu_j_tempv, sub_tempv);
|
||||
sum += _r->element_get(i, j) * alg.norm_sqv(sub_tempv);
|
||||
sub_tempv->subb(input_set_i_tempv, mu_j_tempv);
|
||||
sum += _r->element_get(i, j) * sub_tempv->norm_sq();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,6 @@
|
||||
//
|
||||
|
||||
#include "knn.h"
|
||||
#include "../lin_alg/lin_alg.h"
|
||||
#include "../utilities/utilities.h"
|
||||
|
||||
#include "core/containers/hash_map.h"
|
||||
@ -72,7 +71,6 @@ MLPPKNN::~MLPPKNN() {
|
||||
PoolIntArray MLPPKNN::nearest_neighbors(const Ref<MLPPVector> &x) {
|
||||
ERR_FAIL_COND_V(!_input_set.is_valid(), PoolIntArray());
|
||||
|
||||
MLPPLinAlg alg;
|
||||
// The nearest neighbors
|
||||
PoolIntArray knn;
|
||||
|
||||
@ -97,7 +95,7 @@ PoolIntArray MLPPKNN::nearest_neighbors(const Ref<MLPPVector> &x) {
|
||||
_input_set->row_get_into_mlpp_vector(j, tmpv1);
|
||||
_input_set->row_get_into_mlpp_vector(neighbor, tmpv2);
|
||||
|
||||
bool is_neighbor_nearer = alg.euclidean_distance(x, tmpv1) < alg.euclidean_distance(x, tmpv2);
|
||||
bool is_neighbor_nearer = x->euclidean_distance(tmpv1) < x->euclidean_distance(tmpv2);
|
||||
|
||||
if (is_neighbor_nearer) {
|
||||
neighbor = j;
|
||||
|
Loading…
Reference in New Issue
Block a user