Removed new things from MLPPUtilitiesOld.

This commit is contained in:
Relintai 2023-02-13 19:41:06 +01:00
parent 80f85ee0af
commit d6037730b5
2 changed files with 0 additions and 492 deletions

View File

@ -6,10 +6,6 @@
#include "utilities_old.h"
#include "core/log/logger.h"
#include "core/math/math_funcs.h"
#include "core/math/random_pcg.h"
#include <fstream>
#include <iostream>
#include <random>
@ -109,176 +105,6 @@ std::vector<real_t> MLPPUtilitiesOld::biasInitialization(int n) {
return bias;
}
void MLPPUtilitiesOld::weight_initializationv(Ref<MLPPVector> weights, WeightDistributionType type) {
ERR_FAIL_COND(!weights.is_valid());
int n = weights->size();
real_t *weights_ptr = weights->ptrw();
RandomPCG rnd;
rnd.randomize();
std::random_device rd;
std::default_random_engine generator(rd());
switch (type) {
case WEIGHT_DISTRIBUTION_TYPE_DEFAULT: {
std::uniform_real_distribution<real_t> distribution(0, 1);
for (int i = 0; i < n; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_XAVIER_NORMAL: {
std::normal_distribution<real_t> distribution(0, Math::sqrt(2.0 / (n + 1.0)));
for (int i = 0; i < n; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_XAVIER_UNIFORM: {
std::uniform_real_distribution<real_t> distribution(-Math::sqrt(6.0 / (n + 1.0)), Math::sqrt(6.0 / (n + 1.0)));
for (int i = 0; i < n; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_HE_NORMAL: {
std::normal_distribution<real_t> distribution(0, Math::sqrt(2.0 / n));
for (int i = 0; i < n; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_HE_UNIFORM: {
std::uniform_real_distribution<real_t> distribution(-Math::sqrt(6.0 / n), Math::sqrt(6.0 / n));
for (int i = 0; i < n; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_LE_CUN_NORMAL: {
std::normal_distribution<real_t> distribution(0, Math::sqrt(1.0 / n));
for (int i = 0; i < n; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_LE_CUN_UNIFORM: {
std::uniform_real_distribution<real_t> distribution(-Math::sqrt(3.0 / n), Math::sqrt(3.0 / n));
for (int i = 0; i < n; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_UNIFORM: {
std::uniform_real_distribution<real_t> distribution(-1.0 / Math::sqrt(static_cast<real_t>(n)), 1.0 / Math::sqrt(static_cast<real_t>(n)));
for (int i = 0; i < n; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
default:
break;
}
}
void MLPPUtilitiesOld::weight_initializationm(Ref<MLPPMatrix> weights, WeightDistributionType type) {
ERR_FAIL_COND(!weights.is_valid());
int n = weights->size().x;
int m = weights->size().y;
int data_size = weights->data_size();
real_t *weights_ptr = weights->ptrw();
RandomPCG rnd;
rnd.randomize();
std::random_device rd;
std::default_random_engine generator(rd());
switch (type) {
case WEIGHT_DISTRIBUTION_TYPE_DEFAULT: {
std::uniform_real_distribution<real_t> distribution(0, 1);
for (int i = 0; i < data_size; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_XAVIER_NORMAL: {
std::normal_distribution<real_t> distribution(0, sqrt(2 / (n + m)));
for (int i = 0; i < data_size; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_XAVIER_UNIFORM: {
std::uniform_real_distribution<real_t> distribution(-sqrt(6 / (n + m)), sqrt(6 / (n + m)));
for (int i = 0; i < data_size; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_HE_NORMAL: {
std::normal_distribution<real_t> distribution(0, sqrt(2 / n));
for (int i = 0; i < data_size; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_HE_UNIFORM: {
std::uniform_real_distribution<real_t> distribution(-sqrt(6 / n), sqrt(6 / n));
for (int i = 0; i < data_size; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_LE_CUN_NORMAL: {
std::normal_distribution<real_t> distribution(0, sqrt(1 / n));
for (int i = 0; i < data_size; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_LE_CUN_UNIFORM: {
std::uniform_real_distribution<real_t> distribution(-sqrt(3 / n), sqrt(3 / n));
for (int i = 0; i < data_size; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
case WEIGHT_DISTRIBUTION_TYPE_UNIFORM: {
std::uniform_real_distribution<real_t> distribution(-1 / sqrt(n), 1 / sqrt(n));
for (int i = 0; i < data_size; ++i) {
weights_ptr[i] = distribution(generator);
}
} break;
default:
break;
}
}
real_t MLPPUtilitiesOld::bias_initializationr() {
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_real_distribution<real_t> distribution(0, 1);
return distribution(generator);
}
void MLPPUtilitiesOld::bias_initializationv(Ref<MLPPVector> z) {
ERR_FAIL_COND(!z.is_valid());
std::vector<real_t> bias;
std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_real_distribution<real_t> distribution(0, 1);
int n = z->size();
for (int i = 0; i < n; i++) {
bias.push_back(distribution(generator));
}
}
real_t MLPPUtilitiesOld::performance(std::vector<real_t> y_hat, std::vector<real_t> outputSet) {
real_t correct = 0;
for (uint32_t i = 0; i < y_hat.size(); i++) {
@ -305,50 +131,6 @@ real_t MLPPUtilitiesOld::performance(std::vector<std::vector<real_t>> y_hat, std
return correct / y_hat.size();
}
real_t MLPPUtilitiesOld::performance_vec(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set) {
ERR_FAIL_COND_V(!y_hat.is_valid(), 0);
ERR_FAIL_COND_V(!output_set.is_valid(), 0);
real_t correct = 0;
for (int i = 0; i < y_hat->size(); i++) {
if (Math::is_equal_approx(y_hat->get_element(i), output_set->get_element(i))) {
correct++;
}
}
return correct / y_hat->size();
}
real_t MLPPUtilitiesOld::performance_mat(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
ERR_FAIL_COND_V(!y_hat.is_valid(), 0);
ERR_FAIL_COND_V(!y.is_valid(), 0);
real_t correct = 0;
for (int i = 0; i < y_hat->size().y; i++) {
int sub_correct = 0;
for (int j = 0; j < y_hat->size().x; j++) {
if (Math::round(y_hat->get_element(i, j)) == y->get_element(i, j)) {
sub_correct++;
}
if (sub_correct == y_hat->size().x) {
correct++;
}
}
}
return correct / y_hat->size().y;
}
real_t MLPPUtilitiesOld::performance_pool_int_array_vec(PoolIntArray y_hat, const Ref<MLPPVector> &output_set) {
ERR_FAIL_COND_V(!output_set.is_valid(), 0);
real_t correct = 0;
for (int i = 0; i < y_hat.size(); i++) {
if (y_hat[i] == Math::round(output_set->get_element(i))) {
correct++;
}
}
return correct / y_hat.size();
}
void MLPPUtilitiesOld::saveParameters(std::string fileName, std::vector<real_t> weights, real_t bias, bool app, int layer) {
std::string layer_info = "";
std::ofstream saveFile;
@ -478,36 +260,6 @@ void MLPPUtilitiesOld::UI(std::vector<real_t> weights, std::vector<real_t> initi
std::cout << bias << std::endl;
}
void MLPPUtilitiesOld::print_ui_vb(Ref<MLPPVector> weights, real_t bias) {
String str = "Values of the weight(s):\n";
str += weights->to_string();
str += "\nValue of the bias:\n";
str += String::num(bias);
PLOG_MSG(str);
}
void MLPPUtilitiesOld::print_ui_vib(Ref<MLPPVector> weights, Ref<MLPPVector> initial, real_t bias) {
String str = "Values of the weight(s):\n";
str += weights->to_string();
str += "\nValues of the initial(s):\n";
str += initial->to_string();
str += "\nValue of the bias:\n";
str += String::num(bias);
PLOG_MSG(str);
}
void MLPPUtilitiesOld::print_ui_mb(Ref<MLPPMatrix> weights, Ref<MLPPVector> bias) {
String str = "Values of the weight(s):\n";
str += weights->to_string();
str += "\nValue of the biased:\n";
str += bias->to_string();
PLOG_MSG(str);
}
void MLPPUtilitiesOld::CostInfo(int epoch, real_t cost_prev, real_t Cost) {
std::cout << "-----------------------------------" << std::endl;
std::cout << "This is epoch: " << epoch << std::endl;
@ -516,14 +268,6 @@ void MLPPUtilitiesOld::CostInfo(int epoch, real_t cost_prev, real_t Cost) {
std::cout << Cost << std::endl;
}
void MLPPUtilitiesOld::cost_info(int epoch, real_t cost_prev, real_t cost) {
String str = "This is epoch: " + itos(epoch) + ",";
str += "The cost function has been minimized by " + String::num(cost_prev - cost);
str += ", Current Cost:" + String::num(cost);
PLOG_MSG(str);
}
std::vector<std::vector<std::vector<real_t>>> MLPPUtilitiesOld::createMiniBatches(std::vector<std::vector<real_t>> inputSet, int n_mini_batch) {
int n = inputSet.size();
@ -598,189 +342,6 @@ std::tuple<std::vector<std::vector<std::vector<real_t>>>, std::vector<std::vecto
return { inputMiniBatches, outputMiniBatches };
}
Vector<Ref<MLPPMatrix>> MLPPUtilitiesOld::create_mini_batchesm(const Ref<MLPPMatrix> &input_set, int n_mini_batch) {
Size2i size = input_set->size();
int n = size.y;
int mini_batch_element_count = n / n_mini_batch;
Ref<MLPPVector> row_tmp;
row_tmp.instance();
row_tmp->resize(size.x);
Vector<Ref<MLPPMatrix>> input_mini_batches;
// Creating the mini-batches
for (int i = 0; i < n_mini_batch; i++) {
int mini_batch_start_offset = n_mini_batch * i;
Ref<MLPPMatrix> current_input_set;
current_input_set.instance();
current_input_set->resize(Size2i(size.x, mini_batch_element_count));
for (int j = 0; j < mini_batch_element_count; j++) {
input_set->get_row_into_mlpp_vector(mini_batch_start_offset + j, row_tmp);
current_input_set->set_row_mlpp_vector(j, row_tmp);
}
input_mini_batches.push_back(current_input_set);
}
/* Don't think this can ever happen, todo double check
if (real_t(n) / real_t(n_mini_batch) - int(n / n_mini_batch) != 0) {
for (int i = 0; i < n - n / n_mini_batch * n_mini_batch; i++) {
inputMiniBatches[n_mini_batch - 1].push_back(inputSet[n_mini_batch * n_mini_batch + i]);
}
}
*/
return input_mini_batches;
}
MLPPUtilitiesOld::CreateMiniBatchMVBatch MLPPUtilitiesOld::create_mini_batchesmv(const Ref<MLPPMatrix> &input_set, const Ref<MLPPVector> &output_set, int n_mini_batch) {
Size2i size = input_set->size();
int n = size.y;
int mini_batch_element_count = n / n_mini_batch;
Ref<MLPPVector> row_tmp;
row_tmp.instance();
row_tmp->resize(size.x);
CreateMiniBatchMVBatch ret;
for (int i = 0; i < n_mini_batch; i++) {
int mini_batch_start_offset = n_mini_batch * i;
Ref<MLPPMatrix> current_input_set;
current_input_set.instance();
current_input_set->resize(Size2i(size.x, mini_batch_element_count));
Ref<MLPPVector> current_output_set;
current_output_set.instance();
current_output_set->resize(mini_batch_element_count);
for (int j = 0; j < mini_batch_element_count; j++) {
int main_indx = mini_batch_start_offset + j;
input_set->get_row_into_mlpp_vector(main_indx, row_tmp);
current_input_set->set_row_mlpp_vector(j, row_tmp);
current_output_set->set_element(j, output_set->get_element(j));
}
ret.input_sets.push_back(current_input_set);
ret.output_sets.push_back(current_output_set);
}
/* Don't think this can ever happen, todo double check
if (real_t(n) / real_t(n_mini_batch) - int(n / n_mini_batch) != 0) {
for (int i = 0; i < n - n / n_mini_batch * n_mini_batch; i++) {
inputMiniBatches[n_mini_batch - 1].push_back(inputSet[n / n_mini_batch * n_mini_batch + i]);
outputMiniBatches[n_mini_batch - 1].push_back(outputSet[n / n_mini_batch * n_mini_batch + i]);
}
}
*/
return ret;
}
MLPPUtilitiesOld::CreateMiniBatchMMBatch MLPPUtilitiesOld::create_mini_batchesmm(const Ref<MLPPMatrix> &input_set, const Ref<MLPPMatrix> &output_set, int n_mini_batch) {
Size2i input_set_size = input_set->size();
Size2i output_set_size = output_set->size();
int n = input_set_size.y;
int mini_batch_element_count = n / n_mini_batch;
Ref<MLPPVector> input_row_tmp;
input_row_tmp.instance();
input_row_tmp->resize(input_set_size.x);
Ref<MLPPVector> output_row_tmp;
output_row_tmp.instance();
output_row_tmp->resize(output_set_size.x);
CreateMiniBatchMMBatch ret;
for (int i = 0; i < n_mini_batch; i++) {
int mini_batch_start_offset = n_mini_batch * i;
Ref<MLPPMatrix> current_input_set;
current_input_set.instance();
current_input_set->resize(Size2i(input_set_size.x, mini_batch_element_count));
Ref<MLPPMatrix> current_output_set;
current_output_set.instance();
current_output_set->resize(Size2i(output_set_size.x, mini_batch_element_count));
for (int j = 0; j < mini_batch_element_count; j++) {
int main_indx = mini_batch_start_offset + j;
input_set->get_row_into_mlpp_vector(main_indx, input_row_tmp);
current_input_set->set_row_mlpp_vector(j, input_row_tmp);
output_set->get_row_into_mlpp_vector(main_indx, output_row_tmp);
current_output_set->set_row_mlpp_vector(j, output_row_tmp);
}
ret.input_sets.push_back(current_input_set);
ret.output_sets.push_back(current_output_set);
}
/* Don't think this can ever happen, todo double check
if (real_t(n) / real_t(n_mini_batch) - int(n / n_mini_batch) != 0) {
for (int i = 0; i < n - n / n_mini_batch * n_mini_batch; i++) {
inputMiniBatches[n_mini_batch - 1].push_back(inputSet[n / n_mini_batch * n_mini_batch + i]);
}
}
*/
return ret;
}
Array MLPPUtilitiesOld::create_mini_batchesm_bind(const Ref<MLPPMatrix> &input_set, int n_mini_batch) {
Vector<Ref<MLPPMatrix>> batches = create_mini_batchesm(input_set, n_mini_batch);
Array ret;
for (int i = 0; i < batches.size(); ++i) {
ret.push_back(batches[i].get_ref_ptr());
}
return ret;
}
Array MLPPUtilitiesOld::create_mini_batchesmv_bind(const Ref<MLPPMatrix> &input_set, const Ref<MLPPVector> &output_set, int n_mini_batch) {
CreateMiniBatchMVBatch batches = create_mini_batchesmv(input_set, output_set, n_mini_batch);
Array inputs;
Array outputs;
for (int i = 0; i < batches.input_sets.size(); ++i) {
inputs.push_back(batches.input_sets[i].get_ref_ptr());
outputs.push_back(batches.output_sets[i].get_ref_ptr());
}
Array ret;
ret.push_back(inputs);
ret.push_back(outputs);
return ret;
}
Array MLPPUtilitiesOld::create_mini_batchesmm_bind(const Ref<MLPPMatrix> &input_set, const Ref<MLPPMatrix> &output_set, int n_mini_batch) {
CreateMiniBatchMMBatch batches = create_mini_batchesmm(input_set, output_set, n_mini_batch);
Array inputs;
Array outputs;
for (int i = 0; i < batches.input_sets.size(); ++i) {
inputs.push_back(batches.input_sets[i].get_ref_ptr());
outputs.push_back(batches.output_sets[i].get_ref_ptr());
}
Array ret;
ret.push_back(inputs);
ret.push_back(outputs);
return ret;
}
std::tuple<real_t, real_t, real_t, real_t> MLPPUtilitiesOld::TF_PN(std::vector<real_t> y_hat, std::vector<real_t> y) {
real_t TP = 0;
real_t FP = 0;

View File

@ -8,22 +8,13 @@
// Created by Marc Melikyan on 1/16/21.
//
#include "core/containers/vector.h"
#include "core/math/math_defs.h"
#include "core/string/ustring.h"
#include "core/variant/variant.h"
#include "core/object/reference.h"
#include "../lin_alg/mlpp_matrix.h"
#include "../lin_alg/mlpp_vector.h"
#include <string>
#include <tuple>
#include <vector>
class MLPPUtilitiesOld {
public:
// Weight Init
static std::vector<real_t> weightInitialization(int n, std::string type = "Default");
@ -32,30 +23,10 @@ public:
static std::vector<std::vector<real_t>> weightInitialization(int n, int m, std::string type = "Default");
static std::vector<real_t> biasInitialization(int n);
enum WeightDistributionType {
WEIGHT_DISTRIBUTION_TYPE_DEFAULT = 0,
WEIGHT_DISTRIBUTION_TYPE_XAVIER_NORMAL,
WEIGHT_DISTRIBUTION_TYPE_XAVIER_UNIFORM,
WEIGHT_DISTRIBUTION_TYPE_HE_NORMAL,
WEIGHT_DISTRIBUTION_TYPE_HE_UNIFORM,
WEIGHT_DISTRIBUTION_TYPE_LE_CUN_NORMAL,
WEIGHT_DISTRIBUTION_TYPE_LE_CUN_UNIFORM,
WEIGHT_DISTRIBUTION_TYPE_UNIFORM,
};
void weight_initializationv(Ref<MLPPVector> weights, WeightDistributionType type = WEIGHT_DISTRIBUTION_TYPE_DEFAULT);
void weight_initializationm(Ref<MLPPMatrix> weights, WeightDistributionType type = WEIGHT_DISTRIBUTION_TYPE_DEFAULT);
real_t bias_initializationr();
void bias_initializationv(Ref<MLPPVector> z);
// Cost/Performance related Functions
real_t performance(std::vector<real_t> y_hat, std::vector<real_t> y);
real_t performance(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y);
real_t performance_vec(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &output_set);
real_t performance_mat(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
real_t performance_pool_int_array_vec(PoolIntArray y_hat, const Ref<MLPPVector> &output_set);
// Parameter Saving Functions
void saveParameters(std::string fileName, std::vector<real_t> weights, real_t bias, bool app = false, int layer = -1);
void saveParameters(std::string fileName, std::vector<real_t> weights, std::vector<real_t> initial, real_t bias, bool app = false, int layer = -1);
@ -66,42 +37,18 @@ public:
static void UI(std::vector<real_t> weights, std::vector<real_t> initial, real_t bias);
static void UI(std::vector<std::vector<real_t>> weights, std::vector<real_t> bias);
static void print_ui_vb(Ref<MLPPVector> weights, real_t bias);
static void print_ui_vib(Ref<MLPPVector> weights, Ref<MLPPVector> initial, real_t bias);
static void print_ui_mb(Ref<MLPPMatrix> weights, Ref<MLPPVector> bias);
static void CostInfo(int epoch, real_t cost_prev, real_t Cost);
static void cost_info(int epoch, real_t cost_prev, real_t cost);
static std::vector<std::vector<std::vector<real_t>>> createMiniBatches(std::vector<std::vector<real_t>> inputSet, int n_mini_batch);
static std::tuple<std::vector<std::vector<std::vector<real_t>>>, std::vector<std::vector<real_t>>> createMiniBatches(std::vector<std::vector<real_t>> inputSet, std::vector<real_t> outputSet, int n_mini_batch);
static std::tuple<std::vector<std::vector<std::vector<real_t>>>, std::vector<std::vector<std::vector<real_t>>>> createMiniBatches(std::vector<std::vector<real_t>> inputSet, std::vector<std::vector<real_t>> outputSet, int n_mini_batch);
struct CreateMiniBatchMVBatch {
Vector<Ref<MLPPMatrix>> input_sets;
Vector<Ref<MLPPVector>> output_sets;
};
struct CreateMiniBatchMMBatch {
Vector<Ref<MLPPMatrix>> input_sets;
Vector<Ref<MLPPMatrix>> output_sets;
};
static Vector<Ref<MLPPMatrix>> create_mini_batchesm(const Ref<MLPPMatrix> &input_set, int n_mini_batch);
static CreateMiniBatchMVBatch create_mini_batchesmv(const Ref<MLPPMatrix> &input_set, const Ref<MLPPVector> &output_set, int n_mini_batch);
static CreateMiniBatchMMBatch create_mini_batchesmm(const Ref<MLPPMatrix> &input_set, const Ref<MLPPMatrix> &output_set, int n_mini_batch);
Array create_mini_batchesm_bind(const Ref<MLPPMatrix> &input_set, int n_mini_batch);
Array create_mini_batchesmv_bind(const Ref<MLPPMatrix> &input_set, const Ref<MLPPVector> &output_set, int n_mini_batch);
Array create_mini_batchesmm_bind(const Ref<MLPPMatrix> &input_set, const Ref<MLPPMatrix> &output_set, int n_mini_batch);
// F1 score, Precision/Recall, TP, FP, TN, FN, etc.
std::tuple<real_t, real_t, real_t, real_t> TF_PN(std::vector<real_t> y_hat, std::vector<real_t> y); //TF_PN = "True", "False", "Positive", "Negative"
real_t recall(std::vector<real_t> y_hat, std::vector<real_t> y);
real_t precision(std::vector<real_t> y_hat, std::vector<real_t> y);
real_t accuracy(std::vector<real_t> y_hat, std::vector<real_t> y);
real_t f1_score(std::vector<real_t> y_hat, std::vector<real_t> y);
};
#endif /* Utilities_hpp */