diff --git a/mlpp/ann/ann.cpp b/mlpp/ann/ann.cpp index 75a7955..2ca5304 100644 --- a/mlpp/ann/ann.cpp +++ b/mlpp/ann/ann.cpp @@ -78,7 +78,7 @@ void MLPPANN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { ComputeGradientsResult grads = compute_gradients(_y_hat, _output_set); - grads.cumulative_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, grads.cumulative_hidden_layer_w_grad); + grads.cumulative_hidden_layer_w_grad = alg.scalar_multiplynvt(learning_rate / _n, grads.cumulative_hidden_layer_w_grad); grads.output_w_grad = alg.scalar_multiplynv(learning_rate / _n, grads.output_w_grad); update_parameters(grads.cumulative_hidden_layer_w_grad, grads.output_w_grad, learning_rate); // subject to change. may want bias to have this matrix too. @@ -136,7 +136,7 @@ void MLPPANN::sgd(real_t learning_rate, int max_epoch, bool ui) { ComputeGradientsResult grads = compute_gradients(y_hat_row_tmp, output_set_row_tmp); - grads.cumulative_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, grads.cumulative_hidden_layer_w_grad); + grads.cumulative_hidden_layer_w_grad = alg.scalar_multiplynvt(learning_rate / _n, grads.cumulative_hidden_layer_w_grad); grads.output_w_grad = alg.scalar_multiplynv(learning_rate / _n, grads.output_w_grad); update_parameters(grads.cumulative_hidden_layer_w_grad, grads.output_w_grad, learning_rate); // subject to change. may want bias to have this matrix too. @@ -183,7 +183,7 @@ void MLPPANN::mbgd(real_t learning_rate, int max_epoch, int mini_batch_size, boo ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch); - grads.cumulative_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, grads.cumulative_hidden_layer_w_grad); + grads.cumulative_hidden_layer_w_grad = alg.scalar_multiplynvt(learning_rate / _n, grads.cumulative_hidden_layer_w_grad); grads.output_w_grad = alg.scalar_multiplynv(learning_rate / _n, grads.output_w_grad); update_parameters(grads.cumulative_hidden_layer_w_grad, grads.output_w_grad, learning_rate); // subject to change. may want bias to have this matrix too. @@ -238,7 +238,7 @@ void MLPPANN::momentum(real_t learning_rate, int max_epoch, int mini_batch_size, ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch); if (!_network.empty() && v_hidden.empty()) { // Initing our tensor - v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden = alg.resizenvt(v_hidden, grads.cumulative_hidden_layer_w_grad); } if (v_output->size() == 0) { @@ -249,7 +249,7 @@ void MLPPANN::momentum(real_t learning_rate, int max_epoch, int mini_batch_size, update_parameters(v_hidden, v_output, 0); // DON'T update bias. } - v_hidden = alg.addition_vt(alg.scalar_multiply_vm(gamma, v_hidden), alg.scalar_multiply_vm(learning_rate / _n, grads.cumulative_hidden_layer_w_grad)); + v_hidden = alg.additionnvt(alg.scalar_multiplynvt(gamma, v_hidden), alg.scalar_multiplynvt(learning_rate / _n, grads.cumulative_hidden_layer_w_grad)); v_output = alg.additionnv(alg.scalar_multiplynv(gamma, v_output), alg.scalar_multiplynv(learning_rate / _n, grads.output_w_grad)); update_parameters(v_hidden, v_output, learning_rate); // subject to change. may want bias to have this matrix too. @@ -304,17 +304,17 @@ void MLPPANN::adagrad(real_t learning_rate, int max_epoch, int mini_batch_size, ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch); if (!_network.empty() && v_hidden.empty()) { // Initing our tensor - v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden = alg.resizenvt(v_hidden, grads.cumulative_hidden_layer_w_grad); } if (v_output->size() == 0) { v_output->resize(grads.output_w_grad->size()); } - v_hidden = alg.addition_vt(v_hidden, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2)); + v_hidden = alg.additionnvt(v_hidden, alg.exponentiatenvt(grads.cumulative_hidden_layer_w_grad, 2)); v_output = alg.additionnv(v_output, alg.exponentiatenv(grads.output_w_grad, 2)); - Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_divisionnv_vt(grads.cumulative_hidden_layer_w_grad, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden)))); + Vector> hidden_layer_updations = alg.scalar_multiplynvt(learning_rate / _n, alg.element_wise_divisionnvnvt(grads.cumulative_hidden_layer_w_grad, alg.scalar_addnvt(e, alg.sqrtnvt(v_hidden)))); Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_divisionnv(grads.output_w_grad, alg.scalar_addnv(e, alg.sqrtnv(v_output)))); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. @@ -368,17 +368,17 @@ void MLPPANN::adadelta(real_t learning_rate, int max_epoch, int mini_batch_size, ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch); if (!_network.empty() && v_hidden.empty()) { // Initing our tensor - v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden = alg.resizenvt(v_hidden, grads.cumulative_hidden_layer_w_grad); } if (v_output->size() == 0) { v_output->resize(grads.output_w_grad->size()); } - v_hidden = alg.addition_vt(alg.scalar_multiply_vm(1 - b1, v_hidden), alg.scalar_multiply_vm(b1, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2))); + v_hidden = alg.additionnvt(alg.scalar_multiplynvt(1 - b1, v_hidden), alg.scalar_multiplynvt(b1, alg.exponentiatenvt(grads.cumulative_hidden_layer_w_grad, 2))); v_output = alg.additionnv(v_output, alg.exponentiatenv(grads.output_w_grad, 2)); - Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_divisionnv_vt(grads.cumulative_hidden_layer_w_grad, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden)))); + Vector> hidden_layer_updations = alg.scalar_multiplynvt(learning_rate / _n, alg.element_wise_divisionnvnvt(grads.cumulative_hidden_layer_w_grad, alg.scalar_addnvt(e, alg.sqrtnvt(v_hidden)))); Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_divisionnv(grads.output_w_grad, alg.scalar_addnv(e, alg.sqrtnv(v_output)))); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. @@ -435,8 +435,8 @@ void MLPPANN::adam(real_t learning_rate, int max_epoch, int mini_batch_size, rea ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch); if (!_network.empty() && m_hidden.empty() && v_hidden.empty()) { // Initing our tensor - m_hidden = alg.resize_vt(m_hidden, grads.cumulative_hidden_layer_w_grad); - v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); + m_hidden = alg.resizenvt(m_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden = alg.resizenvt(v_hidden, grads.cumulative_hidden_layer_w_grad); } if (m_output->size() == 0 && v_output->size()) { @@ -444,19 +444,19 @@ void MLPPANN::adam(real_t learning_rate, int max_epoch, int mini_batch_size, rea v_output->resize(grads.output_w_grad->size()); } - m_hidden = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden), alg.scalar_multiply_vm(1 - b1, grads.cumulative_hidden_layer_w_grad)); - v_hidden = alg.addition_vt(alg.scalar_multiply_vm(b2, v_hidden), alg.scalar_multiply_vm(1 - b2, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2))); + m_hidden = alg.additionnvt(alg.scalar_multiplynvt(b1, m_hidden), alg.scalar_multiplynvt(1 - b1, grads.cumulative_hidden_layer_w_grad)); + v_hidden = alg.additionnvt(alg.scalar_multiplynvt(b2, v_hidden), alg.scalar_multiplynvt(1 - b2, alg.exponentiatenvt(grads.cumulative_hidden_layer_w_grad, 2))); m_output = alg.additionnv(alg.scalar_multiplynv(b1, m_output), alg.scalar_multiplynv(1 - b1, grads.output_w_grad)); v_output = alg.additionnv(alg.scalar_multiplynv(b2, v_output), alg.scalar_multiplynv(1 - b2, alg.exponentiatenv(grads.output_w_grad, 2))); - Vector> m_hidden_hat = alg.scalar_multiply_vm(1 / (1 - Math::pow(b1, epoch)), m_hidden); - Vector> v_hidden_hat = alg.scalar_multiply_vm(1 / (1 - Math::pow(b2, epoch)), v_hidden); + Vector> m_hidden_hat = alg.scalar_multiplynvt(1 / (1 - Math::pow(b1, epoch)), m_hidden); + Vector> v_hidden_hat = alg.scalar_multiplynvt(1 / (1 - Math::pow(b2, epoch)), v_hidden); Ref m_output_hat = alg.scalar_multiplynv(1 / (1 - Math::pow(b1, epoch)), m_output); Ref v_output_hat = alg.scalar_multiplynv(1 / (1 - Math::pow(b2, epoch)), v_output); - Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_divisionnv_vt(m_hidden_hat, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden_hat)))); + Vector> hidden_layer_updations = alg.scalar_multiplynvt(learning_rate / _n, alg.element_wise_divisionnvnvt(m_hidden_hat, alg.scalar_addnvt(e, alg.sqrtnvt(v_hidden_hat)))); Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_divisionnv(m_output_hat, alg.scalar_addnv(e, alg.sqrtnv(v_output_hat)))); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. @@ -510,8 +510,8 @@ void MLPPANN::adamax(real_t learning_rate, int max_epoch, int mini_batch_size, r ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch); if (!_network.empty() && m_hidden.empty() && u_hidden.empty()) { // Initing our tensor - m_hidden = alg.resize_vt(m_hidden, grads.cumulative_hidden_layer_w_grad); - u_hidden = alg.resize_vt(u_hidden, grads.cumulative_hidden_layer_w_grad); + m_hidden = alg.resizenvt(m_hidden, grads.cumulative_hidden_layer_w_grad); + u_hidden = alg.resizenvt(u_hidden, grads.cumulative_hidden_layer_w_grad); } if (m_output->size() == 0 && u_output->size() == 0) { @@ -519,17 +519,17 @@ void MLPPANN::adamax(real_t learning_rate, int max_epoch, int mini_batch_size, r u_output->resize(grads.output_w_grad->size()); } - m_hidden = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden), alg.scalar_multiply_vm(1 - b1, grads.cumulative_hidden_layer_w_grad)); - u_hidden = alg.max_vt(alg.scalar_multiply_vm(b2, u_hidden), alg.abs_vt(grads.cumulative_hidden_layer_w_grad)); + m_hidden = alg.additionnvt(alg.scalar_multiplynvt(b1, m_hidden), alg.scalar_multiplynvt(1 - b1, grads.cumulative_hidden_layer_w_grad)); + u_hidden = alg.maxnvt(alg.scalar_multiplynvt(b2, u_hidden), alg.absnvt(grads.cumulative_hidden_layer_w_grad)); m_output = alg.additionnv(alg.scalar_multiplynv(b1, m_output), alg.scalar_multiplynv(1 - b1, grads.output_w_grad)); u_output = alg.maxnvv(alg.scalar_multiplynv(b2, u_output), alg.absv(grads.output_w_grad)); - Vector> m_hidden_hat = alg.scalar_multiply_vm(1 / (1 - Math::pow(b1, epoch)), m_hidden); + Vector> m_hidden_hat = alg.scalar_multiplynvt(1 / (1 - Math::pow(b1, epoch)), m_hidden); Ref m_output_hat = alg.scalar_multiplynv(1 / (1 - Math::pow(b1, epoch)), m_output); - Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_divisionnv_vt(m_hidden_hat, alg.scalar_add_vm(e, u_hidden))); + Vector> hidden_layer_updations = alg.scalar_multiplynvt(learning_rate / _n, alg.element_wise_divisionnvnvt(m_hidden_hat, alg.scalar_addnvt(e, u_hidden))); Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_divisionnv(m_output_hat, alg.scalar_addnv(e, u_output))); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. @@ -583,8 +583,8 @@ void MLPPANN::nadam(real_t learning_rate, int max_epoch, int mini_batch_size, re ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch); if (!_network.empty() && m_hidden.empty() && v_hidden.empty()) { // Initing our tensor - m_hidden = alg.resize_vt(m_hidden, grads.cumulative_hidden_layer_w_grad); - v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); + m_hidden = alg.resizenvt(m_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden = alg.resizenvt(v_hidden, grads.cumulative_hidden_layer_w_grad); } if (m_output->size() == 0 && v_output->size() == 0) { @@ -592,21 +592,21 @@ void MLPPANN::nadam(real_t learning_rate, int max_epoch, int mini_batch_size, re v_output->resize(grads.output_w_grad->size()); } - m_hidden = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden), alg.scalar_multiply_vm(1 - b1, grads.cumulative_hidden_layer_w_grad)); - v_hidden = alg.addition_vt(alg.scalar_multiply_vm(b2, v_hidden), alg.scalar_multiply_vm(1 - b2, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2))); + m_hidden = alg.additionnvt(alg.scalar_multiplynvt(b1, m_hidden), alg.scalar_multiplynvt(1 - b1, grads.cumulative_hidden_layer_w_grad)); + v_hidden = alg.additionnvt(alg.scalar_multiplynvt(b2, v_hidden), alg.scalar_multiplynvt(1 - b2, alg.exponentiatenvt(grads.cumulative_hidden_layer_w_grad, 2))); m_output = alg.additionnv(alg.scalar_multiplynv(b1, m_output), alg.scalar_multiplynv(1 - b1, grads.output_w_grad)); v_output = alg.additionnv(alg.scalar_multiplynv(b2, v_output), alg.scalar_multiplynv(1 - b2, alg.exponentiatenv(grads.output_w_grad, 2))); - Vector> m_hidden_hat = alg.scalar_multiply_vm(1 / (1.0 - Math::pow(b1, epoch)), m_hidden); - Vector> v_hidden_hat = alg.scalar_multiply_vm(1 / (1.0 - Math::pow(b2, epoch)), v_hidden); - Vector> m_hidden_final = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden_hat), alg.scalar_multiply_vm((1 - b1) / (1 - Math::pow(b1, epoch)), grads.cumulative_hidden_layer_w_grad)); + Vector> m_hidden_hat = alg.scalar_multiplynvt(1 / (1.0 - Math::pow(b1, epoch)), m_hidden); + Vector> v_hidden_hat = alg.scalar_multiplynvt(1 / (1.0 - Math::pow(b2, epoch)), v_hidden); + Vector> m_hidden_final = alg.additionnvt(alg.scalar_multiplynvt(b1, m_hidden_hat), alg.scalar_multiplynvt((1 - b1) / (1 - Math::pow(b1, epoch)), grads.cumulative_hidden_layer_w_grad)); Ref m_output_hat = alg.scalar_multiplynv(1 / (1.0 - Math::pow(b1, epoch)), m_output); Ref v_output_hat = alg.scalar_multiplynv(1 / (1.0 - Math::pow(b2, epoch)), v_output); Ref m_output_final = alg.additionnv(alg.scalar_multiplynv(b1, m_output_hat), alg.scalar_multiplynv((1 - b1) / (1.0 - Math::pow(b1, epoch)), grads.output_w_grad)); - Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_divisionnv_vt(m_hidden_final, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden_hat)))); + Vector> hidden_layer_updations = alg.scalar_multiplynvt(learning_rate / _n, alg.element_wise_divisionnvnvt(m_hidden_final, alg.scalar_addnvt(e, alg.sqrtnvt(v_hidden_hat)))); Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_divisionnvnm(m_output_final, alg.scalar_addnv(e, alg.sqrtnv(v_output_hat)))); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. @@ -666,9 +666,9 @@ void MLPPANN::amsgrad(real_t learning_rate, int max_epoch, int mini_batch_size, ComputeGradientsResult grads = compute_gradients(y_hat, current_output_batch); if (!_network.empty() && m_hidden.empty() && v_hidden.empty()) { // Initing our tensor - m_hidden = alg.resize_vt(m_hidden, grads.cumulative_hidden_layer_w_grad); - v_hidden = alg.resize_vt(v_hidden, grads.cumulative_hidden_layer_w_grad); - v_hidden_hat = alg.resize_vt(v_hidden_hat, grads.cumulative_hidden_layer_w_grad); + m_hidden = alg.resizenvt(m_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden = alg.resizenvt(v_hidden, grads.cumulative_hidden_layer_w_grad); + v_hidden_hat = alg.resizenvt(v_hidden_hat, grads.cumulative_hidden_layer_w_grad); } if (m_output->size() == 0 && v_output->size() == 0) { @@ -677,16 +677,16 @@ void MLPPANN::amsgrad(real_t learning_rate, int max_epoch, int mini_batch_size, v_output_hat->resize(grads.output_w_grad->size()); } - m_hidden = alg.addition_vt(alg.scalar_multiply_vm(b1, m_hidden), alg.scalar_multiply_vm(1 - b1, grads.cumulative_hidden_layer_w_grad)); - v_hidden = alg.addition_vt(alg.scalar_multiply_vm(b2, v_hidden), alg.scalar_multiply_vm(1 - b2, alg.exponentiate_vt(grads.cumulative_hidden_layer_w_grad, 2))); + m_hidden = alg.additionnvt(alg.scalar_multiplynvt(b1, m_hidden), alg.scalar_multiplynvt(1 - b1, grads.cumulative_hidden_layer_w_grad)); + v_hidden = alg.additionnvt(alg.scalar_multiplynvt(b2, v_hidden), alg.scalar_multiplynvt(1 - b2, alg.exponentiatenvt(grads.cumulative_hidden_layer_w_grad, 2))); m_output = alg.additionnv(alg.scalar_multiplynv(b1, m_output), alg.scalar_multiplynv(1 - b1, grads.output_w_grad)); v_output = alg.additionnv(alg.scalar_multiplynv(b2, v_output), alg.scalar_multiplynv(1 - b2, alg.exponentiatenv(grads.output_w_grad, 2))); - v_hidden_hat = alg.max_vt(v_hidden_hat, v_hidden); + v_hidden_hat = alg.maxnvt(v_hidden_hat, v_hidden); v_output_hat = alg.maxnvv(v_output_hat, v_output); - Vector> hidden_layer_updations = alg.scalar_multiply_vm(learning_rate / _n, alg.element_wise_divisionnv_vt(m_hidden, alg.scalar_add_vm(e, alg.sqrt_vt(v_hidden_hat)))); + Vector> hidden_layer_updations = alg.scalar_multiplynvt(learning_rate / _n, alg.element_wise_divisionnvnvt(m_hidden, alg.scalar_addnvt(e, alg.sqrtnvt(v_hidden_hat)))); Ref output_layer_updation = alg.scalar_multiplynv(learning_rate / _n, alg.element_wise_divisionnv(m_output, alg.scalar_addnv(e, alg.sqrtnv(v_output_hat)))); update_parameters(hidden_layer_updations, output_layer_updation, learning_rate); // subject to change. may want bias to have this matrix too. diff --git a/mlpp/ann/ann_old.cpp b/mlpp/ann/ann_old.cpp index ed3e945..40381d6 100644 --- a/mlpp/ann/ann_old.cpp +++ b/mlpp/ann/ann_old.cpp @@ -6,9 +6,9 @@ #include "ann_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" +#include "../cost/cost_old.h" #include "../lin_alg/lin_alg_old.h" -#include "../regularization/reg.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -61,7 +61,6 @@ real_t MLPPANNOld::modelTest(std::vector x) { } void MLPPANNOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; int epoch = 1; @@ -97,7 +96,6 @@ void MLPPANNOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { } void MLPPANNOld::SGD(real_t learning_rate, int max_epoch, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; @@ -138,7 +136,6 @@ void MLPPANNOld::SGD(real_t learning_rate, int max_epoch, bool UI) { } void MLPPANNOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; @@ -183,7 +180,6 @@ void MLPPANNOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, } void MLPPANNOld::Momentum(real_t learning_rate, int max_epoch, int mini_batch_size, real_t gamma, bool NAG, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; @@ -245,7 +241,6 @@ void MLPPANNOld::Momentum(real_t learning_rate, int max_epoch, int mini_batch_si } void MLPPANNOld::Adagrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t e, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; @@ -306,7 +301,6 @@ void MLPPANNOld::Adagrad(real_t learning_rate, int max_epoch, int mini_batch_siz } void MLPPANNOld::Adadelta(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t e, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; @@ -367,7 +361,6 @@ void MLPPANNOld::Adadelta(real_t learning_rate, int max_epoch, int mini_batch_si } void MLPPANNOld::Adam(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; @@ -440,7 +433,6 @@ void MLPPANNOld::Adam(real_t learning_rate, int max_epoch, int mini_batch_size, } void MLPPANNOld::Adamax(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; @@ -511,7 +503,6 @@ void MLPPANNOld::Adamax(real_t learning_rate, int max_epoch, int mini_batch_size } void MLPPANNOld::Nadam(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; @@ -586,7 +577,6 @@ void MLPPANNOld::Nadam(real_t learning_rate, int max_epoch, int mini_batch_size, } void MLPPANNOld::AMSGrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool UI) { - class MLPPCost cost; MLPPLinAlgOld alg; real_t cost_prev = 0; @@ -726,8 +716,8 @@ void MLPPANNOld::addOutputLayer(std::string activation, std::string loss, std::s } real_t MLPPANNOld::Cost(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; real_t totalRegTerm = 0; auto cost_function = outputLayer->cost_map[outputLayer->cost]; @@ -775,10 +765,10 @@ void MLPPANNOld::updateParameters(std::vector>> std::tuple>>, std::vector> MLPPANNOld::computeGradients(std::vector y_hat, std::vector outputSet) { // std::cout << "BEGIN" << std::endl; - class MLPPCost cost; + class MLPPCostOld cost; MLPPActivationOld avn; MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; std::vector>> cumulativeHiddenLayerWGrad; // Tensor containing ALL hidden grads. diff --git a/mlpp/auto_encoder/auto_encoder_old.cpp b/mlpp/auto_encoder/auto_encoder_old.cpp index 71f28d0..4b079c4 100644 --- a/mlpp/auto_encoder/auto_encoder_old.cpp +++ b/mlpp/auto_encoder/auto_encoder_old.cpp @@ -7,8 +7,8 @@ #include "auto_encoder_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" #include "../utilities/utilities.h" #include @@ -24,7 +24,7 @@ std::vector MLPPAutoEncoderOld::modelTest(std::vector x) { void MLPPAutoEncoderOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; + MLPPLinAlgOld alg; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -77,7 +77,7 @@ void MLPPAutoEncoderOld::gradientDescent(real_t learning_rate, int max_epoch, bo void MLPPAutoEncoderOld::SGD(real_t learning_rate, int max_epoch, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; + MLPPLinAlgOld alg; real_t cost_prev = 0; int epoch = 1; @@ -131,7 +131,7 @@ void MLPPAutoEncoderOld::SGD(real_t learning_rate, int max_epoch, bool UI) { void MLPPAutoEncoderOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; + MLPPLinAlgOld alg; real_t cost_prev = 0; int epoch = 1; @@ -219,12 +219,12 @@ MLPPAutoEncoderOld::MLPPAutoEncoderOld(std::vector> pinputSe } real_t MLPPAutoEncoderOld::Cost(std::vector> y_hat, std::vector> y) { - class MLPPCost cost; + class MLPPCostOld cost; return cost.MSE(y_hat, inputSet); } std::vector> MLPPAutoEncoderOld::Evaluate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector> z2 = alg.mat_vec_add(alg.matmult(X, weights1), bias1); std::vector> a2 = avn.sigmoid(z2); @@ -232,7 +232,7 @@ std::vector> MLPPAutoEncoderOld::Evaluate(std::vector>, std::vector>> MLPPAutoEncoderOld::propagate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector> z2 = alg.mat_vec_add(alg.matmult(X, weights1), bias1); std::vector> a2 = avn.sigmoid(z2); @@ -240,7 +240,7 @@ std::tuple>, std::vector>> M } std::vector MLPPAutoEncoderOld::Evaluate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector z2 = alg.addition(alg.mat_vec_mult(alg.transpose(weights1), x), bias1); std::vector a2 = avn.sigmoid(z2); @@ -248,7 +248,7 @@ std::vector MLPPAutoEncoderOld::Evaluate(std::vector x) { } std::tuple, std::vector> MLPPAutoEncoderOld::propagate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector z2 = alg.addition(alg.mat_vec_mult(alg.transpose(weights1), x), bias1); std::vector a2 = avn.sigmoid(z2); @@ -256,7 +256,7 @@ std::tuple, std::vector> MLPPAutoEncoderOld::propaga } void MLPPAutoEncoderOld::forwardPass() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; z2 = alg.mat_vec_add(alg.matmult(inputSet, weights1), bias1); a2 = avn.sigmoid(z2); diff --git a/mlpp/bernoulli_nb/bernoulli_nb_old.cpp b/mlpp/bernoulli_nb/bernoulli_nb_old.cpp index 9ba425f..3c736b3 100644 --- a/mlpp/bernoulli_nb/bernoulli_nb_old.cpp +++ b/mlpp/bernoulli_nb/bernoulli_nb_old.cpp @@ -6,7 +6,7 @@ #include "bernoulli_nb_old.h" #include "../data/data.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include "../utilities/utilities.h" #include @@ -77,7 +77,7 @@ real_t MLPPBernoulliNBOld::score() { } void MLPPBernoulliNBOld::computeVocab() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPData data; vocab = data.vecToSet(alg.flatten(inputSet)); } diff --git a/mlpp/c_log_log_reg/c_log_log_reg_old.cpp b/mlpp/c_log_log_reg/c_log_log_reg_old.cpp index 9249058..fb04268 100644 --- a/mlpp/c_log_log_reg/c_log_log_reg_old.cpp +++ b/mlpp/c_log_log_reg/c_log_log_reg_old.cpp @@ -7,9 +7,9 @@ #include "c_log_log_reg_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -32,8 +32,8 @@ real_t MLPPCLogLogRegOld::modelTest(std::vector x) { void MLPPCLogLogRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -66,8 +66,8 @@ void MLPPCLogLogRegOld::gradientDescent(real_t learning_rate, int max_epoch, boo void MLPPCLogLogRegOld::MLE(real_t learning_rate, int max_epoch, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -97,8 +97,8 @@ void MLPPCLogLogRegOld::MLE(real_t learning_rate, int max_epoch, bool UI) { } void MLPPCLogLogRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -139,8 +139,8 @@ void MLPPCLogLogRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { void MLPPCLogLogRegOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -188,30 +188,30 @@ real_t MLPPCLogLogRegOld::score() { } real_t MLPPCLogLogRegOld::Cost(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; return cost.MSE(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg); } std::vector MLPPCLogLogRegOld::Evaluate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.cloglog(alg.scalarAdd(bias, alg.mat_vec_mult(X, weights))); } std::vector MLPPCLogLogRegOld::propagate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarAdd(bias, alg.mat_vec_mult(X, weights)); } real_t MLPPCLogLogRegOld::Evaluate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.cloglog(alg.dot(weights, x) + bias); } real_t MLPPCLogLogRegOld::propagate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.dot(weights, x) + bias; } diff --git a/mlpp/convolutions/convolutions.cpp b/mlpp/convolutions/convolutions.cpp index cd556ce..d101238 100644 --- a/mlpp/convolutions/convolutions.cpp +++ b/mlpp/convolutions/convolutions.cpp @@ -10,6 +10,7 @@ #include #include +/* std::vector> MLPPConvolutions::convolve_2d(std::vector> input, std::vector> filter, int S, int P) { MLPPLinAlg alg; std::vector> feature_map; @@ -219,12 +220,12 @@ std::vector> MLPPConvolutions::gaussian_filter_2d(int size, return filter; } -/* -Indeed a filter could have been used for this purpose, but I decided that it would've just -been easier to carry out the calculation explicitly, mainly because it is more informative, -and also because my convolution algorithm is only built for filters with equally sized -heights and widths. -*/ + +// Indeed a filter could have been used for this purpose, but I decided that it would've just +// been easier to carry out the calculation explicitly, mainly because it is more informative, +// and also because my convolution algorithm is only built for filters with equally sized +// heights and widths. + std::vector> MLPPConvolutions::dx(std::vector> input) { std::vector> deriv; // We assume a gray scale image. deriv.resize(input.size()); @@ -360,8 +361,10 @@ std::vector> MLPPConvolutions::get_roberts_horizontal() { std::vector> MLPPConvolutions::get_roberts_vertical() { return _roberts_vertical; } +*/ MLPPConvolutions::MLPPConvolutions() { + /* _prewitt_horizontal = { { 1, 1, 1 }, { 0, 0, 0 }, { -1, -1, -1 } }; _prewitt_vertical = { { 1, 0, -1 }, { 1, 0, -1 }, { 1, 0, -1 } }; _sobel_horizontal = { { 1, 2, 1 }, { 0, 0, 0 }, { -1, -2, -1 } }; @@ -370,6 +373,7 @@ MLPPConvolutions::MLPPConvolutions() { _scharr_vertical = { { 3, 0, -3 }, { 10, 0, -10 }, { 3, 0, -3 } }; _roberts_horizontal = { { 0, 1 }, { -1, 0 } }; _roberts_vertical = { { 1, 0 }, { 0, -1 } }; + */ } void MLPPConvolutions::_bind_methods() { diff --git a/mlpp/convolutions/convolutions.h b/mlpp/convolutions/convolutions.h index 3a2ee4b..9edb460 100644 --- a/mlpp/convolutions/convolutions.h +++ b/mlpp/convolutions/convolutions.h @@ -13,6 +13,7 @@ class MLPPConvolutions : public Reference { GDCLASS(MLPPConvolutions, Reference); public: + /* std::vector> convolve_2d(std::vector> input, std::vector> filter, int S, int P = 0); std::vector>> convolve_3d(std::vector>> input, std::vector>> filter, int S, int P = 0); @@ -42,12 +43,14 @@ public: std::vector> get_scharr_vertical(); std::vector> get_roberts_horizontal(); std::vector> get_roberts_vertical(); + */ MLPPConvolutions(); protected: static void _bind_methods(); + /* std::vector> _prewitt_horizontal; std::vector> _prewitt_vertical; std::vector> _sobel_horizontal; @@ -56,6 +59,7 @@ protected: std::vector> _scharr_vertical; std::vector> _roberts_horizontal; std::vector> _roberts_vertical; + */ }; #endif // Convolutions_hpp \ No newline at end of file diff --git a/mlpp/convolutions/convolutions_old.cpp b/mlpp/convolutions/convolutions_old.cpp index f807b6f..0e00fca 100644 --- a/mlpp/convolutions/convolutions_old.cpp +++ b/mlpp/convolutions/convolutions_old.cpp @@ -6,13 +6,13 @@ #include "../convolutions/convolutions_old.h" -#include "../lin_alg/lin_alg.h" -#include "../stat/stat.h" +#include "../lin_alg/lin_alg_old.h" +#include "../stat/stat_old.h" #include #include std::vector> MLPPConvolutionsOld::convolve_2d(std::vector> input, std::vector> filter, int S, int P) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> feature_map; uint32_t N = input.size(); uint32_t F = filter.size(); @@ -68,7 +68,7 @@ std::vector> MLPPConvolutionsOld::convolve_2d(std::vector>> MLPPConvolutionsOld::convolve_3d(std::vector>> input, std::vector>> filter, int S, int P) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> feature_map; uint32_t N = input[0].size(); uint32_t F = filter[0].size(); @@ -134,7 +134,7 @@ std::vector>> MLPPConvolutionsOld::convolve_3d(s } std::vector> MLPPConvolutionsOld::pool_2d(std::vector> input, int F, int S, std::string type) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> pooled_map; uint32_t N = input.size(); uint32_t map_size = floor((N - F) / S + 1); @@ -161,7 +161,7 @@ std::vector> MLPPConvolutionsOld::pool_2d(std::vector>> MLPPConvolutionsOld::pool_3d(std:: } real_t MLPPConvolutionsOld::global_pool_2d(std::vector> input, std::string type) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (type == "Average") { - MLPPStat stat; + MLPPStatOld stat; return stat.mean(alg.flatten(input)); } else if (type == "Min") { return alg.min(alg.flatten(input)); @@ -269,7 +269,7 @@ std::vector> MLPPConvolutionsOld::dy(std::vector> MLPPConvolutionsOld::grad_magnitude(std::vector> input) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> x_deriv_2 = alg.hadamard_product(dx(input), dx(input)); std::vector> y_deriv_2 = alg.hadamard_product(dy(input), dy(input)); return alg.sqrt(alg.addition(x_deriv_2, y_deriv_2)); @@ -298,7 +298,7 @@ std::vector>> MLPPConvolutionsOld::compute_m(std real_t const GAUSSIAN_PADDING = ((input.size() - 1) + GAUSSIAN_SIZE - input.size()) / 2; // Convs must be same. std::cout << GAUSSIAN_PADDING << std::endl; - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> x_deriv = dx(input); std::vector> y_deriv = dy(input); @@ -312,7 +312,7 @@ std::vector>> MLPPConvolutionsOld::compute_m(std } std::vector> MLPPConvolutionsOld::harris_corner_detection(std::vector> input) { real_t const k = 0.05; // Empirically determined wherein k -> [0.04, 0.06], though conventionally 0.05 is typically used as well. - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> M = compute_m(input); std::vector> det = alg.subtraction(alg.hadamard_product(M[0], M[1]), alg.hadamard_product(M[2], M[2])); std::vector> trace = alg.addition(M[0], M[1]); diff --git a/mlpp/cost/cost.cpp b/mlpp/cost/cost.cpp index e22b232..083f507 100644 --- a/mlpp/cost/cost.cpp +++ b/mlpp/cost/cost.cpp @@ -763,401 +763,6 @@ Ref MLPPCost::run_cost_deriv_matrix(const CostTypes cost, const Ref< } } -// ====== OLD ====== - -real_t MLPPCost::MSE(std::vector y_hat, std::vector y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - sum += (y_hat[i] - y[i]) * (y_hat[i] - y[i]); - } - return sum / 2 * y_hat.size(); -} - -real_t MLPPCost::MSE(std::vector> y_hat, std::vector> y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - sum += (y_hat[i][j] - y[i][j]) * (y_hat[i][j] - y[i][j]); - } - } - return sum / 2 * y_hat.size(); -} - -std::vector MLPPCost::MSEDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; - return alg.subtraction(y_hat, y); -} - -std::vector> MLPPCost::MSEDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; - return alg.subtraction(y_hat, y); -} - -real_t MLPPCost::RMSE(std::vector y_hat, std::vector y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - sum += (y_hat[i] - y[i]) * (y_hat[i] - y[i]); - } - return sqrt(sum / y_hat.size()); -} - -real_t MLPPCost::RMSE(std::vector> y_hat, std::vector> y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - sum += (y_hat[i][j] - y[i][j]) * (y_hat[i][j] - y[i][j]); - } - } - return sqrt(sum / y_hat.size()); -} - -std::vector MLPPCost::RMSEDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; - return alg.scalarMultiply(1 / (2 * sqrt(MSE(y_hat, y))), MSEDeriv(y_hat, y)); -} - -std::vector> MLPPCost::RMSEDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; - return alg.scalarMultiply(1 / (2 / sqrt(MSE(y_hat, y))), MSEDeriv(y_hat, y)); -} - -real_t MLPPCost::MAE(std::vector y_hat, std::vector y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - sum += abs((y_hat[i] - y[i])); - } - return sum / y_hat.size(); -} - -real_t MLPPCost::MAE(std::vector> y_hat, std::vector> y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - sum += abs((y_hat[i][j] - y[i][j])); - } - } - return sum / y_hat.size(); -} - -std::vector MLPPCost::MAEDeriv(std::vector y_hat, std::vector y) { - std::vector deriv; - deriv.resize(y_hat.size()); - for (uint32_t i = 0; i < deriv.size(); i++) { - if (y_hat[i] < 0) { - deriv[i] = -1; - } else if (y_hat[i] == 0) { - deriv[i] = 0; - } else { - deriv[i] = 1; - } - } - return deriv; -} - -std::vector> MLPPCost::MAEDeriv(std::vector> y_hat, std::vector> y) { - std::vector> deriv; - deriv.resize(y_hat.size()); - for (uint32_t i = 0; i < deriv.size(); i++) { - deriv.resize(y_hat[i].size()); - } - for (uint32_t i = 0; i < deriv.size(); i++) { - for (uint32_t j = 0; j < deriv[i].size(); j++) { - if (y_hat[i][j] < 0) { - deriv[i][j] = -1; - } else if (y_hat[i][j] == 0) { - deriv[i][j] = 0; - } else { - deriv[i][j] = 1; - } - } - } - return deriv; -} - -real_t MLPPCost::MBE(std::vector y_hat, std::vector y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - sum += (y_hat[i] - y[i]); - } - return sum / y_hat.size(); -} - -real_t MLPPCost::MBE(std::vector> y_hat, std::vector> y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - sum += (y_hat[i][j] - y[i][j]); - } - } - return sum / y_hat.size(); -} - -std::vector MLPPCost::MBEDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; - return alg.onevec(y_hat.size()); -} - -std::vector> MLPPCost::MBEDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; - return alg.onemat(y_hat.size(), y_hat[0].size()); -} - -real_t MLPPCost::LogLoss(std::vector y_hat, std::vector y) { - real_t sum = 0; - real_t eps = 1e-8; - for (uint32_t i = 0; i < y_hat.size(); i++) { - sum += -(y[i] * std::log(y_hat[i] + eps) + (1 - y[i]) * std::log(1 - y_hat[i] + eps)); - } - - return sum / y_hat.size(); -} - -real_t MLPPCost::LogLoss(std::vector> y_hat, std::vector> y) { - real_t sum = 0; - real_t eps = 1e-8; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - sum += -(y[i][j] * std::log(y_hat[i][j] + eps) + (1 - y[i][j]) * std::log(1 - y_hat[i][j] + eps)); - } - } - - return sum / y_hat.size(); -} - -std::vector MLPPCost::LogLossDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; - return alg.addition(alg.scalarMultiply(-1, alg.elementWiseDivision(y, y_hat)), alg.elementWiseDivision(alg.scalarMultiply(-1, alg.scalarAdd(-1, y)), alg.scalarMultiply(-1, alg.scalarAdd(-1, y_hat)))); -} - -std::vector> MLPPCost::LogLossDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; - return alg.addition(alg.scalarMultiply(-1, alg.elementWiseDivision(y, y_hat)), alg.elementWiseDivision(alg.scalarMultiply(-1, alg.scalarAdd(-1, y)), alg.scalarMultiply(-1, alg.scalarAdd(-1, y_hat)))); -} - -real_t MLPPCost::CrossEntropy(std::vector y_hat, std::vector y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - sum += y[i] * std::log(y_hat[i]); - } - - return -1 * sum; -} - -real_t MLPPCost::CrossEntropy(std::vector> y_hat, std::vector> y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - sum += y[i][j] * std::log(y_hat[i][j]); - } - } - - return -1 * sum; -} - -std::vector MLPPCost::CrossEntropyDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; - return alg.scalarMultiply(-1, alg.elementWiseDivision(y, y_hat)); -} - -std::vector> MLPPCost::CrossEntropyDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; - return alg.scalarMultiply(-1, alg.elementWiseDivision(y, y_hat)); -} - -real_t MLPPCost::HuberLoss(std::vector y_hat, std::vector y, real_t delta) { - MLPPLinAlg alg; - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - if (abs(y[i] - y_hat[i]) <= delta) { - sum += (y[i] - y_hat[i]) * (y[i] - y_hat[i]); - } else { - sum += 2 * delta * abs(y[i] - y_hat[i]) - delta * delta; - } - } - return sum; -} - -real_t MLPPCost::HuberLoss(std::vector> y_hat, std::vector> y, real_t delta) { - MLPPLinAlg alg; - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - if (abs(y[i][j] - y_hat[i][j]) <= delta) { - sum += (y[i][j] - y_hat[i][j]) * (y[i][j] - y_hat[i][j]); - } else { - sum += 2 * delta * abs(y[i][j] - y_hat[i][j]) - delta * delta; - } - } - } - return sum; -} - -std::vector MLPPCost::HuberLossDeriv(std::vector y_hat, std::vector y, real_t delta) { - MLPPLinAlg alg; - std::vector deriv; - deriv.resize(y_hat.size()); - - for (uint32_t i = 0; i < y_hat.size(); i++) { - if (abs(y[i] - y_hat[i]) <= delta) { - deriv.push_back(-(y[i] - y_hat[i])); - } else { - if (y_hat[i] > 0 || y_hat[i] < 0) { - deriv.push_back(2 * delta * (y_hat[i] / abs(y_hat[i]))); - } else { - deriv.push_back(0); - } - } - } - return deriv; -} - -std::vector> MLPPCost::HuberLossDeriv(std::vector> y_hat, std::vector> y, real_t delta) { - MLPPLinAlg alg; - - std::vector> deriv; - deriv.resize(y_hat.size()); - for (uint32_t i = 0; i < deriv.size(); i++) { - deriv[i].resize(y_hat[i].size()); - } - - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - if (abs(y[i][j] - y_hat[i][j]) <= delta) { - deriv[i].push_back(-(y[i][j] - y_hat[i][j])); - } else { - if (y_hat[i][j] > 0 || y_hat[i][j] < 0) { - deriv[i].push_back(2 * delta * (y_hat[i][j] / abs(y_hat[i][j]))); - } else { - deriv[i].push_back(0); - } - } - } - } - return deriv; -} - -real_t MLPPCost::HingeLoss(std::vector y_hat, std::vector y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - sum += fmax(0, 1 - y[i] * y_hat[i]); - } - - return sum / y_hat.size(); -} - -real_t MLPPCost::HingeLoss(std::vector> y_hat, std::vector> y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - sum += fmax(0, 1 - y[i][j] * y_hat[i][j]); - } - } - - return sum / y_hat.size(); -} - -std::vector MLPPCost::HingeLossDeriv(std::vector y_hat, std::vector y) { - std::vector deriv; - deriv.resize(y_hat.size()); - for (uint32_t i = 0; i < y_hat.size(); i++) { - if (1 - y[i] * y_hat[i] > 0) { - deriv[i] = -y[i]; - } else { - deriv[i] = 0; - } - } - return deriv; -} - -std::vector> MLPPCost::HingeLossDeriv(std::vector> y_hat, std::vector> y) { - std::vector> deriv; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - if (1 - y[i][j] * y_hat[i][j] > 0) { - deriv[i][j] = -y[i][j]; - } else { - deriv[i][j] = 0; - } - } - } - return deriv; -} - -real_t MLPPCost::WassersteinLoss(std::vector y_hat, std::vector y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - sum += y_hat[i] * y[i]; - } - return -sum / y_hat.size(); -} - -real_t MLPPCost::WassersteinLoss(std::vector> y_hat, std::vector> y) { - real_t sum = 0; - for (uint32_t i = 0; i < y_hat.size(); i++) { - for (uint32_t j = 0; j < y_hat[i].size(); j++) { - sum += y_hat[i][j] * y[i][j]; - } - } - return -sum / y_hat.size(); -} - -std::vector MLPPCost::WassersteinLossDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; - return alg.scalarMultiply(-1, y); // Simple. -} - -std::vector> MLPPCost::WassersteinLossDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; - return alg.scalarMultiply(-1, y); // Simple. -} - -real_t MLPPCost::HingeLoss(std::vector y_hat, std::vector y, std::vector weights, real_t C) { - MLPPLinAlg alg; - MLPPReg regularization; - return C * HingeLoss(y_hat, y) + regularization.regTerm(weights, 1, 0, "Ridge"); -} -real_t MLPPCost::HingeLoss(std::vector> y_hat, std::vector> y, std::vector> weights, real_t C) { - MLPPLinAlg alg; - MLPPReg regularization; - return C * HingeLoss(y_hat, y) + regularization.regTerm(weights, 1, 0, "Ridge"); -} - -std::vector MLPPCost::HingeLossDeriv(std::vector y_hat, std::vector y, real_t C) { - MLPPLinAlg alg; - MLPPReg regularization; - return alg.scalarMultiply(C, HingeLossDeriv(y_hat, y)); -} -std::vector> MLPPCost::HingeLossDeriv(std::vector> y_hat, std::vector> y, real_t C) { - MLPPLinAlg alg; - MLPPReg regularization; - return alg.scalarMultiply(C, HingeLossDeriv(y_hat, y)); -} - -real_t MLPPCost::dualFormSVM(std::vector alpha, std::vector> X, std::vector y) { - MLPPLinAlg alg; - std::vector> Y = alg.diag(y); // Y is a diagnoal matrix. Y[i][j] = y[i] if i = i, else Y[i][j] = 0. Yt = Y. - std::vector> K = alg.matmult(X, alg.transpose(X)); // TO DO: DON'T forget to add non-linear kernelizations. - std::vector> Q = alg.matmult(alg.matmult(alg.transpose(Y), K), Y); - real_t alphaQ = alg.matmult(alg.matmult({ alpha }, Q), alg.transpose({ alpha }))[0][0]; - std::vector one = alg.onevec(alpha.size()); - - return -alg.dot(one, alpha) + 0.5 * alphaQ; -} - -std::vector MLPPCost::dualFormSVMDeriv(std::vector alpha, std::vector> X, std::vector y) { - MLPPLinAlg alg; - std::vector> Y = alg.zeromat(y.size(), y.size()); - for (uint32_t i = 0; i < y.size(); i++) { - Y[i][i] = y[i]; // Y is a diagnoal matrix. Y[i][j] = y[i] if i = i, else Y[i][j] = 0. Yt = Y. - } - std::vector> K = alg.matmult(X, alg.transpose(X)); // TO DO: DON'T forget to add non-linear kernelizations. - std::vector> Q = alg.matmult(alg.matmult(alg.transpose(Y), K), Y); - std::vector alphaQDeriv = alg.mat_vec_mult(Q, alpha); - std::vector one = alg.onevec(alpha.size()); - - return alg.subtraction(alphaQDeriv, one); -} - void MLPPCost::_bind_methods() { ClassDB::bind_method(D_METHOD("msev", "y_hat", "y"), &MLPPCost::msev); ClassDB::bind_method(D_METHOD("msem", "y_hat", "y"), &MLPPCost::msem); diff --git a/mlpp/cost/cost.h b/mlpp/cost/cost.h index ad3783c..a25c3e7 100644 --- a/mlpp/cost/cost.h +++ b/mlpp/cost/cost.h @@ -119,72 +119,6 @@ public: Ref run_cost_deriv_vector(const CostTypes cost, const Ref &y_hat, const Ref &y); Ref run_cost_deriv_matrix(const CostTypes cost, const Ref &y_hat, const Ref &y); - // Regression Costs - real_t MSE(std::vector y_hat, std::vector y); - real_t MSE(std::vector> y_hat, std::vector> y); - - std::vector MSEDeriv(std::vector y_hat, std::vector y); - std::vector> MSEDeriv(std::vector> y_hat, std::vector> y); - - real_t RMSE(std::vector y_hat, std::vector y); - real_t RMSE(std::vector> y_hat, std::vector> y); - - std::vector RMSEDeriv(std::vector y_hat, std::vector y); - std::vector> RMSEDeriv(std::vector> y_hat, std::vector> y); - - real_t MAE(std::vector y_hat, std::vector y); - real_t MAE(std::vector> y_hat, std::vector> y); - - std::vector MAEDeriv(std::vector y_hat, std::vector y); - std::vector> MAEDeriv(std::vector> y_hat, std::vector> y); - - real_t MBE(std::vector y_hat, std::vector y); - real_t MBE(std::vector> y_hat, std::vector> y); - - std::vector MBEDeriv(std::vector y_hat, std::vector y); - std::vector> MBEDeriv(std::vector> y_hat, std::vector> y); - - // Classification Costs - real_t LogLoss(std::vector y_hat, std::vector y); - real_t LogLoss(std::vector> y_hat, std::vector> y); - - std::vector LogLossDeriv(std::vector y_hat, std::vector y); - std::vector> LogLossDeriv(std::vector> y_hat, std::vector> y); - - real_t CrossEntropy(std::vector y_hat, std::vector y); - real_t CrossEntropy(std::vector> y_hat, std::vector> y); - - std::vector CrossEntropyDeriv(std::vector y_hat, std::vector y); - std::vector> CrossEntropyDeriv(std::vector> y_hat, std::vector> y); - - real_t HuberLoss(std::vector y_hat, std::vector y, real_t delta); - real_t HuberLoss(std::vector> y_hat, std::vector> y, real_t delta); - - std::vector HuberLossDeriv(std::vector y_hat, std::vector y, real_t delta); - std::vector> HuberLossDeriv(std::vector> y_hat, std::vector> y, real_t delta); - - real_t HingeLoss(std::vector y_hat, std::vector y); - real_t HingeLoss(std::vector> y_hat, std::vector> y); - - std::vector HingeLossDeriv(std::vector y_hat, std::vector y); - std::vector> HingeLossDeriv(std::vector> y_hat, std::vector> y); - - real_t HingeLoss(std::vector y_hat, std::vector y, std::vector weights, real_t C); - real_t HingeLoss(std::vector> y_hat, std::vector> y, std::vector> weights, real_t C); - - std::vector HingeLossDeriv(std::vector y_hat, std::vector y, real_t C); - std::vector> HingeLossDeriv(std::vector> y_hat, std::vector> y, real_t C); - - real_t WassersteinLoss(std::vector y_hat, std::vector y); - real_t WassersteinLoss(std::vector> y_hat, std::vector> y); - - std::vector WassersteinLossDeriv(std::vector y_hat, std::vector y); - std::vector> WassersteinLossDeriv(std::vector> y_hat, std::vector> y); - - real_t dualFormSVM(std::vector alpha, std::vector> X, std::vector y); // TO DO: DON'T forget to add non-linear kernelizations. - - std::vector dualFormSVMDeriv(std::vector alpha, std::vector> X, std::vector y); - protected: static void _bind_methods(); }; diff --git a/mlpp/cost/cost_old.cpp b/mlpp/cost/cost_old.cpp index 42c121a..90d36ec 100644 --- a/mlpp/cost/cost_old.cpp +++ b/mlpp/cost/cost_old.cpp @@ -5,8 +5,8 @@ // #include "cost_old.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include #include @@ -29,12 +29,12 @@ real_t MLPPCostOld::MSE(std::vector> y_hat, std::vector MLPPCostOld::MSEDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.subtraction(y_hat, y); } std::vector> MLPPCostOld::MSEDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.subtraction(y_hat, y); } @@ -57,12 +57,12 @@ real_t MLPPCostOld::RMSE(std::vector> y_hat, std::vector MLPPCostOld::RMSEDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarMultiply(1 / (2 * sqrt(MSE(y_hat, y))), MSEDeriv(y_hat, y)); } std::vector> MLPPCostOld::RMSEDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarMultiply(1 / (2 / sqrt(MSE(y_hat, y))), MSEDeriv(y_hat, y)); } @@ -138,12 +138,12 @@ real_t MLPPCostOld::MBE(std::vector> y_hat, std::vector MLPPCostOld::MBEDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.onevec(y_hat.size()); } std::vector> MLPPCostOld::MBEDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.onemat(y_hat.size(), y_hat[0].size()); } @@ -170,12 +170,12 @@ real_t MLPPCostOld::LogLoss(std::vector> y_hat, std::vector< } std::vector MLPPCostOld::LogLossDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.addition(alg.scalarMultiply(-1, alg.elementWiseDivision(y, y_hat)), alg.elementWiseDivision(alg.scalarMultiply(-1, alg.scalarAdd(-1, y)), alg.scalarMultiply(-1, alg.scalarAdd(-1, y_hat)))); } std::vector> MLPPCostOld::LogLossDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.addition(alg.scalarMultiply(-1, alg.elementWiseDivision(y, y_hat)), alg.elementWiseDivision(alg.scalarMultiply(-1, alg.scalarAdd(-1, y)), alg.scalarMultiply(-1, alg.scalarAdd(-1, y_hat)))); } @@ -200,17 +200,16 @@ real_t MLPPCostOld::CrossEntropy(std::vector> y_hat, std::ve } std::vector MLPPCostOld::CrossEntropyDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarMultiply(-1, alg.elementWiseDivision(y, y_hat)); } std::vector> MLPPCostOld::CrossEntropyDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarMultiply(-1, alg.elementWiseDivision(y, y_hat)); } real_t MLPPCostOld::HuberLoss(std::vector y_hat, std::vector y, real_t delta) { - MLPPLinAlg alg; real_t sum = 0; for (uint32_t i = 0; i < y_hat.size(); i++) { if (abs(y[i] - y_hat[i]) <= delta) { @@ -223,7 +222,6 @@ real_t MLPPCostOld::HuberLoss(std::vector y_hat, std::vector y, } real_t MLPPCostOld::HuberLoss(std::vector> y_hat, std::vector> y, real_t delta) { - MLPPLinAlg alg; real_t sum = 0; for (uint32_t i = 0; i < y_hat.size(); i++) { for (uint32_t j = 0; j < y_hat[i].size(); j++) { @@ -238,7 +236,6 @@ real_t MLPPCostOld::HuberLoss(std::vector> y_hat, std::vecto } std::vector MLPPCostOld::HuberLossDeriv(std::vector y_hat, std::vector y, real_t delta) { - MLPPLinAlg alg; std::vector deriv; deriv.resize(y_hat.size()); @@ -257,8 +254,6 @@ std::vector MLPPCostOld::HuberLossDeriv(std::vector y_hat, std:: } std::vector> MLPPCostOld::HuberLossDeriv(std::vector> y_hat, std::vector> y, real_t delta) { - MLPPLinAlg alg; - std::vector> deriv; deriv.resize(y_hat.size()); for (uint32_t i = 0; i < deriv.size(); i++) { @@ -347,39 +342,35 @@ real_t MLPPCostOld::WassersteinLoss(std::vector> y_hat, std: } std::vector MLPPCostOld::WassersteinLossDeriv(std::vector y_hat, std::vector y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarMultiply(-1, y); // Simple. } std::vector> MLPPCostOld::WassersteinLossDeriv(std::vector> y_hat, std::vector> y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarMultiply(-1, y); // Simple. } real_t MLPPCostOld::HingeLoss(std::vector y_hat, std::vector y, std::vector weights, real_t C) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPRegOld regularization; return C * HingeLoss(y_hat, y) + regularization.regTerm(weights, 1, 0, "Ridge"); } real_t MLPPCostOld::HingeLoss(std::vector> y_hat, std::vector> y, std::vector> weights, real_t C) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPRegOld regularization; return C * HingeLoss(y_hat, y) + regularization.regTerm(weights, 1, 0, "Ridge"); } std::vector MLPPCostOld::HingeLossDeriv(std::vector y_hat, std::vector y, real_t C) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; return alg.scalarMultiply(C, HingeLossDeriv(y_hat, y)); } std::vector> MLPPCostOld::HingeLossDeriv(std::vector> y_hat, std::vector> y, real_t C) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; return alg.scalarMultiply(C, HingeLossDeriv(y_hat, y)); } real_t MLPPCostOld::dualFormSVM(std::vector alpha, std::vector> X, std::vector y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> Y = alg.diag(y); // Y is a diagnoal matrix. Y[i][j] = y[i] if i = i, else Y[i][j] = 0. Yt = Y. std::vector> K = alg.matmult(X, alg.transpose(X)); // TO DO: DON'T forget to add non-linear kernelizations. std::vector> Q = alg.matmult(alg.matmult(alg.transpose(Y), K), Y); @@ -390,7 +381,7 @@ real_t MLPPCostOld::dualFormSVM(std::vector alpha, std::vector MLPPCostOld::dualFormSVMDeriv(std::vector alpha, std::vector> X, std::vector y) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> Y = alg.zeromat(y.size(), y.size()); for (uint32_t i = 0; i < y.size(); i++) { Y[i][i] = y[i]; // Y is a diagnoal matrix. Y[i][j] = y[i] if i = i, else Y[i][j] = 0. Yt = Y. diff --git a/mlpp/data/data.cpp b/mlpp/data/data.cpp index 512e99b..3d37659 100644 --- a/mlpp/data/data.cpp +++ b/mlpp/data/data.cpp @@ -10,8 +10,10 @@ #include "core/os/file_access.h" #include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include "../softmax_net/softmax_net_old.h" #include "../stat/stat.h" +#include "../stat/stat_old.h" #include #include @@ -513,7 +515,7 @@ std::tuple>, std::vector>, s // MULTIVARIATE SUPERVISED void MLPPData::setData(int k, std::string fileName, std::vector> &inputSet, std::vector &outputSet) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::string inputTemp; std::string outputTemp; @@ -541,7 +543,7 @@ void MLPPData::setData(int k, std::string fileName, std::vector inputName, std::string outputName, std::vector> inputSet, std::vector outputSet) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; inputSet = alg.transpose(inputSet); for (uint32_t i = 0; i < inputSet.size(); i++) { std::cout << inputName[i] << std::endl; @@ -559,7 +561,7 @@ void MLPPData::printData(std::vector inputName, std::string outputN // UNSUPERVISED void MLPPData::setData(int k, std::string fileName, std::vector> &inputSet) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::string inputTemp; inputSet.resize(k); @@ -583,7 +585,7 @@ void MLPPData::setData(int k, std::string fileName, std::vector inputName, std::vector> inputSet) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; inputSet = alg.transpose(inputSet); for (uint32_t i = 0; i < inputSet.size(); i++) { std::cout << inputName[i] << std::endl; @@ -646,7 +648,7 @@ std::vector> MLPPData::rgb2gray(std::vector>> MLPPData::rgb2ycbcr(std::vector>> input) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> YCbCr; YCbCr = alg.resize(YCbCr, input); for (uint32_t i = 0; i < YCbCr[0].size(); i++) { @@ -662,7 +664,7 @@ std::vector>> MLPPData::rgb2ycbcr(std::vector>> MLPPData::rgb2hsv(std::vector>> input) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> HSV; HSV = alg.resize(HSV, input); for (uint32_t i = 0; i < HSV[0].size(); i++) { @@ -704,7 +706,7 @@ std::vector>> MLPPData::rgb2hsv(std::vector>> MLPPData::rgb2xyz(std::vector>> input) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> XYZ; XYZ = alg.resize(XYZ, input); std::vector> RGB2XYZ = { { 0.4124564, 0.3575761, 0.1804375 }, { 0.2126726, 0.7151522, 0.0721750 }, { 0.0193339, 0.1191920, 0.9503041 } }; @@ -712,7 +714,7 @@ std::vector>> MLPPData::rgb2xyz(std::vector>> MLPPData::xyz2rgb(std::vector>> input) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> XYZ; XYZ = alg.resize(XYZ, input); std::vector> RGB2XYZ = alg.inverse({ { 0.4124564, 0.3575761, 0.1804375 }, { 0.2126726, 0.7151522, 0.0721750 }, { 0.0193339, 0.1191920, 0.9503041 } }); @@ -907,7 +909,7 @@ std::vector> MLPPData::BOW(std::vector sentence } std::vector> MLPPData::TFIDF(std::vector sentences) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector wordList = removeNullByte(removeStopWords(createWordList(sentences))); std::vector> segmented_sentences; @@ -1093,10 +1095,10 @@ MLPPData::WordsToVecResult MLPPData::word_to_vec(std::vector senten } std::vector> MLPPData::LSA(std::vector sentences, int dim) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> docWordData = BOW(sentences, "Binary"); - MLPPLinAlg::SVDResultOld svr_res = alg.SVD(docWordData); + MLPPLinAlgOld::SVDResultOld svr_res = alg.SVD(docWordData); std::vector> S_trunc = alg.zeromat(dim, dim); std::vector> Vt_trunc; for (int i = 0; i < dim; i++) { @@ -1136,7 +1138,7 @@ void MLPPData::setInputNames(std::string fileName, std::vector &inp } std::vector> MLPPData::featureScaling(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; X = alg.transpose(X); std::vector max_elements, min_elements; max_elements.resize(X.size()); @@ -1156,8 +1158,8 @@ std::vector> MLPPData::featureScaling(std::vector> MLPPData::meanNormalization(std::vector> X) { - MLPPLinAlg alg; - MLPPStat stat; + MLPPLinAlgOld alg; + MLPPStatOld stat; // (X_j - mu_j) / std_j, for every j X = meanCentering(X); @@ -1168,7 +1170,7 @@ std::vector> MLPPData::meanNormalization(std::vector> MLPPData::meanCentering(std::vector> X) { - MLPPStat stat; + MLPPStatOld stat; for (uint32_t i = 0; i < X.size(); i++) { real_t mean_i = stat.mean(X[i]); for (uint32_t j = 0; j < X[i].size(); j++) { diff --git a/mlpp/data/data_old.cpp b/mlpp/data/data_old.cpp index 1ba47af..2fec574 100644 --- a/mlpp/data/data_old.cpp +++ b/mlpp/data/data_old.cpp @@ -9,9 +9,9 @@ #include "core/os/file_access.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include "../softmax_net/softmax_net_old.h" -#include "../stat/stat.h" +#include "../stat/stat_old.h" #include #include @@ -132,7 +132,7 @@ std::tuple>, std::vector>, s // MULTIVARIATE SUPERVISED void MLPPDataOld::setData(int k, std::string fileName, std::vector> &inputSet, std::vector &outputSet) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::string inputTemp; std::string outputTemp; @@ -160,7 +160,7 @@ void MLPPDataOld::setData(int k, std::string fileName, std::vector inputName, std::string outputName, std::vector> inputSet, std::vector outputSet) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; inputSet = alg.transpose(inputSet); for (uint32_t i = 0; i < inputSet.size(); i++) { std::cout << inputName[i] << std::endl; @@ -178,7 +178,7 @@ void MLPPDataOld::printData(std::vector inputName, std::string outp // UNSUPERVISED void MLPPDataOld::setData(int k, std::string fileName, std::vector> &inputSet) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::string inputTemp; inputSet.resize(k); @@ -202,7 +202,7 @@ void MLPPDataOld::setData(int k, std::string fileName, std::vector inputName, std::vector> inputSet) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; inputSet = alg.transpose(inputSet); for (uint32_t i = 0; i < inputSet.size(); i++) { std::cout << inputName[i] << std::endl; @@ -265,7 +265,7 @@ std::vector> MLPPDataOld::rgb2gray(std::vector>> MLPPDataOld::rgb2ycbcr(std::vector>> input) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> YCbCr; YCbCr = alg.resize(YCbCr, input); for (uint32_t i = 0; i < YCbCr[0].size(); i++) { @@ -281,7 +281,7 @@ std::vector>> MLPPDataOld::rgb2ycbcr(std::vector // Conversion formulas available here: // https://www.rapidtables.com/convert/color/rgb-to-hsv.html std::vector>> MLPPDataOld::rgb2hsv(std::vector>> input) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> HSV; HSV = alg.resize(HSV, input); for (uint32_t i = 0; i < HSV[0].size(); i++) { @@ -323,7 +323,7 @@ std::vector>> MLPPDataOld::rgb2hsv(std::vector>> MLPPDataOld::rgb2xyz(std::vector>> input) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> XYZ; XYZ = alg.resize(XYZ, input); std::vector> RGB2XYZ = { { 0.4124564, 0.3575761, 0.1804375 }, { 0.2126726, 0.7151522, 0.0721750 }, { 0.0193339, 0.1191920, 0.9503041 } }; @@ -331,7 +331,7 @@ std::vector>> MLPPDataOld::rgb2xyz(std::vector>> MLPPDataOld::xyz2rgb(std::vector>> input) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector>> XYZ; XYZ = alg.resize(XYZ, input); std::vector> RGB2XYZ = alg.inverse({ { 0.4124564, 0.3575761, 0.1804375 }, { 0.2126726, 0.7151522, 0.0721750 }, { 0.0193339, 0.1191920, 0.9503041 } }); @@ -526,7 +526,7 @@ std::vector> MLPPDataOld::BOW(std::vector sente } std::vector> MLPPDataOld::TFIDF(std::vector sentences) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector wordList = removeNullByte(removeStopWords(createWordList(sentences))); std::vector> segmented_sentences; @@ -712,10 +712,10 @@ MLPPDataOld::WordsToVecResult MLPPDataOld::word_to_vec(std::vector } std::vector> MLPPDataOld::LSA(std::vector sentences, int dim) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> docWordData = BOW(sentences, "Binary"); - MLPPLinAlg::SVDResultOld svr_res = alg.SVD(docWordData); + MLPPLinAlgOld::SVDResultOld svr_res = alg.SVD(docWordData); std::vector> S_trunc = alg.zeromat(dim, dim); std::vector> Vt_trunc; for (int i = 0; i < dim; i++) { @@ -755,7 +755,7 @@ void MLPPDataOld::setInputNames(std::string fileName, std::vector & } std::vector> MLPPDataOld::featureScaling(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; X = alg.transpose(X); std::vector max_elements, min_elements; max_elements.resize(X.size()); @@ -775,8 +775,8 @@ std::vector> MLPPDataOld::featureScaling(std::vector> MLPPDataOld::meanNormalization(std::vector> X) { - MLPPLinAlg alg; - MLPPStat stat; + MLPPLinAlgOld alg; + MLPPStatOld stat; // (X_j - mu_j) / std_j, for every j X = meanCentering(X); @@ -787,7 +787,7 @@ std::vector> MLPPDataOld::meanNormalization(std::vector> MLPPDataOld::meanCentering(std::vector> X) { - MLPPStat stat; + MLPPStatOld stat; for (uint32_t i = 0; i < X.size(); i++) { real_t mean_i = stat.mean(X[i]); for (uint32_t j = 0; j < X[i].size(); j++) { diff --git a/mlpp/dual_svc/dual_svc_old.cpp b/mlpp/dual_svc/dual_svc_old.cpp index 4651efc..35de9cd 100644 --- a/mlpp/dual_svc/dual_svc_old.cpp +++ b/mlpp/dual_svc/dual_svc_old.cpp @@ -6,9 +6,9 @@ #include "dual_svc_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -37,9 +37,8 @@ real_t MLPPDualSVCOld::modelTest(std::vector x) { } void MLPPDualSVCOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { - class MLPPCost cost; - MLPPLinAlg alg; - MLPPReg regularization; + class MLPPCostOld cost; + MLPPLinAlgOld alg; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -84,10 +83,10 @@ void MLPPDualSVCOld::gradientDescent(real_t learning_rate, int max_epoch, bool U } // void MLPPDualSVCOld::SGD(real_t learning_rate, int max_epoch, bool UI){ -// class MLPPCost cost; +// class MLPPCostOld cost; // MLPPActivationOld avn; -// MLPPLinAlg alg; -// MLPPReg regularization; +// MLPPLinAlgOld alg; +// MLPPRegOld regularization; // real_t cost_prev = 0; // int epoch = 1; @@ -117,10 +116,10 @@ void MLPPDualSVCOld::gradientDescent(real_t learning_rate, int max_epoch, bool U // } // void MLPPDualSVCOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI){ -// class MLPPCost cost; +// class MLPPCostOld cost; // MLPPActivationOld avn; -// MLPPLinAlg alg; -// MLPPReg regularization; +// MLPPLinAlgOld alg; +// MLPPRegOld regularization; // real_t cost_prev = 0; // int epoch = 1; @@ -167,7 +166,7 @@ void MLPPDualSVCOld::save(std::string fileName) { } real_t MLPPDualSVCOld::Cost(std::vector alpha, std::vector> X, std::vector y) { - class MLPPCost cost; + class MLPPCostOld cost; return cost.dualFormSVM(alpha, X, y); } @@ -177,7 +176,7 @@ std::vector MLPPDualSVCOld::Evaluate(std::vector> X) } std::vector MLPPDualSVCOld::propagate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector z; for (uint32_t i = 0; i < X.size(); i++) { real_t sum = 0; @@ -198,7 +197,7 @@ real_t MLPPDualSVCOld::Evaluate(std::vector x) { } real_t MLPPDualSVCOld::propagate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; real_t z = 0; for (uint32_t j = 0; j < alpha.size(); j++) { if (alpha[j] != 0) { @@ -227,7 +226,7 @@ void MLPPDualSVCOld::alphaProjection() { } real_t MLPPDualSVCOld::kernelFunction(std::vector u, std::vector v, std::string kernel) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (kernel == "Linear") { return alg.dot(u, v); } @@ -236,7 +235,7 @@ real_t MLPPDualSVCOld::kernelFunction(std::vector u, std::vector } std::vector> MLPPDualSVCOld::kernelFunction(std::vector> A, std::vector> B, std::string kernel) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (kernel == "Linear") { return alg.matmult(inputSet, alg.transpose(inputSet)); } diff --git a/mlpp/exp_reg/exp_reg_old.cpp b/mlpp/exp_reg/exp_reg_old.cpp index 8e5668d..1c51155 100644 --- a/mlpp/exp_reg/exp_reg_old.cpp +++ b/mlpp/exp_reg/exp_reg_old.cpp @@ -6,10 +6,10 @@ #include "exp_reg_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" -#include "../stat/stat.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" +#include "../stat/stat_old.h" #include "../utilities/utilities.h" #include @@ -39,8 +39,8 @@ real_t MLPPExpRegOld::modelTest(std::vector x) { } void MLPPExpRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -96,7 +96,7 @@ void MLPPExpRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI } void MLPPExpRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -142,8 +142,8 @@ void MLPPExpRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { } void MLPPExpRegOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -214,8 +214,8 @@ void MLPPExpRegOld::save(std::string fileName) { } real_t MLPPExpRegOld::Cost(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; return cost.MSE(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg); } diff --git a/mlpp/gan/gan.cpp b/mlpp/gan/gan.cpp index b27efd5..7799c9a 100644 --- a/mlpp/gan/gan.cpp +++ b/mlpp/gan/gan.cpp @@ -69,7 +69,7 @@ void MLPPGAN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { ComputeDiscriminatorGradientsResult dgrads = compute_discriminator_gradients(y_hat, _output_set); - dgrads.cumulative_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, dgrads.cumulative_hidden_layer_w_grad); + 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); update_discriminator_parameters(dgrads.cumulative_hidden_layer_w_grad, dgrads.output_w_grad, learning_rate); @@ -80,7 +80,7 @@ void MLPPGAN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { _output_set = alg.onevecnv(_n); Vector> cumulative_generator_hidden_layer_w_grad = compute_generator_gradients(y_hat, _output_set); - cumulative_generator_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, cumulative_generator_hidden_layer_w_grad); + 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(); diff --git a/mlpp/gan/gan_old.cpp b/mlpp/gan/gan_old.cpp index b935020..519e0ef 100644 --- a/mlpp/gan/gan_old.cpp +++ b/mlpp/gan/gan_old.cpp @@ -6,9 +6,9 @@ #include "gan_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -23,13 +23,13 @@ MLPPGANOld::~MLPPGANOld() { } std::vector> MLPPGANOld::generateExample(int n) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return modelSetTestGenerator(alg.gaussianNoise(n, k)); } void MLPPGANOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { class MLPPCost cost; - MLPPLinAlg alg; + MLPPLinAlgOld alg; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -79,7 +79,7 @@ void MLPPGANOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { } real_t MLPPGANOld::score() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPUtilities util; forwardPass(); return util.performance(y_hat, alg.onevec(n)); @@ -99,7 +99,7 @@ void MLPPGANOld::save(std::string fileName) { } void MLPPGANOld::addLayer(int n_hidden, std::string activation, std::string weightInit, std::string reg, real_t lambda, real_t alpha) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (network.empty()) { network.push_back(MLPPOldHiddenLayer(n_hidden, activation, alg.gaussianNoise(n, k), weightInit, reg, lambda, alpha)); network[0].forwardPass(); @@ -110,7 +110,7 @@ void MLPPGANOld::addLayer(int n_hidden, std::string activation, std::string weig } void MLPPGANOld::addOutputLayer(std::string weightInit, std::string reg, real_t lambda, real_t alpha) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (!network.empty()) { outputLayer = new MLPPOldOutputLayer(network[network.size() - 1].n_hidden, "Sigmoid", "LogLoss", network[network.size() - 1].a, weightInit, reg, lambda, alpha); } else { @@ -148,8 +148,8 @@ std::vector MLPPGANOld::modelSetTestDiscriminator(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; real_t totalRegTerm = 0; auto cost_function = outputLayer->cost_map[outputLayer->cost]; @@ -162,7 +162,7 @@ real_t MLPPGANOld::Cost(std::vector y_hat, std::vector y) { } void MLPPGANOld::forwardPass() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (!network.empty()) { network[0].input = alg.gaussianNoise(n, k); network[0].forwardPass(); @@ -180,7 +180,7 @@ void MLPPGANOld::forwardPass() { } void MLPPGANOld::updateDiscriminatorParameters(std::vector>> hiddenLayerUpdations, std::vector outputLayerUpdation, real_t learning_rate) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; outputLayer->weights = alg.subtraction(outputLayer->weights, outputLayerUpdation); outputLayer->bias -= learning_rate * alg.sum_elements(outputLayer->delta) / n; @@ -197,7 +197,7 @@ void MLPPGANOld::updateDiscriminatorParameters(std::vector>> hiddenLayerUpdations, real_t learning_rate) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (!network.empty()) { for (int i = network.size() / 2; i >= 0; i--) { @@ -210,10 +210,10 @@ void MLPPGANOld::updateGeneratorParameters(std::vector>>, std::vector> MLPPGANOld::computeDiscriminatorGradients(std::vector y_hat, std::vector outputSet) { - class MLPPCost cost; + class MLPPCostOld cost; MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; std::vector>> cumulativeHiddenLayerWGrad; // Tensor containing ALL hidden grads. @@ -246,10 +246,10 @@ std::tuple>>, std::vector> M } std::vector>> MLPPGANOld::computeGeneratorGradients(std::vector y_hat, std::vector outputSet) { - class MLPPCost cost; + class MLPPCostOld cost; MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; std::vector>> cumulativeHiddenLayerWGrad; // Tensor containing ALL hidden grads. diff --git a/mlpp/gauss_markov_checker/gauss_markov_checker.cpp b/mlpp/gauss_markov_checker/gauss_markov_checker.cpp index 8cea1ec..c3a3ea2 100644 --- a/mlpp/gauss_markov_checker/gauss_markov_checker.cpp +++ b/mlpp/gauss_markov_checker/gauss_markov_checker.cpp @@ -8,6 +8,7 @@ #include "../stat/stat.h" #include +/* void MLPPGaussMarkovChecker::checkGMConditions(std::vector eps) { bool condition1 = arithmeticMean(eps); bool condition2 = homoscedasticity(eps); @@ -55,6 +56,7 @@ bool MLPPGaussMarkovChecker::exogeneity(std::vector eps) { return true; } +*/ void MLPPGaussMarkovChecker::_bind_methods() { } diff --git a/mlpp/gauss_markov_checker/gauss_markov_checker.h b/mlpp/gauss_markov_checker/gauss_markov_checker.h index 7afc42b..a66c8a2 100644 --- a/mlpp/gauss_markov_checker/gauss_markov_checker.h +++ b/mlpp/gauss_markov_checker/gauss_markov_checker.h @@ -19,12 +19,14 @@ class MLPPGaussMarkovChecker : public Reference { GDCLASS(MLPPGaussMarkovChecker, Reference); public: + /* void checkGMConditions(std::vector eps); // Independent, 3 Gauss-Markov Conditions bool arithmeticMean(std::vector eps); // 1) Arithmetic Mean of 0. bool homoscedasticity(std::vector eps); // 2) Homoscedasticity bool exogeneity(std::vector eps); // 3) Cov of any 2 non-equal eps values = 0. + */ protected: static void _bind_methods(); diff --git a/mlpp/gauss_markov_checker/gauss_markov_checker_old.cpp b/mlpp/gauss_markov_checker/gauss_markov_checker_old.cpp index de8f671..e8c090e 100644 --- a/mlpp/gauss_markov_checker/gauss_markov_checker_old.cpp +++ b/mlpp/gauss_markov_checker/gauss_markov_checker_old.cpp @@ -5,7 +5,7 @@ // #include "gauss_markov_checker_old.h" -#include "../stat/stat.h" +#include "../stat/stat_old.h" #include void MLPPGaussMarkovCheckerOld::checkGMConditions(std::vector eps) { @@ -21,7 +21,7 @@ void MLPPGaussMarkovCheckerOld::checkGMConditions(std::vector eps) { } bool MLPPGaussMarkovCheckerOld::arithmeticMean(std::vector eps) { - MLPPStat stat; + MLPPStatOld stat; if (stat.mean(eps) == 0) { return true; } else { @@ -30,7 +30,7 @@ bool MLPPGaussMarkovCheckerOld::arithmeticMean(std::vector eps) { } bool MLPPGaussMarkovCheckerOld::homoscedasticity(std::vector eps) { - MLPPStat stat; + MLPPStatOld stat; real_t currentVar = (eps[0] - stat.mean(eps)) * (eps[0] - stat.mean(eps)) / eps.size(); for (uint32_t i = 0; i < eps.size(); i++) { if (currentVar != (eps[i] - stat.mean(eps)) * (eps[i] - stat.mean(eps)) / eps.size()) { @@ -42,7 +42,7 @@ bool MLPPGaussMarkovCheckerOld::homoscedasticity(std::vector eps) { } bool MLPPGaussMarkovCheckerOld::exogeneity(std::vector eps) { - MLPPStat stat; + MLPPStatOld stat; for (uint32_t i = 0; i < eps.size(); i++) { for (uint32_t j = 0; j < eps.size(); j++) { if (i != j) { diff --git a/mlpp/gaussian_nb/gaussian_nb_old.cpp b/mlpp/gaussian_nb/gaussian_nb_old.cpp index 267be0e..ade3f1c 100644 --- a/mlpp/gaussian_nb/gaussian_nb_old.cpp +++ b/mlpp/gaussian_nb/gaussian_nb_old.cpp @@ -6,8 +6,8 @@ #include "gaussian_nb_old.h" -#include "../lin_alg/lin_alg.h" -#include "../stat/stat.h" +#include "../lin_alg/lin_alg_old.h" +#include "../stat/stat_old.h" #include "../utilities/utilities.h" #include @@ -47,8 +47,8 @@ real_t MLPPGaussianNBOld::score() { } void MLPPGaussianNBOld::Evaluate() { - MLPPStat stat; - MLPPLinAlg alg; + MLPPStatOld stat; + MLPPLinAlgOld alg; // Computing mu_k_y and sigma_k_y mu.resize(class_num); diff --git a/mlpp/hidden_layer/hidden_layer_old.cpp b/mlpp/hidden_layer/hidden_layer_old.cpp index da10483..3f74b7f 100644 --- a/mlpp/hidden_layer/hidden_layer_old.cpp +++ b/mlpp/hidden_layer/hidden_layer_old.cpp @@ -6,7 +6,7 @@ #include "hidden_layer_old.h" #include "../activation/activation.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include #include @@ -103,7 +103,7 @@ MLPPOldHiddenLayer::MLPPOldHiddenLayer(int p_n_hidden, std::string p_activation, } void MLPPOldHiddenLayer::forwardPass() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; z = alg.mat_vec_add(alg.matmult(input, weights), bias); @@ -111,7 +111,7 @@ void MLPPOldHiddenLayer::forwardPass() { } void MLPPOldHiddenLayer::Test(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; z_test = alg.addition(alg.mat_vec_mult(alg.transpose(weights), x), bias); a_test = (avn.*activationTest_map[activation])(z_test, false); diff --git a/mlpp/lin_alg/lin_alg.cpp b/mlpp/lin_alg/lin_alg.cpp index 117621a..8805297 100644 --- a/mlpp/lin_alg/lin_alg.cpp +++ b/mlpp/lin_alg/lin_alg.cpp @@ -14,32 +14,20 @@ #include #include +/* std::vector> MLPPLinAlg::gramMatrix(std::vector> A) { return matmult(transpose(A), A); // AtA } +*/ +/* bool MLPPLinAlg::linearIndependenceChecker(std::vector> A) { if (det(gramMatrix(A), A.size()) == 0) { return false; } return true; } - -std::vector> MLPPLinAlg::gaussianNoise(int n, int m) { - std::random_device rd; - std::default_random_engine generator(rd()); - - std::vector> A; - A.resize(n); - for (int i = 0; i < n; i++) { - A[i].resize(m); - for (int j = 0; j < m; j++) { - std::normal_distribution distribution(0, 1); // Standard normal distribution. Mean of 0, std of 1. - A[i][j] = distribution(generator); - } - } - return A; -} +*/ Ref MLPPLinAlg::gaussian_noise(int n, int m) { std::random_device rd; @@ -60,53 +48,6 @@ Ref MLPPLinAlg::gaussian_noise(int n, int m) { return A; } -std::vector> MLPPLinAlg::addition(std::vector> A, std::vector> B) { - std::vector> C; - C.resize(A.size()); - for (uint32_t i = 0; i < C.size(); i++) { - C[i].resize(A[0].size()); - } - - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[0].size(); j++) { - C[i][j] = A[i][j] + B[i][j]; - } - } - return C; -} - -std::vector> MLPPLinAlg::subtraction(std::vector> A, std::vector> B) { - std::vector> C; - C.resize(A.size()); - for (uint32_t i = 0; i < C.size(); i++) { - C[i].resize(A[0].size()); - } - - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[0].size(); j++) { - C[i][j] = A[i][j] - B[i][j]; - } - } - return C; -} - -std::vector> MLPPLinAlg::matmult(std::vector> A, std::vector> B) { - std::vector> C; - C.resize(A.size()); - for (uint32_t i = 0; i < C.size(); i++) { - C[i].resize(B[0].size()); - } - - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t k = 0; k < B.size(); k++) { - for (uint32_t j = 0; j < B[0].size(); j++) { - C[i][j] += A[i][k] * B[k][j]; - } - } - } - return C; -} - Ref MLPPLinAlg::additionnm(const Ref &A, const Ref &B) { ERR_FAIL_COND_V(!A.is_valid() || !B.is_valid(), Ref()); Size2i a_size = A->size(); @@ -184,64 +125,6 @@ Ref MLPPLinAlg::matmultnm(const Ref &A, const Ref> MLPPLinAlg::hadamard_product(std::vector> A, std::vector> B) { - std::vector> C; - C.resize(A.size()); - for (uint32_t i = 0; i < C.size(); i++) { - C[i].resize(A[0].size()); - } - - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[0].size(); j++) { - C[i][j] = A[i][j] * B[i][j]; - } - } - return C; -} - -std::vector> MLPPLinAlg::kronecker_product(std::vector> A, std::vector> B) { - std::vector> C; - - // [1,1,1,1] [1,2,3,4,5] - // [1,1,1,1] [1,2,3,4,5] - // [1,2,3,4,5] - - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - // [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] [1,2,3,4,5] - - // Resulting matrix: A.size() * B.size() - // A[0].size() * B[0].size() - - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < B.size(); j++) { - std::vector> row; - for (uint32_t k = 0; k < A[0].size(); k++) { - row.push_back(scalarMultiply(A[i][k], B[j])); - } - C.push_back(flatten(row)); - } - } - return C; -} - -std::vector> MLPPLinAlg::elementWiseDivision(std::vector> A, std::vector> B) { - std::vector> C; - C.resize(A.size()); - for (uint32_t i = 0; i < C.size(); i++) { - C[i].resize(A[0].size()); - } - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - C[i][j] = A[i][j] / B[i][j]; - } - } - return C; -} - Ref MLPPLinAlg::hadamard_productnm(const Ref &A, const Ref &B) { ERR_FAIL_COND_V(!A.is_valid() || !B.is_valid(), Ref()); Size2i a_size = A->size(); @@ -333,39 +216,6 @@ Ref MLPPLinAlg::element_wise_divisionnvnm(const Ref &A, return C; } -std::vector> MLPPLinAlg::transpose(std::vector> A) { - std::vector> AT; - AT.resize(A[0].size()); - for (uint32_t i = 0; i < AT.size(); i++) { - AT[i].resize(A.size()); - } - - for (uint32_t i = 0; i < A[0].size(); i++) { - for (uint32_t j = 0; j < A.size(); j++) { - AT[i][j] = A[j][i]; - } - } - return AT; -} - -std::vector> MLPPLinAlg::scalarMultiply(real_t scalar, std::vector> A) { - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - A[i][j] *= scalar; - } - } - return A; -} - -std::vector> MLPPLinAlg::scalarAdd(real_t scalar, std::vector> A) { - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - A[i][j] += scalar; - } - } - return A; -} - Ref MLPPLinAlg::transposenm(const Ref &A) { Size2i a_size = A->size(); @@ -412,79 +262,6 @@ Ref MLPPLinAlg::scalar_addnm(real_t scalar, const Ref &A return AN; } -std::vector> MLPPLinAlg::log(std::vector> A) { - std::vector> B; - B.resize(A.size()); - for (uint32_t i = 0; i < B.size(); i++) { - B[i].resize(A[0].size()); - } - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - B[i][j] = Math::log(A[i][j]); - } - } - return B; -} - -std::vector> MLPPLinAlg::log10(std::vector> A) { - std::vector> B; - B.resize(A.size()); - for (uint32_t i = 0; i < B.size(); i++) { - B[i].resize(A[0].size()); - } - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - B[i][j] = Math::log10(A[i][j]); - } - } - return B; -} - -std::vector> MLPPLinAlg::exp(std::vector> A) { - std::vector> B; - B.resize(A.size()); - for (uint32_t i = 0; i < B.size(); i++) { - B[i].resize(A[0].size()); - } - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - B[i][j] = std::exp(A[i][j]); - } - } - return B; -} - -std::vector> MLPPLinAlg::erf(std::vector> A) { - std::vector> B; - B.resize(A.size()); - for (uint32_t i = 0; i < B.size(); i++) { - B[i].resize(A[0].size()); - } - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - B[i][j] = std::erf(A[i][j]); - } - } - return B; -} - -std::vector> MLPPLinAlg::exponentiate(std::vector> A, real_t p) { - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - A[i][j] = std::pow(A[i][j], p); - } - } - return A; -} - -std::vector> MLPPLinAlg::sqrt(std::vector> A) { - return exponentiate(A, 0.5); -} - -std::vector> MLPPLinAlg::cbrt(std::vector> A) { - return exponentiate(A, real_t(1) / real_t(3)); -} - Ref MLPPLinAlg::lognm(const Ref &A) { ERR_FAIL_COND_V(!A.is_valid(), Ref()); @@ -597,6 +374,7 @@ Ref MLPPLinAlg::cbrtnm(const Ref &A) { return exponentiatenm(A, real_t(1) / real_t(3)); } +/* std::vector> MLPPLinAlg::matrixPower(std::vector> A, int n) { std::vector> B = identity(A.size()); if (n == 0) { @@ -609,20 +387,7 @@ std::vector> MLPPLinAlg::matrixPower(std::vector> MLPPLinAlg::abs(std::vector> A) { - std::vector> B; - B.resize(A.size()); - for (uint32_t i = 0; i < B.size(); i++) { - B[i].resize(A[0].size()); - } - for (uint32_t i = 0; i < B.size(); i++) { - for (uint32_t j = 0; j < B[i].size(); j++) { - B[i][j] = Math::abs(A[i][j]); - } - } - return B; -} +*/ Ref MLPPLinAlg::absnm(const Ref &A) { ERR_FAIL_COND_V(!A.is_valid(), Ref()); @@ -643,41 +408,6 @@ Ref MLPPLinAlg::absnm(const Ref &A) { return out; } -real_t MLPPLinAlg::det(std::vector> A, int d) { - real_t deter = 0; - std::vector> B; - B.resize(d); - for (int i = 0; i < d; i++) { - B[i].resize(d); - } - - /* This is the base case in which the input is a 2x2 square matrix. - Recursion is performed unless and until we reach this base case, - such that we recieve a scalar as the result. */ - if (d == 2) { - return A[0][0] * A[1][1] - A[0][1] * A[1][0]; - } - - else { - for (int i = 0; i < d; i++) { - int sub_i = 0; - for (int j = 1; j < d; j++) { - int sub_j = 0; - for (int k = 0; k < d; k++) { - if (k == i) { - continue; - } - B[sub_i][sub_j] = A[j][k]; - sub_j++; - } - sub_i++; - } - deter += Math::pow(-1.0, i) * A[0][i] * det(B, d - 1); - } - } - return deter; -} - real_t MLPPLinAlg::detm(const Ref &A, int d) { ERR_FAIL_COND_V(!A.is_valid(), 0); @@ -715,6 +445,7 @@ real_t MLPPLinAlg::detm(const Ref &A, int d) { return deter; } +/* real_t MLPPLinAlg::trace(std::vector> A) { real_t trace = 0; for (uint32_t i = 0; i < A.size(); i++) { @@ -722,73 +453,7 @@ real_t MLPPLinAlg::trace(std::vector> A) { } return trace; } - -std::vector> MLPPLinAlg::cofactor(std::vector> A, int n, int i, int j) { - std::vector> cof; - cof.resize(A.size()); - for (uint32_t ii = 0; ii < cof.size(); ii++) { - cof[ii].resize(A.size()); - } - int sub_i = 0, sub_j = 0; - - for (int row = 0; row < n; row++) { - for (int col = 0; col < n; col++) { - if (row != i && col != j) { - cof[sub_i][sub_j++] = A[row][col]; - - if (sub_j == n - 1) { - sub_j = 0; - sub_i++; - } - } - } - } - return cof; -} - -std::vector> MLPPLinAlg::adjoint(std::vector> A) { - //Resizing the initial adjoint matrix - std::vector> adj; - adj.resize(A.size()); - for (uint32_t i = 0; i < adj.size(); i++) { - adj[i].resize(A.size()); - } - - // Checking for the case where the given N x N matrix is a scalar - if (A.size() == 1) { - adj[0][0] = 1; - return adj; - } - - if (A.size() == 2) { - adj[0][0] = A[1][1]; - adj[1][1] = A[0][0]; - - adj[0][1] = -A[0][1]; - adj[1][0] = -A[1][0]; - return adj; - } - - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A.size(); j++) { - std::vector> cof = cofactor(A, int(A.size()), i, j); - // 1 if even, -1 if odd - int sign = (i + j) % 2 == 0 ? 1 : -1; - adj[j][i] = sign * det(cof, int(A.size()) - 1); - } - } - return adj; -} - -// The inverse can be computed as (1 / determinant(A)) * adjoint(A) -std::vector> MLPPLinAlg::inverse(std::vector> A) { - return scalarMultiply(1 / det(A, int(A.size())), adjoint(A)); -} - -// This is simply the Moore-Penrose least squares approximation of the inverse. -std::vector> MLPPLinAlg::pinverse(std::vector> A) { - return matmult(inverse(matmult(transpose(A), A)), transpose(A)); -} +*/ Ref MLPPLinAlg::cofactornm(const Ref &A, int n, int i, int j) { Ref cof; @@ -859,20 +524,6 @@ Ref MLPPLinAlg::inversenm(const Ref &A) { Ref MLPPLinAlg::pinversenm(const Ref &A) { return matmultnm(inversenm(matmultnm(transposenm(A), A)), transposenm(A)); } - -std::vector> MLPPLinAlg::zeromat(int n, int m) { - std::vector> zeromat; - zeromat.resize(n); - for (uint32_t i = 0; i < zeromat.size(); i++) { - zeromat[i].resize(m); - } - return zeromat; -} - -std::vector> MLPPLinAlg::onemat(int n, int m) { - return full(n, m, 1); -} - Ref MLPPLinAlg::zeromatnm(int n, int m) { Ref mat; mat.instance(); @@ -901,48 +552,6 @@ Ref MLPPLinAlg::fullnm(int n, int m, int k) { return mat; } -std::vector> MLPPLinAlg::full(int n, int m, int k) { - std::vector> full; - full.resize(n); - for (uint32_t i = 0; i < full.size(); i++) { - full[i].resize(m); - } - for (uint32_t i = 0; i < full.size(); i++) { - for (uint32_t j = 0; j < full[i].size(); j++) { - full[i][j] = k; - } - } - return full; -} - -std::vector> MLPPLinAlg::sin(std::vector> A) { - std::vector> B; - B.resize(A.size()); - for (uint32_t i = 0; i < B.size(); i++) { - B[i].resize(A[0].size()); - } - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - B[i][j] = Math::sin(A[i][j]); - } - } - return B; -} - -std::vector> MLPPLinAlg::cos(std::vector> A) { - std::vector> B; - B.resize(A.size()); - for (uint32_t i = 0; i < B.size(); i++) { - B[i].resize(A[0].size()); - } - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - B[i][j] = Math::cos(A[i][j]); - } - } - return B; -} - Ref MLPPLinAlg::sinnm(const Ref &A) { ERR_FAIL_COND_V(!A.is_valid(), Ref()); @@ -980,19 +589,6 @@ Ref MLPPLinAlg::cosnm(const Ref &A) { return out; } -std::vector MLPPLinAlg::max(std::vector a, std::vector b) { - std::vector c; - c.resize(a.size()); - for (uint32_t i = 0; i < c.size(); i++) { - if (a[i] >= b[i]) { - c[i] = a[i]; - } else { - c[i] = b[i]; - } - } - return c; -} - Ref MLPPLinAlg::maxnvv(const Ref &a, const Ref &b) { Ref ret; ret.instance(); @@ -1023,6 +619,7 @@ Ref MLPPLinAlg::maxnvv(const Ref &a, const Ref> A) { return max(flatten(A)); } @@ -1044,7 +641,9 @@ std::vector> MLPPLinAlg::round(std::vector> A) { real_t sum = 0; for (uint32_t i = 0; i < A.size(); i++) { @@ -1054,24 +653,7 @@ real_t MLPPLinAlg::norm_2(std::vector> A) { } return Math::sqrt(sum); } - -std::vector> MLPPLinAlg::identity(real_t d) { - std::vector> identityMat; - identityMat.resize(d); - for (uint32_t i = 0; i < identityMat.size(); i++) { - identityMat[i].resize(d); - } - for (uint32_t i = 0; i < identityMat.size(); i++) { - for (uint32_t j = 0; j < identityMat.size(); j++) { - if (i == j) { - identityMat[i][j] = 1; - } else { - identityMat[i][j] = 0; - } - } - } - return identityMat; -} +*/ Ref MLPPLinAlg::identitym(int d) { Ref identity_mat; @@ -1088,21 +670,6 @@ Ref MLPPLinAlg::identitym(int d) { return identity_mat; } -std::vector> MLPPLinAlg::cov(std::vector> A) { - MLPPStat stat; - std::vector> covMat; - covMat.resize(A.size()); - for (uint32_t i = 0; i < covMat.size(); i++) { - covMat[i].resize(A.size()); - } - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A.size(); j++) { - covMat[i][j] = stat.covariance(A[i], A[j]); - } - } - return covMat; -} - Ref MLPPLinAlg::covnm(const Ref &A) { MLPPStat stat; @@ -1134,251 +701,6 @@ Ref MLPPLinAlg::covnm(const Ref &A) { return cov_mat; } -std::tuple>, std::vector>> MLPPLinAlg::eig(std::vector> A) { - /* - A (the entered parameter) in most use cases will be X'X, XX', etc. and must be symmetric. - That simply means that 1) X' = X and 2) X is a square matrix. This function that computes the - eigenvalues of a matrix is utilizing Jacobi's method. - */ - - real_t diagonal = true; // Perform the iterative Jacobi algorithm unless and until we reach a diagonal matrix which yields us the eigenvals. - - std::map val_to_vec; - std::vector> a_new; - std::vector> eigenvectors = identity(A.size()); - do { - real_t a_ij = A[0][1]; - real_t sub_i = 0; - real_t sub_j = 1; - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - if (i != j && Math::abs(A[i][j]) > a_ij) { - a_ij = A[i][j]; - sub_i = i; - sub_j = j; - } else if (i != j && Math::abs(A[i][j]) == a_ij) { - if (i < sub_i) { - a_ij = A[i][j]; - sub_i = i; - sub_j = j; - } - } - } - } - - real_t a_ii = A[sub_i][sub_i]; - real_t a_jj = A[sub_j][sub_j]; - //real_t a_ji = A[sub_j][sub_i]; - real_t theta; - - if (a_ii == a_jj) { - theta = M_PI / 4; - } else { - theta = 0.5 * atan(2 * a_ij / (a_ii - a_jj)); - } - - std::vector> P = identity(A.size()); - P[sub_i][sub_j] = -Math::sin(theta); - P[sub_i][sub_i] = Math::cos(theta); - P[sub_j][sub_j] = Math::cos(theta); - P[sub_j][sub_i] = Math::sin(theta); - - a_new = matmult(matmult(inverse(P), A), P); - - for (uint32_t i = 0; i < a_new.size(); i++) { - for (uint32_t j = 0; j < a_new[i].size(); j++) { - if (i != j && Math::round(a_new[i][j]) == 0) { - a_new[i][j] = 0; - } - } - } - - bool non_zero = false; - for (uint32_t i = 0; i < a_new.size(); i++) { - for (uint32_t j = 0; j < a_new[i].size(); j++) { - if (i != j && Math::round(a_new[i][j]) != 0) { - non_zero = true; - } - } - } - - if (non_zero) { - diagonal = false; - } else { - diagonal = true; - } - - if (a_new == A) { - diagonal = true; - for (uint32_t i = 0; i < a_new.size(); i++) { - for (uint32_t j = 0; j < a_new[i].size(); j++) { - if (i != j) { - a_new[i][j] = 0; - } - } - } - } - - eigenvectors = matmult(eigenvectors, P); - A = a_new; - - } while (!diagonal); - - std::vector> a_new_prior = a_new; - - // Bubble Sort. Should change this later. - for (uint32_t i = 0; i < a_new.size() - 1; i++) { - for (uint32_t j = 0; j < a_new.size() - 1 - i; j++) { - if (a_new[j][j] < a_new[j + 1][j + 1]) { - real_t temp = a_new[j + 1][j + 1]; - a_new[j + 1][j + 1] = a_new[j][j]; - a_new[j][j] = temp; - } - } - } - - for (uint32_t i = 0; i < a_new.size(); i++) { - for (uint32_t j = 0; j < a_new.size(); j++) { - if (a_new[i][i] == a_new_prior[j][j]) { - val_to_vec[i] = j; - } - } - } - - std::vector> eigen_temp = eigenvectors; - for (uint32_t i = 0; i < eigenvectors.size(); i++) { - for (uint32_t j = 0; j < eigenvectors[i].size(); j++) { - eigenvectors[i][j] = eigen_temp[i][val_to_vec[j]]; - } - } - return { eigenvectors, a_new }; -} - -MLPPLinAlg::EigenResultOld MLPPLinAlg::eigen_old(std::vector> A) { - /* - A (the entered parameter) in most use cases will be X'X, XX', etc. and must be symmetric. - That simply means that 1) X' = X and 2) X is a square matrix. This function that computes the - eigenvalues of a matrix is utilizing Jacobi's method. - */ - - real_t diagonal = true; // Perform the iterative Jacobi algorithm unless and until we reach a diagonal matrix which yields us the eigenvals. - - std::map val_to_vec; - std::vector> a_new; - std::vector> eigenvectors = identity(A.size()); - do { - real_t a_ij = A[0][1]; - real_t sub_i = 0; - real_t sub_j = 1; - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - if (i != j && Math::abs(A[i][j]) > a_ij) { - a_ij = A[i][j]; - sub_i = i; - sub_j = j; - } else if (i != j && Math::abs(A[i][j]) == a_ij) { - if (i < sub_i) { - a_ij = A[i][j]; - sub_i = i; - sub_j = j; - } - } - } - } - - real_t a_ii = A[sub_i][sub_i]; - real_t a_jj = A[sub_j][sub_j]; - //real_t a_ji = A[sub_j][sub_i]; - real_t theta; - - if (a_ii == a_jj) { - theta = M_PI / 4; - } else { - theta = 0.5 * atan(2 * a_ij / (a_ii - a_jj)); - } - - std::vector> P = identity(A.size()); - P[sub_i][sub_j] = -Math::sin(theta); - P[sub_i][sub_i] = Math::cos(theta); - P[sub_j][sub_j] = Math::cos(theta); - P[sub_j][sub_i] = Math::sin(theta); - - a_new = matmult(matmult(inverse(P), A), P); - - for (uint32_t i = 0; i < a_new.size(); i++) { - for (uint32_t j = 0; j < a_new[i].size(); j++) { - if (i != j && Math::round(a_new[i][j]) == 0) { - a_new[i][j] = 0; - } - } - } - - bool non_zero = false; - for (uint32_t i = 0; i < a_new.size(); i++) { - for (uint32_t j = 0; j < a_new[i].size(); j++) { - if (i != j && Math::round(a_new[i][j]) != 0) { - non_zero = true; - } - } - } - - if (non_zero) { - diagonal = false; - } else { - diagonal = true; - } - - if (a_new == A) { - diagonal = true; - for (uint32_t i = 0; i < a_new.size(); i++) { - for (uint32_t j = 0; j < a_new[i].size(); j++) { - if (i != j) { - a_new[i][j] = 0; - } - } - } - } - - eigenvectors = matmult(eigenvectors, P); - A = a_new; - - } while (!diagonal); - - std::vector> a_new_prior = a_new; - - // Bubble Sort. Should change this later. - for (uint32_t i = 0; i < a_new.size() - 1; i++) { - for (uint32_t j = 0; j < a_new.size() - 1 - i; j++) { - if (a_new[j][j] < a_new[j + 1][j + 1]) { - real_t temp = a_new[j + 1][j + 1]; - a_new[j + 1][j + 1] = a_new[j][j]; - a_new[j][j] = temp; - } - } - } - - for (uint32_t i = 0; i < a_new.size(); i++) { - for (uint32_t j = 0; j < a_new.size(); j++) { - if (a_new[i][i] == a_new_prior[j][j]) { - val_to_vec[i] = j; - } - } - } - - std::vector> eigen_temp = eigenvectors; - for (uint32_t i = 0; i < eigenvectors.size(); i++) { - for (uint32_t j = 0; j < eigenvectors[i].size(); j++) { - eigenvectors[i][j] = eigen_temp[i][val_to_vec[j]]; - } - } - - EigenResultOld res; - res.eigen_vectors = eigenvectors; - res.eigen_values = a_new; - - return res; -} - MLPPLinAlg::EigenResult MLPPLinAlg::eigen(Ref A) { EigenResult res; @@ -1519,26 +841,6 @@ MLPPLinAlg::EigenResult MLPPLinAlg::eigen(Ref A) { return res; } -MLPPLinAlg::SVDResultOld MLPPLinAlg::SVD(std::vector> A) { - EigenResultOld left_eigen = eigen_old(matmult(A, transpose(A))); - EigenResultOld right_eigen = eigen_old(matmult(transpose(A), A)); - - std::vector> singularvals = sqrt(left_eigen.eigen_values); - std::vector> sigma = zeromat(A.size(), A[0].size()); - for (uint32_t i = 0; i < singularvals.size(); i++) { - for (uint32_t j = 0; j < singularvals[i].size(); j++) { - sigma[i][j] = singularvals[i][j]; - } - } - - SVDResultOld res; - res.U = left_eigen.eigen_vectors; - res.S = sigma; - res.Vt = right_eigen.eigen_vectors; - - return res; -} - MLPPLinAlg::SVDResult MLPPLinAlg::svd(const Ref &A) { SVDResult res; @@ -1567,11 +869,14 @@ MLPPLinAlg::SVDResult MLPPLinAlg::svd(const Ref &A) { return res; } +/* std::vector MLPPLinAlg::vectorProjection(std::vector a, std::vector b) { real_t product = dot(a, b) / dot(a, a); return scalarMultiply(product, a); // Projection of vector a onto b. Denotated as proj_a(b). } +*/ +/* std::vector> MLPPLinAlg::gramSchmidtProcess(std::vector> A) { A = transpose(A); // C++ vectors lack a mechanism to directly index columns. So, we transpose *a copy* of A for this purpose for ease of use. std::vector> B; @@ -1591,13 +896,9 @@ std::vector> MLPPLinAlg::gramSchmidtProcess(std::vector>, std::vector>> MLPPLinAlg::QRD(std::vector> A) { - std::vector> Q = gramSchmidtProcess(A); - std::vector> R = matmult(transpose(Q), A); - return { Q, R }; -} - +/* MLPPLinAlg::QRDResult MLPPLinAlg::qrd(std::vector> A) { QRDResult res; @@ -1606,29 +907,9 @@ MLPPLinAlg::QRDResult MLPPLinAlg::qrd(std::vector> A) { return res; } +*/ -std::tuple>, std::vector>> MLPPLinAlg::chol(std::vector> A) { - std::vector> L = zeromat(A.size(), A[0].size()); - for (uint32_t j = 0; j < L.size(); j++) { // Matrices entered must be square. No problem here. - for (uint32_t i = j; i < L.size(); i++) { - if (i == j) { - real_t sum = 0; - for (uint32_t k = 0; k < j; k++) { - sum += L[i][k] * L[i][k]; - } - L[i][j] = Math::sqrt(A[i][j] - sum); - } else { // That is, i!=j - real_t sum = 0; - for (uint32_t k = 0; k < j; k++) { - sum += L[i][k] * L[j][k]; - } - L[i][j] = (A[i][j] - sum) / L[j][j]; - } - } - } - return { L, transpose(L) }; // Indeed, L.T is our upper triangular matrix. -} - +/* MLPPLinAlg::CholeskyResult MLPPLinAlg::cholesky(std::vector> A) { std::vector> L = zeromat(A.size(), A[0].size()); for (uint32_t j = 0; j < L.size(); j++) { // Matrices entered must be square. No problem here. @@ -1655,7 +936,9 @@ MLPPLinAlg::CholeskyResult MLPPLinAlg::cholesky(std::vector> return res; } +*/ +/* real_t MLPPLinAlg::sum_elements(std::vector> A) { real_t sum = 0; for (uint32_t i = 0; i < A.size(); i++) { @@ -1665,16 +948,7 @@ real_t MLPPLinAlg::sum_elements(std::vector> A) { } return sum; } - -std::vector MLPPLinAlg::flatten(std::vector> A) { - std::vector a; - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - a.push_back(A[i][j]); - } - } - return a; -} +*/ Ref MLPPLinAlg::flattenmnv(const Vector> &A) { Ref a; @@ -1722,6 +996,7 @@ Ref MLPPLinAlg::flattenvvnv(const Ref &A) { return res; } +/* std::vector MLPPLinAlg::solve(std::vector> A, std::vector b) { return mat_vec_mult(inverse(A), b); } @@ -1776,24 +1051,7 @@ bool MLPPLinAlg::zeroEigenvalue(std::vector> A) { } return false; } - -void MLPPLinAlg::printMatrix(std::vector> A) { - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - std::cout << A[i][j] << " "; - } - std::cout << std::endl; - } -} - -std::vector> MLPPLinAlg::outerProduct(std::vector a, std::vector b) { - std::vector> C; - C.resize(a.size()); - for (uint32_t i = 0; i < C.size(); i++) { - C[i] = scalarMultiply(a[i], b); - } - return C; -} +*/ Ref MLPPLinAlg::outer_product(const Ref &a, const Ref &b) { Ref C; @@ -1815,17 +1073,6 @@ Ref MLPPLinAlg::outer_product(const Ref &a, const Ref MLPPLinAlg::hadamard_product(std::vector a, std::vector b) { - std::vector c; - c.resize(a.size()); - - for (uint32_t i = 0; i < a.size(); i++) { - c[i] = a[i] * b[i]; - } - - return c; -} - Ref MLPPLinAlg::hadamard_productnv(const Ref &a, const Ref &b) { ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); @@ -1868,16 +1115,6 @@ void MLPPLinAlg::hadamard_productv(const Ref &a, const Ref MLPPLinAlg::elementWiseDivision(std::vector a, std::vector b) { - std::vector c; - c.resize(a.size()); - - for (uint32_t i = 0; i < a.size(); i++) { - c[i] = a[i] / b[i]; - } - return c; -} - Ref MLPPLinAlg::element_wise_divisionnv(const Ref &a, const Ref &b) { ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); @@ -1901,13 +1138,6 @@ Ref MLPPLinAlg::element_wise_divisionnv(const Ref &a, co return out; } -std::vector MLPPLinAlg::scalarMultiply(real_t scalar, std::vector a) { - for (uint32_t i = 0; i < a.size(); i++) { - a[i] *= scalar; - } - return a; -} - Ref MLPPLinAlg::scalar_multiplynv(real_t scalar, const Ref &a) { ERR_FAIL_COND_V(!a.is_valid(), Ref()); @@ -1944,13 +1174,6 @@ void MLPPLinAlg::scalar_multiplyv(real_t scalar, const Ref &a, Ref MLPPLinAlg::scalarAdd(real_t scalar, std::vector a) { - for (uint32_t i = 0; i < a.size(); i++) { - a[i] += scalar; - } - return a; -} - Ref MLPPLinAlg::scalar_addnv(real_t scalar, const Ref &a) { ERR_FAIL_COND_V(!a.is_valid(), Ref()); @@ -1987,15 +1210,6 @@ void MLPPLinAlg::scalar_addv(real_t scalar, const Ref &a, Ref MLPPLinAlg::addition(std::vector a, std::vector b) { - std::vector c; - c.resize(a.size()); - for (uint32_t i = 0; i < a.size(); i++) { - c[i] = a[i] + b[i]; - } - return c; -} - Ref MLPPLinAlg::additionnv(const Ref &a, const Ref &b) { ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); @@ -2037,15 +1251,6 @@ void MLPPLinAlg::additionv(const Ref &a, const Ref &b, R } } -std::vector MLPPLinAlg::subtraction(std::vector a, std::vector b) { - std::vector c; - c.resize(a.size()); - for (uint32_t i = 0; i < a.size(); i++) { - c[i] = a[i] - b[i]; - } - return c; -} - Ref MLPPLinAlg::subtractionnv(const Ref &a, const Ref &b) { ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), Ref()); @@ -2092,13 +1297,6 @@ void MLPPLinAlg::subtractionv(const Ref &a, const Ref &b } } -std::vector MLPPLinAlg::subtractMatrixRows(std::vector a, std::vector> B) { - for (uint32_t i = 0; i < B.size(); i++) { - a = subtraction(a, B[i]); - } - return a; -} - Ref MLPPLinAlg::subtract_matrix_rowsnv(const Ref &a, const Ref &B) { Ref c = a->duplicate(); @@ -2230,14 +1428,6 @@ Ref MLPPLinAlg::cbrtnv(const Ref &a) { return exponentiatenv(a, static_cast(1) / static_cast(3)); } -real_t MLPPLinAlg::dot(std::vector a, std::vector b) { - real_t c = 0; - for (uint32_t i = 0; i < a.size(); i++) { - c += a[i] * b[i]; - } - return c; -} - real_t MLPPLinAlg::dotnv(const Ref &a, const Ref &b) { int a_size = a->size(); @@ -2253,6 +1443,7 @@ real_t MLPPLinAlg::dotnv(const Ref &a, const Ref &b) { return c; } +/* std::vector MLPPLinAlg::cross(std::vector a, std::vector b) { // Cross products exist in R^7 also. Though, I will limit it to R^3 as Wolfram does this. std::vector> mat = { onevec(3), a, b }; @@ -2263,33 +1454,7 @@ std::vector MLPPLinAlg::cross(std::vector a, std::vector return { det1, det2, det3 }; } - -std::vector MLPPLinAlg::abs(std::vector a) { - std::vector b; - b.resize(a.size()); - for (uint32_t i = 0; i < b.size(); i++) { - b[i] = Math::abs(a[i]); - } - return b; -} - -std::vector MLPPLinAlg::zerovec(int n) { - std::vector zerovec; - zerovec.resize(n); - return zerovec; -} - -std::vector MLPPLinAlg::onevec(int n) { - return full(n, 1); -} - -std::vector> MLPPLinAlg::diag(std::vector a) { - std::vector> B = zeromat(a.size(), a.size()); - for (uint32_t i = 0; i < B.size(); i++) { - B[i][i] = a[i]; - } - return B; -} +*/ Ref MLPPLinAlg::diagnm(const Ref &a) { int a_size = a->size(); @@ -2310,15 +1475,6 @@ Ref MLPPLinAlg::diagnm(const Ref &a) { return B; } -std::vector MLPPLinAlg::full(int n, int k) { - std::vector full; - full.resize(n); - for (uint32_t i = 0; i < full.size(); i++) { - full[i] = k; - } - return full; -} - Ref MLPPLinAlg::absv(const Ref &a) { ERR_FAIL_COND_V(!a.is_valid(), Ref()); @@ -2366,24 +1522,6 @@ Ref MLPPLinAlg::fullnv(int n, int k) { return vec; } -std::vector MLPPLinAlg::sin(std::vector a) { - std::vector b; - b.resize(a.size()); - for (uint32_t i = 0; i < a.size(); i++) { - b[i] = Math::sin(a[i]); - } - return b; -} - -std::vector MLPPLinAlg::cos(std::vector a) { - std::vector b; - b.resize(a.size()); - for (uint32_t i = 0; i < a.size(); i++) { - b[i] = Math::cos(a[i]); - } - return b; -} - Ref MLPPLinAlg::sinnv(const Ref &a) { ERR_FAIL_COND_V(!a.is_valid(), Ref()); @@ -2421,6 +1559,7 @@ Ref MLPPLinAlg::cosnv(const Ref &a) { return out; } +/* std::vector> MLPPLinAlg::rotate(std::vector> A, real_t theta, int axis) { std::vector> rotationMatrix = { { Math::cos(theta), -Math::sin(theta) }, { Math::sin(theta), Math::cos(theta) } }; if (axis == 0) { @@ -2433,18 +1572,7 @@ std::vector> MLPPLinAlg::rotate(std::vector> MLPPLinAlg::max(std::vector> A, std::vector> B) { - std::vector> C; - C.resize(A.size()); - for (uint32_t i = 0; i < C.size(); i++) { - C[i].resize(A[0].size()); - } - for (uint32_t i = 0; i < A.size(); i++) { - C[i] = max(A[i], B[i]); - } - return C; -} +*/ Ref MLPPLinAlg::maxnm(const Ref &A, const Ref &B) { Ref C; @@ -2464,26 +1592,6 @@ Ref MLPPLinAlg::maxnm(const Ref &A, const Ref a) { - int max = a[0]; - for (uint32_t i = 0; i < a.size(); i++) { - if (a[i] > max) { - max = a[i]; - } - } - return max; -} - -real_t MLPPLinAlg::min(std::vector a) { - int min = a[0]; - for (uint32_t i = 0; i < a.size(); i++) { - if (a[i] < min) { - min = a[i]; - } - } - return min; -} - real_t MLPPLinAlg::maxvr(const Ref &a) { ERR_FAIL_COND_V(!a.is_valid(), -Math_INF); @@ -2523,6 +1631,7 @@ real_t MLPPLinAlg::minvr(const Ref &a) { return min_element; } +/* std::vector MLPPLinAlg::round(std::vector a) { std::vector b; b.resize(a.size()); @@ -2531,15 +1640,9 @@ std::vector MLPPLinAlg::round(std::vector a) { } return b; } +*/ // Multidimensional Euclidean Distance -real_t MLPPLinAlg::euclideanDistance(std::vector a, std::vector b) { - real_t dist = 0; - for (uint32_t i = 0; i < a.size(); i++) { - dist += (a[i] - b[i]) * (a[i] - b[i]); - } - return Math::sqrt(dist); -} real_t MLPPLinAlg::euclidean_distance(const Ref &a, const Ref &b) { ERR_FAIL_COND_V(!a.is_valid() || !b.is_valid(), 0); @@ -2578,17 +1681,12 @@ real_t MLPPLinAlg::euclidean_distance_squared(const Ref &a, const Re return dist; } +/* real_t MLPPLinAlg::norm_2(std::vector a) { return Math::sqrt(norm_sq(a)); } +*/ -real_t MLPPLinAlg::norm_sq(std::vector a) { - real_t n_sq = 0; - for (uint32_t i = 0; i < a.size(); i++) { - n_sq += a[i] * a[i]; - } - return n_sq; -} real_t MLPPLinAlg::norm_sqv(const Ref &a) { ERR_FAIL_COND_V(!a.is_valid(), 0); @@ -2602,14 +1700,6 @@ real_t MLPPLinAlg::norm_sqv(const Ref &a) { return n_sq; } -real_t MLPPLinAlg::sum_elements(std::vector a) { - real_t sum = 0; - for (uint32_t i = 0; i < a.size(); i++) { - sum += a[i]; - } - return sum; -} - real_t MLPPLinAlg::sum_elementsv(const Ref &a) { int a_size = a->size(); @@ -2622,37 +1712,11 @@ real_t MLPPLinAlg::sum_elementsv(const Ref &a) { return sum; } +/* real_t MLPPLinAlg::cosineSimilarity(std::vector a, std::vector b) { return dot(a, b) / (norm_2(a) * norm_2(b)); } - -void MLPPLinAlg::printVector(std::vector a) { - for (uint32_t i = 0; i < a.size(); i++) { - std::cout << a[i] << " "; - } - std::cout << std::endl; -} - -std::vector> MLPPLinAlg::mat_vec_add(std::vector> A, std::vector b) { - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t j = 0; j < A[i].size(); j++) { - A[i][j] += b[j]; - } - } - return A; -} - -std::vector MLPPLinAlg::mat_vec_mult(std::vector> A, std::vector b) { - std::vector c; - c.resize(A.size()); - - for (uint32_t i = 0; i < A.size(); i++) { - for (uint32_t k = 0; k < b.size(); k++) { - c[i] += A[i][k] * b[k]; - } - } - return c; -} +*/ Ref MLPPLinAlg::mat_vec_addnm(const Ref &A, const Ref &b) { ERR_FAIL_COND_V(!A.is_valid() || !b.is_valid(), Ref()); @@ -2707,14 +1771,7 @@ Ref MLPPLinAlg::mat_vec_multnv(const Ref &A, const Ref>> MLPPLinAlg::addition(std::vector>> A, std::vector>> B) { - for (uint32_t i = 0; i < A.size(); i++) { - A[i] = addition(A[i], B[i]); - } - return A; -} - -Vector> MLPPLinAlg::addition_vt(const Vector> &A, const Vector> &B) { +Vector> MLPPLinAlg::additionnvt(const Vector> &A, const Vector> &B) { Vector> res; res.resize(A.size()); @@ -2725,14 +1782,7 @@ Vector> MLPPLinAlg::addition_vt(const Vector> &A return res; } -std::vector>> MLPPLinAlg::elementWiseDivision(std::vector>> A, std::vector>> B) { - for (uint32_t i = 0; i < A.size(); i++) { - A[i] = elementWiseDivision(A[i], B[i]); - } - return A; -} - -Vector> MLPPLinAlg::element_wise_divisionnv_vt(const Vector> &A, const Vector> &B) { +Vector> MLPPLinAlg::element_wise_divisionnvnvt(const Vector> &A, const Vector> &B) { Vector> res; res.resize(A.size()); @@ -2743,14 +1793,7 @@ Vector> MLPPLinAlg::element_wise_divisionnv_vt(const Vector>> MLPPLinAlg::sqrt(std::vector>> A) { - for (uint32_t i = 0; i < A.size(); i++) { - A[i] = sqrt(A[i]); - } - return A; -} - -Vector> MLPPLinAlg::sqrt_vt(const Vector> &A) { +Vector> MLPPLinAlg::sqrtnvt(const Vector> &A) { Vector> res; res.resize(A.size()); @@ -2761,14 +1804,7 @@ Vector> MLPPLinAlg::sqrt_vt(const Vector> &A) { return res; } -std::vector>> MLPPLinAlg::exponentiate(std::vector>> A, real_t p) { - for (uint32_t i = 0; i < A.size(); i++) { - A[i] = exponentiate(A[i], p); - } - return A; -} - -Vector> MLPPLinAlg::exponentiate_vt(const Vector> &A, real_t p) { +Vector> MLPPLinAlg::exponentiatenvt(const Vector> &A, real_t p) { Vector> res; res.resize(A.size()); @@ -2779,6 +1815,7 @@ Vector> MLPPLinAlg::exponentiate_vt(const Vector return res; } +/* std::vector> MLPPLinAlg::tensor_vec_mult(std::vector>> A, std::vector b) { std::vector> C; C.resize(A.size()); @@ -2792,7 +1829,9 @@ std::vector> MLPPLinAlg::tensor_vec_mult(std::vector MLPPLinAlg::flatten(std::vector>> A) { std::vector c; for (uint32_t i = 0; i < A.size(); i++) { @@ -2801,55 +1840,22 @@ std::vector MLPPLinAlg::flatten(std::vector>> A) { - for (uint32_t i = 0; i < A.size(); i++) { - printMatrix(A[i]); - if (i != A.size() - 1) { - std::cout << std::endl; - } - } -} - -std::vector>> MLPPLinAlg::scalarMultiply(real_t scalar, std::vector>> A) { - for (uint32_t i = 0; i < A.size(); i++) { - A[i] = scalarMultiply(scalar, A[i]); - } - return A; -} - -std::vector>> MLPPLinAlg::scalarAdd(real_t scalar, std::vector>> A) { - for (uint32_t i = 0; i < A.size(); i++) { - A[i] = scalarAdd(scalar, A[i]); - } - return A; -} - -Vector> MLPPLinAlg::scalar_multiply_vm(real_t scalar, Vector> A) { +Vector> MLPPLinAlg::scalar_multiplynvt(real_t scalar, Vector> A) { for (int i = 0; i < A.size(); i++) { A.write[i] = scalar_multiplynm(scalar, A[i]); } return A; } -Vector> MLPPLinAlg::scalar_add_vm(real_t scalar, Vector> A) { +Vector> MLPPLinAlg::scalar_addnvt(real_t scalar, Vector> A) { for (int i = 0; i < A.size(); i++) { A.write[i] = scalar_addnm(scalar, A[i]); } return A; } -std::vector>> MLPPLinAlg::resize(std::vector>> A, std::vector>> B) { - A.resize(B.size()); - for (uint32_t i = 0; i < B.size(); i++) { - A[i].resize(B[i].size()); - for (uint32_t j = 0; j < B[i].size(); j++) { - A[i][j].resize(B[i][j].size()); - } - } - return A; -} - -Vector> MLPPLinAlg::resize_vt(const Vector> &A, const Vector> &B) { +Vector> MLPPLinAlg::resizenvt(const Vector> &A, const Vector> &B) { Vector> res; res.resize(B.size()); @@ -2864,14 +1870,7 @@ Vector> MLPPLinAlg::resize_vt(const Vector> &A, return res; } -std::vector>> MLPPLinAlg::max(std::vector>> A, std::vector>> B) { - for (uint32_t i = 0; i < A.size(); i++) { - A[i] = max(A[i], B[i]); - } - return A; -} - -Vector> MLPPLinAlg::max_vt(const Vector> &A, const Vector> &B) { +Vector> MLPPLinAlg::maxnvt(const Vector> &A, const Vector> &B) { Vector> res; res.resize(A.size()); @@ -2882,14 +1881,7 @@ Vector> MLPPLinAlg::max_vt(const Vector> &A, con return res; } -std::vector>> MLPPLinAlg::abs(std::vector>> A) { - for (uint32_t i = 0; i < A.size(); i++) { - A[i] = abs(A[i]); - } - return A; -} - -Vector> MLPPLinAlg::abs_vt(const Vector> &A) { +Vector> MLPPLinAlg::absnvt(const Vector> &A) { Vector> res; res.resize(A.size()); @@ -2900,6 +1892,7 @@ Vector> MLPPLinAlg::abs_vt(const Vector> &A) { return A; } +/* real_t MLPPLinAlg::norm_2(std::vector>> A) { real_t sum = 0; for (uint32_t i = 0; i < A.size(); i++) { @@ -2911,7 +1904,9 @@ real_t MLPPLinAlg::norm_2(std::vector>> A) { } return Math::sqrt(sum); } +*/ +/* // Bad implementation. Change this later. std::vector>> MLPPLinAlg::vector_wise_tensor_product(std::vector>> A, std::vector> B) { std::vector>> C; @@ -2934,6 +1929,7 @@ std::vector>> MLPPLinAlg::vector_wise_tensor_pro } return C; } +*/ void MLPPLinAlg::_bind_methods() { } diff --git a/mlpp/lin_alg/lin_alg.h b/mlpp/lin_alg/lin_alg.h index ed71ef8..c3d7fb6 100644 --- a/mlpp/lin_alg/lin_alg.h +++ b/mlpp/lin_alg/lin_alg.h @@ -26,45 +26,23 @@ class MLPPLinAlg : public Reference { public: // MATRIX FUNCTIONS - std::vector> gramMatrix(std::vector> A); + //std::vector> gramMatrix(std::vector> A); + //bool linearIndependenceChecker(std::vector> A); - bool linearIndependenceChecker(std::vector> A); - - std::vector> gaussianNoise(int n, int m); Ref gaussian_noise(int n, int m); - std::vector> addition(std::vector> A, std::vector> B); - std::vector> subtraction(std::vector> A, std::vector> B); - std::vector> matmult(std::vector> A, std::vector> B); - Ref additionnm(const Ref &A, const Ref &B); Ref subtractionnm(const Ref &A, const Ref &B); Ref matmultnm(const Ref &A, const Ref &B); - std::vector> hadamard_product(std::vector> A, std::vector> B); - std::vector> kronecker_product(std::vector> A, std::vector> B); - std::vector> elementWiseDivision(std::vector> A, std::vector> B); - Ref hadamard_productnm(const Ref &A, const Ref &B); Ref kronecker_productnm(const Ref &A, const Ref &B); Ref element_wise_divisionnvnm(const Ref &A, const Ref &B); - std::vector> transpose(std::vector> A); - std::vector> scalarMultiply(real_t scalar, std::vector> A); - std::vector> scalarAdd(real_t scalar, std::vector> A); - Ref transposenm(const Ref &A); Ref scalar_multiplynm(real_t scalar, const Ref &A); Ref scalar_addnm(real_t scalar, const Ref &A); - std::vector> log(std::vector> A); - std::vector> log10(std::vector> A); - std::vector> exp(std::vector> A); - std::vector> erf(std::vector> A); - std::vector> exponentiate(std::vector> A, real_t p); - std::vector> sqrt(std::vector> A); - std::vector> cbrt(std::vector> A); - Ref lognm(const Ref &A); Ref log10nm(const Ref &A); Ref expnm(const Ref &A); @@ -73,68 +51,41 @@ public: Ref sqrtnm(const Ref &A); Ref cbrtnm(const Ref &A); - std::vector> matrixPower(std::vector> A, int n); - - std::vector> abs(std::vector> A); + //std::vector> matrixPower(std::vector> A, int n); Ref absnm(const Ref &A); - real_t det(std::vector> A, int d); real_t detm(const Ref &A, int d); - real_t trace(std::vector> A); - - std::vector> cofactor(std::vector> A, int n, int i, int j); - std::vector> adjoint(std::vector> A); - std::vector> inverse(std::vector> A); - std::vector> pinverse(std::vector> A); + //real_t trace(std::vector> A); Ref cofactornm(const Ref &A, int n, int i, int j); Ref adjointnm(const Ref &A); Ref inversenm(const Ref &A); Ref pinversenm(const Ref &A); - std::vector> zeromat(int n, int m); - std::vector> onemat(int n, int m); - std::vector> full(int n, int m, int k); - Ref zeromatnm(int n, int m); Ref onematnm(int n, int m); Ref fullnm(int n, int m, int k); - std::vector> sin(std::vector> A); - std::vector> cos(std::vector> A); - Ref sinnm(const Ref &A); Ref cosnm(const Ref &A); - std::vector> rotate(std::vector> A, real_t theta, int axis = -1); + //std::vector> rotate(std::vector> A, real_t theta, int axis = -1); - std::vector> max(std::vector> A, std::vector> B); Ref maxnm(const Ref &A, const Ref &B); - real_t max(std::vector> A); - real_t min(std::vector> A); + //real_t max(std::vector> A); + //real_t min(std::vector> A); - std::vector> round(std::vector> A); + //std::vector> round(std::vector> A); - real_t norm_2(std::vector> A); + //real_t norm_2(std::vector> A); - std::vector> identity(real_t d); Ref identitym(int d); - std::vector> cov(std::vector> A); Ref covnm(const Ref &A); - std::tuple>, std::vector>> eig(std::vector> A); - - struct EigenResultOld { - std::vector> eigen_vectors; - std::vector> eigen_values; - }; - - EigenResultOld eigen_old(std::vector> A); - struct EigenResult { Ref eigen_vectors; Ref eigen_values; @@ -142,14 +93,6 @@ public: EigenResult eigen(Ref A); - struct SVDResultOld { - std::vector> U; - std::vector> S; - std::vector> Vt; - }; - - SVDResultOld SVD(std::vector> A); - struct SVDResult { Ref U; Ref S; @@ -158,34 +101,34 @@ public: SVDResult svd(const Ref &A); - std::vector vectorProjection(std::vector a, std::vector b); + //std::vector vectorProjection(std::vector a, std::vector b); - std::vector> gramSchmidtProcess(std::vector> A); - - std::tuple>, std::vector>> QRD(std::vector> A); + //std::vector> gramSchmidtProcess(std::vector> A); + /* struct QRDResult { std::vector> Q; std::vector> R; }; + */ - QRDResult qrd(std::vector> A); - - std::tuple>, std::vector>> chol(std::vector> A); + //QRDResult qrd(std::vector> A); + /* struct CholeskyResult { std::vector> L; std::vector> Lt; }; CholeskyResult cholesky(std::vector> A); + */ - real_t sum_elements(std::vector> A); + //real_t sum_elements(std::vector> A); - std::vector flatten(std::vector> A); Ref flattenmnv(const Vector> &A); Ref flattenvvnv(const Ref &A); + /* std::vector solve(std::vector> A, std::vector b); bool positiveDefiniteChecker(std::vector> A); @@ -193,38 +136,29 @@ public: bool negativeDefiniteChecker(std::vector> A); bool zeroEigenvalue(std::vector> A); - - void printMatrix(std::vector> A); + */ // VECTOR FUNCTIONS - std::vector> outerProduct(std::vector a, std::vector b); // This multiplies a, bT Ref outer_product(const Ref &a, const Ref &b); // This multiplies a, bT - std::vector hadamard_product(std::vector a, std::vector b); Ref hadamard_productnv(const Ref &a, const Ref &b); void hadamard_productv(const Ref &a, const Ref &b, Ref out); - std::vector elementWiseDivision(std::vector a, std::vector b); Ref element_wise_divisionnv(const Ref &a, const Ref &b); - std::vector scalarMultiply(real_t scalar, std::vector a); Ref scalar_multiplynv(real_t scalar, const Ref &a); void scalar_multiplyv(real_t scalar, const Ref &a, Ref out); - std::vector scalarAdd(real_t scalar, std::vector a); Ref scalar_addnv(real_t scalar, const Ref &a); void scalar_addv(real_t scalar, const Ref &a, Ref out); - std::vector addition(std::vector a, std::vector b); Ref additionnv(const Ref &a, const Ref &b); void additionv(const Ref &a, const Ref &b, Ref out); - std::vector subtraction(std::vector a, std::vector b); Ref subtractionnv(const Ref &a, const Ref &b); void subtractionv(const Ref &a, const Ref &b, Ref out); - std::vector subtractMatrixRows(std::vector a, std::vector> B); Ref subtract_matrix_rowsnv(const Ref &a, const Ref &B); Ref lognv(const Ref &a); @@ -235,16 +169,9 @@ public: Ref sqrtnv(const Ref &a); Ref cbrtnv(const Ref &a); - real_t dot(std::vector a, std::vector b); real_t dotnv(const Ref &a, const Ref &b); - std::vector cross(std::vector a, std::vector b); - - std::vector abs(std::vector a); - - std::vector zerovec(int n); - std::vector onevec(int n); - std::vector full(int n, int k); + //std::vector cross(std::vector a, std::vector b); Ref absv(const Ref &a); @@ -252,91 +179,62 @@ public: Ref onevecnv(int n); Ref fullnv(int n, int k); - std::vector> diag(std::vector a); Ref diagnm(const Ref &a); - std::vector sin(std::vector a); - std::vector cos(std::vector a); - Ref sinnv(const Ref &a); Ref cosnv(const Ref &a); - std::vector max(std::vector a, std::vector b); - Ref maxnvv(const Ref &a, const Ref &b); - real_t max(std::vector a); - real_t min(std::vector a); - real_t maxvr(const Ref &a); real_t minvr(const Ref &a); - std::vector round(std::vector a); + //std::vector round(std::vector a); - real_t euclideanDistance(std::vector a, std::vector b); real_t euclidean_distance(const Ref &a, const Ref &b); real_t euclidean_distance_squared(const Ref &a, const Ref &b); + /* real_t norm_2(std::vector a); + */ - real_t norm_sq(std::vector a); real_t norm_sqv(const Ref &a); - real_t sum_elements(std::vector a); + real_t sum_elementsv(const Ref &a); - real_t cosineSimilarity(std::vector a, std::vector b); - - void printVector(std::vector a); + //real_t cosineSimilarity(std::vector a, std::vector b); // MATRIX-VECTOR FUNCTIONS - std::vector> mat_vec_add(std::vector> A, std::vector b); - std::vector mat_vec_mult(std::vector> A, std::vector b); - Ref mat_vec_addnm(const Ref &A, const Ref &b); Ref mat_vec_multnv(const Ref &A, const Ref &b); // TENSOR FUNCTIONS - std::vector>> addition(std::vector>> A, std::vector>> B); + Vector> additionnvt(const Vector> &A, const Vector> &B); - Vector> addition_vt(const Vector> &A, const Vector> &B); + Vector> element_wise_divisionnvnvt(const Vector> &A, const Vector> &B); - std::vector>> elementWiseDivision(std::vector>> A, std::vector>> B); - Vector> element_wise_divisionnv_vt(const Vector> &A, const Vector> &B); + Vector> sqrtnvt(const Vector> &A); - std::vector>> sqrt(std::vector>> A); - Vector> sqrt_vt(const Vector> &A); + Vector> exponentiatenvt(const Vector> &A, real_t p); - std::vector>> exponentiate(std::vector>> A, real_t p); - Vector> exponentiate_vt(const Vector> &A, real_t p); + //std::vector> tensor_vec_mult(std::vector>> A, std::vector b); - std::vector> tensor_vec_mult(std::vector>> A, std::vector b); + //std::vector flatten(std::vector>> A); - std::vector flatten(std::vector>> A); + Vector> scalar_multiplynvt(real_t scalar, Vector> A); + Vector> scalar_addnvt(real_t scalar, Vector> A); - void printTensor(std::vector>> A); + Vector> resizenvt(const Vector> &A, const Vector> &B); - std::vector>> scalarMultiply(real_t scalar, std::vector>> A); - std::vector>> scalarAdd(real_t scalar, std::vector>> A); + //std::vector>> hadamard_product(std::vector>> A, std::vector>> B); - Vector> scalar_multiply_vm(real_t scalar, Vector> A); - Vector> scalar_add_vm(real_t scalar, Vector> A); + Vector> maxnvt(const Vector> &A, const Vector> &B); + Vector> absnvt(const Vector> &A); - std::vector>> resize(std::vector>> A, std::vector>> B); + //real_t norm_2(std::vector>> A); - Vector> resize_vt(const Vector> &A, const Vector> &B); - - std::vector>> hadamard_product(std::vector>> A, std::vector>> B); - - std::vector>> max(std::vector>> A, std::vector>> B); - Vector> max_vt(const Vector> &A, const Vector> &B); - - std::vector>> abs(std::vector>> A); - Vector> abs_vt(const Vector> &A); - - real_t norm_2(std::vector>> A); - - std::vector>> vector_wise_tensor_product(std::vector>> A, std::vector> B); + //std::vector>> vector_wise_tensor_product(std::vector>> A, std::vector> B); protected: static void _bind_methods(); diff --git a/mlpp/lin_alg/lin_alg_old.cpp b/mlpp/lin_alg/lin_alg_old.cpp index e945992..ca212c2 100644 --- a/mlpp/lin_alg/lin_alg_old.cpp +++ b/mlpp/lin_alg/lin_alg_old.cpp @@ -8,7 +8,7 @@ #include "core/math/math_funcs.h" -#include "../stat/stat.h" +#include "../stat/stat_old.h" #include #include @@ -509,7 +509,7 @@ std::vector> MLPPLinAlgOld::identity(real_t d) { } std::vector> MLPPLinAlgOld::cov(std::vector> A) { - MLPPStat stat; + MLPPStatOld stat; std::vector> covMat; covMat.resize(A.size()); for (uint32_t i = 0; i < covMat.size(); i++) { diff --git a/mlpp/lin_reg/lin_reg_old.cpp b/mlpp/lin_reg/lin_reg_old.cpp index 345bcff..59ac17b 100644 --- a/mlpp/lin_reg/lin_reg_old.cpp +++ b/mlpp/lin_reg/lin_reg_old.cpp @@ -6,10 +6,10 @@ #include "lin_reg_old.h" -#include "../cost/cost.h" +#include "../cost/cost_old.h" #include "../lin_alg/lin_alg_old.h" -#include "../regularization/reg.h" -#include "../stat/stat.h" +#include "../regularization/reg_old.h" +#include "../stat/stat_old.h" #include "../utilities/utilities.h" #include @@ -41,7 +41,7 @@ real_t MLPPLinRegOld::modelTest(std::vector x) { void MLPPLinRegOld::NewtonRaphson(real_t learning_rate, int max_epoch, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -73,7 +73,7 @@ void MLPPLinRegOld::NewtonRaphson(real_t learning_rate, int max_epoch, bool UI) void MLPPLinRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -104,7 +104,7 @@ void MLPPLinRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI void MLPPLinRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -143,7 +143,7 @@ void MLPPLinRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { void MLPPLinRegOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -183,7 +183,7 @@ void MLPPLinRegOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_siz void MLPPLinRegOld::Momentum(real_t learning_rate, int max_epoch, int mini_batch_size, real_t gamma, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -230,7 +230,7 @@ void MLPPLinRegOld::Momentum(real_t learning_rate, int max_epoch, int mini_batch void MLPPLinRegOld::NAG(real_t learning_rate, int max_epoch, int mini_batch_size, real_t gamma, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -279,7 +279,7 @@ void MLPPLinRegOld::NAG(real_t learning_rate, int max_epoch, int mini_batch_size void MLPPLinRegOld::Adagrad(real_t learning_rate, int max_epoch, int mini_batch_size, real_t e, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -327,7 +327,7 @@ void MLPPLinRegOld::Adagrad(real_t learning_rate, int max_epoch, int mini_batch_ void MLPPLinRegOld::Adadelta(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t e, bool UI) { // Adagrad upgrade. Momentum is applied. MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -374,7 +374,7 @@ void MLPPLinRegOld::Adadelta(real_t learning_rate, int max_epoch, int mini_batch void MLPPLinRegOld::Adam(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -427,7 +427,7 @@ void MLPPLinRegOld::Adam(real_t learning_rate, int max_epoch, int mini_batch_siz void MLPPLinRegOld::Adamax(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -478,7 +478,7 @@ void MLPPLinRegOld::Adamax(real_t learning_rate, int max_epoch, int mini_batch_s void MLPPLinRegOld::Nadam(real_t learning_rate, int max_epoch, int mini_batch_size, real_t b1, real_t b2, real_t e, bool UI) { MLPPLinAlgOld alg; - MLPPReg regularization; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -532,7 +532,7 @@ void MLPPLinRegOld::Nadam(real_t learning_rate, int max_epoch, int mini_batch_si void MLPPLinRegOld::normalEquation() { MLPPLinAlgOld alg; - MLPPStat stat; + MLPPStatOld stat; std::vector x_means; std::vector> inputSetT = alg.transpose(inputSet); @@ -577,8 +577,8 @@ void MLPPLinRegOld::save(std::string fileName) { } real_t MLPPLinRegOld::Cost(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; return cost.MSE(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg); } diff --git a/mlpp/log_reg/log_reg_old.cpp b/mlpp/log_reg/log_reg_old.cpp index fdbdd99..4116bec 100644 --- a/mlpp/log_reg/log_reg_old.cpp +++ b/mlpp/log_reg/log_reg_old.cpp @@ -7,9 +7,9 @@ #include "log_reg_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -38,8 +38,8 @@ real_t MLPPLogRegOld::modelTest(std::vector x) { } void MLPPLogRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -70,8 +70,8 @@ void MLPPLogRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI } void MLPPLogRegOld::MLE(real_t learning_rate, int max_epoch, bool UI) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -101,8 +101,8 @@ void MLPPLogRegOld::MLE(real_t learning_rate, int max_epoch, bool UI) { } void MLPPLogRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -140,8 +140,8 @@ void MLPPLogRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { } void MLPPLogRegOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -190,19 +190,19 @@ void MLPPLogRegOld::save(std::string fileName) { } real_t MLPPLogRegOld::Cost(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; return cost.LogLoss(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg); } std::vector MLPPLogRegOld::Evaluate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.sigmoid(alg.scalarAdd(bias, alg.mat_vec_mult(X, weights))); } real_t MLPPLogRegOld::Evaluate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.sigmoid(alg.dot(weights, x) + bias); } diff --git a/mlpp/mann/mann_old.cpp b/mlpp/mann/mann_old.cpp index fd79b15..2cd087b 100644 --- a/mlpp/mann/mann_old.cpp +++ b/mlpp/mann/mann_old.cpp @@ -7,9 +7,9 @@ #include "mann_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -53,10 +53,10 @@ std::vector MLPPMANNOld::modelTest(std::vector x) { } void MLPPMANNOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { - class MLPPCost cost; + class MLPPCostOld cost; MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -158,8 +158,8 @@ void MLPPMANNOld::addOutputLayer(std::string activation, std::string loss, std:: } real_t MLPPMANNOld::Cost(std::vector> y_hat, std::vector> y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; real_t totalRegTerm = 0; auto cost_function = outputLayer->cost_map[outputLayer->cost]; diff --git a/mlpp/mlp/mlp_old.cpp b/mlpp/mlp/mlp_old.cpp index 1da3c0e..c5b8a30 100644 --- a/mlpp/mlp/mlp_old.cpp +++ b/mlpp/mlp/mlp_old.cpp @@ -9,9 +9,9 @@ #include "core/log/logger.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -45,8 +45,8 @@ real_t MLPPMLPOld::modelTest(std::vector x) { void MLPPMLPOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -104,8 +104,8 @@ void MLPPMLPOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { void MLPPMLPOld::SGD(real_t learning_rate, int max_epoch, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -160,8 +160,8 @@ void MLPPMLPOld::SGD(real_t learning_rate, int max_epoch, bool UI) { void MLPPMLPOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -241,13 +241,13 @@ void MLPPMLPOld::save(std::string fileName) { } real_t MLPPMLPOld::Cost(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; return cost.LogLoss(y_hat, y) + regularization.regTerm(weights2, lambda, alpha, reg) + regularization.regTerm(weights1, lambda, alpha, reg); } std::vector MLPPMLPOld::Evaluate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector> z2 = alg.mat_vec_add(alg.matmult(X, weights1), bias1); std::vector> a2 = avn.sigmoid(z2); @@ -255,7 +255,7 @@ std::vector MLPPMLPOld::Evaluate(std::vector> X) { } std::tuple>, std::vector>> MLPPMLPOld::propagate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector> z2 = alg.mat_vec_add(alg.matmult(X, weights1), bias1); std::vector> a2 = avn.sigmoid(z2); @@ -263,7 +263,7 @@ std::tuple>, std::vector>> M } real_t MLPPMLPOld::Evaluate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector z2 = alg.addition(alg.mat_vec_mult(alg.transpose(weights1), x), bias1); std::vector a2 = avn.sigmoid(z2); @@ -271,7 +271,7 @@ real_t MLPPMLPOld::Evaluate(std::vector x) { } std::tuple, std::vector> MLPPMLPOld::propagate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector z2 = alg.addition(alg.mat_vec_mult(alg.transpose(weights1), x), bias1); std::vector a2 = avn.sigmoid(z2); @@ -279,7 +279,7 @@ std::tuple, std::vector> MLPPMLPOld::propagate(std:: } void MLPPMLPOld::forwardPass() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; z2 = alg.mat_vec_add(alg.matmult(inputSet, weights1), bias1); a2 = avn.sigmoid(z2); diff --git a/mlpp/multi_output_layer/multi_output_layer_old.cpp b/mlpp/multi_output_layer/multi_output_layer_old.cpp index ec74ffd..b87894e 100644 --- a/mlpp/multi_output_layer/multi_output_layer_old.cpp +++ b/mlpp/multi_output_layer/multi_output_layer_old.cpp @@ -5,7 +5,7 @@ // #include "multi_output_layer_old.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include "../utilities/utilities.h" #include @@ -106,33 +106,33 @@ MLPPOldMultiOutputLayer::MLPPOldMultiOutputLayer(int p_n_output, int p_n_hidden, activation_map["Arcoth"] = &MLPPActivationOld::arcoth; activationTest_map["Arcoth"] = &MLPPActivationOld::arcoth; - costDeriv_map["MSE"] = &MLPPCost::MSEDeriv; - cost_map["MSE"] = &MLPPCost::MSE; - costDeriv_map["RMSE"] = &MLPPCost::RMSEDeriv; - cost_map["RMSE"] = &MLPPCost::RMSE; - costDeriv_map["MAE"] = &MLPPCost::MAEDeriv; - cost_map["MAE"] = &MLPPCost::MAE; - costDeriv_map["MBE"] = &MLPPCost::MBEDeriv; - cost_map["MBE"] = &MLPPCost::MBE; - costDeriv_map["LogLoss"] = &MLPPCost::LogLossDeriv; - cost_map["LogLoss"] = &MLPPCost::LogLoss; - costDeriv_map["CrossEntropy"] = &MLPPCost::CrossEntropyDeriv; - cost_map["CrossEntropy"] = &MLPPCost::CrossEntropy; - costDeriv_map["HingeLoss"] = &MLPPCost::HingeLossDeriv; - cost_map["HingeLoss"] = &MLPPCost::HingeLoss; - costDeriv_map["WassersteinLoss"] = &MLPPCost::HingeLossDeriv; - cost_map["WassersteinLoss"] = &MLPPCost::HingeLoss; + costDeriv_map["MSE"] = &MLPPCostOld::MSEDeriv; + cost_map["MSE"] = &MLPPCostOld::MSE; + costDeriv_map["RMSE"] = &MLPPCostOld::RMSEDeriv; + cost_map["RMSE"] = &MLPPCostOld::RMSE; + costDeriv_map["MAE"] = &MLPPCostOld::MAEDeriv; + cost_map["MAE"] = &MLPPCostOld::MAE; + costDeriv_map["MBE"] = &MLPPCostOld::MBEDeriv; + cost_map["MBE"] = &MLPPCostOld::MBE; + costDeriv_map["LogLoss"] = &MLPPCostOld::LogLossDeriv; + cost_map["LogLoss"] = &MLPPCostOld::LogLoss; + costDeriv_map["CrossEntropy"] = &MLPPCostOld::CrossEntropyDeriv; + cost_map["CrossEntropy"] = &MLPPCostOld::CrossEntropy; + costDeriv_map["HingeLoss"] = &MLPPCostOld::HingeLossDeriv; + cost_map["HingeLoss"] = &MLPPCostOld::HingeLoss; + costDeriv_map["WassersteinLoss"] = &MLPPCostOld::HingeLossDeriv; + cost_map["WassersteinLoss"] = &MLPPCostOld::HingeLoss; } void MLPPOldMultiOutputLayer::forwardPass() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; z = alg.mat_vec_add(alg.matmult(input, weights), bias); a = (avn.*activation_map[activation])(z, false); } void MLPPOldMultiOutputLayer::Test(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; z_test = alg.addition(alg.mat_vec_mult(alg.transpose(weights), x), bias); a_test = (avn.*activationTest_map[activation])(z_test, false); diff --git a/mlpp/multi_output_layer/multi_output_layer_old.h b/mlpp/multi_output_layer/multi_output_layer_old.h index a3da569..974ce05 100644 --- a/mlpp/multi_output_layer/multi_output_layer_old.h +++ b/mlpp/multi_output_layer/multi_output_layer_old.h @@ -14,7 +14,7 @@ #include "core/object/reference.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" +#include "../cost/cost_old.h" #include "../regularization/reg.h" #include "../utilities/utilities.h" @@ -44,8 +44,8 @@ public: std::map> (MLPPActivationOld::*)(std::vector>, bool)> activation_map; std::map (MLPPActivationOld::*)(std::vector, bool)> activationTest_map; - std::map>, std::vector>)> cost_map; - std::map> (MLPPCost::*)(std::vector>, std::vector>)> costDeriv_map; + std::map>, std::vector>)> cost_map; + std::map> (MLPPCostOld::*)(std::vector>, std::vector>)> costDeriv_map; std::vector z_test; std::vector a_test; diff --git a/mlpp/multinomial_nb/multinomial_nb_old.cpp b/mlpp/multinomial_nb/multinomial_nb_old.cpp index 343d481..2894152 100644 --- a/mlpp/multinomial_nb/multinomial_nb_old.cpp +++ b/mlpp/multinomial_nb/multinomial_nb_old.cpp @@ -6,7 +6,7 @@ #include "multinomial_nb_old.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include "../utilities/utilities.h" #include @@ -81,7 +81,7 @@ void MLPPMultinomialNBOld::computeTheta() { } void MLPPMultinomialNBOld::Evaluate() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; for (uint32_t i = 0; i < outputSet.size(); i++) { // Pr(B | A) * Pr(A) real_t score[class_num]; diff --git a/mlpp/numerical_analysis/numerical_analysis.cpp b/mlpp/numerical_analysis/numerical_analysis.cpp index f012d9a..582a83c 100644 --- a/mlpp/numerical_analysis/numerical_analysis.cpp +++ b/mlpp/numerical_analysis/numerical_analysis.cpp @@ -12,6 +12,7 @@ #include #include +/* real_t MLPPNumericalAnalysis::numDiff(real_t (*function)(real_t), real_t x) { real_t eps = 1e-10; return (function(x + eps) - function(x)) / eps; // This is just the formal def. of the derivative. @@ -164,16 +165,15 @@ real_t MLPPNumericalAnalysis::eulerianMethod(real_t (*derivative)(std::vector), std::vector c, std::vector x) { - /* - Not completely sure as the literature seldom discusses the third order taylor approximation, - in particular for multivariate cases, but ostensibly, the matrix/tensor/vector multiplies - should look something like this: + //Not completely sure as the literature seldom discusses the third order taylor approximation, + //in particular for multivariate cases, but ostensibly, the matrix/tensor/vector multiplies + //should look something like this: - (N x N x N) (N x 1) [tensor vector mult] => (N x N x 1) => (N x N) - Perform remaining multiplies as done for the 2nd order approximation. - Result is a scalar. - */ + //(N x N x N) (N x 1) [tensor vector mult] => (N x N x 1) => (N x N) + //Perform remaining multiplies as done for the 2nd order approximation. + //Result is a scalar. + MLPPLinAlg alg; std::vector> resultMat = alg.tensor_vec_mult(thirdOrderTensor(function, c), alg.subtraction(x, c)); real_t resultScalar = alg.matmult({ (alg.subtraction(x, c)) }, alg.matmult(resultMat, alg.transpose({ alg.subtraction(x, c) })))[0][0]; @@ -270,10 +269,10 @@ real_t MLPPNumericalAnalysis::laplacian(real_t (*function)(std::vector), std::string MLPPNumericalAnalysis::secondPartialDerivativeTest(real_t (*function)(std::vector), std::vector x) { MLPPLinAlg alg; std::vector> hessianMatrix = hessian(function, x); - /* - The reason we do this is because the 2nd partial derivative test is less conclusive for functions of variables greater than - 2, and the calculations specific to the bivariate case are less computationally intensive. - */ + + // The reason we do this is because the 2nd partial derivative test is less conclusive for functions of variables greater than + // 2, and the calculations specific to the bivariate case are less computationally intensive. + if (x.size() == 2) { real_t det = alg.det(hessianMatrix, hessianMatrix.size()); real_t secondDerivative = numDiff_2(function, x, 0, 0); @@ -298,6 +297,7 @@ std::string MLPPNumericalAnalysis::secondPartialDerivativeTest(real_t (*function } } } +*/ void MLPPNumericalAnalysis::_bind_methods() { } diff --git a/mlpp/numerical_analysis/numerical_analysis.h b/mlpp/numerical_analysis/numerical_analysis.h index 6d72a7f..ffe6a06 100644 --- a/mlpp/numerical_analysis/numerical_analysis.h +++ b/mlpp/numerical_analysis/numerical_analysis.h @@ -22,6 +22,7 @@ public: as an analytical method for calculating derivatives will most likely be used in the future. */ + /* real_t numDiff(real_t (*function)(real_t), real_t x); real_t numDiff_2(real_t (*function)(real_t), real_t x); real_t numDiff_3(real_t (*function)(real_t), real_t x); @@ -56,6 +57,7 @@ public: real_t laplacian(real_t (*function)(std::vector), std::vector x); // laplacian std::string secondPartialDerivativeTest(real_t (*function)(std::vector), std::vector x); + */ protected: static void _bind_methods(); diff --git a/mlpp/numerical_analysis/numerical_analysis_old.cpp b/mlpp/numerical_analysis/numerical_analysis_old.cpp index 9966c04..d03124d 100644 --- a/mlpp/numerical_analysis/numerical_analysis_old.cpp +++ b/mlpp/numerical_analysis/numerical_analysis_old.cpp @@ -5,7 +5,7 @@ // #include "numerical_analysis_old.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include #include @@ -230,12 +230,12 @@ real_t MLPPNumericalAnalysisOld::constantApproximation(real_t (*function)(std::v } real_t MLPPNumericalAnalysisOld::linearApproximation(real_t (*function)(std::vector), std::vector c, std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return constantApproximation(function, c) + alg.matmult(alg.transpose({ jacobian(function, c) }), { alg.subtraction(x, c) })[0][0]; } real_t MLPPNumericalAnalysisOld::quadraticApproximation(real_t (*function)(std::vector), std::vector c, std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return linearApproximation(function, c, x) + 0.5 * alg.matmult({ (alg.subtraction(x, c)) }, alg.matmult(hessian(function, c), alg.transpose({ alg.subtraction(x, c) })))[0][0]; } @@ -249,7 +249,7 @@ real_t MLPPNumericalAnalysisOld::cubicApproximation(real_t (*function)(std::vect Perform remaining multiplies as done for the 2nd order approximation. Result is a scalar. */ - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> resultMat = alg.tensor_vec_mult(thirdOrderTensor(function, c), alg.subtraction(x, c)); real_t resultScalar = alg.matmult({ (alg.subtraction(x, c)) }, alg.matmult(resultMat, alg.transpose({ alg.subtraction(x, c) })))[0][0]; @@ -268,7 +268,7 @@ real_t MLPPNumericalAnalysisOld::laplacian(real_t (*function)(std::vector), std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> hessianMatrix = hessian(function, x); /* The reason we do this is because the 2nd partial derivative test is less conclusive for functions of variables greater than diff --git a/mlpp/outlier_finder/outlier_finder_old.cpp b/mlpp/outlier_finder/outlier_finder_old.cpp index 7b95ff6..6b83a54 100644 --- a/mlpp/outlier_finder/outlier_finder_old.cpp +++ b/mlpp/outlier_finder/outlier_finder_old.cpp @@ -6,7 +6,7 @@ #include "outlier_finder_old.h" -#include "../stat/stat.h" +#include "../stat/stat_old.h" #include @@ -15,7 +15,7 @@ MLPPOutlierFinderOld::MLPPOutlierFinderOld(int threshold) : } std::vector> MLPPOutlierFinderOld::modelSetTest(std::vector> inputSet) { - MLPPStat stat; + MLPPStatOld stat; std::vector> outliers; outliers.resize(inputSet.size()); for (uint32_t i = 0; i < inputSet.size(); i++) { @@ -30,7 +30,7 @@ std::vector> MLPPOutlierFinderOld::modelSetTest(std::vector< } std::vector MLPPOutlierFinderOld::modelTest(std::vector inputSet) { - MLPPStat stat; + MLPPStatOld stat; std::vector outliers; for (uint32_t i = 0; i < inputSet.size(); i++) { real_t z = (inputSet[i] - stat.mean(inputSet)) / stat.standardDeviation(inputSet); diff --git a/mlpp/output_layer/output_layer_old.cpp b/mlpp/output_layer/output_layer_old.cpp index b64af60..857c038 100644 --- a/mlpp/output_layer/output_layer_old.cpp +++ b/mlpp/output_layer/output_layer_old.cpp @@ -5,7 +5,7 @@ // #include "output_layer_old.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include "../utilities/utilities.h" #include @@ -102,33 +102,33 @@ MLPPOldOutputLayer::MLPPOldOutputLayer(int p_n_hidden, std::string p_activation, activation_map["Arcoth"] = &MLPPActivationOld::arcoth; activationTest_map["Arcoth"] = &MLPPActivationOld::arcoth; - costDeriv_map["MSE"] = &MLPPCost::MSEDeriv; - cost_map["MSE"] = &MLPPCost::MSE; - costDeriv_map["RMSE"] = &MLPPCost::RMSEDeriv; - cost_map["RMSE"] = &MLPPCost::RMSE; - costDeriv_map["MAE"] = &MLPPCost::MAEDeriv; - cost_map["MAE"] = &MLPPCost::MAE; - costDeriv_map["MBE"] = &MLPPCost::MBEDeriv; - cost_map["MBE"] = &MLPPCost::MBE; - costDeriv_map["LogLoss"] = &MLPPCost::LogLossDeriv; - cost_map["LogLoss"] = &MLPPCost::LogLoss; - costDeriv_map["CrossEntropy"] = &MLPPCost::CrossEntropyDeriv; - cost_map["CrossEntropy"] = &MLPPCost::CrossEntropy; - costDeriv_map["HingeLoss"] = &MLPPCost::HingeLossDeriv; - cost_map["HingeLoss"] = &MLPPCost::HingeLoss; - costDeriv_map["WassersteinLoss"] = &MLPPCost::HingeLossDeriv; - cost_map["WassersteinLoss"] = &MLPPCost::HingeLoss; + costDeriv_map["MSE"] = &MLPPCostOld::MSEDeriv; + cost_map["MSE"] = &MLPPCostOld::MSE; + costDeriv_map["RMSE"] = &MLPPCostOld::RMSEDeriv; + cost_map["RMSE"] = &MLPPCostOld::RMSE; + costDeriv_map["MAE"] = &MLPPCostOld::MAEDeriv; + cost_map["MAE"] = &MLPPCostOld::MAE; + costDeriv_map["MBE"] = &MLPPCostOld::MBEDeriv; + cost_map["MBE"] = &MLPPCostOld::MBE; + costDeriv_map["LogLoss"] = &MLPPCostOld::LogLossDeriv; + cost_map["LogLoss"] = &MLPPCostOld::LogLoss; + costDeriv_map["CrossEntropy"] = &MLPPCostOld::CrossEntropyDeriv; + cost_map["CrossEntropy"] = &MLPPCostOld::CrossEntropy; + costDeriv_map["HingeLoss"] = &MLPPCostOld::HingeLossDeriv; + cost_map["HingeLoss"] = &MLPPCostOld::HingeLoss; + costDeriv_map["WassersteinLoss"] = &MLPPCostOld::HingeLossDeriv; + cost_map["WassersteinLoss"] = &MLPPCostOld::HingeLoss; } void MLPPOldOutputLayer::forwardPass() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; z = alg.scalarAdd(bias, alg.mat_vec_mult(input, weights)); a = (avn.*activation_map[activation])(z, false); } void MLPPOldOutputLayer::Test(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; z_test = alg.dot(weights, x) + bias; a_test = (avn.*activationTest_map[activation])(z_test, false); diff --git a/mlpp/output_layer/output_layer_old.h b/mlpp/output_layer/output_layer_old.h index e15a74c..f471ecd 100644 --- a/mlpp/output_layer/output_layer_old.h +++ b/mlpp/output_layer/output_layer_old.h @@ -14,7 +14,7 @@ #include "core/object/reference.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" +#include "../cost/cost_old.h" #include "../regularization/reg.h" #include "../utilities/utilities.h" @@ -43,8 +43,8 @@ public: std::map (MLPPActivationOld::*)(std::vector, bool)> activation_map; std::map activationTest_map; - std::map, std::vector)> cost_map; - std::map (MLPPCost::*)(std::vector, std::vector)> costDeriv_map; + std::map, std::vector)> cost_map; + std::map (MLPPCostOld::*)(std::vector, std::vector)> costDeriv_map; real_t z_test; real_t a_test; diff --git a/mlpp/pca/pca_old.cpp b/mlpp/pca/pca_old.cpp index fef43f3..2f916cf 100644 --- a/mlpp/pca/pca_old.cpp +++ b/mlpp/pca/pca_old.cpp @@ -6,7 +6,7 @@ #include "pca_old.h" #include "../data/data.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include #include @@ -18,10 +18,10 @@ MLPPPCAOld::MLPPPCAOld(std::vector> inputSet, int k) : } std::vector> MLPPPCAOld::principalComponents() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPData data; - MLPPLinAlg::SVDResultOld svr_res = alg.SVD(alg.cov(inputSet)); + MLPPLinAlgOld::SVDResultOld svr_res = alg.SVD(alg.cov(inputSet)); X_normalized = data.meanCentering(inputSet); U_reduce.resize(svr_res.U.size()); for (int i = 0; i < k; i++) { @@ -35,7 +35,7 @@ std::vector> MLPPPCAOld::principalComponents() { // Simply tells us the percentage of variance maintained. real_t MLPPPCAOld::score() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> X_approx = alg.matmult(U_reduce, Z); real_t num = 0; real_t den = 0; diff --git a/mlpp/probit_reg/probit_reg_old.cpp b/mlpp/probit_reg/probit_reg_old.cpp index eaaf5d1..b803b31 100644 --- a/mlpp/probit_reg/probit_reg_old.cpp +++ b/mlpp/probit_reg/probit_reg_old.cpp @@ -6,9 +6,9 @@ #include "probit_reg_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -31,8 +31,8 @@ real_t MLPPProbitRegOld::modelTest(std::vector x) { void MLPPProbitRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -64,8 +64,8 @@ void MLPPProbitRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool void MLPPProbitRegOld::MLE(real_t learning_rate, int max_epoch, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -97,8 +97,8 @@ void MLPPProbitRegOld::MLE(real_t learning_rate, int max_epoch, bool UI) { void MLPPProbitRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { // NOTE: ∂y_hat/∂z is sparse - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -138,8 +138,8 @@ void MLPPProbitRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { void MLPPProbitRegOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -208,30 +208,30 @@ void MLPPProbitRegOld::save(std::string fileName) { } real_t MLPPProbitRegOld::Cost(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; return cost.MSE(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg); } std::vector MLPPProbitRegOld::Evaluate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.gaussianCDF(alg.scalarAdd(bias, alg.mat_vec_mult(X, weights))); } std::vector MLPPProbitRegOld::propagate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarAdd(bias, alg.mat_vec_mult(X, weights)); } real_t MLPPProbitRegOld::Evaluate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.gaussianCDF(alg.dot(weights, x) + bias); } real_t MLPPProbitRegOld::propagate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.dot(weights, x) + bias; } diff --git a/mlpp/regularization/reg.cpp b/mlpp/regularization/reg.cpp index a529845..243603d 100644 --- a/mlpp/regularization/reg.cpp +++ b/mlpp/regularization/reg.cpp @@ -211,154 +211,3 @@ real_t MLPPReg::reg_deriv_termmr(const Ref &weights, real_t lambda, return 0; } } - -real_t MLPPReg::regTerm(std::vector weights, real_t lambda, real_t alpha, std::string p_reg) { - if (p_reg == "Ridge") { - real_t reg = 0; - for (uint32_t i = 0; i < weights.size(); i++) { - reg += weights[i] * weights[i]; - } - return reg * lambda / 2; - } else if (p_reg == "Lasso") { - real_t reg = 0; - for (uint32_t i = 0; i < weights.size(); i++) { - reg += abs(weights[i]); - } - return reg * lambda; - } else if (p_reg == "ElasticNet") { - real_t reg = 0; - for (uint32_t i = 0; i < weights.size(); i++) { - reg += alpha * abs(weights[i]); // Lasso Reg - reg += ((1 - alpha) / 2) * weights[i] * weights[i]; // Ridge Reg - } - return reg * lambda; - } - return 0; -} - -real_t MLPPReg::regTerm(std::vector> weights, real_t lambda, real_t alpha, std::string p_reg) { - if (p_reg == "Ridge") { - real_t reg = 0; - for (uint32_t i = 0; i < weights.size(); i++) { - for (uint32_t j = 0; j < weights[i].size(); j++) { - reg += weights[i][j] * weights[i][j]; - } - } - return reg * lambda / 2; - } else if (p_reg == "Lasso") { - real_t reg = 0; - for (uint32_t i = 0; i < weights.size(); i++) { - for (uint32_t j = 0; j < weights[i].size(); j++) { - reg += abs(weights[i][j]); - } - } - return reg * lambda; - } else if (p_reg == "ElasticNet") { - real_t reg = 0; - for (uint32_t i = 0; i < weights.size(); i++) { - for (uint32_t j = 0; j < weights[i].size(); j++) { - reg += alpha * abs(weights[i][j]); // Lasso Reg - reg += ((1 - alpha) / 2) * weights[i][j] * weights[i][j]; // Ridge Reg - } - } - return reg * lambda; - } - return 0; -} - -std::vector MLPPReg::regWeights(std::vector weights, real_t lambda, real_t alpha, std::string reg) { - MLPPLinAlg alg; - if (reg == "WeightClipping") { - return regDerivTerm(weights, lambda, alpha, reg); - } - return alg.subtraction(weights, regDerivTerm(weights, lambda, alpha, reg)); - // for(int i = 0; i < weights.size(); i++){ - // weights[i] -= regDerivTerm(weights, lambda, alpha, reg, i); - // } - // return weights; -} - -std::vector> MLPPReg::regWeights(std::vector> weights, real_t lambda, real_t alpha, std::string reg) { - MLPPLinAlg alg; - if (reg == "WeightClipping") { - return regDerivTerm(weights, lambda, alpha, reg); - } - return alg.subtraction(weights, regDerivTerm(weights, lambda, alpha, reg)); - // for(int i = 0; i < weights.size(); i++){ - // for(int j = 0; j < weights[i].size(); j++){ - // weights[i][j] -= regDerivTerm(weights, lambda, alpha, reg, i, j); - // } - // } - // return weights; -} - -std::vector MLPPReg::regDerivTerm(std::vector weights, real_t lambda, real_t alpha, std::string reg) { - std::vector regDeriv; - regDeriv.resize(weights.size()); - - for (uint32_t i = 0; i < regDeriv.size(); i++) { - regDeriv[i] = regDerivTerm(weights, lambda, alpha, reg, i); - } - return regDeriv; -} - -std::vector> MLPPReg::regDerivTerm(std::vector> weights, real_t lambda, real_t alpha, std::string reg) { - std::vector> regDeriv; - regDeriv.resize(weights.size()); - for (uint32_t i = 0; i < regDeriv.size(); i++) { - regDeriv[i].resize(weights[0].size()); - } - - for (uint32_t i = 0; i < regDeriv.size(); i++) { - for (uint32_t j = 0; j < regDeriv[i].size(); j++) { - regDeriv[i][j] = regDerivTerm(weights, lambda, alpha, reg, i, j); - } - } - return regDeriv; -} - -real_t MLPPReg::regDerivTerm(std::vector weights, real_t lambda, real_t alpha, std::string reg, int j) { - MLPPActivation act; - if (reg == "Ridge") { - return lambda * weights[j]; - } else if (reg == "Lasso") { - return lambda * act.sign_normr(weights[j]); - } else if (reg == "ElasticNet") { - return alpha * lambda * act.sign_normr(weights[j]) + (1 - alpha) * lambda * weights[j]; - } else if (reg == "WeightClipping") { // Preparation for Wasserstein GANs. - // We assume lambda is the lower clipping threshold, while alpha is the higher clipping threshold. - // alpha > lambda. - if (weights[j] > alpha) { - return alpha; - } else if (weights[j] < lambda) { - return lambda; - } else { - return weights[j]; - } - } else { - return 0; - } -} - -real_t MLPPReg::regDerivTerm(std::vector> weights, real_t lambda, real_t alpha, std::string reg, int i, int j) { - MLPPActivation act; - if (reg == "Ridge") { - return lambda * weights[i][j]; - } else if (reg == "Lasso") { - return lambda * act.sign_normr(weights[i][j]); - } else if (reg == "ElasticNet") { - return alpha * lambda * act.sign_normr(weights[i][j]) + (1 - alpha) * lambda * weights[i][j]; - } else if (reg == "WeightClipping") { // Preparation for Wasserstein GANs. - // We assume lambda is the lower clipping threshold, while alpha is the higher clipping threshold. - // alpha > lambda. - if (weights[i][j] > alpha) { - return alpha; - } else if (weights[i][j] < lambda) { - return lambda; - } else { - return weights[i][j]; - } - } else { - return 0; - } -} diff --git a/mlpp/regularization/reg.h b/mlpp/regularization/reg.h index e9689c1..9e75d64 100644 --- a/mlpp/regularization/reg.h +++ b/mlpp/regularization/reg.h @@ -49,22 +49,6 @@ protected: private: real_t reg_deriv_termvr(const Ref &weights, real_t lambda, real_t alpha, RegularizationType reg, int j); real_t reg_deriv_termmr(const Ref &weights, real_t lambda, real_t alpha, RegularizationType reg, int i, int j); - -public: - // ======== OLD ========= - - real_t regTerm(std::vector weights, real_t lambda, real_t alpha, std::string reg); - real_t regTerm(std::vector> weights, real_t lambda, real_t alpha, std::string reg); - - std::vector regWeights(std::vector weights, real_t lambda, real_t alpha, std::string reg); - std::vector> regWeights(std::vector> weights, real_t lambda, real_t alpha, std::string reg); - - std::vector regDerivTerm(std::vector weights, real_t lambda, real_t alpha, std::string reg); - std::vector> regDerivTerm(std::vector>, real_t lambda, real_t alpha, std::string reg); - -private: - real_t regDerivTerm(std::vector weights, real_t lambda, real_t alpha, std::string reg, int j); - real_t regDerivTerm(std::vector> weights, real_t lambda, real_t alpha, std::string reg, int i, int j); }; VARIANT_ENUM_CAST(MLPPReg::RegularizationType); diff --git a/mlpp/regularization/reg_old.cpp b/mlpp/regularization/reg_old.cpp index 6ed659e..49aea1d 100644 --- a/mlpp/regularization/reg_old.cpp +++ b/mlpp/regularization/reg_old.cpp @@ -9,13 +9,67 @@ #include "core/math/math_defs.h" #include "../activation/activation_old.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include #include +real_t MLPPRegOld::regTerm(std::vector weights, real_t lambda, real_t alpha, std::string p_reg) { + if (p_reg == "Ridge") { + real_t reg = 0; + for (uint32_t i = 0; i < weights.size(); i++) { + reg += weights[i] * weights[i]; + } + return reg * lambda / 2; + } else if (p_reg == "Lasso") { + real_t reg = 0; + for (uint32_t i = 0; i < weights.size(); i++) { + reg += abs(weights[i]); + } + return reg * lambda; + } else if (p_reg == "ElasticNet") { + real_t reg = 0; + for (uint32_t i = 0; i < weights.size(); i++) { + reg += alpha * abs(weights[i]); // Lasso Reg + reg += ((1 - alpha) / 2) * weights[i] * weights[i]; // Ridge Reg + } + return reg * lambda; + } + return 0; +} + +real_t MLPPRegOld::regTerm(std::vector> weights, real_t lambda, real_t alpha, std::string p_reg) { + if (p_reg == "Ridge") { + real_t reg = 0; + for (uint32_t i = 0; i < weights.size(); i++) { + for (uint32_t j = 0; j < weights[i].size(); j++) { + reg += weights[i][j] * weights[i][j]; + } + } + return reg * lambda / 2; + } else if (p_reg == "Lasso") { + real_t reg = 0; + for (uint32_t i = 0; i < weights.size(); i++) { + for (uint32_t j = 0; j < weights[i].size(); j++) { + reg += abs(weights[i][j]); + } + } + return reg * lambda; + } else if (p_reg == "ElasticNet") { + real_t reg = 0; + for (uint32_t i = 0; i < weights.size(); i++) { + for (uint32_t j = 0; j < weights[i].size(); j++) { + reg += alpha * abs(weights[i][j]); // Lasso Reg + reg += ((1 - alpha) / 2) * weights[i][j] * weights[i][j]; // Ridge Reg + } + } + return reg * lambda; + } + return 0; +} + std::vector MLPPRegOld::regWeights(std::vector weights, real_t lambda, real_t alpha, std::string reg) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (reg == "WeightClipping") { return regDerivTerm(weights, lambda, alpha, reg); } @@ -27,7 +81,7 @@ std::vector MLPPRegOld::regWeights(std::vector weights, real_t l } std::vector> MLPPRegOld::regWeights(std::vector> weights, real_t lambda, real_t alpha, std::string reg) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (reg == "WeightClipping") { return regDerivTerm(weights, lambda, alpha, reg); } diff --git a/mlpp/regularization/reg_old.h b/mlpp/regularization/reg_old.h index a3e2ba9..8f88cec 100644 --- a/mlpp/regularization/reg_old.h +++ b/mlpp/regularization/reg_old.h @@ -16,8 +16,6 @@ class MLPPRegOld { public: - // ======== OLD ========= - real_t regTerm(std::vector weights, real_t lambda, real_t alpha, std::string reg); real_t regTerm(std::vector> weights, real_t lambda, real_t alpha, std::string reg); diff --git a/mlpp/softmax_net/softmax_net_old.cpp b/mlpp/softmax_net/softmax_net_old.cpp index 4192eec..ce8dfa3 100644 --- a/mlpp/softmax_net/softmax_net_old.cpp +++ b/mlpp/softmax_net/softmax_net_old.cpp @@ -7,10 +7,10 @@ #include "softmax_net_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" +#include "../cost/cost_old.h" #include "../data/data.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -45,8 +45,8 @@ std::vector> MLPPSoftmaxNetOld::modelSetTest(std::vector> MLPPSoftmaxNetOld::getEmbeddings() { } real_t MLPPSoftmaxNetOld::Cost(std::vector> y_hat, std::vector> y) { - MLPPReg regularization; + MLPPRegOld regularization; MLPPData data; - class MLPPCost cost; + class MLPPCostOld cost; return cost.CrossEntropy(y_hat, y) + regularization.regTerm(weights1, lambda, alpha, reg) + regularization.regTerm(weights2, lambda, alpha, reg); } std::vector> MLPPSoftmaxNetOld::Evaluate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector> z2 = alg.mat_vec_add(alg.matmult(X, weights1), bias1); std::vector> a2 = avn.sigmoid(z2); @@ -277,7 +277,7 @@ std::vector> MLPPSoftmaxNetOld::Evaluate(std::vector>, std::vector>> MLPPSoftmaxNetOld::propagate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector> z2 = alg.mat_vec_add(alg.matmult(X, weights1), bias1); std::vector> a2 = avn.sigmoid(z2); @@ -285,7 +285,7 @@ std::tuple>, std::vector>> M } std::vector MLPPSoftmaxNetOld::Evaluate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector z2 = alg.addition(alg.mat_vec_mult(alg.transpose(weights1), x), bias1); std::vector a2 = avn.sigmoid(z2); @@ -293,7 +293,7 @@ std::vector MLPPSoftmaxNetOld::Evaluate(std::vector x) { } std::tuple, std::vector> MLPPSoftmaxNetOld::propagate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; std::vector z2 = alg.addition(alg.mat_vec_mult(alg.transpose(weights1), x), bias1); std::vector a2 = avn.sigmoid(z2); @@ -301,7 +301,7 @@ std::tuple, std::vector> MLPPSoftmaxNetOld::propagat } void MLPPSoftmaxNetOld::forwardPass() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; z2 = alg.mat_vec_add(alg.matmult(inputSet, weights1), bias1); a2 = avn.sigmoid(z2); diff --git a/mlpp/softmax_reg/softmax_reg_old.cpp b/mlpp/softmax_reg/softmax_reg_old.cpp index 31b72fe..76e3c53 100644 --- a/mlpp/softmax_reg/softmax_reg_old.cpp +++ b/mlpp/softmax_reg/softmax_reg_old.cpp @@ -6,9 +6,9 @@ #include "softmax_reg_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -30,8 +30,8 @@ std::vector> MLPPSoftmaxRegOld::modelSetTest(std::vector> y_hat, std::vector> y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; return cost.CrossEntropy(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg); } std::vector MLPPSoftmaxRegOld::Evaluate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.softmax(alg.addition(bias, alg.mat_vec_mult(alg.transpose(weights), x))); } std::vector> MLPPSoftmaxRegOld::Evaluate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.softmax(alg.mat_vec_add(alg.matmult(X, weights), bias)); @@ -186,7 +186,7 @@ std::vector> MLPPSoftmaxRegOld::Evaluate(std::vector -real_t MLPPStat::b0Estimation(const std::vector &x, const std::vector &y) { - return mean(y) - b1Estimation(x, y) * mean(x); -} - -real_t MLPPStat::b1Estimation(const std::vector &x, const std::vector &y) { - return covariance(x, y) / variance(x); -} - real_t MLPPStat::b0_estimation(const Ref &x, const Ref &y) { return meanv(y) - b1_estimation(x, y) * meanv(x); } @@ -29,14 +21,7 @@ real_t MLPPStat::b1_estimation(const Ref &x, const Ref & return covariancev(x, y) / variancev(x); } -real_t MLPPStat::mean(const std::vector &x) { - real_t sum = 0; - for (uint32_t i = 0; i < x.size(); i++) { - sum += x[i]; - } - return sum / x.size(); -} - +/* real_t MLPPStat::median(std::vector x) { real_t center = real_t(x.size()) / real_t(2); sort(x.begin(), x.end()); @@ -88,26 +73,6 @@ real_t MLPPStat::absAvgDeviation(const std::vector &x) { return sum / x.size(); } -real_t MLPPStat::standardDeviation(const std::vector &x) { - return std::sqrt(variance(x)); -} - -real_t MLPPStat::variance(const std::vector &x) { - real_t sum = 0; - for (uint32_t i = 0; i < x.size(); i++) { - sum += (x[i] - mean(x)) * (x[i] - mean(x)); - } - return sum / (x.size() - 1); -} - -real_t MLPPStat::covariance(const std::vector &x, const std::vector &y) { - real_t sum = 0; - for (uint32_t i = 0; i < x.size(); i++) { - sum += (x[i] - mean(x)) * (y[i] - mean(y)); - } - return sum / (x.size() - 1); -} - real_t MLPPStat::correlation(const std::vector &x, const std::vector &y) { return covariance(x, y) / (standardDeviation(x) * standardDeviation(y)); } @@ -120,6 +85,7 @@ real_t MLPPStat::chebyshevIneq(const real_t k) { // X may or may not belong to a Gaussian Distribution return 1 - 1 / (k * k); } +*/ real_t MLPPStat::meanv(const Ref &x) { int x_size = x->size(); @@ -171,6 +137,7 @@ real_t MLPPStat::covariancev(const Ref &x, const Ref &y) return sum / (x_size - 1); } +/* real_t MLPPStat::weightedMean(const std::vector &x, const std::vector &weights) { real_t sum = 0; real_t weights_sum = 0; @@ -270,6 +237,7 @@ real_t MLPPStat::logMean(const real_t x, const real_t y) { } return (y - x) / (log(y) - std::log(x)); } +*/ void MLPPStat::_bind_methods() { } diff --git a/mlpp/stat/stat.h b/mlpp/stat/stat.h index 8397e30..10eb4aa 100644 --- a/mlpp/stat/stat.h +++ b/mlpp/stat/stat.h @@ -22,25 +22,20 @@ class MLPPStat : public Reference { public: // These functions are for univariate lin reg module- not for users. - real_t b0Estimation(const std::vector &x, const std::vector &y); - real_t b1Estimation(const std::vector &x, const std::vector &y); - real_t b0_estimation(const Ref &x, const Ref &y); real_t b1_estimation(const Ref &x, const Ref &y); // Statistical Functions - real_t mean(const std::vector &x); + /* real_t median(std::vector x); std::vector mode(const std::vector &x); real_t range(const std::vector &x); real_t midrange(const std::vector &x); real_t absAvgDeviation(const std::vector &x); - real_t standardDeviation(const std::vector &x); - real_t variance(const std::vector &x); - real_t covariance(const std::vector &x, const std::vector &y); real_t correlation(const std::vector &x, const std::vector &y); real_t R2(const std::vector &x, const std::vector &y); real_t chebyshevIneq(const real_t k); + */ real_t meanv(const Ref &x); real_t standard_deviationv(const Ref &x); @@ -48,6 +43,7 @@ public: real_t covariancev(const Ref &x, const Ref &y); // Extras + /* real_t weightedMean(const std::vector &x, const std::vector &weights); real_t geometricMean(const std::vector &x); real_t harmonicMean(const std::vector &x); @@ -62,6 +58,7 @@ public: real_t stolarskyMean(const real_t x, const real_t y, const real_t p); real_t identricMean(const real_t x, const real_t y); real_t logMean(const real_t x, const real_t y); + */ protected: static void _bind_methods(); diff --git a/mlpp/stat/stat_old.cpp b/mlpp/stat/stat_old.cpp index 8d6eff4..8e60e20 100644 --- a/mlpp/stat/stat_old.cpp +++ b/mlpp/stat/stat_old.cpp @@ -7,7 +7,7 @@ #include "stat_old.h" #include "../activation/activation_old.h" #include "../data/data.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include #include #include @@ -65,7 +65,7 @@ std::vector MLPPStatOld::mode(const std::vector &x) { } real_t MLPPStatOld::range(const std::vector &x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.max(x) - alg.min(x); } diff --git a/mlpp/svc/svc_old.cpp b/mlpp/svc/svc_old.cpp index 23cb1e7..95233eb 100644 --- a/mlpp/svc/svc_old.cpp +++ b/mlpp/svc/svc_old.cpp @@ -6,9 +6,9 @@ #include "svc_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -23,9 +23,9 @@ real_t MLPPSVCOld::modelTest(std::vector x) { } void MLPPSVCOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { - class MLPPCost cost; - MLPPLinAlg alg; - MLPPReg regularization; + class MLPPCostOld cost; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -55,9 +55,9 @@ void MLPPSVCOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { } void MLPPSVCOld::SGD(real_t learning_rate, int max_epoch, bool UI) { - class MLPPCost cost; - MLPPLinAlg alg; - MLPPReg regularization; + class MLPPCostOld cost; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -98,9 +98,9 @@ void MLPPSVCOld::SGD(real_t learning_rate, int max_epoch, bool UI) { } void MLPPSVCOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { - class MLPPCost cost; - MLPPLinAlg alg; - MLPPReg regularization; + class MLPPCostOld cost; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -163,29 +163,29 @@ MLPPSVCOld::MLPPSVCOld(std::vector> p_inputSet, std::vector< } real_t MLPPSVCOld::Cost(std::vector z, std::vector y, std::vector weights, real_t C) { - class MLPPCost cost; + class MLPPCostOld cost; return cost.HingeLoss(z, y, weights, C); } std::vector MLPPSVCOld::Evaluate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.sign(alg.scalarAdd(bias, alg.mat_vec_mult(X, weights))); } std::vector MLPPSVCOld::propagate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarAdd(bias, alg.mat_vec_mult(X, weights)); } real_t MLPPSVCOld::Evaluate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.sign(alg.dot(weights, x) + bias); } real_t MLPPSVCOld::propagate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.dot(weights, x) + bias; } diff --git a/mlpp/tanh_reg/tanh_reg_old.cpp b/mlpp/tanh_reg/tanh_reg_old.cpp index e77e5c6..55a0261 100644 --- a/mlpp/tanh_reg/tanh_reg_old.cpp +++ b/mlpp/tanh_reg/tanh_reg_old.cpp @@ -7,9 +7,9 @@ #include "tanh_reg_old.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include @@ -32,8 +32,8 @@ real_t MLPPTanhRegOld::modelTest(std::vector x) { void MLPPTanhRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -65,8 +65,8 @@ void MLPPTanhRegOld::gradientDescent(real_t learning_rate, int max_epoch, bool U } void MLPPTanhRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -105,8 +105,8 @@ void MLPPTanhRegOld::SGD(real_t learning_rate, int max_epoch, bool UI) { void MLPPTanhRegOld::MBGD(real_t learning_rate, int max_epoch, int mini_batch_size, bool UI) { MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; real_t cost_prev = 0; int epoch = 1; @@ -160,30 +160,30 @@ void MLPPTanhRegOld::save(std::string fileName) { } real_t MLPPTanhRegOld::Cost(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; return cost.MSE(y_hat, y) + regularization.regTerm(weights, lambda, alpha, reg); } std::vector MLPPTanhRegOld::Evaluate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.tanh(alg.scalarAdd(bias, alg.mat_vec_mult(X, weights))); } std::vector MLPPTanhRegOld::propagate(std::vector> X) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarAdd(bias, alg.mat_vec_mult(X, weights)); } real_t MLPPTanhRegOld::Evaluate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPActivationOld avn; return avn.tanh(alg.dot(weights, x) + bias); } real_t MLPPTanhRegOld::propagate(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.dot(weights, x) + bias; } diff --git a/mlpp/transforms/transforms.cpp b/mlpp/transforms/transforms.cpp index c92f827..30d7802 100644 --- a/mlpp/transforms/transforms.cpp +++ b/mlpp/transforms/transforms.cpp @@ -10,6 +10,7 @@ #include #include +/* // DCT ii. // https://www.mathworks.com/help/images/discrete-cosine-transform.html std::vector> MLPPTransforms::discreteCosineTransform(std::vector> A) { @@ -51,6 +52,7 @@ std::vector> MLPPTransforms::discreteCosineTransform(std::ve } return B; } +*/ void MLPPTransforms::_bind_methods() { } diff --git a/mlpp/transforms/transforms.h b/mlpp/transforms/transforms.h index e75f6ef..39004a1 100644 --- a/mlpp/transforms/transforms.h +++ b/mlpp/transforms/transforms.h @@ -18,7 +18,7 @@ class MLPPTransforms : public Reference { GDCLASS(MLPPTransforms, Reference); public: - std::vector> discreteCosineTransform(std::vector> A); + //std::vector> discreteCosineTransform(std::vector> A); protected: static void _bind_methods(); diff --git a/mlpp/transforms/transforms_old.cpp b/mlpp/transforms/transforms_old.cpp index 9a5f7d2..ccc1766 100644 --- a/mlpp/transforms/transforms_old.cpp +++ b/mlpp/transforms/transforms_old.cpp @@ -5,7 +5,7 @@ // #include "transforms_old.h" -#include "../lin_alg/lin_alg.h" +#include "../lin_alg/lin_alg_old.h" #include #include #include @@ -13,7 +13,7 @@ // DCT ii. // https://www.mathworks.com/help/images/discrete-cosine-transform.html std::vector> MLPPTransformsOld::discreteCosineTransform(std::vector> A) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; A = alg.scalarAdd(-128, A); // Center around 0. std::vector> B; diff --git a/mlpp/uni_lin_reg/uni_lin_reg_old.cpp b/mlpp/uni_lin_reg/uni_lin_reg_old.cpp index 7c98b64..afa16c9 100644 --- a/mlpp/uni_lin_reg/uni_lin_reg_old.cpp +++ b/mlpp/uni_lin_reg/uni_lin_reg_old.cpp @@ -6,8 +6,8 @@ #include "uni_lin_reg_old.h" -#include "../lin_alg/lin_alg.h" -#include "../stat/stat.h" +#include "../lin_alg/lin_alg_old.h" +#include "../stat/stat_old.h" #include @@ -19,13 +19,13 @@ MLPPUniLinRegOld::MLPPUniLinRegOld(std::vector x, std::vector y) : inputSet(x), outputSet(y) { - MLPPStat estimator; + MLPPStatOld estimator; b1 = estimator.b1Estimation(inputSet, outputSet); b0 = estimator.b0Estimation(inputSet, outputSet); } std::vector MLPPUniLinRegOld::modelSetTest(std::vector x) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return alg.scalarAdd(b0, alg.scalarMultiply(b1, x)); } diff --git a/mlpp/wgan/wgan.cpp b/mlpp/wgan/wgan.cpp index f3ccc4e..de67b2a 100644 --- a/mlpp/wgan/wgan.cpp +++ b/mlpp/wgan/wgan.cpp @@ -77,7 +77,7 @@ void MLPPWGAN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { Vector> cumulative_discriminator_hidden_layer_w_grad = discriminator_gradient_results.cumulative_hidden_layer_w_grad; Ref output_discriminator_w_grad = discriminator_gradient_results.output_w_grad; - cumulative_discriminator_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, cumulative_discriminator_hidden_layer_w_grad); + cumulative_discriminator_hidden_layer_w_grad = alg.scalar_multiplynvt(learning_rate / _n, cumulative_discriminator_hidden_layer_w_grad); output_discriminator_w_grad = alg.scalar_multiplynv(learning_rate / _n, output_discriminator_w_grad); update_discriminator_parameters(cumulative_discriminator_hidden_layer_w_grad, output_discriminator_w_grad, learning_rate); } @@ -89,7 +89,7 @@ void MLPPWGAN::gradient_descent(real_t learning_rate, int max_epoch, bool ui) { loutput_set = alg.onevecnv(_n); Vector> cumulative_generator_hidden_layer_w_grad = compute_generator_gradients(_y_hat, loutput_set); - cumulative_generator_hidden_layer_w_grad = alg.scalar_multiply_vm(learning_rate / _n, cumulative_generator_hidden_layer_w_grad); + 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(); diff --git a/mlpp/wgan/wgan_old.cpp b/mlpp/wgan/wgan_old.cpp index 230e696..819c07f 100644 --- a/mlpp/wgan/wgan_old.cpp +++ b/mlpp/wgan/wgan_old.cpp @@ -9,9 +9,9 @@ #include "core/log/logger.h" #include "../activation/activation_old.h" -#include "../cost/cost.h" -#include "../lin_alg/lin_alg.h" -#include "../regularization/reg.h" +#include "../cost/cost_old.h" +#include "../lin_alg/lin_alg_old.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include "core/object/method_bind_ext.gen.inc" @@ -28,13 +28,12 @@ MLPPWGANOld::~MLPPWGANOld() { } std::vector> MLPPWGANOld::generateExample(int n) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; return modelSetTestGenerator(alg.gaussianNoise(n, k)); } void MLPPWGANOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) { - class MLPPCost cost; - MLPPLinAlg alg; + MLPPLinAlgOld alg; real_t cost_prev = 0; int epoch = 1; forwardPass(); @@ -94,7 +93,7 @@ void MLPPWGANOld::gradientDescent(real_t learning_rate, int max_epoch, bool UI) } real_t MLPPWGANOld::score() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPUtilities util; forwardPass(); return util.performance(y_hat, alg.onevec(n)); @@ -114,7 +113,7 @@ void MLPPWGANOld::save(std::string fileName) { } void MLPPWGANOld::addLayer(int n_hidden, std::string activation, std::string weightInit, std::string reg, real_t lambda, real_t alpha) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (network.empty()) { network.push_back(MLPPOldHiddenLayer(n_hidden, activation, alg.gaussianNoise(n, k), weightInit, reg, lambda, alpha)); network[0].forwardPass(); @@ -125,7 +124,7 @@ void MLPPWGANOld::addLayer(int n_hidden, std::string activation, std::string wei } void MLPPWGANOld::addOutputLayer(std::string weightInit, std::string reg, real_t lambda, real_t alpha) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (!network.empty()) { outputLayer = new MLPPOldOutputLayer(network[network.size() - 1].n_hidden, "Linear", "WassersteinLoss", network[network.size() - 1].a, weightInit, "WeightClipping", -0.01, 0.01); } else { // Should never happen. @@ -163,8 +162,8 @@ std::vector MLPPWGANOld::modelSetTestDiscriminator(std::vector y_hat, std::vector y) { - MLPPReg regularization; - class MLPPCost cost; + MLPPRegOld regularization; + class MLPPCostOld cost; real_t totalRegTerm = 0; auto cost_function = outputLayer->cost_map[outputLayer->cost]; @@ -177,7 +176,7 @@ real_t MLPPWGANOld::Cost(std::vector y_hat, std::vector y) { } void MLPPWGANOld::forwardPass() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (!network.empty()) { network[0].input = alg.gaussianNoise(n, k); network[0].forwardPass(); @@ -195,7 +194,7 @@ void MLPPWGANOld::forwardPass() { } void MLPPWGANOld::updateDiscriminatorParameters(std::vector>> hiddenLayerUpdations, std::vector outputLayerUpdation, real_t learning_rate) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; outputLayer->weights = alg.subtraction(outputLayer->weights, outputLayerUpdation); outputLayer->bias -= learning_rate * alg.sum_elements(outputLayer->delta) / n; @@ -212,7 +211,7 @@ void MLPPWGANOld::updateDiscriminatorParameters(std::vector>> hiddenLayerUpdations, real_t learning_rate) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; if (!network.empty()) { for (int ii = network.size() / 2; ii >= 0; ii--) { @@ -227,10 +226,10 @@ void MLPPWGANOld::updateGeneratorParameters(std::vector>>, std::vector> MLPPWGANOld::computeDiscriminatorGradients(std::vector y_hat, std::vector outputSet) { - class MLPPCost cost; + class MLPPCostOld cost; MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; std::vector>> cumulativeHiddenLayerWGrad; // Tensor containing ALL hidden grads. @@ -262,10 +261,10 @@ std::tuple>>, std::vector> M } std::vector>> MLPPWGANOld::computeGeneratorGradients(std::vector y_hat, std::vector outputSet) { - class MLPPCost cost; + class MLPPCostOld cost; MLPPActivationOld avn; - MLPPLinAlg alg; - MLPPReg regularization; + MLPPLinAlgOld alg; + MLPPRegOld regularization; std::vector>> cumulativeHiddenLayerWGrad; // Tensor containing ALL hidden grads. diff --git a/mlpp/wgan/wgan_old.h b/mlpp/wgan/wgan_old.h index a28916d..7c46610 100644 --- a/mlpp/wgan/wgan_old.h +++ b/mlpp/wgan/wgan_old.h @@ -22,7 +22,7 @@ #include "../activation/activation.h" #include "../cost/cost.h" -#include "../regularization/reg.h" +#include "../regularization/reg_old.h" #include "../utilities/utilities.h" #include diff --git a/test/mlpp_tests.cpp b/test/mlpp_tests.cpp index 8addd6b..60abbea 100644 --- a/test/mlpp_tests.cpp +++ b/test/mlpp_tests.cpp @@ -52,6 +52,9 @@ #include "../mlpp/auto_encoder/auto_encoder_old.h" #include "../mlpp/bernoulli_nb/bernoulli_nb_old.h" #include "../mlpp/c_log_log_reg/c_log_log_reg_old.h" +#include "../mlpp/convolutions/convolutions_old.h" +#include "../mlpp/cost/cost_old.h" +#include "../mlpp/data/data_old.h" #include "../mlpp/dual_svc/dual_svc_old.h" #include "../mlpp/exp_reg/exp_reg_old.h" #include "../mlpp/gan/gan_old.h" @@ -71,8 +74,10 @@ #include "../mlpp/probit_reg/probit_reg_old.h" #include "../mlpp/softmax_net/softmax_net_old.h" #include "../mlpp/softmax_reg/softmax_reg_old.h" +#include "../mlpp/stat/stat_old.h" #include "../mlpp/svc/svc_old.h" #include "../mlpp/tanh_reg/tanh_reg_old.h" +#include "../mlpp/transforms/transforms_old.h" #include "../mlpp/uni_lin_reg/uni_lin_reg_old.h" #include "../mlpp/wgan/wgan_old.h" @@ -102,7 +107,7 @@ Vector> dstd_mat_to_mat(const std::vector> &i void MLPPTests::test_statistics() { ERR_PRINT("MLPPTests::test_statistics() Started!"); - MLPPStat stat; + MLPPStatOld stat; MLPPConvolutions conv; // STATISTICS @@ -146,7 +151,7 @@ void MLPPTests::test_statistics() { } void MLPPTests::test_linear_algebra() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> square = { { 1, 1 }, { -1, 1 }, { 1, -1 }, { -1, -1 } }; std::vector> square_rot_res = { { 1.41421, 1.11022e-16 }, { -1.11022e-16, 1.41421 }, { 1.11022e-16, -1.41421 }, { -1.41421, -1.11022e-16 } }; @@ -242,7 +247,7 @@ void MLPPTests::test_univariate_linear_regression() { void MLPPTests::test_multivariate_linear_regression_gradient_descent(bool ui) { MLPPData data; - MLPPLinAlg alg; + MLPPLinAlgOld alg; Ref ds = data.load_california_housing(_california_housing_data_path); @@ -257,7 +262,7 @@ void MLPPTests::test_multivariate_linear_regression_gradient_descent(bool ui) { void MLPPTests::test_multivariate_linear_regression_sgd(bool ui) { MLPPData data; - MLPPLinAlg alg; + MLPPLinAlgOld alg; Ref ds = data.load_california_housing(_california_housing_data_path); @@ -272,7 +277,7 @@ void MLPPTests::test_multivariate_linear_regression_sgd(bool ui) { void MLPPTests::test_multivariate_linear_regression_mbgd(bool ui) { MLPPData data; - MLPPLinAlg alg; + MLPPLinAlgOld alg; Ref ds = data.load_california_housing(_california_housing_data_path); @@ -287,7 +292,7 @@ void MLPPTests::test_multivariate_linear_regression_mbgd(bool ui) { void MLPPTests::test_multivariate_linear_regression_normal_equation(bool ui) { MLPPData data; - MLPPLinAlg alg; + MLPPLinAlgOld alg; Ref ds = data.load_california_housing(_california_housing_data_path); @@ -302,7 +307,8 @@ void MLPPTests::test_multivariate_linear_regression_normal_equation(bool ui) { void MLPPTests::test_multivariate_linear_regression_adam() { MLPPData data; - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; Ref ds = data.load_california_housing(_california_housing_data_path); @@ -310,14 +316,15 @@ void MLPPTests::test_multivariate_linear_regression_adam() { alg.printVector(adamModelOld.modelSetTest(ds->get_input()->to_std_vector())); std::cout << "ACCURACY: " << 100 * adamModelOld.score() << "%" << std::endl; - MLPPLinReg adam_model(alg.transposenm(ds->get_input()), ds->get_output()); + MLPPLinReg adam_model(algn.transposenm(ds->get_input()), ds->get_output()); PLOG_MSG(adam_model.model_set_test(ds->get_input())->to_string()); PLOG_MSG("ACCURACY: " + String::num(100 * adam_model.score()) + "%"); } void MLPPTests::test_multivariate_linear_regression_score_sgd_adam(bool ui) { MLPPData data; - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; Ref ds = data.load_california_housing(_california_housing_data_path); @@ -330,7 +337,7 @@ void MLPPTests::test_multivariate_linear_regression_score_sgd_adam(bool ui) { modelf_old.MBGD(0.001, 5, 1, ui); scoreSGD += modelf_old.score(); - MLPPLinReg modelf(alg.transposenm(ds->get_input()), ds->get_output()); + MLPPLinReg modelf(algn.transposenm(ds->get_input()), ds->get_output()); modelf.mbgd(0.001, 5, 1, ui); scoreSGD += modelf.score(); @@ -338,7 +345,7 @@ void MLPPTests::test_multivariate_linear_regression_score_sgd_adam(bool ui) { adamModelf_old.Adam(0.1, 5, 1, 0.9, 0.999, 1e-8, ui); // Change batch size = sgd, bgd scoreADAM += adamModelf_old.score(); - MLPPLinReg adamModelf(alg.transposenm(ds->get_input()), ds->get_output()); + MLPPLinReg adamModelf(algn.transposenm(ds->get_input()), ds->get_output()); adamModelf.adam(0.1, 5, 1, 0.9, 0.999, 1e-8, ui); // Change batch size = sgd, bgd scoreADAM += adamModelf.score(); } @@ -350,7 +357,8 @@ void MLPPTests::test_multivariate_linear_regression_score_sgd_adam(bool ui) { void MLPPTests::test_multivariate_linear_regression_epochs_gradient_descent(bool ui) { MLPPData data; - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; Ref ds = data.load_california_housing(_california_housing_data_path); @@ -361,14 +369,15 @@ void MLPPTests::test_multivariate_linear_regression_epochs_gradient_descent(bool model3_old.gradientDescent(0.001, 300, ui); alg.printVector(model3_old.modelSetTest(ds->get_input()->to_std_vector())); - MLPPLinReg model3(alg.transposenm(ds->get_input()), ds->get_output()); // Can use Lasso, Ridge, ElasticNet Reg + MLPPLinReg model3(algn.transposenm(ds->get_input()), ds->get_output()); // Can use Lasso, Ridge, ElasticNet Reg model3.gradient_descent(0.001, 300, ui); PLOG_MSG(model3.model_set_test(ds->get_input())->to_string()); } void MLPPTests::test_multivariate_linear_regression_newton_raphson(bool ui) { MLPPData data; - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; Ref ds = data.load_california_housing(_california_housing_data_path); @@ -380,13 +389,13 @@ void MLPPTests::test_multivariate_linear_regression_newton_raphson(bool ui) { model2_old.NewtonRaphson(1.5, 300, ui); alg.printVector(model2_old.modelSetTest(ds->get_input()->to_std_vector())); - MLPPLinReg model2(alg.transposenm(ds->get_input()), ds->get_output()); + MLPPLinReg model2(algn.transposenm(ds->get_input()), ds->get_output()); model2.newton_raphson(1.5, 300, ui); PLOG_MSG(model2.model_set_test(ds->get_input())->to_string()); } void MLPPTests::test_logistic_regression(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPData data; Ref dt = data.load_breast_cancer(_breast_cancer_data_path); @@ -404,7 +413,7 @@ void MLPPTests::test_logistic_regression(bool ui) { std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl; } void MLPPTests::test_probit_regression(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPData data; // PROBIT REGRESSION @@ -421,7 +430,8 @@ void MLPPTests::test_probit_regression(bool ui) { PLOG_MSG("ACCURACY: " + String::num(100 * model.score()) + "%"); } void MLPPTests::test_c_log_log_regression(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; // CLOGLOG REGRESSION std::vector> inputSet = { { 1, 2, 3, 4, 5, 6, 7, 8 }, { 0, 0, 0, 0, 1, 1, 1, 1 } }; @@ -440,13 +450,14 @@ void MLPPTests::test_c_log_log_regression(bool ui) { output_set.instance(); output_set->set_from_std_vector(outputSet); - MLPPCLogLogReg model(alg.transposenm(input_set), output_set); + MLPPCLogLogReg model(algn.transposenm(input_set), output_set); model.sgd(0.1, 10000, ui); - PLOG_MSG(model.model_set_test(alg.transposenm(input_set))->to_string()); + PLOG_MSG(model.model_set_test(algn.transposenm(input_set))->to_string()); PLOG_MSG("ACCURACY: " + String::num(100 * model.score()) + "%"); } void MLPPTests::test_exp_reg_regression(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; // EXPREG REGRESSION std::vector> inputSet = { { 0, 1, 2, 3, 4 } }; @@ -465,13 +476,13 @@ void MLPPTests::test_exp_reg_regression(bool ui) { output_set.instance(); output_set->set_from_std_vector(outputSet); - MLPPExpReg model(alg.transposenm(input_set), output_set); + MLPPExpReg model(algn.transposenm(input_set), output_set); model.sgd(0.001, 10000, ui); - PLOG_MSG(model.model_set_test(alg.transposenm(input_set))->to_string()); + PLOG_MSG(model.model_set_test(algn.transposenm(input_set))->to_string()); PLOG_MSG("ACCURACY: " + String::num(100 * model.score()) + "%"); } void MLPPTests::test_tanh_regression(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; // TANH REGRESSION std::vector> inputSet = { { 4, 3, 0, -3, -4 }, { 0, 0, 0, 1, 1 } }; @@ -483,7 +494,7 @@ void MLPPTests::test_tanh_regression(bool ui) { std::cout << "ACCURACY (Old): " << 100 * model_old.score() << "%" << std::endl; } void MLPPTests::test_softmax_regression(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPData data; Ref dt = data.load_iris(_iris_data_path); @@ -502,7 +513,7 @@ void MLPPTests::test_softmax_regression(bool ui) { } void MLPPTests::test_support_vector_classification(bool ui) { //MLPPStat stat; - MLPPLinAlg alg; + MLPPLinAlgOld alg; //MLPPActivation avn; //MLPPCost cost; MLPPData data; @@ -523,7 +534,7 @@ void MLPPTests::test_support_vector_classification(bool ui) { } void MLPPTests::test_mlp(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; // MLP std::vector> inputSet = { @@ -569,7 +580,7 @@ void MLPPTests::test_mlp(bool ui) { PLOG_MSG(res); } void MLPPTests::test_soft_max_network(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPData data; // SOFTMAX NETWORK @@ -586,7 +597,8 @@ void MLPPTests::test_soft_max_network(bool ui) { std::cout << "ACCURACY: " << 100 * model.score() << "%" << std::endl; } void MLPPTests::test_autoencoder(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; std::vector> inputSet = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, { 3, 5, 9, 12, 15, 18, 21, 24, 27, 30 } }; @@ -600,13 +612,14 @@ void MLPPTests::test_autoencoder(bool ui) { input_set.instance(); input_set->set_from_std_vectors(inputSet); - MLPPAutoEncoder model(alg.transposenm(input_set), 5); + MLPPAutoEncoder model(algn.transposenm(input_set), 5); model.sgd(0.001, 300000, ui); - PLOG_MSG(model.model_set_test(alg.transposenm(input_set))->to_string()); + PLOG_MSG(model.model_set_test(algn.transposenm(input_set))->to_string()); PLOG_MSG("ACCURACY: " + String::num(100 * model.score()) + "%"); } void MLPPTests::test_dynamically_sized_ann(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; // DYNAMICALLY SIZED ANN // Possible Weight Init Methods: Default, Uniform, HeNormal, HeUniform, XavierNormal, XavierUniform @@ -636,7 +649,7 @@ void MLPPTests::test_dynamically_sized_ann(bool ui) { output_set.instance(); output_set->set_from_std_vector(outputSet); - MLPPANN ann(alg.transposenm(input_set), output_set); + MLPPANN ann(algn.transposenm(input_set), output_set); ann.add_layer(2, MLPPActivation::ACTIVATION_FUNCTION_COSH); ann.add_output_layer(MLPPActivation::ACTIVATION_FUNCTION_SIGMOID, MLPPCost::COST_TYPE_LOGISTIC_LOSS); @@ -646,12 +659,12 @@ void MLPPTests::test_dynamically_sized_ann(bool ui) { ann.set_learning_rate_scheduler_drop(MLPPANN::SCHEDULER_TYPE_STEP, 0.5, 1000); ann.gradient_descent(0.01, 30000); - PLOG_MSG(ann.model_set_test(alg.transposenm(input_set))->to_string()); + PLOG_MSG(ann.model_set_test(algn.transposenm(input_set))->to_string()); PLOG_MSG("ACCURACY: " + String::num(100 * ann.score()) + "%"); } void MLPPTests::test_wgan_old(bool ui) { //MLPPStat stat; - MLPPLinAlg alg; + MLPPLinAlgOld alg; //MLPPActivation avn; //MLPPCost cost; //MLPPData data; @@ -673,7 +686,7 @@ void MLPPTests::test_wgan_old(bool ui) { } void MLPPTests::test_wgan(bool ui) { //MLPPStat stat; - MLPPLinAlg alg; + MLPPLinAlgOld alg; //MLPPActivation avn; //MLPPCost cost; //MLPPData data; @@ -700,7 +713,7 @@ void MLPPTests::test_wgan(bool ui) { PLOG_MSG(str); } void MLPPTests::test_ann(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; std::vector> inputSet = { { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }; // XOR std::vector outputSet = { 0, 1, 1, 0 }; @@ -734,7 +747,7 @@ void MLPPTests::test_ann(bool ui) { PLOG_MSG("ACCURACY: " + String::num(100 * ann.score()) + "%"); // Accuracy. } void MLPPTests::test_dynamically_sized_mann(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPData data; // DYNAMICALLY SIZED MANN (Multidimensional Output ANN) @@ -762,7 +775,8 @@ void MLPPTests::test_dynamically_sized_mann(bool ui) { PLOG_MSG("ACCURACY: " + String::num(100 * mann.score()) + "%"); } void MLPPTests::test_train_test_split_mann(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; MLPPData data; // TRAIN TEST SPLIT CHECK @@ -780,8 +794,8 @@ void MLPPTests::test_train_test_split_mann(bool ui) { Ref d; d.instance(); - d->set_input(alg.transposenm(input_set_1)); - d->set_output(alg.transposenm(output_set_1)); + d->set_input(algn.transposenm(input_set_1)); + d->set_output(algn.transposenm(output_set_1)); MLPPData::SplitComplexData split_data = data.train_test_split(d, 0.2); @@ -806,7 +820,8 @@ void MLPPTests::test_train_test_split_mann(bool ui) { } void MLPPTests::test_naive_bayes() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; // NAIVE BAYES std::vector> inputSet = { { 1, 1, 1, 1, 1 }, { 0, 0, 1, 1, 1 }, { 0, 0, 1, 0, 1 } }; @@ -829,14 +844,14 @@ void MLPPTests::test_naive_bayes() { MLPPBernoulliNBOld BNBOld(alg.transpose(inputSet), outputSet); alg.printVector(BNBOld.modelSetTest(alg.transpose(inputSet))); - MLPPBernoulliNB BNB(alg.transposenm(input_set), output_set); - PLOG_MSG(BNB.model_set_test(alg.transposenm(input_set))->to_string()); + MLPPBernoulliNB BNB(algn.transposenm(input_set), output_set); + PLOG_MSG(BNB.model_set_test(algn.transposenm(input_set))->to_string()); MLPPGaussianNBOld GNBOld(alg.transpose(inputSet), outputSet, 2); alg.printVector(GNBOld.modelSetTest(alg.transpose(inputSet))); - MLPPGaussianNB GNB(alg.transposenm(input_set), output_set, 2); - PLOG_MSG(GNB.model_set_test(alg.transposenm(input_set))->to_string()); + MLPPGaussianNB GNB(algn.transposenm(input_set), output_set, 2); + PLOG_MSG(GNB.model_set_test(algn.transposenm(input_set))->to_string()); } void MLPPTests::test_k_means(bool ui) { // KMeans @@ -858,7 +873,7 @@ void MLPPTests::test_k_means(bool ui) { PLOG_MSG(kmeans->silhouette_scores()->to_string()); } void MLPPTests::test_knn(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; // kNN std::vector> inputSet = { @@ -897,9 +912,10 @@ void MLPPTests::test_knn(bool ui) { } void MLPPTests::test_convolution_tensors_etc() { - MLPPLinAlg alg; + MLPPLinAlgOld alg; + MLPPLinAlg algn; MLPPData data; - MLPPConvolutions conv; + MLPPConvolutionsOld conv; // CONVOLUTION, POOLING, ETC.. std::vector> input = { @@ -924,7 +940,7 @@ void MLPPTests::test_convolution_tensors_etc() { { 109, 121, 127, 133, 139, 141, 140, 133 }, }; - MLPPTransforms trans; + MLPPTransformsOld trans; alg.printMatrix(trans.discreteCosineTransform(input2)); @@ -940,12 +956,12 @@ void MLPPTests::test_convolution_tensors_etc() { alg.printMatrix(conv.convolve_2d(conv.gaussian_filter_2d(5, 1), laplacian, 1)); } void MLPPTests::test_pca_svd_eigenvalues_eigenvectors(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; // PCA, SVD, eigenvalues & eigenvectors std::vector> inputSet = { { 1, 1 }, { 1, 1 } }; - MLPPLinAlg::EigenResultOld eigen = alg.eigen_old(inputSet); + MLPPLinAlgOld::EigenResultOld eigen = alg.eigen_old(inputSet); std::cout << "Eigenvectors:" << std::endl; alg.printMatrix(eigen.eigen_vectors); @@ -955,7 +971,7 @@ void MLPPTests::test_pca_svd_eigenvalues_eigenvectors(bool ui) { std::cout << "SVD OLD START" << std::endl; - MLPPLinAlg::SVDResultOld svd_old = alg.SVD(inputSet); + MLPPLinAlgOld::SVDResultOld svd_old = alg.SVD(inputSet); std::cout << "U:" << std::endl; alg.printMatrix(svd_old.U); @@ -970,9 +986,10 @@ void MLPPTests::test_pca_svd_eigenvalues_eigenvectors(bool ui) { input_set.instance(); input_set->set_from_std_vectors(inputSet); + /* String str_svd = "SVD\n"; - MLPPLinAlg::SVDResult svd = alg.svd(input_set); + MLPPLinAlgOld::SVDResult svd = alg.svd(input_set); str_svd += "U:\n"; str_svd += svd.U->to_string(); @@ -983,6 +1000,7 @@ void MLPPTests::test_pca_svd_eigenvalues_eigenvectors(bool ui) { str_svd += "\n"; PLOG_MSG(str_svd); + */ std::cout << "PCA" << std::endl; @@ -1003,7 +1021,7 @@ void MLPPTests::test_pca_svd_eigenvalues_eigenvectors(bool ui) { } void MLPPTests::test_nlp_and_data(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPData data; // NLP/DATA @@ -1053,7 +1071,7 @@ void MLPPTests::test_nlp_and_data(bool ui) { std::cout << std::endl; } void MLPPTests::test_outlier_finder(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; // Outlier Finder //std::vector inputSet = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 23554332523523 }; @@ -1109,13 +1127,13 @@ void MLPPTests::test_new_math_functions() { alg.printMatrix(alg.gramSchmidtProcess(P)); - //MLPPLinAlg::QRDResult qrd_result = alg.qrd(P); // It works! + //MLPPLinAlgOld::QRDResult qrd_result = alg.qrd(P); // It works! //alg.printMatrix(qrd_result.Q); //alg.printMatrix(qrd_result.R); } void MLPPTests::test_positive_definiteness_checker() { //MLPPStat stat; - MLPPLinAlg alg; + MLPPLinAlgOld alg; //MLPPActivation avn; //MLPPCost cost; //MLPPData data; @@ -1130,7 +1148,7 @@ void MLPPTests::test_positive_definiteness_checker() { }; std::cout << std::boolalpha << alg.positiveDefiniteChecker(A) << std::endl; - MLPPLinAlg::CholeskyResult chres = alg.cholesky(A); // works. + MLPPLinAlgOld::CholeskyResult chres = alg.cholesky(A); // works. alg.printMatrix(chres.L); alg.printMatrix(chres.Lt); } @@ -1200,8 +1218,8 @@ real_t f_mv(std::vector x) { */ void MLPPTests::test_numerical_analysis() { - MLPPLinAlg alg; - MLPPConvolutions conv; + MLPPLinAlgOld alg; + MLPPConvolutionsOld conv; // Checks for numerical analysis class. MLPPNumericalAnalysisOld numAn; @@ -1273,7 +1291,7 @@ void MLPPTests::test_numerical_analysis() { alg.printVector(alg.cross(a, b)); } void MLPPTests::test_support_vector_classification_kernel(bool ui) { - MLPPLinAlg alg; + MLPPLinAlgOld alg; MLPPData data; //SUPPORT VECTOR CLASSIFICATION (kernel method)