Now MLPPMultinomialNB also uses engine classes.

This commit is contained in:
Relintai 2023-02-14 12:08:49 +01:00
parent 999d55b667
commit 7fb1827630
3 changed files with 130 additions and 59 deletions

View File

@ -5,11 +5,12 @@
// //
#include "multinomial_nb.h" #include "multinomial_nb.h"
#include "core/containers/local_vector.h"
#include "../lin_alg/lin_alg.h" #include "../lin_alg/lin_alg.h"
#include "../utilities/utilities.h" #include "../utilities/utilities.h"
#include <algorithm>
#include <iostream>
#include <random> #include <random>
/* /*
@ -41,38 +42,72 @@ void MLPPMultinomialNB::set_class_num(const real_t val) {
} }
*/ */
std::vector<real_t> MLPPMultinomialNB::model_set_test(std::vector<std::vector<real_t>> X) { Ref<MLPPVector> MLPPMultinomialNB::model_set_test(const Ref<MLPPMatrix> &X) {
ERR_FAIL_COND_V(!_initialized, std::vector<real_t>()); ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
std::vector<real_t> y_hat; Size2i x_size = X->size();
for (uint32_t i = 0; i < X.size(); i++) {
y_hat.push_back(model_test(X[i])); Ref<MLPPVector> x_row_tmp;
x_row_tmp.instance();
x_row_tmp->resize(x_size.x);
Ref<MLPPVector> y_hat;
y_hat.instance();
y_hat->resize(x_size.y);
for (int i = 0; i < x_size.y; i++) {
X->get_row_into_mlpp_vector(i, x_row_tmp);
y_hat->set_element(i, model_test(x_row_tmp));
} }
return y_hat; return y_hat;
} }
real_t MLPPMultinomialNB::model_test(std::vector<real_t> x) { real_t MLPPMultinomialNB::model_test(const Ref<MLPPVector> &x) {
ERR_FAIL_COND_V(!_initialized, 0); ERR_FAIL_COND_V(!_initialized, 0);
real_t score[_class_num]; int x_size = x->size();
LocalVector<real_t> score;
score.resize(_class_num);
compute_theta(); compute_theta();
for (uint32_t j = 0; j < x.size(); j++) { int vocab_size = _vocab->size();
for (uint32_t k = 0; k < _vocab.size(); k++) {
if (x[j] == _vocab[k]) { for (int j = 0; j < x_size; j++) {
for (int k = 0; k < vocab_size; k++) {
real_t x_j = x->get_element(j);
real_t vocab_k = _vocab->get_element(k);
if (Math::is_equal_approx(x_j, vocab_k)) {
for (int p = _class_num - 1; p >= 0; p--) { for (int p = _class_num - 1; p >= 0; p--) {
score[p] += std::log(_theta[p][_vocab[k]]); real_t theta_p_k = _theta[p][vocab_k];
score[p] += Math::log(theta_p_k);
} }
} }
} }
} }
for (uint32_t i = 0; i < _priors.size(); i++) { for (int i = 0; i < _priors->size(); i++) {
score[i] += std::log(_priors[i]); score[i] += std::log(_priors->get_element(i));
} }
return std::distance(score, std::max_element(score, score + sizeof(score) / sizeof(real_t))); int max_index = 0;
real_t max_element = score[0];
for (uint32_t i = 1; i < score.size(); ++i) {
real_t si = score[i];
if (si > max_element) {
max_index = i;
max_element = si;
}
}
return max_index;
} }
real_t MLPPMultinomialNB::score() { real_t MLPPMultinomialNB::score() {
@ -80,7 +115,7 @@ real_t MLPPMultinomialNB::score() {
MLPPUtilities util; MLPPUtilities util;
return util.performance(_y_hat, _output_set); return util.performance_vec(_y_hat, _output_set);
} }
bool MLPPMultinomialNB::is_initialized() { bool MLPPMultinomialNB::is_initialized() {
@ -96,12 +131,13 @@ void MLPPMultinomialNB::initialize() {
_initialized = true; _initialized = true;
} }
MLPPMultinomialNB::MLPPMultinomialNB(std::vector<std::vector<real_t>> p_input_set, std::vector<real_t> p_output_set, int pclass_num) { MLPPMultinomialNB::MLPPMultinomialNB(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPVector> &p_output_set, int pclass_num) {
_input_set = p_input_set; _input_set = p_input_set;
_output_set = p_output_set; _output_set = p_output_set;
_class_num = pclass_num; _class_num = pclass_num;
_y_hat.resize(_output_set.size()); _y_hat.instance();
_y_hat->resize(_output_set->size());
_initialized = true; _initialized = true;
@ -118,22 +154,28 @@ void MLPPMultinomialNB::compute_theta() {
// Resizing theta for the sake of ease & proper access of the elements. // Resizing theta for the sake of ease & proper access of the elements.
_theta.resize(_class_num); _theta.resize(_class_num);
int vocab_size = _vocab->size();
// Setting all values in the hasmap by default to 0. // Setting all values in the hasmap by default to 0.
for (int i = _class_num - 1; i >= 0; i--) { for (int i = _class_num - 1; i >= 0; i--) {
for (uint32_t j = 0; j < _vocab.size(); j++) { for (int j = 0; j < vocab_size; j++) {
_theta[i][_vocab[j]] = 0; _theta.write[i][_vocab->get_element(j)] = 0;
} }
} }
for (uint32_t i = 0; i < _input_set.size(); i++) { Size2i input_set_size = _input_set->size();
for (uint32_t j = 0; j < _input_set[0].size(); j++) {
_theta[_output_set[i]][_input_set[i][j]]++; for (int i = 0; i < input_set_size.y; i++) {
for (int j = 0; j < input_set_size.x; j++) {
_theta.write[_output_set->get_element(i)][_input_set->get_element(i, j)]++;
} }
} }
for (uint32_t i = 0; i < _theta.size(); i++) { for (int i = 0; i < _theta.size(); i++) {
for (uint32_t j = 0; j < _theta[i].size(); j++) { uint32_t theta_i_size = _theta[i].size();
_theta[i][j] /= _priors[i] * _y_hat.size();
for (uint32_t j = 0; j < theta_i_size; j++) {
_theta.write[i][j] /= _priors->get_element(i) * _y_hat->size();
} }
} }
} }
@ -141,42 +183,64 @@ void MLPPMultinomialNB::compute_theta() {
void MLPPMultinomialNB::evaluate() { void MLPPMultinomialNB::evaluate() {
MLPPLinAlg alg; MLPPLinAlg alg;
for (uint32_t i = 0; i < _output_set.size(); i++) { int output_set_size = _output_set->size();
Size2i input_set_size = _input_set->size();
for (int i = 0; i < output_set_size; i++) {
// Pr(B | A) * Pr(A) // Pr(B | A) * Pr(A)
real_t score[_class_num]; LocalVector<real_t> score;
score.resize(_class_num);
// Easy computation of priors, i.e. Pr(C_k) // Easy computation of priors, i.e. Pr(C_k)
_priors.resize(_class_num); _priors->resize(_class_num);
for (uint32_t ii = 0; ii < _output_set.size(); ii++) { for (int ii = 0; ii < _output_set->size(); ii++) {
_priors[int(_output_set[ii])]++; int osii = static_cast<int>(_output_set->get_element(ii));
_priors->set_element(osii, _priors->get_element(osii) + 1);
} }
_priors = alg.scalarMultiply(real_t(1) / real_t(_output_set.size()), _priors); _priors = alg.scalar_multiplynv(real_t(1) / real_t(output_set_size), _priors);
// Evaluating Theta... // Evaluating Theta...
compute_theta(); compute_theta();
for (uint32_t j = 0; j < _input_set.size(); j++) { for (int j = 0; j < input_set_size.y; j++) {
for (uint32_t k = 0; k < _vocab.size(); k++) { for (int k = 0; k < _vocab->size(); k++) {
if (_input_set[i][j] == _vocab[k]) { real_t input_set_i_j = _input_set->get_element(i, j);
real_t vocab_k = _vocab->get_element(k);
if (Math::is_equal_approx(input_set_i_j, vocab_k)) {
real_t theta_i_k = _theta[i][vocab_k];
theta_i_k = Math::log(theta_i_k);
for (int p = _class_num - 1; p >= 0; p--) { for (int p = _class_num - 1; p >= 0; p--) {
score[p] += std::log(_theta[i][_vocab[k]]); score[p] += theta_i_k;
} }
} }
} }
} }
for (uint32_t ii = 0; ii < _priors.size(); ii++) { int priors_size = _priors->size();
score[ii] += std::log(_priors[ii]);
score[ii] = exp(score[ii]);
}
for (int ii = 0; ii < 2; ii++) { for (int ii = 0; ii < priors_size; ii++) {
std::cout << score[ii] << std::endl; score[ii] += Math::log(_priors->get_element(ii));
score[ii] = Math::exp(score[ii]);
} }
// Assigning the traning example's y_hat to a class // Assigning the traning example's y_hat to a class
_y_hat[i] = std::distance(score, std::max_element(score, score + sizeof(score) / sizeof(real_t)));
int max_index = 0;
real_t max_element = score[0];
for (uint32_t ii = 1; ii < score.size(); ++ii) {
real_t si = score[ii];
if (si > max_element) {
max_index = ii;
max_element = si;
}
}
_y_hat->set_element(i, max_index);
} }
} }

View File

@ -8,6 +8,8 @@
// Created by Marc Melikyan on 1/17/21. // Created by Marc Melikyan on 1/17/21.
// //
#include "core/containers/hash_map.h"
#include "core/containers/vector.h"
#include "core/math/math_defs.h" #include "core/math/math_defs.h"
#include "core/object/reference.h" #include "core/object/reference.h"
@ -15,9 +17,6 @@
#include "../lin_alg/mlpp_matrix.h" #include "../lin_alg/mlpp_matrix.h"
#include "../lin_alg/mlpp_vector.h" #include "../lin_alg/mlpp_vector.h"
#include <map>
#include <vector>
class MLPPMultinomialNB : public Reference { class MLPPMultinomialNB : public Reference {
GDCLASS(MLPPMultinomialNB, Reference); GDCLASS(MLPPMultinomialNB, Reference);
@ -26,20 +25,20 @@ public:
void set_input_set(const Ref<MLPPMatrix> &val); void set_input_set(const Ref<MLPPMatrix> &val);
Ref<MLPPVector> get_output_set(); Ref<MLPPVector> get_output_set();
void set_output_set(const Ref<MLPPMatrix> &val); void set_output_set(const Ref<MLPPVector> &val);
real_t get_class_num(); real_t get_class_num();
void set_class_num(const real_t val); void set_class_num(const real_t val);
std::vector<real_t> model_set_test(std::vector<std::vector<real_t>> X); Ref<MLPPVector> model_set_test(const Ref<MLPPMatrix> &X);
real_t model_test(std::vector<real_t> x); real_t model_test(const Ref<MLPPVector> &x);
real_t score(); real_t score();
bool is_initialized(); bool is_initialized();
void initialize(); void initialize();
MLPPMultinomialNB(std::vector<std::vector<real_t>> _input_set, std::vector<real_t> _output_set, int class_num); MLPPMultinomialNB(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPVector> &p_output_set, int class_num);
MLPPMultinomialNB(); MLPPMultinomialNB();
~MLPPMultinomialNB(); ~MLPPMultinomialNB();
@ -51,16 +50,16 @@ protected:
static void _bind_methods(); static void _bind_methods();
// Model Params // Model Params
std::vector<real_t> _priors; Ref<MLPPVector> _priors;
std::vector<std::map<real_t, int>> _theta; Vector<HashMap<real_t, int>> _theta;
std::vector<real_t> _vocab; Ref<MLPPVector> _vocab;
int _class_num; int _class_num;
// Datasets // Datasets
std::vector<std::vector<real_t>> _input_set; Ref<MLPPMatrix> _input_set;
std::vector<real_t> _output_set; Ref<MLPPVector> _output_set;
std::vector<real_t> _y_hat; Ref<MLPPVector> _y_hat;
bool _initialized; bool _initialized;
}; };

View File

@ -769,8 +769,16 @@ void MLPPTests::test_naive_bayes() {
MLPPMultinomialNBOld MNB_old(alg.transpose(inputSet), outputSet, 2); MLPPMultinomialNBOld MNB_old(alg.transpose(inputSet), outputSet, 2);
alg.printVector(MNB_old.modelSetTest(alg.transpose(inputSet))); alg.printVector(MNB_old.modelSetTest(alg.transpose(inputSet)));
MLPPMultinomialNB MNB(alg.transpose(inputSet), outputSet, 2); Ref<MLPPMatrix> input_set;
alg.printVector(MNB.model_set_test(alg.transpose(inputSet))); input_set.instance();
input_set->set_from_std_vectors(alg.transpose(inputSet));
Ref<MLPPVector> output_set;
output_set.instance();
output_set->set_from_std_vector(outputSet);
MLPPMultinomialNB MNB(input_set, output_set, 2);
PLOG_MSG(MNB.model_set_test(input_set)->to_string());
MLPPBernoulliNBOld BNBOld(alg.transpose(inputSet), outputSet); MLPPBernoulliNBOld BNBOld(alg.transpose(inputSet), outputSet);
alg.printVector(BNBOld.modelSetTest(alg.transpose(inputSet))); alg.printVector(BNBOld.modelSetTest(alg.transpose(inputSet)));