From fd13337eb787cb29933aea3146d63137489532de Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 4 Feb 2023 13:19:01 +0100 Subject: [PATCH] Added more helper methods to Cost. --- mlpp/cost/cost.cpp | 195 +++++++++++++++++++++++++++++++++++++++++++++ mlpp/cost/cost.h | 32 ++++++++ 2 files changed, 227 insertions(+) diff --git a/mlpp/cost/cost.cpp b/mlpp/cost/cost.cpp index c7194af..ffe2851 100644 --- a/mlpp/cost/cost.cpp +++ b/mlpp/cost/cost.cpp @@ -585,6 +585,186 @@ Ref MLPPCost::dual_form_svm_deriv(const Ref &alpha, cons return alg.subtractionm(alphaQDeriv, one); } +MLPPCost::VectorCostFunctionPointer MLPPCost::get_cost_function_ptr_normal_vector(const MLPPCost::CostTypes cost) { + switch (cost) { + case COST_TYPE_MSE: + return &MLPPCost::msev; + case COST_TYPE_RMSE: + return &MLPPCost::rmsev; + case COST_TYPE_MAE: + return &MLPPCost::maev; + case COST_TYPE_MBE: + return &MLPPCost::mbev; + case COST_TYPE_LOGISTIC_LOSS: + return &MLPPCost::log_lossv; + case COST_TYPE_CROSS_ENTROPY: + return &MLPPCost::cross_entropyv; + case COST_TYPE_HINGE_LOSS: + return &MLPPCost::hinge_lossv; + case COST_TYPE_WASSERSTEIN_LOSS: + return &MLPPCost::wasserstein_lossv; + default: + return NULL; + } +} +MLPPCost::MatrixCostFunctionPointer MLPPCost::get_cost_function_ptr_normal_matrix(const MLPPCost::CostTypes cost) { + switch (cost) { + case COST_TYPE_MSE: + return &MLPPCost::msem; + case COST_TYPE_RMSE: + return &MLPPCost::rmsem; + case COST_TYPE_MAE: + return &MLPPCost::maem; + case COST_TYPE_MBE: + return &MLPPCost::mbem; + case COST_TYPE_LOGISTIC_LOSS: + return &MLPPCost::log_lossm; + case COST_TYPE_CROSS_ENTROPY: + return &MLPPCost::cross_entropym; + case COST_TYPE_HINGE_LOSS: + return &MLPPCost::hinge_lossm; + case COST_TYPE_WASSERSTEIN_LOSS: + return &MLPPCost::wasserstein_lossm; + default: + return NULL; + } +} + +MLPPCost::VectorDerivCostFunctionPointer MLPPCost::get_cost_function_ptr_deriv_vector(const MLPPCost::CostTypes cost) { + switch (cost) { + case COST_TYPE_MSE: + return &MLPPCost::mse_derivv; + case COST_TYPE_RMSE: + return &MLPPCost::rmse_derivv; + case COST_TYPE_MAE: + return &MLPPCost::mae_derivv; + case COST_TYPE_MBE: + return &MLPPCost::mbe_derivv; + case COST_TYPE_LOGISTIC_LOSS: + return &MLPPCost::log_loss_derivv; + case COST_TYPE_CROSS_ENTROPY: + return &MLPPCost::cross_entropy_derivv; + case COST_TYPE_HINGE_LOSS: + return &MLPPCost::hinge_loss_derivv; + case COST_TYPE_WASSERSTEIN_LOSS: + return &MLPPCost::wasserstein_loss_derivv; + default: + return NULL; + } +} +MLPPCost::MatrixDerivCostFunctionPointer MLPPCost::get_cost_function_ptr_deriv_matrix(const MLPPCost::CostTypes cost) { + switch (cost) { + case COST_TYPE_MSE: + return &MLPPCost::mse_derivm; + case COST_TYPE_RMSE: + return &MLPPCost::rmse_derivm; + case COST_TYPE_MAE: + return &MLPPCost::mae_derivm; + case COST_TYPE_MBE: + return &MLPPCost::mbe_derivm; + case COST_TYPE_LOGISTIC_LOSS: + return &MLPPCost::log_loss_derivm; + case COST_TYPE_CROSS_ENTROPY: + return &MLPPCost::cross_entropy_derivm; + case COST_TYPE_HINGE_LOSS: + return &MLPPCost::hinge_loss_derivm; + case COST_TYPE_WASSERSTEIN_LOSS: + return &MLPPCost::wasserstein_loss_derivm; + default: + return NULL; + } +} + +real_t MLPPCost::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 MLPPCost::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 MLPPCost::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 MLPPCost::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 MLPPCost::MSE(std::vector y_hat, std::vector y) { @@ -1044,4 +1224,19 @@ void MLPPCost::_bind_methods() { ClassDB::bind_method(D_METHOD("dual_form_svm", "alpha", "X", "y"), &MLPPCost::dual_form_svm); ClassDB::bind_method(D_METHOD("dual_form_svm_deriv", "alpha", "X", "y"), &MLPPCost::dual_form_svm_deriv); + + ClassDB::bind_method(D_METHOD("run_cost_norm_vector", "cost", "y_hat", "y"), &MLPPCost::run_cost_norm_vector); + ClassDB::bind_method(D_METHOD("run_cost_norm_matrix", "cost", "y_hat", "y"), &MLPPCost::run_cost_norm_matrix); + + ClassDB::bind_method(D_METHOD("run_cost_deriv_vector", "cost", "y_hat", "y"), &MLPPCost::run_cost_deriv_vector); + ClassDB::bind_method(D_METHOD("run_cost_deriv_matrix", "cost", "y_hat", "y"), &MLPPCost::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.h b/mlpp/cost/cost.h index d849322..ad3783c 100644 --- a/mlpp/cost/cost.h +++ b/mlpp/cost/cost.h @@ -23,6 +23,18 @@ class MLPPCost : public Reference { GDCLASS(MLPPCost, Reference); +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); @@ -89,6 +101,24 @@ public: Ref dual_form_svm_deriv(const Ref &alpha, const Ref &X, const Ref &y); + typedef real_t (MLPPCost::*VectorCostFunctionPointer)(const Ref &, const Ref &); + typedef real_t (MLPPCost::*MatrixCostFunctionPointer)(const Ref &, const Ref &); + + typedef Ref (MLPPCost::*VectorDerivCostFunctionPointer)(const Ref &, const Ref &); + typedef Ref (MLPPCost::*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); @@ -159,4 +189,6 @@ protected: static void _bind_methods(); }; +VARIANT_ENUM_CAST(MLPPCost::CostTypes); + #endif /* Cost_hpp */