Removed new things from MLPPCostOld.

This commit is contained in:
Relintai 2023-02-13 18:29:05 +01:00
parent 25b81d24ff
commit 0a51b845f7
2 changed files with 1 additions and 946 deletions

View File

@ -10,761 +10,6 @@
#include <cmath>
#include <iostream>
real_t MLPPCostOld::msev(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_size; ++i) {
sum += (y_hat_ptr[i] - y_ptr[i]) * (y_hat_ptr[i] - y_ptr[i]);
}
return sum / 2 * y_hat_size;
}
real_t MLPPCostOld::msem(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_data_size; ++i) {
sum += (y_hat_ptr[i] - y_ptr[i]) * (y_hat_ptr[i] - y_ptr[i]);
}
return sum / 2.0 * static_cast<real_t>(y_hat_data_size);
}
Ref<MLPPVector> MLPPCostOld::mse_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
MLPPLinAlg alg;
return alg.subtractionnv(y_hat, y);
}
Ref<MLPPMatrix> MLPPCostOld::mse_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
MLPPLinAlg alg;
return alg.subtractionm(y_hat, y);
}
real_t MLPPCostOld::rmsev(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_size; ++i) {
sum += (y_hat_ptr[i] - y_ptr[i]) * (y_hat_ptr[i] - y_ptr[i]);
}
return Math::sqrt(sum / static_cast<real_t>(y_hat_size));
}
real_t MLPPCostOld::rmsem(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_data_size; ++i) {
sum += (y_hat_ptr[i] - y_ptr[i]) * (y_hat_ptr[i] - y_ptr[i]);
}
return Math::sqrt(sum / static_cast<real_t>(y_hat->size().y));
}
Ref<MLPPVector> MLPPCostOld::rmse_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
MLPPLinAlg alg;
return alg.scalar_multiplynv(1 / (2.0 * Math::sqrt(msev(y_hat, y))), mse_derivv(y_hat, y));
}
Ref<MLPPMatrix> MLPPCostOld::rmse_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
MLPPLinAlg alg;
return alg.scalar_multiplym(1 / (2.0 / Math::sqrt(msem(y_hat, y))), mse_derivm(y_hat, y));
}
real_t MLPPCostOld::maev(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_size; i++) {
sum += ABS((y_hat_ptr[i] - y_ptr[i]));
}
return sum / static_cast<real_t>(y_hat_size);
}
real_t MLPPCostOld::maem(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_data_size; ++i) {
sum += ABS((y_hat_ptr[i] - y_ptr[i]));
}
return sum / static_cast<real_t>(y_hat_data_size);
}
Ref<MLPPVector> MLPPCostOld::mae_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
const real_t *y_hat_ptr = y_hat->ptr();
Ref<MLPPVector> deriv;
deriv.instance();
deriv->resize(y_hat_size);
real_t *deriv_ptr = deriv->ptrw();
for (int i = 0; i < y_hat_size; ++i) {
int y_hat_ptr_i = y_hat_ptr[i];
if (y_hat_ptr_i < 0) {
deriv_ptr[i] = -1;
} else if (y_hat_ptr_i == 0) {
deriv_ptr[i] = 0;
} else {
deriv_ptr[i] = 1;
}
}
return deriv;
}
Ref<MLPPMatrix> MLPPCostOld::mae_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
const real_t *y_hat_ptr = y_hat->ptr();
Ref<MLPPMatrix> deriv;
deriv.instance();
deriv->resize(y_hat->size());
real_t *deriv_ptr = deriv->ptrw();
for (int i = 0; i < y_hat_data_size; ++i) {
int y_hat_ptr_i = y_hat_ptr[i];
if (y_hat_ptr_i < 0) {
deriv_ptr[i] = -1;
} else if (y_hat_ptr_i == 0) {
deriv_ptr[i] = 0;
} else {
deriv_ptr[i] = 1;
}
}
return deriv;
}
real_t MLPPCostOld::mbev(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_size; ++i) {
sum += (y_hat_ptr[i] - y_ptr[i]);
}
return sum / static_cast<real_t>(y_hat_size);
}
real_t MLPPCostOld::mbem(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_data_size; ++i) {
sum += (y_hat_ptr[i] - y_ptr[i]);
}
return sum / static_cast<real_t>(y_hat_data_size);
}
Ref<MLPPVector> MLPPCostOld::mbe_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
MLPPLinAlg alg;
return alg.onevecv(y_hat->size());
}
Ref<MLPPMatrix> MLPPCostOld::mbe_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
MLPPLinAlg alg;
return alg.onematm(y_hat->size().x, y_hat->size().y);
}
// Classification Costs
real_t MLPPCostOld::log_lossv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
real_t eps = 1e-8;
for (int i = 0; i < y_hat_size; ++i) {
sum += -(y_ptr[i] * Math::log(y_hat_ptr[i] + eps) + (1 - y_ptr[i]) * Math::log(1 - y_hat_ptr[i] + eps));
}
return sum / static_cast<real_t>(y_hat_size);
}
real_t MLPPCostOld::log_lossm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
real_t eps = 1e-8;
for (int i = 0; i < y_hat_data_size; ++i) {
sum += -(y_ptr[i] * Math::log(y_hat_ptr[i] + eps) + (1 - y_ptr[i]) * Math::log(1 - y_hat_ptr[i] + eps));
}
return sum / static_cast<real_t>(y_hat_data_size);
}
Ref<MLPPVector> MLPPCostOld::log_loss_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
MLPPLinAlg alg;
return alg.additionnv(
alg.scalar_multiplynv(-1, alg.element_wise_division(y, y_hat)),
alg.element_wise_division(alg.scalar_multiplynv(-1, alg.scalar_addnv(-1, y)), alg.scalar_multiplynv(-1, alg.scalar_addnv(-1, y_hat))));
}
Ref<MLPPMatrix> MLPPCostOld::log_loss_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
MLPPLinAlg alg;
return alg.additionm(
alg.scalar_multiplym(-1, alg.element_wise_divisionm(y, y_hat)),
alg.element_wise_divisionm(alg.scalar_multiplym(-1, alg.scalar_addm(-1, y)), alg.scalar_multiplym(-1, alg.scalar_addm(-1, y_hat))));
}
real_t MLPPCostOld::cross_entropyv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_size; ++i) {
sum += y_ptr[i] * Math::log(y_hat_ptr[i]);
}
return -1 * sum;
}
real_t MLPPCostOld::cross_entropym(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_data_size; ++i) {
sum += y_ptr[i] * Math::log(y_hat_ptr[i]);
}
return -1 * sum;
}
Ref<MLPPVector> MLPPCostOld::cross_entropy_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
MLPPLinAlg alg;
return alg.scalar_multiplynv(-1, alg.element_wise_division(y, y_hat));
}
Ref<MLPPMatrix> MLPPCostOld::cross_entropy_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
MLPPLinAlg alg;
return alg.scalar_multiplym(-1, alg.element_wise_divisionm(y, y_hat));
}
real_t MLPPCostOld::huber_lossv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y, real_t delta) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
MLPPLinAlg alg;
real_t sum = 0;
for (int i = 0; i < y_hat_size; ++i) {
if (ABS(y_ptr[i] - y_hat_ptr[i]) <= delta) {
sum += (y_ptr[i] - y_hat_ptr[i]) * (y_ptr[i] - y_hat_ptr[i]);
} else {
sum += 2 * delta * ABS(y_ptr[i] - y_hat_ptr[i]) - delta * delta;
}
}
return sum;
}
real_t MLPPCostOld::huber_lossm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y, real_t delta) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
MLPPLinAlg alg;
real_t sum = 0;
for (int i = 0; i < y_hat_data_size; ++i) {
if (ABS(y_ptr[i] - y_hat_ptr[i]) <= delta) {
sum += (y_ptr[i] - y_hat_ptr[i]) * (y_ptr[i] - y_hat_ptr[i]);
} else {
sum += 2 * delta * ABS(y_ptr[i] - y_hat_ptr[i]) - delta * delta;
}
}
return sum;
}
Ref<MLPPVector> MLPPCostOld::huber_loss_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y, real_t delta) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
MLPPLinAlg alg;
Ref<MLPPVector> deriv;
deriv.instance();
deriv->resize(y_hat->size());
real_t *deriv_ptr = deriv->ptrw();
for (int i = 0; i < y_hat_size; ++i) {
if (ABS(y_ptr[i] - y_hat_ptr[i]) <= delta) {
deriv_ptr[i] = (-(y_ptr[i] - y_hat_ptr[i]));
} else {
if (y_hat_ptr[i] > 0 || y_hat_ptr[i] < 0) {
deriv_ptr[i] = (2 * delta * (y_hat_ptr[i] / ABS(y_hat_ptr[i])));
} else {
deriv_ptr[i] = (0);
}
}
}
return deriv;
}
Ref<MLPPMatrix> MLPPCostOld::huber_loss_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y, real_t delta) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
MLPPLinAlg alg;
Ref<MLPPMatrix> deriv;
deriv.instance();
deriv->resize(y_hat->size());
real_t *deriv_ptr = deriv->ptrw();
for (int i = 0; i < y_hat_data_size; ++i) {
if (ABS(y_ptr[i] - y_hat_ptr[i]) <= delta) {
deriv_ptr[i] = (-(y_ptr[i] - y_hat_ptr[i]));
} else {
if (y_hat_ptr[i] > 0 || y_hat_ptr[i] < 0) {
deriv_ptr[i] = (2 * delta * (y_hat_ptr[i] / ABS(y_hat_ptr[i])));
} else {
deriv_ptr[i] = (0);
}
}
}
return deriv;
}
real_t MLPPCostOld::hinge_lossv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_size; ++i) {
sum += MAX(0, 1 - y_ptr[i] * y_hat_ptr[i]);
}
return sum / static_cast<real_t>(y_hat_size);
}
real_t MLPPCostOld::hinge_lossm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_data_size; ++i) {
sum += MAX(0, 1 - y_ptr[i] * y_hat_ptr[i]);
}
return sum / static_cast<real_t>(y_hat_data_size);
}
Ref<MLPPVector> MLPPCostOld::hinge_loss_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
Ref<MLPPVector> deriv;
deriv.instance();
deriv->resize(y_hat->size());
real_t *deriv_ptr = deriv->ptrw();
for (int i = 0; i < y_hat_size; ++i) {
if (1 - y_ptr[i] * y_hat_ptr[i] > 0) {
deriv_ptr[i] = -y_ptr[i];
} else {
deriv_ptr[i] = 0;
}
}
return deriv;
}
Ref<MLPPMatrix> MLPPCostOld::hinge_loss_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
Ref<MLPPMatrix> deriv;
deriv.instance();
deriv->resize(y_hat->size());
real_t *deriv_ptr = deriv->ptrw();
for (int i = 0; i < y_hat_data_size; ++i) {
if (1 - y_ptr[i] * y_hat_ptr[i] > 0) {
deriv_ptr[i] = -y_ptr[i];
} else {
deriv_ptr[i] = 0;
}
}
return deriv;
}
real_t MLPPCostOld::hinge_losswv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y, const Ref<MLPPVector> &weights, real_t C) {
MLPPLinAlg alg;
MLPPReg regularization;
return C * hinge_lossv(y_hat, y) + regularization.reg_termv(weights, 1, 0, MLPPReg::REGULARIZATION_TYPE_RIDGE);
}
real_t MLPPCostOld::hinge_losswm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y, const Ref<MLPPMatrix> &weights, real_t C) {
MLPPLinAlg alg;
MLPPReg regularization;
return C * hinge_lossm(y_hat, y) + regularization.reg_termv(weights, 1, 0, MLPPReg::REGULARIZATION_TYPE_RIDGE);
}
Ref<MLPPVector> MLPPCostOld::hinge_loss_derivwv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y, real_t C) {
MLPPLinAlg alg;
MLPPReg regularization;
return alg.scalar_multiplynv(C, hinge_loss_derivv(y_hat, y));
}
Ref<MLPPMatrix> MLPPCostOld::hinge_loss_derivwm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y, real_t C) {
MLPPLinAlg alg;
MLPPReg regularization;
return alg.scalar_multiplym(C, hinge_loss_derivm(y_hat, y));
}
real_t MLPPCostOld::wasserstein_lossv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
int y_hat_size = y_hat->size();
ERR_FAIL_COND_V(y_hat_size != y->size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_size; ++i) {
sum += y_hat_ptr[i] * y_ptr[i];
}
return -sum / static_cast<real_t>(y_hat_size);
}
real_t MLPPCostOld::wasserstein_lossm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
int y_hat_data_size = y_hat->data_size();
ERR_FAIL_COND_V(y_hat_data_size != y->data_size(), 0);
const real_t *y_hat_ptr = y_hat->ptr();
const real_t *y_ptr = y->ptr();
real_t sum = 0;
for (int i = 0; i < y_hat_data_size; ++i) {
sum += y_hat_ptr[i] * y_ptr[i];
}
return -sum / static_cast<real_t>(y_hat_data_size);
}
Ref<MLPPVector> MLPPCostOld::wasserstein_loss_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
MLPPLinAlg alg;
return alg.scalar_multiplynv(-1, y); // Simple.
}
Ref<MLPPMatrix> MLPPCostOld::wasserstein_loss_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
MLPPLinAlg alg;
return alg.scalar_multiplym(-1, y); // Simple.
}
real_t MLPPCostOld::dual_form_svm(const Ref<MLPPVector> &alpha, const Ref<MLPPMatrix> &X, const Ref<MLPPVector> &y) {
MLPPLinAlg alg;
Ref<MLPPMatrix> Y = alg.diagm(y); // Y is a diagnoal matrix. Y[i][j] = y[i] if i = i, else Y[i][j] = 0. Yt = Y.
Ref<MLPPMatrix> K = alg.matmultm(X, alg.transposem(X)); // TO DO: DON'T forget to add non-linear kernelizations.
Ref<MLPPMatrix> Q = alg.matmultm(alg.matmultm(alg.transposem(Y), K), Y);
Ref<MLPPMatrix> alpha_m;
alpha_m.instance();
alpha_m->resize(Size2i(alpha->size(), 1));
alpha_m->set_row_mlpp_vector(0, alpha);
Ref<MLPPMatrix> alpha_m_res = alg.matmultm(alg.matmultm(alpha_m, Q), alg.transposem(alpha_m));
real_t alphaQ = alpha_m_res->get_element(0, 0);
Ref<MLPPVector> one = alg.onevecv(alpha->size());
return -alg.dotv(one, alpha) + 0.5 * alphaQ;
}
Ref<MLPPVector> MLPPCostOld::dual_form_svm_deriv(const Ref<MLPPVector> &alpha, const Ref<MLPPMatrix> &X, const Ref<MLPPVector> &y) {
MLPPLinAlg alg;
Ref<MLPPMatrix> Y = alg.diagm(y); // Y is a diagnoal matrix. Y[i][j] = y[i] if i = i, else Y[i][j] = 0. Yt = Y.
Ref<MLPPMatrix> K = alg.matmultm(X, alg.transposem(X)); // TO DO: DON'T forget to add non-linear kernelizations.
Ref<MLPPMatrix> Q = alg.matmultm(alg.matmultm(alg.transposem(Y), K), Y);
Ref<MLPPVector> alphaQDeriv = alg.mat_vec_multv(Q, alpha);
Ref<MLPPVector> one = alg.onevecv(alpha->size());
return alg.subtractionm(alphaQDeriv, one);
}
MLPPCostOld::VectorCostFunctionPointer MLPPCostOld::get_cost_function_ptr_normal_vector(const MLPPCostOld::CostTypes cost) {
switch (cost) {
case COST_TYPE_MSE:
return &MLPPCostOld::msev;
case COST_TYPE_RMSE:
return &MLPPCostOld::rmsev;
case COST_TYPE_MAE:
return &MLPPCostOld::maev;
case COST_TYPE_MBE:
return &MLPPCostOld::mbev;
case COST_TYPE_LOGISTIC_LOSS:
return &MLPPCostOld::log_lossv;
case COST_TYPE_CROSS_ENTROPY:
return &MLPPCostOld::cross_entropyv;
case COST_TYPE_HINGE_LOSS:
return &MLPPCostOld::hinge_lossv;
case COST_TYPE_WASSERSTEIN_LOSS:
return &MLPPCostOld::wasserstein_lossv;
default:
return NULL;
}
}
MLPPCostOld::MatrixCostFunctionPointer MLPPCostOld::get_cost_function_ptr_normal_matrix(const MLPPCostOld::CostTypes cost) {
switch (cost) {
case COST_TYPE_MSE:
return &MLPPCostOld::msem;
case COST_TYPE_RMSE:
return &MLPPCostOld::rmsem;
case COST_TYPE_MAE:
return &MLPPCostOld::maem;
case COST_TYPE_MBE:
return &MLPPCostOld::mbem;
case COST_TYPE_LOGISTIC_LOSS:
return &MLPPCostOld::log_lossm;
case COST_TYPE_CROSS_ENTROPY:
return &MLPPCostOld::cross_entropym;
case COST_TYPE_HINGE_LOSS:
return &MLPPCostOld::hinge_lossm;
case COST_TYPE_WASSERSTEIN_LOSS:
return &MLPPCostOld::wasserstein_lossm;
default:
return NULL;
}
}
MLPPCostOld::VectorDerivCostFunctionPointer MLPPCostOld::get_cost_function_ptr_deriv_vector(const MLPPCostOld::CostTypes cost) {
switch (cost) {
case COST_TYPE_MSE:
return &MLPPCostOld::mse_derivv;
case COST_TYPE_RMSE:
return &MLPPCostOld::rmse_derivv;
case COST_TYPE_MAE:
return &MLPPCostOld::mae_derivv;
case COST_TYPE_MBE:
return &MLPPCostOld::mbe_derivv;
case COST_TYPE_LOGISTIC_LOSS:
return &MLPPCostOld::log_loss_derivv;
case COST_TYPE_CROSS_ENTROPY:
return &MLPPCostOld::cross_entropy_derivv;
case COST_TYPE_HINGE_LOSS:
return &MLPPCostOld::hinge_loss_derivv;
case COST_TYPE_WASSERSTEIN_LOSS:
return &MLPPCostOld::wasserstein_loss_derivv;
default:
return NULL;
}
}
MLPPCostOld::MatrixDerivCostFunctionPointer MLPPCostOld::get_cost_function_ptr_deriv_matrix(const MLPPCostOld::CostTypes cost) {
switch (cost) {
case COST_TYPE_MSE:
return &MLPPCostOld::mse_derivm;
case COST_TYPE_RMSE:
return &MLPPCostOld::rmse_derivm;
case COST_TYPE_MAE:
return &MLPPCostOld::mae_derivm;
case COST_TYPE_MBE:
return &MLPPCostOld::mbe_derivm;
case COST_TYPE_LOGISTIC_LOSS:
return &MLPPCostOld::log_loss_derivm;
case COST_TYPE_CROSS_ENTROPY:
return &MLPPCostOld::cross_entropy_derivm;
case COST_TYPE_HINGE_LOSS:
return &MLPPCostOld::hinge_loss_derivm;
case COST_TYPE_WASSERSTEIN_LOSS:
return &MLPPCostOld::wasserstein_loss_derivm;
default:
return NULL;
}
}
real_t MLPPCostOld::run_cost_norm_vector(const CostTypes cost, const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
switch (cost) {
case COST_TYPE_MSE:
return msev(y_hat, y);
case COST_TYPE_RMSE:
return rmsev(y_hat, y);
case COST_TYPE_MAE:
return maev(y_hat, y);
case COST_TYPE_MBE:
return mbev(y_hat, y);
case COST_TYPE_LOGISTIC_LOSS:
return log_lossv(y_hat, y);
case COST_TYPE_CROSS_ENTROPY:
return cross_entropyv(y_hat, y);
case COST_TYPE_HINGE_LOSS:
return hinge_lossv(y_hat, y);
case COST_TYPE_WASSERSTEIN_LOSS:
return wasserstein_lossv(y_hat, y);
default:
return 0;
}
}
real_t MLPPCostOld::run_cost_norm_matrix(const CostTypes cost, const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
switch (cost) {
case COST_TYPE_MSE:
return msem(y_hat, y);
case COST_TYPE_RMSE:
return rmsem(y_hat, y);
case COST_TYPE_MAE:
return maem(y_hat, y);
case COST_TYPE_MBE:
return mbem(y_hat, y);
case COST_TYPE_LOGISTIC_LOSS:
return log_lossm(y_hat, y);
case COST_TYPE_CROSS_ENTROPY:
return cross_entropym(y_hat, y);
case COST_TYPE_HINGE_LOSS:
return hinge_lossm(y_hat, y);
case COST_TYPE_WASSERSTEIN_LOSS:
return wasserstein_lossm(y_hat, y);
default:
return 0;
}
}
Ref<MLPPVector> MLPPCostOld::run_cost_deriv_vector(const CostTypes cost, const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y) {
switch (cost) {
case COST_TYPE_MSE:
return mse_derivv(y_hat, y);
case COST_TYPE_RMSE:
return rmse_derivv(y_hat, y);
case COST_TYPE_MAE:
return mae_derivv(y_hat, y);
case COST_TYPE_MBE:
return mbe_derivv(y_hat, y);
case COST_TYPE_LOGISTIC_LOSS:
return log_loss_derivv(y_hat, y);
case COST_TYPE_CROSS_ENTROPY:
return cross_entropy_derivv(y_hat, y);
case COST_TYPE_HINGE_LOSS:
return hinge_loss_derivv(y_hat, y);
case COST_TYPE_WASSERSTEIN_LOSS:
return wasserstein_loss_derivv(y_hat, y);
default:
return Ref<MLPPVector>();
}
}
Ref<MLPPMatrix> MLPPCostOld::run_cost_deriv_matrix(const CostTypes cost, const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y) {
switch (cost) {
case COST_TYPE_MSE:
return mse_derivm(y_hat, y);
case COST_TYPE_RMSE:
return rmse_derivm(y_hat, y);
case COST_TYPE_MAE:
return mae_derivm(y_hat, y);
case COST_TYPE_MBE:
return mbe_derivm(y_hat, y);
case COST_TYPE_LOGISTIC_LOSS:
return log_loss_derivm(y_hat, y);
case COST_TYPE_CROSS_ENTROPY:
return cross_entropy_derivm(y_hat, y);
case COST_TYPE_HINGE_LOSS:
return hinge_loss_derivm(y_hat, y);
case COST_TYPE_WASSERSTEIN_LOSS:
return wasserstein_loss_derivm(y_hat, y);
default:
return Ref<MLPPMatrix>();
}
}
// ====== OLD ======
real_t MLPPCostOld::MSE(std::vector<real_t> y_hat, std::vector<real_t> y) {
real_t sum = 0;
for (uint32_t i = 0; i < y_hat.size(); i++) {
@ -1157,83 +402,3 @@ std::vector<real_t> MLPPCostOld::dualFormSVMDeriv(std::vector<real_t> alpha, std
return alg.subtraction(alphaQDeriv, one);
}
void MLPPCostOld::_bind_methods() {
ClassDB::bind_method(D_METHOD("msev", "y_hat", "y"), &MLPPCostOld::msev);
ClassDB::bind_method(D_METHOD("msem", "y_hat", "y"), &MLPPCostOld::msem);
ClassDB::bind_method(D_METHOD("mse_derivv", "y_hat", "y"), &MLPPCostOld::mse_derivv);
ClassDB::bind_method(D_METHOD("mse_derivm", "y_hat", "y"), &MLPPCostOld::mse_derivm);
ClassDB::bind_method(D_METHOD("rmsev", "y_hat", "y"), &MLPPCostOld::rmsev);
ClassDB::bind_method(D_METHOD("rmsem", "y_hat", "y"), &MLPPCostOld::rmsem);
ClassDB::bind_method(D_METHOD("rmse_derivv", "y_hat", "y"), &MLPPCostOld::rmse_derivv);
ClassDB::bind_method(D_METHOD("rmse_derivm", "y_hat", "y"), &MLPPCostOld::rmse_derivm);
ClassDB::bind_method(D_METHOD("maev", "y_hat", "y"), &MLPPCostOld::maev);
ClassDB::bind_method(D_METHOD("maem", "y_hat", "y"), &MLPPCostOld::maem);
ClassDB::bind_method(D_METHOD("mae_derivv", "y_hat", "y"), &MLPPCostOld::mae_derivv);
ClassDB::bind_method(D_METHOD("mae_derivm", "y_hat", "y"), &MLPPCostOld::mae_derivm);
ClassDB::bind_method(D_METHOD("mbev", "y_hat", "y"), &MLPPCostOld::mbev);
ClassDB::bind_method(D_METHOD("mbem", "y_hat", "y"), &MLPPCostOld::mbem);
ClassDB::bind_method(D_METHOD("mbe_derivv", "y_hat", "y"), &MLPPCostOld::mbe_derivv);
ClassDB::bind_method(D_METHOD("mbe_derivm", "y_hat", "y"), &MLPPCostOld::mbe_derivm);
ClassDB::bind_method(D_METHOD("log_lossv", "y_hat", "y"), &MLPPCostOld::log_lossv);
ClassDB::bind_method(D_METHOD("log_lossm", "y_hat", "y"), &MLPPCostOld::log_lossm);
ClassDB::bind_method(D_METHOD("log_loss_derivv", "y_hat", "y"), &MLPPCostOld::log_loss_derivv);
ClassDB::bind_method(D_METHOD("log_loss_derivm", "y_hat", "y"), &MLPPCostOld::log_loss_derivm);
ClassDB::bind_method(D_METHOD("cross_entropyv", "y_hat", "y"), &MLPPCostOld::cross_entropyv);
ClassDB::bind_method(D_METHOD("cross_entropym", "y_hat", "y"), &MLPPCostOld::cross_entropym);
ClassDB::bind_method(D_METHOD("cross_entropy_derivv", "y_hat", "y"), &MLPPCostOld::cross_entropy_derivv);
ClassDB::bind_method(D_METHOD("cross_entropy_derivm", "y_hat", "y"), &MLPPCostOld::cross_entropy_derivm);
ClassDB::bind_method(D_METHOD("huber_lossv", "y_hat", "y"), &MLPPCostOld::huber_lossv);
ClassDB::bind_method(D_METHOD("huber_lossm", "y_hat", "y"), &MLPPCostOld::huber_lossm);
ClassDB::bind_method(D_METHOD("huber_loss_derivv", "y_hat", "y"), &MLPPCostOld::huber_loss_derivv);
ClassDB::bind_method(D_METHOD("huber_loss_derivm", "y_hat", "y"), &MLPPCostOld::huber_loss_derivm);
ClassDB::bind_method(D_METHOD("hinge_lossv", "y_hat", "y"), &MLPPCostOld::hinge_lossv);
ClassDB::bind_method(D_METHOD("hinge_lossm", "y_hat", "y"), &MLPPCostOld::hinge_lossm);
ClassDB::bind_method(D_METHOD("hinge_loss_derivv", "y_hat", "y"), &MLPPCostOld::hinge_loss_derivv);
ClassDB::bind_method(D_METHOD("hinge_loss_derivm", "y_hat", "y"), &MLPPCostOld::hinge_loss_derivm);
ClassDB::bind_method(D_METHOD("hinge_losswv", "y_hat", "y"), &MLPPCostOld::hinge_losswv);
ClassDB::bind_method(D_METHOD("hinge_losswm", "y_hat", "y"), &MLPPCostOld::hinge_losswm);
ClassDB::bind_method(D_METHOD("hinge_loss_derivwv", "y_hat", "y", "C"), &MLPPCostOld::hinge_loss_derivwv);
ClassDB::bind_method(D_METHOD("hinge_loss_derivwm", "y_hat", "y", "C"), &MLPPCostOld::hinge_loss_derivwm);
ClassDB::bind_method(D_METHOD("wasserstein_lossv", "y_hat", "y"), &MLPPCostOld::wasserstein_lossv);
ClassDB::bind_method(D_METHOD("wasserstein_lossm", "y_hat", "y"), &MLPPCostOld::wasserstein_lossm);
ClassDB::bind_method(D_METHOD("wasserstein_loss_derivv", "y_hat", "y"), &MLPPCostOld::wasserstein_loss_derivv);
ClassDB::bind_method(D_METHOD("wasserstein_loss_derivm", "y_hat", "y"), &MLPPCostOld::wasserstein_loss_derivm);
ClassDB::bind_method(D_METHOD("dual_form_svm", "alpha", "X", "y"), &MLPPCostOld::dual_form_svm);
ClassDB::bind_method(D_METHOD("dual_form_svm_deriv", "alpha", "X", "y"), &MLPPCostOld::dual_form_svm_deriv);
ClassDB::bind_method(D_METHOD("run_cost_norm_vector", "cost", "y_hat", "y"), &MLPPCostOld::run_cost_norm_vector);
ClassDB::bind_method(D_METHOD("run_cost_norm_matrix", "cost", "y_hat", "y"), &MLPPCostOld::run_cost_norm_matrix);
ClassDB::bind_method(D_METHOD("run_cost_deriv_vector", "cost", "y_hat", "y"), &MLPPCostOld::run_cost_deriv_vector);
ClassDB::bind_method(D_METHOD("run_cost_deriv_matrix", "cost", "y_hat", "y"), &MLPPCostOld::run_cost_deriv_matrix);
BIND_ENUM_CONSTANT(COST_TYPE_MSE);
BIND_ENUM_CONSTANT(COST_TYPE_RMSE);
BIND_ENUM_CONSTANT(COST_TYPE_MAE);
BIND_ENUM_CONSTANT(COST_TYPE_MBE);
BIND_ENUM_CONSTANT(COST_TYPE_LOGISTIC_LOSS);
BIND_ENUM_CONSTANT(COST_TYPE_CROSS_ENTROPY);
BIND_ENUM_CONSTANT(COST_TYPE_HINGE_LOSS);
BIND_ENUM_CONSTANT(COST_TYPE_WASSERSTEIN_LOSS);
}

View File

@ -10,115 +10,10 @@
#include "core/math/math_defs.h"
#include "core/object/reference.h"
#include <vector>
#include "../lin_alg/mlpp_matrix.h"
#include "../lin_alg/mlpp_vector.h"
//void set_weights(const Ref<MLPPMatrix> &val);
//void set_bias(const Ref<MLPPVector> &val);
class MLPPCostOld : public Reference {
GDCLASS(MLPPCostOld, Reference);
class MLPPCostOld {
public:
enum CostTypes {
COST_TYPE_MSE = 0,
COST_TYPE_RMSE,
COST_TYPE_MAE,
COST_TYPE_MBE,
COST_TYPE_LOGISTIC_LOSS,
COST_TYPE_CROSS_ENTROPY,
COST_TYPE_HINGE_LOSS,
COST_TYPE_WASSERSTEIN_LOSS,
};
public:
real_t msev(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t msem(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
Ref<MLPPVector> mse_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
Ref<MLPPMatrix> mse_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
real_t rmsev(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t rmsem(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
Ref<MLPPVector> rmse_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
Ref<MLPPMatrix> rmse_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
real_t maev(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t maem(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
Ref<MLPPVector> mae_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
Ref<MLPPMatrix> mae_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
real_t mbev(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t mbem(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
Ref<MLPPVector> mbe_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
Ref<MLPPMatrix> mbe_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
// Classification Costs
real_t log_lossv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t log_lossm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
Ref<MLPPVector> log_loss_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
Ref<MLPPMatrix> log_loss_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
real_t cross_entropyv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t cross_entropym(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
Ref<MLPPVector> cross_entropy_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
Ref<MLPPMatrix> cross_entropy_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
real_t huber_lossv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y, real_t delta);
real_t huber_lossm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y, real_t delta);
Ref<MLPPVector> huber_loss_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y, real_t delta);
Ref<MLPPMatrix> huber_loss_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y, real_t delta);
real_t hinge_lossv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t hinge_lossm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
Ref<MLPPVector> hinge_loss_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
Ref<MLPPMatrix> hinge_loss_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
real_t hinge_losswv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y, const Ref<MLPPVector> &weights, real_t C);
real_t hinge_losswm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y, const Ref<MLPPMatrix> &weights, real_t C);
Ref<MLPPVector> hinge_loss_derivwv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y, real_t C);
Ref<MLPPMatrix> hinge_loss_derivwm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y, real_t C);
real_t wasserstein_lossv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t wasserstein_lossm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
Ref<MLPPVector> wasserstein_loss_derivv(const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
Ref<MLPPMatrix> wasserstein_loss_derivm(const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
real_t dual_form_svm(const Ref<MLPPVector> &alpha, const Ref<MLPPMatrix> &X, const Ref<MLPPVector> &y); // TO DO: DON'T forget to add non-linear kernelizations.
Ref<MLPPVector> dual_form_svm_deriv(const Ref<MLPPVector> &alpha, const Ref<MLPPMatrix> &X, const Ref<MLPPVector> &y);
typedef real_t (MLPPCostOld::*VectorCostFunctionPointer)(const Ref<MLPPVector> &, const Ref<MLPPVector> &);
typedef real_t (MLPPCostOld::*MatrixCostFunctionPointer)(const Ref<MLPPMatrix> &, const Ref<MLPPMatrix> &);
typedef Ref<MLPPVector> (MLPPCostOld::*VectorDerivCostFunctionPointer)(const Ref<MLPPVector> &, const Ref<MLPPVector> &);
typedef Ref<MLPPMatrix> (MLPPCostOld::*MatrixDerivCostFunctionPointer)(const Ref<MLPPMatrix> &, const Ref<MLPPMatrix> &);
VectorCostFunctionPointer get_cost_function_ptr_normal_vector(const CostTypes cost);
MatrixCostFunctionPointer get_cost_function_ptr_normal_matrix(const CostTypes cost);
VectorDerivCostFunctionPointer get_cost_function_ptr_deriv_vector(const CostTypes cost);
MatrixDerivCostFunctionPointer get_cost_function_ptr_deriv_matrix(const CostTypes cost);
real_t run_cost_norm_vector(const CostTypes cost, const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
real_t run_cost_norm_matrix(const CostTypes cost, const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
Ref<MLPPVector> run_cost_deriv_vector(const CostTypes cost, const Ref<MLPPVector> &y_hat, const Ref<MLPPVector> &y);
Ref<MLPPMatrix> run_cost_deriv_matrix(const CostTypes cost, const Ref<MLPPMatrix> &y_hat, const Ref<MLPPMatrix> &y);
// Regression Costs
real_t MSE(std::vector<real_t> y_hat, std::vector<real_t> y);
real_t MSE(std::vector<std::vector<real_t>> y_hat, std::vector<std::vector<real_t>> y);
@ -184,11 +79,6 @@ public:
real_t dualFormSVM(std::vector<real_t> alpha, std::vector<std::vector<real_t>> X, std::vector<real_t> y); // TO DO: DON'T forget to add non-linear kernelizations.
std::vector<real_t> dualFormSVMDeriv(std::vector<real_t> alpha, std::vector<std::vector<real_t>> X, std::vector<real_t> y);
protected:
static void _bind_methods();
};
VARIANT_ENUM_CAST(MLPPCostOld::CostTypes);
#endif /* Cost_hpp */