2023-12-30 00:41:59 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* multinomial_nb.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* PMLPP Machine Learning Library */
|
|
|
|
/* https://github.com/Relintai/pmlpp */
|
|
|
|
/*************************************************************************/
|
2023-12-30 00:43:39 +01:00
|
|
|
/* Copyright (c) 2023-present Péter Magyar. */
|
2023-12-30 00:41:59 +01:00
|
|
|
/* Copyright (c) 2022-2023 Marc Melikyan */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2023-01-23 21:13:26 +01:00
|
|
|
|
2023-01-24 18:12:23 +01:00
|
|
|
#include "multinomial_nb.h"
|
2023-02-14 12:08:49 +01:00
|
|
|
|
2024-01-25 13:42:45 +01:00
|
|
|
#ifdef USING_SFW
|
|
|
|
#include "sfw.h"
|
|
|
|
#else
|
2023-02-14 12:08:49 +01:00
|
|
|
#include "core/containers/local_vector.h"
|
2024-01-25 13:42:45 +01:00
|
|
|
#endif
|
2023-02-14 12:08:49 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
#include "../utilities/utilities.h"
|
2023-01-23 21:13:26 +01:00
|
|
|
|
|
|
|
#include <random>
|
|
|
|
|
2023-02-11 09:33:09 +01:00
|
|
|
/*
|
|
|
|
Ref<MLPPMatrix> MLPPMultinomialNB::get_input_set() {
|
|
|
|
return _input_set;
|
|
|
|
}
|
|
|
|
void MLPPMultinomialNB::set_input_set(const Ref<MLPPMatrix> &val) {
|
|
|
|
_input_set = val;
|
|
|
|
|
|
|
|
_initialized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<MLPPVector> MLPPMultinomialNB::get_output_set() {
|
|
|
|
return _output_set;
|
|
|
|
}
|
|
|
|
void MLPPMultinomialNB::set_output_set(const Ref<MLPPMatrix> &val) {
|
|
|
|
_output_set = val;
|
2023-01-24 19:20:18 +01:00
|
|
|
|
2023-02-11 09:33:09 +01:00
|
|
|
_initialized = false;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-11 09:33:09 +01:00
|
|
|
real_t MLPPMultinomialNB::get_class_num() {
|
|
|
|
return _class_num;
|
|
|
|
}
|
|
|
|
void MLPPMultinomialNB::set_class_num(const real_t val) {
|
|
|
|
_class_num = val;
|
|
|
|
|
|
|
|
_initialized = false;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
Ref<MLPPVector> MLPPMultinomialNB::model_set_test(const Ref<MLPPMatrix> &X) {
|
|
|
|
ERR_FAIL_COND_V(!_initialized, Ref<MLPPVector>());
|
|
|
|
|
|
|
|
Size2i x_size = X->size();
|
|
|
|
|
|
|
|
Ref<MLPPVector> x_row_tmp;
|
|
|
|
x_row_tmp.instance();
|
|
|
|
x_row_tmp->resize(x_size.x);
|
2023-02-11 09:33:09 +01:00
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
Ref<MLPPVector> y_hat;
|
|
|
|
y_hat.instance();
|
|
|
|
y_hat->resize(x_size.y);
|
|
|
|
|
|
|
|
for (int i = 0; i < x_size.y; i++) {
|
2023-04-29 15:07:30 +02:00
|
|
|
X->row_get_into_mlpp_vector(i, x_row_tmp);
|
2023-02-14 12:08:49 +01:00
|
|
|
|
2023-04-29 13:44:18 +02:00
|
|
|
y_hat->element_set(i, model_test(x_row_tmp));
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-02-14 12:08:49 +01:00
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
return y_hat;
|
|
|
|
}
|
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
real_t MLPPMultinomialNB::model_test(const Ref<MLPPVector> &x) {
|
2023-02-11 09:33:09 +01:00
|
|
|
ERR_FAIL_COND_V(!_initialized, 0);
|
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
int x_size = x->size();
|
|
|
|
|
|
|
|
LocalVector<real_t> score;
|
|
|
|
score.resize(_class_num);
|
2023-02-11 09:33:09 +01:00
|
|
|
|
|
|
|
compute_theta();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
int vocab_size = _vocab->size();
|
|
|
|
|
|
|
|
for (int j = 0; j < x_size; j++) {
|
|
|
|
for (int k = 0; k < vocab_size; k++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
real_t x_j = x->element_get(j);
|
|
|
|
real_t vocab_k = _vocab->element_get(k);
|
2023-02-14 12:08:49 +01:00
|
|
|
|
|
|
|
if (Math::is_equal_approx(x_j, vocab_k)) {
|
2023-02-11 09:33:09 +01:00
|
|
|
for (int p = _class_num - 1; p >= 0; p--) {
|
2023-02-14 12:08:49 +01:00
|
|
|
real_t theta_p_k = _theta[p][vocab_k];
|
|
|
|
|
|
|
|
score[p] += Math::log(theta_p_k);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
for (int i = 0; i < _priors->size(); i++) {
|
2023-04-29 20:10:49 +02:00
|
|
|
score[i] += Math::log(_priors->element_get(i));
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
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;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 13:01:16 +01:00
|
|
|
real_t MLPPMultinomialNB::score() {
|
2023-02-11 09:33:09 +01:00
|
|
|
ERR_FAIL_COND_V(!_initialized, 0);
|
|
|
|
|
2023-02-10 21:35:43 +01:00
|
|
|
MLPPUtilities util;
|
2023-02-11 09:33:09 +01:00
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
return util.performance_vec(_y_hat, _output_set);
|
2023-02-11 09:33:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MLPPMultinomialNB::is_initialized() {
|
|
|
|
return _initialized;
|
|
|
|
}
|
|
|
|
void MLPPMultinomialNB::initialize() {
|
|
|
|
if (_initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//ERR_FAIL_COND(!_input_set.is_valid() || !_output_set.is_valid());
|
|
|
|
|
|
|
|
_initialized = true;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
MLPPMultinomialNB::MLPPMultinomialNB(const Ref<MLPPMatrix> &p_input_set, const Ref<MLPPVector> &p_output_set, int pclass_num) {
|
2023-02-11 09:33:09 +01:00
|
|
|
_input_set = p_input_set;
|
|
|
|
_output_set = p_output_set;
|
|
|
|
_class_num = pclass_num;
|
|
|
|
|
2023-12-28 12:03:17 +01:00
|
|
|
_priors.instance();
|
|
|
|
_vocab.instance();
|
2023-02-14 12:08:49 +01:00
|
|
|
_y_hat.instance();
|
2023-12-28 12:03:17 +01:00
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
_y_hat->resize(_output_set->size());
|
2023-02-11 09:33:09 +01:00
|
|
|
|
|
|
|
_initialized = true;
|
|
|
|
|
|
|
|
evaluate();
|
|
|
|
}
|
|
|
|
|
|
|
|
MLPPMultinomialNB::MLPPMultinomialNB() {
|
|
|
|
_initialized = false;
|
|
|
|
}
|
|
|
|
MLPPMultinomialNB::~MLPPMultinomialNB() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void MLPPMultinomialNB::compute_theta() {
|
2023-01-24 19:00:54 +01:00
|
|
|
// Resizing theta for the sake of ease & proper access of the elements.
|
2023-02-11 09:33:09 +01:00
|
|
|
_theta.resize(_class_num);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
int vocab_size = _vocab->size();
|
|
|
|
|
2023-01-24 19:00:54 +01:00
|
|
|
// Setting all values in the hasmap by default to 0.
|
2023-02-11 09:33:09 +01:00
|
|
|
for (int i = _class_num - 1; i >= 0; i--) {
|
2023-02-14 12:08:49 +01:00
|
|
|
for (int j = 0; j < vocab_size; j++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
_theta.write[i][_vocab->element_get(j)] = 0;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
Size2i input_set_size = _input_set->size();
|
|
|
|
|
|
|
|
for (int i = 0; i < input_set_size.y; i++) {
|
|
|
|
for (int j = 0; j < input_set_size.x; j++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
_theta.write[_output_set->element_get(i)][_input_set->element_get(i, j)]++;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
for (int i = 0; i < _theta.size(); i++) {
|
|
|
|
uint32_t theta_i_size = _theta[i].size();
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < theta_i_size; j++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
_theta.write[i][j] /= _priors->element_get(i) * _y_hat->size();
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-11 09:33:09 +01:00
|
|
|
void MLPPMultinomialNB::evaluate() {
|
2023-02-14 12:08:49 +01:00
|
|
|
int output_set_size = _output_set->size();
|
|
|
|
Size2i input_set_size = _input_set->size();
|
|
|
|
|
|
|
|
for (int i = 0; i < output_set_size; i++) {
|
2023-01-24 19:00:54 +01:00
|
|
|
// Pr(B | A) * Pr(A)
|
2023-02-14 12:08:49 +01:00
|
|
|
LocalVector<real_t> score;
|
|
|
|
score.resize(_class_num);
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Easy computation of priors, i.e. Pr(C_k)
|
2023-02-14 12:08:49 +01:00
|
|
|
_priors->resize(_class_num);
|
|
|
|
for (int ii = 0; ii < _output_set->size(); ii++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
int osii = static_cast<int>(_output_set->element_get(ii));
|
|
|
|
_priors->element_set(osii, _priors->element_get(osii) + 1);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
2023-02-11 09:33:09 +01:00
|
|
|
|
2023-04-29 20:10:49 +02:00
|
|
|
_priors->scalar_multiply(real_t(1) / real_t(output_set_size));
|
2023-01-24 19:00:54 +01:00
|
|
|
|
|
|
|
// Evaluating Theta...
|
2023-02-11 09:33:09 +01:00
|
|
|
compute_theta();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
for (int j = 0; j < input_set_size.y; j++) {
|
|
|
|
for (int k = 0; k < _vocab->size(); k++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
real_t input_set_i_j = _input_set->element_get(i, j);
|
|
|
|
real_t vocab_k = _vocab->element_get(k);
|
2023-02-14 12:08:49 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2023-02-11 09:33:09 +01:00
|
|
|
for (int p = _class_num - 1; p >= 0; p--) {
|
2023-02-14 12:08:49 +01:00
|
|
|
score[p] += theta_i_k;
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
int priors_size = _priors->size();
|
2023-01-24 19:00:54 +01:00
|
|
|
|
2023-02-14 12:08:49 +01:00
|
|
|
for (int ii = 0; ii < priors_size; ii++) {
|
2023-04-29 13:44:18 +02:00
|
|
|
score[ii] += Math::log(_priors->element_get(ii));
|
2023-02-14 12:08:49 +01:00
|
|
|
score[ii] = Math::exp(score[ii]);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assigning the traning example's y_hat to a class
|
2023-02-14 12:08:49 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 13:44:18 +02:00
|
|
|
_y_hat->element_set(i, max_index);
|
2023-01-24 19:00:54 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-11 09:33:09 +01:00
|
|
|
|
|
|
|
void MLPPMultinomialNB::_bind_methods() {
|
|
|
|
/*
|
|
|
|
ClassDB::bind_method(D_METHOD("get_input_set"), &MLPPMultinomialNB::get_input_set);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_input_set", "val"), &MLPPMultinomialNB::set_input_set);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "input_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPMatrix"), "set_input_set", "get_input_set");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_output_set"), &MLPPMultinomialNB::get_output_set);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_output_set", "val"), &MLPPMultinomialNB::set_output_set);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "output_set", PROPERTY_HINT_RESOURCE_TYPE, "MLPPVector"), "set_output_set", "get_output_set");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_c"), &MLPPMultinomialNB::get_c);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_c", "val"), &MLPPMultinomialNB::set_c);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "c"), "set_c", "get_c");
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("model_set_test", "X"), &MLPPMultinomialNB::model_set_test);
|
|
|
|
ClassDB::bind_method(D_METHOD("model_test", "x"), &MLPPMultinomialNB::model_test);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("gradient_descent", "learning_rate", "max_epoch", "ui"), &MLPPMultinomialNB::gradient_descent, false);
|
|
|
|
ClassDB::bind_method(D_METHOD("sgd", "learning_rate", "max_epoch", "ui"), &MLPPMultinomialNB::sgd, false);
|
|
|
|
ClassDB::bind_method(D_METHOD("mbgd", "learning_rate", "max_epoch", "mini_batch_size", "ui"), &MLPPMultinomialNB::mbgd, false);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("score"), &MLPPMultinomialNB::score);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("save", "file_name"), &MLPPMultinomialNB::save);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("is_initialized"), &MLPPMultinomialNB::is_initialized);
|
|
|
|
ClassDB::bind_method(D_METHOD("initialize"), &MLPPMultinomialNB::initialize);
|
|
|
|
*/
|
|
|
|
}
|