Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entirely numerical agtboost model class #58

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions R-package/R/gbt.pred.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,24 @@ predict.Rcpp_ENSEMBLE <- function(object, newdata, ...){

# This is default: Predict g^{-1}(preds)
# Get link function
# Mock loss_function enum
LossFunction <- list(
MSE = 0L,
LOGLOSS = 1L,
POISSON = 2L,
GAMMANEGINV = 3L,
GAMMALOG = 4L,
NEGBINOM = 5L
)
loss_fun_type <- object$get_loss_function()
link_type = ""
if(loss_fun_type %in% c("mse")){
if(loss_fun_type %in% c(LossFunction$MSE)){
link_type = "identity"
}else if(loss_fun_type %in% c("logloss")){
}else if(loss_fun_type %in% c(LossFunction$LOGLOSS)){
link_type = "logit"
}else if(loss_fun_type %in% c("poisson", "gamma::log", "negbinom")){
}else if(loss_fun_type %in% c(LossFunction$POISSON, LossFunction$GAMMALOG, LossFunction$NEGBINOM)){
link_type = "log"
}else if(loss_fun_type %in% c("gamma::neginv")){
}else if(loss_fun_type %in% c(LossFunction$GAMMANEGINV)){
link_type = "neginv"
}else{
# if no match
Expand Down
22 changes: 21 additions & 1 deletion R-package/R/gbt.train.R
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,26 @@ gbt.train <- function(y, x, learning_rate = 0.01,
# mod$train(y,x, verbose, gsub_compare)
#
# }else

# Mock loss_function enum
LossFunction <- list(
MSE = 0L,
LOGLOSS = 1L,
POISSON = 2L,
GAMMANEGINV = 3L,
GAMMALOG = 4L,
NEGBINOM = 5L
)
loss_function_enum = switch(
loss_function,
"mse" = LossFunction$MSE,
"logloss" = LossFunction$LOGLOSS,
"poisson" = LossFunction$POISSON,
"gamma::neginv" = LossFunction$GAMMANEGINV,
"gamma::log" = LossFunction$GAMMALOG,
"negbinom" = LossFunction$NEGBINOM
)

if(loss_function %in% c("count::auto")){
mod <- new(GBT_COUNT_AUTO)
mod$set_param(param)
Expand All @@ -267,7 +287,7 @@ gbt.train <- function(y, x, learning_rate = 0.01,
}else{
# create agtboost ensemble object
mod <- new(ENSEMBLE)
mod$set_param(nrounds, learning_rate, extra_param, loss_function)
mod$set_param(nrounds, learning_rate, extra_param, loss_function_enum)

# train ensemble
if(is.null(previous_pred)){
Expand Down
2 changes: 1 addition & 1 deletion R-package/inst/include/agtboost.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "tree.hpp"
#include "ensemble.hpp"
#include "optimization.hpp"
#include "loss_functions.hpp"
//#include "loss_functions.hpp"
#include "gbt_count_auto.hpp"
#include "initial_prediction.hpp"

Expand Down
9 changes: 5 additions & 4 deletions R-package/inst/include/ensemble.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


#include "tree.hpp"
#include "loss_functions.hpp"


// -- TRY WITHOUT EXPORT //' @export ENSEMBLE
Expand All @@ -16,7 +17,7 @@ class ENSEMBLE
double learning_rate;
double initial_score;
double extra_param; // Needed for certain distributions s.a. negative binomial, typically a dispersion param
std::string loss_function;
LossFunction loss_function;
GBTREE* first_tree;
//Rcpp::List param;

Expand All @@ -26,15 +27,15 @@ class ENSEMBLE
ENSEMBLE(double learning_rate_);

// Getters and setters
void set_param(int nrounds_, double learning_rate_, double extra_param_, std::string loss_function_);
void set_param(int nrounds_, double learning_rate_, double extra_param_, LossFunction loss_function_);

int get_nrounds();

double get_learning_rate();

double get_extra_param();

std::string get_loss_function();
LossFunction get_loss_function();

// Loss-related functions
double loss(Tvec<double> &y, Tvec<double> &pred, Tvec<double> &w);
Expand All @@ -47,7 +48,7 @@ class ENSEMBLE

double inverse_link_function(double pred);

double initial_prediction(Tvec<double> &y, std::string loss_function, Tvec<double> &w);
double initial_prediction(Tvec<double> &y, Tvec<double> &w);

// Training and prediction
void train(
Expand Down
Loading