From 0a51b845f7dff9dadcef8fac191a5c1b9b2c8fc3 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 13 Feb 2023 18:29:05 +0100 Subject: [PATCH] Removed new things from MLPPCostOld. --- mlpp/cost/cost_old.cpp | 835 ----------------------------------------- mlpp/cost/cost_old.h | 112 +----- 2 files changed, 1 insertion(+), 946 deletions(-) diff --git a/mlpp/cost/cost_old.cpp b/mlpp/cost/cost_old.cpp index 30d6312..42c121a 100644 --- a/mlpp/cost/cost_old.cpp +++ b/mlpp/cost/cost_old.cpp @@ -10,761 +10,6 @@ #include #include -real_t MLPPCostOld::msev(const Ref &y_hat, const Ref &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 &y_hat, const Ref &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(y_hat_data_size); -} - -Ref MLPPCostOld::mse_derivv(const Ref &y_hat, const Ref &y) { - MLPPLinAlg alg; - return alg.subtractionnv(y_hat, y); -} - -Ref MLPPCostOld::mse_derivm(const Ref &y_hat, const Ref &y) { - MLPPLinAlg alg; - return alg.subtractionm(y_hat, y); -} - -real_t MLPPCostOld::rmsev(const Ref &y_hat, const Ref &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(y_hat_size)); -} -real_t MLPPCostOld::rmsem(const Ref &y_hat, const Ref &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(y_hat->size().y)); -} - -Ref MLPPCostOld::rmse_derivv(const Ref &y_hat, const Ref &y) { - MLPPLinAlg alg; - - return alg.scalar_multiplynv(1 / (2.0 * Math::sqrt(msev(y_hat, y))), mse_derivv(y_hat, y)); -} -Ref MLPPCostOld::rmse_derivm(const Ref &y_hat, const Ref &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 &y_hat, const Ref &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(y_hat_size); -} -real_t MLPPCostOld::maem(const Ref &y_hat, const Ref &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(y_hat_data_size); -} - -Ref MLPPCostOld::mae_derivv(const Ref &y_hat, const Ref &y) { - int y_hat_size = y_hat->size(); - - const real_t *y_hat_ptr = y_hat->ptr(); - - Ref 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 MLPPCostOld::mae_derivm(const Ref &y_hat, const Ref &y) { - int y_hat_data_size = y_hat->data_size(); - - const real_t *y_hat_ptr = y_hat->ptr(); - - Ref 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 &y_hat, const Ref &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(y_hat_size); -} -real_t MLPPCostOld::mbem(const Ref &y_hat, const Ref &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(y_hat_data_size); -} - -Ref MLPPCostOld::mbe_derivv(const Ref &y_hat, const Ref &y) { - MLPPLinAlg alg; - return alg.onevecv(y_hat->size()); -} -Ref MLPPCostOld::mbe_derivm(const Ref &y_hat, const Ref &y) { - MLPPLinAlg alg; - return alg.onematm(y_hat->size().x, y_hat->size().y); -} - -// Classification Costs -real_t MLPPCostOld::log_lossv(const Ref &y_hat, const Ref &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(y_hat_size); -} - -real_t MLPPCostOld::log_lossm(const Ref &y_hat, const Ref &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(y_hat_data_size); -} - -Ref MLPPCostOld::log_loss_derivv(const Ref &y_hat, const Ref &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 MLPPCostOld::log_loss_derivm(const Ref &y_hat, const Ref &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 &y_hat, const Ref &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 &y_hat, const Ref &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 MLPPCostOld::cross_entropy_derivv(const Ref &y_hat, const Ref &y) { - MLPPLinAlg alg; - return alg.scalar_multiplynv(-1, alg.element_wise_division(y, y_hat)); -} -Ref MLPPCostOld::cross_entropy_derivm(const Ref &y_hat, const Ref &y) { - MLPPLinAlg alg; - return alg.scalar_multiplym(-1, alg.element_wise_divisionm(y, y_hat)); -} - -real_t MLPPCostOld::huber_lossv(const Ref &y_hat, const Ref &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 &y_hat, const Ref &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 MLPPCostOld::huber_loss_derivv(const Ref &y_hat, const Ref &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 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 MLPPCostOld::huber_loss_derivm(const Ref &y_hat, const Ref &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 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 &y_hat, const Ref &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(y_hat_size); -} -real_t MLPPCostOld::hinge_lossm(const Ref &y_hat, const Ref &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(y_hat_data_size); -} - -Ref MLPPCostOld::hinge_loss_derivv(const Ref &y_hat, const Ref &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 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 MLPPCostOld::hinge_loss_derivm(const Ref &y_hat, const Ref &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 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 &y_hat, const Ref &y, const Ref &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 &y_hat, const Ref &y, const Ref &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 MLPPCostOld::hinge_loss_derivwv(const Ref &y_hat, const Ref &y, real_t C) { - MLPPLinAlg alg; - MLPPReg regularization; - - return alg.scalar_multiplynv(C, hinge_loss_derivv(y_hat, y)); -} -Ref MLPPCostOld::hinge_loss_derivwm(const Ref &y_hat, const Ref &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 &y_hat, const Ref &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(y_hat_size); -} -real_t MLPPCostOld::wasserstein_lossm(const Ref &y_hat, const Ref &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(y_hat_data_size); -} - -Ref MLPPCostOld::wasserstein_loss_derivv(const Ref &y_hat, const Ref &y) { - MLPPLinAlg alg; - - return alg.scalar_multiplynv(-1, y); // Simple. -} -Ref MLPPCostOld::wasserstein_loss_derivm(const Ref &y_hat, const Ref &y) { - MLPPLinAlg alg; - - return alg.scalar_multiplym(-1, y); // Simple. -} - -real_t MLPPCostOld::dual_form_svm(const Ref &alpha, const Ref &X, const Ref &y) { - MLPPLinAlg alg; - - Ref 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 K = alg.matmultm(X, alg.transposem(X)); // TO DO: DON'T forget to add non-linear kernelizations. - Ref Q = alg.matmultm(alg.matmultm(alg.transposem(Y), K), Y); - - Ref alpha_m; - alpha_m.instance(); - alpha_m->resize(Size2i(alpha->size(), 1)); - alpha_m->set_row_mlpp_vector(0, alpha); - - Ref 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 one = alg.onevecv(alpha->size()); - - return -alg.dotv(one, alpha) + 0.5 * alphaQ; -} - -Ref MLPPCostOld::dual_form_svm_deriv(const Ref &alpha, const Ref &X, const Ref &y) { - MLPPLinAlg alg; - - Ref 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 K = alg.matmultm(X, alg.transposem(X)); // TO DO: DON'T forget to add non-linear kernelizations. - Ref Q = alg.matmultm(alg.matmultm(alg.transposem(Y), K), Y); - Ref alphaQDeriv = alg.mat_vec_multv(Q, alpha); - Ref 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 &y_hat, const Ref &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 &y_hat, const Ref &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 MLPPCostOld::run_cost_deriv_vector(const CostTypes cost, const Ref &y_hat, const Ref &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(); - } -} -Ref MLPPCostOld::run_cost_deriv_matrix(const CostTypes cost, const Ref &y_hat, const Ref &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(); - } -} - -// ====== OLD ====== - real_t MLPPCostOld::MSE(std::vector y_hat, std::vector y) { real_t sum = 0; for (uint32_t i = 0; i < y_hat.size(); i++) { @@ -1157,83 +402,3 @@ std::vector MLPPCostOld::dualFormSVMDeriv(std::vector 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); -} diff --git a/mlpp/cost/cost_old.h b/mlpp/cost/cost_old.h index d6f500f..b98f6bc 100644 --- a/mlpp/cost/cost_old.h +++ b/mlpp/cost/cost_old.h @@ -10,115 +10,10 @@ #include "core/math/math_defs.h" -#include "core/object/reference.h" - #include -#include "../lin_alg/mlpp_matrix.h" -#include "../lin_alg/mlpp_vector.h" - -//void set_weights(const Ref &val); -//void set_bias(const Ref &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 &y_hat, const Ref &y); - real_t msem(const Ref &y_hat, const Ref &y); - - Ref mse_derivv(const Ref &y_hat, const Ref &y); - Ref mse_derivm(const Ref &y_hat, const Ref &y); - - real_t rmsev(const Ref &y_hat, const Ref &y); - real_t rmsem(const Ref &y_hat, const Ref &y); - - Ref rmse_derivv(const Ref &y_hat, const Ref &y); - Ref rmse_derivm(const Ref &y_hat, const Ref &y); - - real_t maev(const Ref &y_hat, const Ref &y); - real_t maem(const Ref &y_hat, const Ref &y); - - Ref mae_derivv(const Ref &y_hat, const Ref &y); - Ref mae_derivm(const Ref &y_hat, const Ref &y); - - real_t mbev(const Ref &y_hat, const Ref &y); - real_t mbem(const Ref &y_hat, const Ref &y); - - Ref mbe_derivv(const Ref &y_hat, const Ref &y); - Ref mbe_derivm(const Ref &y_hat, const Ref &y); - - // Classification Costs - real_t log_lossv(const Ref &y_hat, const Ref &y); - real_t log_lossm(const Ref &y_hat, const Ref &y); - - Ref log_loss_derivv(const Ref &y_hat, const Ref &y); - Ref log_loss_derivm(const Ref &y_hat, const Ref &y); - - real_t cross_entropyv(const Ref &y_hat, const Ref &y); - real_t cross_entropym(const Ref &y_hat, const Ref &y); - - Ref cross_entropy_derivv(const Ref &y_hat, const Ref &y); - Ref cross_entropy_derivm(const Ref &y_hat, const Ref &y); - - real_t huber_lossv(const Ref &y_hat, const Ref &y, real_t delta); - real_t huber_lossm(const Ref &y_hat, const Ref &y, real_t delta); - - Ref huber_loss_derivv(const Ref &y_hat, const Ref &y, real_t delta); - Ref huber_loss_derivm(const Ref &y_hat, const Ref &y, real_t delta); - - real_t hinge_lossv(const Ref &y_hat, const Ref &y); - real_t hinge_lossm(const Ref &y_hat, const Ref &y); - - Ref hinge_loss_derivv(const Ref &y_hat, const Ref &y); - Ref hinge_loss_derivm(const Ref &y_hat, const Ref &y); - - real_t hinge_losswv(const Ref &y_hat, const Ref &y, const Ref &weights, real_t C); - real_t hinge_losswm(const Ref &y_hat, const Ref &y, const Ref &weights, real_t C); - - Ref hinge_loss_derivwv(const Ref &y_hat, const Ref &y, real_t C); - Ref hinge_loss_derivwm(const Ref &y_hat, const Ref &y, real_t C); - - real_t wasserstein_lossv(const Ref &y_hat, const Ref &y); - real_t wasserstein_lossm(const Ref &y_hat, const Ref &y); - - Ref wasserstein_loss_derivv(const Ref &y_hat, const Ref &y); - Ref wasserstein_loss_derivm(const Ref &y_hat, const Ref &y); - - real_t dual_form_svm(const Ref &alpha, const Ref &X, const Ref &y); // TO DO: DON'T forget to add non-linear kernelizations. - - Ref dual_form_svm_deriv(const Ref &alpha, const Ref &X, const Ref &y); - - typedef real_t (MLPPCostOld::*VectorCostFunctionPointer)(const Ref &, const Ref &); - typedef real_t (MLPPCostOld::*MatrixCostFunctionPointer)(const Ref &, const Ref &); - - typedef Ref (MLPPCostOld::*VectorDerivCostFunctionPointer)(const Ref &, const Ref &); - typedef Ref (MLPPCostOld::*MatrixDerivCostFunctionPointer)(const Ref &, const Ref &); - - 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 &y_hat, const Ref &y); - real_t run_cost_norm_matrix(const CostTypes cost, const Ref &y_hat, const Ref &y); - - 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); @@ -184,11 +79,6 @@ public: 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(); }; -VARIANT_ENUM_CAST(MLPPCostOld::CostTypes); - #endif /* Cost_hpp */