| Title: | High-Level Modeling Functions with 'torch' |
|---|---|
| Description: | Provides high-level modeling functions to define and train models using the 'torch' R package. Models include linear, logistic, and multinomial regression as well as multilayer perceptrons. |
| Authors: | Max Kuhn [aut, cre] (ORCID: <https://orcid.org/0000-0003-2402-136X>), Daniel Falbel [aut], Posit Software, PBC [cph, fnd] |
| Maintainer: | Max Kuhn <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.6.0.9001 |
| Built: | 2026-05-27 22:33:51 UTC |
| Source: | https://github.com/tidymodels/brulee |
Activation functions for neural networks in brulee
brulee_activations()brulee_activations()
A character vector of values.
brulee_auto_int() fits AutoInt from Song at al (2019) that use multi-head
columnar self-attention to help exploit how combinations of embeddings can be
used to improve specific predictions.
brulee_auto_int(x, ...) ## Default S3 method: brulee_auto_int(x, ...) ## S3 method for class 'data.frame' brulee_auto_int( x, y, epochs = 100L, num_embedding = 16L, num_attn_feat = 16L, num_attn_heads = 2L, num_attn_blocks = 3L, activation = "relu", hidden_units = NULL, hidden_activations = NULL, penalty = 0.001, mixture = 0, dropout = 0, dropout_attn = 0, dropout_embedding = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_auto_int( x, y, epochs = 100L, num_embedding = 16L, num_attn_feat = 16L, num_attn_heads = 2L, num_attn_blocks = 3L, activation = "relu", hidden_units = NULL, hidden_activations = NULL, dropout = 0, penalty = 0.001, mixture = 0, dropout_attn = 0, dropout_embedding = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_auto_int( formula, data, epochs = 100L, num_embedding = 16L, num_attn_feat = 16L, num_attn_heads = 2L, num_attn_blocks = 3L, activation = "relu", hidden_units = NULL, hidden_activations = NULL, dropout = 0, penalty = 0.001, mixture = 0, dropout_attn = 0, dropout_embedding = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_auto_int( x, data, epochs = 100L, num_embedding = 16L, num_attn_feat = 16L, num_attn_heads = 2L, num_attn_blocks = 3L, activation = "relu", hidden_units = NULL, hidden_activations = NULL, dropout = 0, penalty = 0.001, mixture = 0, dropout_attn = 0, dropout_embedding = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... )brulee_auto_int(x, ...) ## Default S3 method: brulee_auto_int(x, ...) ## S3 method for class 'data.frame' brulee_auto_int( x, y, epochs = 100L, num_embedding = 16L, num_attn_feat = 16L, num_attn_heads = 2L, num_attn_blocks = 3L, activation = "relu", hidden_units = NULL, hidden_activations = NULL, penalty = 0.001, mixture = 0, dropout = 0, dropout_attn = 0, dropout_embedding = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_auto_int( x, y, epochs = 100L, num_embedding = 16L, num_attn_feat = 16L, num_attn_heads = 2L, num_attn_blocks = 3L, activation = "relu", hidden_units = NULL, hidden_activations = NULL, dropout = 0, penalty = 0.001, mixture = 0, dropout_attn = 0, dropout_embedding = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_auto_int( formula, data, epochs = 100L, num_embedding = 16L, num_attn_feat = 16L, num_attn_heads = 2L, num_attn_blocks = 3L, activation = "relu", hidden_units = NULL, hidden_activations = NULL, dropout = 0, penalty = 0.001, mixture = 0, dropout_attn = 0, dropout_embedding = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_auto_int( x, data, epochs = 100L, num_embedding = 16L, num_attn_feat = 16L, num_attn_heads = 2L, num_attn_blocks = 3L, activation = "relu", hidden_units = NULL, hidden_activations = NULL, dropout = 0, penalty = 0.001, mixture = 0, dropout_attn = 0, dropout_embedding = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... )
x |
Depending on the context:
The predictor data should be standardized (e.g. centered or scaled). |
... |
Options to pass to the learning rate schedulers via
|
y |
When
|
epochs |
An integer for the number of epochs of training. |
num_embedding |
An integer for the embedding dimension. Each feature (categorical or continuous) is mapped to a vector of this dimension. Must be >= 1. |
num_attn_feat |
An integer for the per-head attention dimension. The
total attention dimension is |
num_attn_heads |
An integer for the number of attention heads. Each head learns different interaction patterns in parallel. Must be >= 1. |
num_attn_blocks |
An integer for the number of stacked self-attention layers. More layers capture higher-order interactions. Must be >= 1. |
activation |
A single character string for the activation function used
in the self-attention backbone (applied after each residual connection in
each attention block). This does not affect the optional hidden layers; use
|
|
An integer vector for the number of units in optional
hidden layers between the attention backbone and the output head. For
example, |
|
|
A character vector of activation functions for the
hidden layers. Must be the same length as |
|
penalty |
The amount of weight decay (i.e., L2 regularization). |
mixture |
Proportion of Lasso Penalty (type: double, default: 0.0). A
value of mixture = 1 corresponds to a pure lasso model, while mixture = 0
indicates ridge regression (a.k.a weight decay). Must be zero for
optimizers |
dropout |
A number in |
dropout_attn |
A number in |
dropout_embedding |
A number in |
validation |
The proportion of the data randomly assigned to a validation set. |
optimizer |
The method used in the optimization procedure. Possible choices
are |
learn_rate |
A positive number that controls the initial rapidity that the model moves along the descent path. Values around 0.1 or less are typical. |
rate_schedule |
A single character value for how the learning rate
should change as the optimization proceeds. Possible values are
|
momentum |
A positive number usually on |
batch_size |
An integer for the number of training set points in each
batch. ( |
class_weights |
Numeric class weights (classification only). The value can be:
|
stop_iter |
A non-negative integer for how many iterations with no improvement before stopping. |
verbose |
A logical that prints out the iteration history. |
device |
A single character string for the device to train on (e.g.,
|
formula |
A formula specifying the outcome term(s) on the left-hand side, and the predictor term(s) on the right-hand side. |
data |
When a recipe or formula is used,
|
In statistics, an interaction occurs when two or more predictors jointly predict the outcome. You need to know the values of all predictors within the interaction effect to appropriately model the data. AutoInt is often described as "automatically learning feature interactions," but that is not an accurate description.
In neural networks, the original predictors are converted to embeddings, which are often the hidden units of the network.
AutoInt uses column attention to change how embeddings are represented. It learns how to make the embeddings more relevant to the outcome by creating mixtures of them. For example, if we predict a data point in one part of the predictor space, attention will refocus (i.e., transform) the embedding to be more relevant to that part of the space.
The AutoInt architecture has three stages:
Embedding layer: Maps every feature (categorical or continuous) into
a shared vector space of dimension num_embedding.
Self-attention backbone: A stack of num_attn_blocks multi-head
self-attention layers. After all blocks, a residual connection from
the original embeddings is added and an activation is applied.
Hidden layers (optional): If hidden_units is specified, one or more
fully-connected layers with activations process the flattened attention
output before the output head.
Output head: Projects to the output dimension via a linear layer.
Unlike other brulee models, brulee_auto_int() natively handles factor
predictors via learned embeddings. Factor columns are automatically detected
and embedded, while numeric columns use a scaled embedding. There is no need
to pre-encode factors as indicators.
The self-attention backbone has several tuning parameters that control its capacity and regularization:
num_attn_heads: The number of attention heads that operate in parallel
within each attention block. Each head independently learns which features
interact, giving the model multiple "views" of the feature relationships.
The total attention dimension per block is num_attn_feat * num_attn_heads.
num_attn_feat: The per-head attention dimension. Each head projects
features into a space of this size to compute attention scores. Larger
values give each head more capacity to represent complex interactions.
num_attn_blocks: The number of attention layers stacked sequentially.
Each block's output feeds into the next, allowing the model to build
higher-order interactions (e.g., block 1 captures pairwise interactions,
block 2 can combine those into three-way interactions, etc.).
activation: The activation function applied after the residual connection
at the end of the attention backbone.
dropout_attn: Dropout applied to the attention weight matrix within each
block, which randomly zeroes out attention connections during training.
The learning rate can be set to constant (the default) or dynamically set
via a learning rate scheduler (via the rate_schedule). Using
rate_schedule = 'none' uses the learn_rate argument. Otherwise, any
arguments to the schedulers can be passed via ....
When the outcome is a number, the function internally standardizes the outcome data to have mean zero and a standard deviation of one. The prediction function creates predictions on the original scale.
By default, training halts when the validation loss increases for at least
stop_iter iterations. If validation = 0 the training set loss is used.
The model objects are saved for each epoch so that the number of epochs can
be efficiently tuned. Both the predict() method for this model has an
epoch argument (which defaults to the epoch with the best loss value).
The use of the L1 penalty (a.k.a. the lasso penalty) does not force parameters to be strictly zero (as it does in packages such as glmnet). The zeroing out of parameters is a specific feature the optimization method used in those packages.
A brulee_auto_int object with elements:
models_obj: a serialized raw vector for the torch module.
estimates: a list of matrices with the model parameter estimates per
epoch.
best_epoch: an integer for the epoch with the smallest loss.
loss: A vector of loss values (MSE for regression, negative log-
likelihood for classification) at each epoch.
dim: A list of data dimensions and feature metadata.
top_interactions: A tibble containing the top 10 two-way feature
interactions.
y_stats: A list of summary statistics for numeric outcomes.
parameters: A list of some tuning parameter values.
device: A character string for the device used during training.
blueprint: The hardhat blueprint data.
Song, W., Shi, C., Xiao, Z., Duan, Z., Xu, Y., Zhang, M., & Tang, J. (2019). AutoInt: Automatic Feature Interaction Learning via Self-Attentive Neural Networks. In Proceedings of the 28th ACM International Conference on Information and Knowledge Management (CIKM).
predict.brulee_auto_int(), autoplot.brulee_auto_int()
pkgs <- c("recipes", "yardstick", "modeldata") if (torch::torch_is_installed() & rlang::is_installed(pkgs)) { set.seed(87261) tr_data <- modeldata::sim_regression(500) te_data <- modeldata::sim_regression(50) set.seed(2) fit <- brulee_auto_int(outcome ~ ., data = tr_data, epochs = 50L, batch_size = 64L, stop_iter = 10L, learn_rate = 0.01, penalty = 0.01) fit autoplot(fit) library(yardstick) predict(fit, te_data) |> dplyr::bind_cols(te_data) |> rmse(outcome, .pred) }pkgs <- c("recipes", "yardstick", "modeldata") if (torch::torch_is_installed() & rlang::is_installed(pkgs)) { set.seed(87261) tr_data <- modeldata::sim_regression(500) te_data <- modeldata::sim_regression(50) set.seed(2) fit <- brulee_auto_int(outcome ~ ., data = tr_data, epochs = 50L, batch_size = 64L, stop_iter = 10L, learn_rate = 0.01, penalty = 0.01) fit autoplot(fit) library(yardstick) predict(fit, te_data) |> dplyr::bind_cols(te_data) |> rmse(outcome, .pred) }
brulee_linear_reg() fits a linear regression model.
brulee_linear_reg(x, ...) ## Default S3 method: brulee_linear_reg(x, ...) ## S3 method for class 'data.frame' brulee_linear_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_linear_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_linear_reg( formula, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_linear_reg( x, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... )brulee_linear_reg(x, ...) ## Default S3 method: brulee_linear_reg(x, ...) ## S3 method for class 'data.frame' brulee_linear_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_linear_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_linear_reg( formula, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_linear_reg( x, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... )
x |
Depending on the context:
The predictor data should be standardized (e.g. centered or scaled). |
... |
Options to pass to the learning rate schedulers via
|
y |
When
|
epochs |
An integer for the number of epochs of training. |
penalty |
The amount of weight decay (i.e., L2 regularization). |
mixture |
Proportion of Lasso Penalty (type: double, default: 0.0). A
value of mixture = 1 corresponds to a pure lasso model, while mixture = 0
indicates ridge regression (a.k.a weight decay). Must be zero for
optimizers |
validation |
The proportion of the data randomly assigned to a validation set. |
optimizer |
The method used in the optimization procedure. Possible choices
are |
learn_rate |
A positive number that controls the initial rapidity that the model moves along the descent path. Values around 0.1 or less are typical. |
momentum |
A positive number usually on |
batch_size |
An integer for the number of training set points in each
batch. ( |
stop_iter |
A non-negative integer for how many iterations with no improvement before stopping. |
verbose |
A logical that prints out the iteration history. |
device |
A single character string for the device to train on (e.g.,
|
formula |
A formula specifying the outcome term(s) on the left-hand side, and the predictor term(s) on the right-hand side. |
data |
When a recipe or formula is used,
|
This function fits a linear combination of coefficients and predictors to model the numeric outcome. The training process optimizes the mean squared error loss function.
The function internally standardizes the outcome data to have mean zero and a standard deviation of one. The prediction function creates predictions on the original scale.
By default, training halts when the validation loss increases for at least
step_iter iterations. If validation = 0 the training set loss is used.
The predictors data should all be numeric and encoded in the same units (e.g. standardized to the same range or distribution). If there are factor predictors, use a recipe or formula to create indicator variables (or some other method) to make them numeric. Predictors should be in the same units before training.
The model objects are saved for each epoch so that the number of epochs can
be efficiently tuned. Both the coef() and predict() methods for this
model have an epoch argument (which defaults to the epoch with the best
loss value).
The use of the L1 penalty (a.k.a. the lasso penalty) does not force parameters to be strictly zero (as it does in packages such as glmnet). The zeroing out of parameters is a specific feature the optimization method used in those packages.
A brulee_linear_reg object with elements:
models_obj: a serialized raw vector for the torch module.
estimates: a list of matrices with the model parameter estimates per
epoch.
best_epoch: an integer for the epoch with the smallest loss.
loss: A vector of loss values (MSE) at each epoch.
dim: A list of data dimensions.
y_stats: A list of summary statistics for numeric outcomes.
parameters: A list of some tuning parameter values.
blueprint: The hardhat blueprint data.
predict.brulee_linear_reg(), coef.brulee_linear_reg(),
autoplot.brulee_linear_reg()
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { ## ----------------------------------------------------------------------------- library(recipes) library(yardstick) data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(122) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using matrices set.seed(1) brulee_linear_reg(x = as.matrix(ames_train[, c("Longitude", "Latitude")]), y = ames_train$Sale_Price, penalty = 0.10, epochs = 1, batch_size = 64) # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Bldg_Type + Neighborhood + Year_Built + Gr_Liv_Area + Full_Bath + Year_Sold + Lot_Area + Central_Air + Longitude + Latitude, data = ames_train) |> # Transform some highly skewed predictors step_BoxCox(Lot_Area, Gr_Liv_Area) |> # Lump some rarely occurring categories into "other" step_other(Neighborhood, threshold = 0.05) |> # Encode categorical predictors as binary. step_dummy(all_nominal_predictors(), one_hot = TRUE) |> # Add an interaction effect: step_interact(~ starts_with("Central_Air"):Year_Built) |> step_zv(all_predictors()) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_linear_reg(ames_rec, data = ames_train, epochs = 5) fit autoplot(fit) library(ggplot2) predict(fit, ames_test) |> bind_cols(ames_test) |> ggplot(aes(x = .pred, y = Sale_Price)) + geom_abline(col = "green") + geom_point(alpha = .3) + lims(x = c(4, 6), y = c(4, 6)) + coord_fixed(ratio = 1) library(yardstick) predict(fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { ## ----------------------------------------------------------------------------- library(recipes) library(yardstick) data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(122) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using matrices set.seed(1) brulee_linear_reg(x = as.matrix(ames_train[, c("Longitude", "Latitude")]), y = ames_train$Sale_Price, penalty = 0.10, epochs = 1, batch_size = 64) # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Bldg_Type + Neighborhood + Year_Built + Gr_Liv_Area + Full_Bath + Year_Sold + Lot_Area + Central_Air + Longitude + Latitude, data = ames_train) |> # Transform some highly skewed predictors step_BoxCox(Lot_Area, Gr_Liv_Area) |> # Lump some rarely occurring categories into "other" step_other(Neighborhood, threshold = 0.05) |> # Encode categorical predictors as binary. step_dummy(all_nominal_predictors(), one_hot = TRUE) |> # Add an interaction effect: step_interact(~ starts_with("Central_Air"):Year_Built) |> step_zv(all_predictors()) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_linear_reg(ames_rec, data = ames_train, epochs = 5) fit autoplot(fit) library(ggplot2) predict(fit, ames_test) |> bind_cols(ames_test) |> ggplot(aes(x = .pred, y = Sale_Price)) + geom_abline(col = "green") + geom_point(alpha = .3) + lims(x = c(4, 6), y = c(4, 6)) + coord_fixed(ratio = 1) library(yardstick) predict(fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) }
brulee_logistic_reg() fits a model.
brulee_logistic_reg(x, ...) ## Default S3 method: brulee_logistic_reg(x, ...) ## S3 method for class 'data.frame' brulee_logistic_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_logistic_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_logistic_reg( formula, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_logistic_reg( x, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... )brulee_logistic_reg(x, ...) ## Default S3 method: brulee_logistic_reg(x, ...) ## S3 method for class 'data.frame' brulee_logistic_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_logistic_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_logistic_reg( formula, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_logistic_reg( x, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... )
x |
Depending on the context:
The predictor data should be standardized (e.g. centered or scaled). |
... |
Options to pass to the learning rate schedulers via
|
y |
When
|
epochs |
An integer for the number of epochs of training. |
penalty |
The amount of weight decay (i.e., L2 regularization). |
mixture |
Proportion of Lasso Penalty (type: double, default: 0.0). A
value of mixture = 1 corresponds to a pure lasso model, while mixture = 0
indicates ridge regression (a.k.a weight decay). Must be zero for
optimizers |
validation |
The proportion of the data randomly assigned to a validation set. |
optimizer |
The method used in the optimization procedure. Possible choices
are |
learn_rate |
A positive number that controls the initial rapidity that the model moves along the descent path. Values around 0.1 or less are typical. |
momentum |
A positive number usually on |
batch_size |
An integer for the number of training set points in each
batch. ( |
class_weights |
Numeric class weights (classification only). The value can be:
|
stop_iter |
A non-negative integer for how many iterations with no improvement before stopping. |
verbose |
A logical that prints out the iteration history. |
device |
A single character string for the device to train on (e.g.,
|
formula |
A formula specifying the outcome term(s) on the left-hand side, and the predictor term(s) on the right-hand side. |
data |
When a recipe or formula is used,
|
This function fits a linear combination of coefficients and predictors to model the log odds of the classes. The training process optimizes the cross-entropy loss function (a.k.a Bernoulli loss).
By default, training halts when the validation loss increases for at least
step_iter iterations. If validation = 0 the training set loss is used.
The predictors data should all be numeric and encoded in the same units (e.g. standardized to the same range or distribution). If there are factor predictors, use a recipe or formula to create indicator variables (or some other method) to make them numeric. Predictors should be in the same units before training.
The model objects are saved for each epoch so that the number of epochs can
be efficiently tuned. Both the coef() and predict() methods for this
model have an epoch argument (which defaults to the epoch with the best
loss value).
The use of the L1 penalty (a.k.a. the lasso penalty) does not force parameters to be strictly zero (as it does in packages such as glmnet). The zeroing out of parameters is a specific feature the optimization method used in those packages.
A brulee_logistic_reg object with elements:
models_obj: a serialized raw vector for the torch module.
estimates: a list of matrices with the model parameter estimates per
epoch.
best_epoch: an integer for the epoch with the smallest loss.
loss: A vector of loss values (MSE for regression, negative log-
likelihood for classification) at each epoch.
dim: A list of data dimensions.
parameters: A list of some tuning parameter values.
blueprint: The hardhat blueprint data.
predict.brulee_logistic_reg(), coef.brulee_logistic_reg(),
autoplot.brulee_logistic_reg()
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(recipes) library(yardstick) ## ----------------------------------------------------------------------------- # increase # epochs to get better results data(cells, package = "modeldata") cells$case <- NULL set.seed(122) in_train <- sample(1:nrow(cells), 1000) cells_train <- cells[ in_train,] cells_test <- cells[-in_train,] # Using matrices set.seed(1) brulee_logistic_reg(x = as.matrix(cells_train[, c("fiber_width_ch_1", "width_ch_1")]), y = cells_train$class, penalty = 0.10, epochs = 3) # Using recipe library(recipes) cells_rec <- recipe(class ~ ., data = cells_train) |> # Transform some highly skewed predictors step_YeoJohnson(all_numeric_predictors()) |> step_normalize(all_numeric_predictors()) |> step_pca(all_numeric_predictors(), num_comp = 10) set.seed(2) fit <- brulee_logistic_reg(cells_rec, data = cells_train, penalty = .01, epochs = 5) fit autoplot(fit) library(yardstick) predict(fit, cells_test, type = "prob") |> bind_cols(cells_test) |> roc_auc(class, .pred_PS) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(recipes) library(yardstick) ## ----------------------------------------------------------------------------- # increase # epochs to get better results data(cells, package = "modeldata") cells$case <- NULL set.seed(122) in_train <- sample(1:nrow(cells), 1000) cells_train <- cells[ in_train,] cells_test <- cells[-in_train,] # Using matrices set.seed(1) brulee_logistic_reg(x = as.matrix(cells_train[, c("fiber_width_ch_1", "width_ch_1")]), y = cells_train$class, penalty = 0.10, epochs = 3) # Using recipe library(recipes) cells_rec <- recipe(class ~ ., data = cells_train) |> # Transform some highly skewed predictors step_YeoJohnson(all_numeric_predictors()) |> step_normalize(all_numeric_predictors()) |> step_pca(all_numeric_predictors(), num_comp = 10) set.seed(2) fit <- brulee_logistic_reg(cells_rec, data = cells_train, penalty = .01, epochs = 5) fit autoplot(fit) library(yardstick) predict(fit, cells_test, type = "prob") |> bind_cols(cells_test) |> roc_auc(class, .pred_PS) }
brulee_mlp() fits neural network models. Multiple layers can be used. For
working with two-layer networks in tidymodels, brulee_mlp_two_layer() can
be helpful for specifying tuning parameters as scalars.
brulee_mlp(x, ...) ## Default S3 method: brulee_mlp(x, ...) ## S3 method for class 'data.frame' brulee_mlp( x, y, epochs = 100L, hidden_units = 3L, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_mlp( x, y, epochs = 100L, hidden_units = 3L, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_mlp( formula, data, epochs = 100L, hidden_units = 3L, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_mlp( x, data, epochs = 100L, hidden_units = 3L, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) brulee_mlp_two_layer(x, ...) ## Default S3 method: brulee_mlp_two_layer(x, ...) ## S3 method for class 'data.frame' brulee_mlp_two_layer( x, y, epochs = 100L, hidden_units = 3L, hidden_units_2 = 3L, activation = "relu", activation_2 = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_mlp_two_layer( x, y, epochs = 100L, hidden_units = 3L, hidden_units_2 = 3L, activation = "relu", activation_2 = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_mlp_two_layer( formula, data, epochs = 100L, hidden_units = 3L, hidden_units_2 = 3L, activation = "relu", activation_2 = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_mlp_two_layer( x, data, epochs = 100L, hidden_units = 3L, hidden_units_2 = 3L, activation = "relu", activation_2 = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... )brulee_mlp(x, ...) ## Default S3 method: brulee_mlp(x, ...) ## S3 method for class 'data.frame' brulee_mlp( x, y, epochs = 100L, hidden_units = 3L, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_mlp( x, y, epochs = 100L, hidden_units = 3L, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_mlp( formula, data, epochs = 100L, hidden_units = 3L, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_mlp( x, data, epochs = 100L, hidden_units = 3L, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) brulee_mlp_two_layer(x, ...) ## Default S3 method: brulee_mlp_two_layer(x, ...) ## S3 method for class 'data.frame' brulee_mlp_two_layer( x, y, epochs = 100L, hidden_units = 3L, hidden_units_2 = 3L, activation = "relu", activation_2 = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_mlp_two_layer( x, y, epochs = 100L, hidden_units = 3L, hidden_units_2 = 3L, activation = "relu", activation_2 = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_mlp_two_layer( formula, data, epochs = 100L, hidden_units = 3L, hidden_units_2 = 3L, activation = "relu", activation_2 = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_mlp_two_layer( x, data, epochs = 100L, hidden_units = 3L, hidden_units_2 = 3L, activation = "relu", activation_2 = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... )
x |
Depending on the context:
The predictor data should be standardized (e.g. centered or scaled). |
... |
Options to pass to the learning rate schedulers via
|
y |
When
|
epochs |
An integer for the number of epochs of training. |
|
An integer for the number of hidden units, or a vector
of integers. If a vector of integers, the model will have |
|
activation |
A character vector for the activation function (such as
"relu", "tanh", "sigmoid", and so on). See |
penalty |
The amount of weight decay (i.e., L2 regularization). |
mixture |
Proportion of Lasso Penalty (type: double, default: 0.0). A
value of mixture = 1 corresponds to a pure lasso model, while mixture = 0
indicates ridge regression (a.k.a weight decay). Must be zero for
optimizers |
dropout |
The proportion of parameters set to zero. |
validation |
The proportion of the data randomly assigned to a validation set. |
optimizer |
The method used in the optimization procedure. Possible choices
are |
learn_rate |
A positive number that controls the initial rapidity that the model moves along the descent path. Values around 0.1 or less are typical. |
rate_schedule |
A single character value for how the learning rate
should change as the optimization proceeds. Possible values are
|
momentum |
A positive number usually on |
batch_size |
An integer for the number of training set points in each
batch. ( |
class_weights |
Numeric class weights (classification only). The value can be:
|
stop_iter |
A non-negative integer for how many iterations with no improvement before stopping. |
grad_norm_clip, grad_value_clip
|
Two numeric values, possibly |
verbose |
A logical that prints out the iteration history. |
device |
A single character string for the device to train on (e.g.,
|
formula |
A formula specifying the outcome term(s) on the left-hand side, and the predictor term(s) on the right-hand side. |
data |
When a recipe or formula is used,
|
|
An integer for the number of hidden units for a second layer. |
|
activation_2 |
A character vector for the activation function for a second layer. |
This function fits feed-forward neural network models for regression (when the outcome is a number) or classification (a factor). For regression, the mean squared error is optimized and cross-entropy is the loss function for classification.
When the outcome is a number, the function internally standardizes the outcome data to have mean zero and a standard deviation of one. The prediction function creates predictions on the original scale.
By default, training halts when the validation loss increases for at least
step_iter iterations. If validation = 0 the training set loss is used.
The predictors data should all be numeric and encoded in the same units (e.g. standardized to the same range or distribution). If there are factor predictors, use a recipe or formula to create indicator variables (or some other method) to make them numeric. Predictors should be in the same units before training.
The model objects are saved for each epoch so that the number of epochs can
be efficiently tuned. Both the coef() and predict() methods for this
model have an epoch argument (which defaults to the epoch with the best
loss value).
The use of the L1 penalty (a.k.a. the lasso penalty) does not force parameters to be strictly zero (as it does in packages such as glmnet). The zeroing out of parameters is a specific feature the optimization method used in those packages.
The learning rate can be set to constant (the default) or dynamically set
via a learning rate scheduler (via the rate_schedule). Using
rate_schedule = 'none' uses the learn_rate argument. Otherwise, any
arguments to the schedulers can be passed via ....
A brulee_mlp object with elements:
models_obj: a serialized raw vector for the torch module.
estimates: a list of matrices with the model parameter estimates per
epoch.
best_epoch: an integer for the epoch with the smallest loss.
loss: A vector of loss values (MSE for regression, negative log-
likelihood for classification) at each epoch.
dim: A list of data dimensions.
y_stats: A list of summary statistics for numeric outcomes.
parameters: A list of some tuning parameter values.
blueprint: The hardhat blueprint data.
adagrad (adaptive gradient algorithm): Duchi, J., Hazan, E., & Singer, Y. (2011). Adaptive subgradient methods for online learning and stochastic optimization. Journal of machine learning research, 12(7).
adadelta: Zeiler, M. D. (2012). Adadelta: an adaptive learning rate method. arXiv preprint arXiv:1212.5701.
ADAMw: Loshchilov, I., & Hutter, F. (2017). Decoupled weight decay regularization. arXiv preprint arXiv:1711.05101.
predict.brulee_mlp(), coef.brulee_mlp(), autoplot.brulee_mlp()
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { ## ----------------------------------------------------------------------------- # regression examples (increase # epochs to get better results) data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(122) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using matrices set.seed(1) fit <- brulee_mlp(x = as.matrix(ames_train[, c("Longitude", "Latitude")]), y = ames_train$Sale_Price, penalty = 0.10) # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Bldg_Type + Neighborhood + Year_Built + Gr_Liv_Area + Full_Bath + Year_Sold + Lot_Area + Central_Air + Longitude + Latitude, data = ames_train) |> # Transform some highly skewed predictors step_BoxCox(Lot_Area, Gr_Liv_Area) |> # Lump some rarely occurring categories into "other" step_other(Neighborhood, threshold = 0.05) |> # Encode categorical predictors as binary. step_dummy(all_nominal_predictors(), one_hot = TRUE) |> # Add an interaction effect: step_interact(~ starts_with("Central_Air"):Year_Built) |> step_zv(all_predictors()) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_mlp(ames_rec, data = ames_train, hidden_units = 20, dropout = 0.05, rate_schedule = "cyclic", step_size = 4) fit autoplot(fit) library(ggplot2) predict(fit, ames_test) |> bind_cols(ames_test) |> ggplot(aes(x = .pred, y = Sale_Price)) + geom_abline(col = "green") + geom_point(alpha = .3) + lims(x = c(4, 6), y = c(4, 6)) + coord_fixed(ratio = 1) library(yardstick) predict(fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) # Using multiple hidden layers and activation functions set.seed(2) hidden_fit <- brulee_mlp(ames_rec, data = ames_train, hidden_units = c(15L, 17L), activation = c("relu", "elu"), dropout = 0.05, rate_schedule = "cyclic", step_size = 4) predict(hidden_fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) # ------------------------------------------------------------------------------ # classification library(dplyr) library(ggplot2) data("parabolic", package = "modeldata") set.seed(1) in_train <- sample(1:nrow(parabolic), 300) parabolic_tr <- parabolic[ in_train,] parabolic_te <- parabolic[-in_train,] set.seed(2) cls_fit <- brulee_mlp(class ~ ., data = parabolic_tr, hidden_units = 2, epochs = 200L, learn_rate = 0.1, activation = "elu", penalty = 0.1, batch_size = 2^8, optimizer = "SGD") summary(cls_fit) autoplot(cls_fit) grid_points <- seq(-4, 4, length.out = 100) grid <- expand.grid(X1 = grid_points, X2 = grid_points) predict(cls_fit, grid, type = "prob") |> bind_cols(grid) |> ggplot(aes(X1, X2)) + geom_contour(aes(z = .pred_Class1), breaks = 1/2, col = "black") + geom_point(data = parabolic_te, aes(col = class)) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { ## ----------------------------------------------------------------------------- # regression examples (increase # epochs to get better results) data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(122) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using matrices set.seed(1) fit <- brulee_mlp(x = as.matrix(ames_train[, c("Longitude", "Latitude")]), y = ames_train$Sale_Price, penalty = 0.10) # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Bldg_Type + Neighborhood + Year_Built + Gr_Liv_Area + Full_Bath + Year_Sold + Lot_Area + Central_Air + Longitude + Latitude, data = ames_train) |> # Transform some highly skewed predictors step_BoxCox(Lot_Area, Gr_Liv_Area) |> # Lump some rarely occurring categories into "other" step_other(Neighborhood, threshold = 0.05) |> # Encode categorical predictors as binary. step_dummy(all_nominal_predictors(), one_hot = TRUE) |> # Add an interaction effect: step_interact(~ starts_with("Central_Air"):Year_Built) |> step_zv(all_predictors()) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_mlp(ames_rec, data = ames_train, hidden_units = 20, dropout = 0.05, rate_schedule = "cyclic", step_size = 4) fit autoplot(fit) library(ggplot2) predict(fit, ames_test) |> bind_cols(ames_test) |> ggplot(aes(x = .pred, y = Sale_Price)) + geom_abline(col = "green") + geom_point(alpha = .3) + lims(x = c(4, 6), y = c(4, 6)) + coord_fixed(ratio = 1) library(yardstick) predict(fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) # Using multiple hidden layers and activation functions set.seed(2) hidden_fit <- brulee_mlp(ames_rec, data = ames_train, hidden_units = c(15L, 17L), activation = c("relu", "elu"), dropout = 0.05, rate_schedule = "cyclic", step_size = 4) predict(hidden_fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) # ------------------------------------------------------------------------------ # classification library(dplyr) library(ggplot2) data("parabolic", package = "modeldata") set.seed(1) in_train <- sample(1:nrow(parabolic), 300) parabolic_tr <- parabolic[ in_train,] parabolic_te <- parabolic[-in_train,] set.seed(2) cls_fit <- brulee_mlp(class ~ ., data = parabolic_tr, hidden_units = 2, epochs = 200L, learn_rate = 0.1, activation = "elu", penalty = 0.1, batch_size = 2^8, optimizer = "SGD") summary(cls_fit) autoplot(cls_fit) grid_points <- seq(-4, 4, length.out = 100) grid <- expand.grid(X1 = grid_points, X2 = grid_points) predict(cls_fit, grid, type = "prob") |> bind_cols(grid) |> ggplot(aes(X1, X2)) + geom_contour(aes(z = .pred_Class1), breaks = 1/2, col = "black") + geom_point(data = parabolic_te, aes(col = class)) }
brulee_multinomial_reg() fits a model.
brulee_multinomial_reg(x, ...) ## Default S3 method: brulee_multinomial_reg(x, ...) ## S3 method for class 'data.frame' brulee_multinomial_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_multinomial_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_multinomial_reg( formula, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_multinomial_reg( x, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... )brulee_multinomial_reg(x, ...) ## Default S3 method: brulee_multinomial_reg(x, ...) ## S3 method for class 'data.frame' brulee_multinomial_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_multinomial_reg( x, y, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_multinomial_reg( formula, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_multinomial_reg( x, data, epochs = 20L, penalty = 0.001, mixture = 0, validation = 0.1, optimizer = "LBFGS", learn_rate = 1, momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, verbose = FALSE, device = NULL, ... )
x |
Depending on the context:
The predictor data should be standardized (e.g. centered or scaled). |
... |
Options to pass to the learning rate schedulers via
|
y |
When
|
epochs |
An integer for the number of epochs of training. |
penalty |
The amount of weight decay (i.e., L2 regularization). |
mixture |
Proportion of Lasso Penalty (type: double, default: 0.0). A
value of mixture = 1 corresponds to a pure lasso model, while mixture = 0
indicates ridge regression (a.k.a weight decay). Must be zero for
optimizers |
validation |
The proportion of the data randomly assigned to a validation set. |
optimizer |
The method used in the optimization procedure. Possible choices
are |
learn_rate |
A positive number that controls the initial rapidity that the model moves along the descent path. Values around 0.1 or less are typical. |
momentum |
A positive number usually on |
batch_size |
An integer for the number of training set points in each
batch. ( |
class_weights |
Numeric class weights (classification only). The value can be:
|
stop_iter |
A non-negative integer for how many iterations with no improvement before stopping. |
verbose |
A logical that prints out the iteration history. |
device |
A single character string for the device to train on (e.g.,
|
formula |
A formula specifying the outcome term(s) on the left-hand side, and the predictor term(s) on the right-hand side. |
data |
When a recipe or formula is used,
|
This function fits a linear combination of coefficients and predictors to model the log of the class probabilities. The training process optimizes the cross-entropy loss function.
By default, training halts when the validation loss increases for at least
step_iter iterations. If validation = 0 the training set loss is used.
The predictors data should all be numeric and encoded in the same units (e.g. standardized to the same range or distribution). If there are factor predictors, use a recipe or formula to create indicator variables (or some other method) to make them numeric. Predictors should be in the same units before training.
The model objects are saved for each epoch so that the number of epochs can
be efficiently tuned. Both the coef() and predict() methods for this
model have an epoch argument (which defaults to the epoch with the best
loss value).
The use of the L1 penalty (a.k.a. the lasso penalty) does not force parameters to be strictly zero (as it does in packages such as glmnet). The zeroing out of parameters is a specific feature the optimization method used in those packages.
A brulee_multinomial_reg object with elements:
models_obj: a serialized raw vector for the torch module.
estimates: a list of matrices with the model parameter estimates per
epoch.
best_epoch: an integer for the epoch with the smallest loss.
loss: A vector of loss values (MSE for regression, negative log-
likelihood for classification) at each epoch.
dim: A list of data dimensions.
parameters: A list of some tuning parameter values.
blueprint: The hardhat blueprint data.
predict.brulee_multinomial_reg(), coef.brulee_multinomial_reg(),
autoplot.brulee_multinomial_reg()
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(recipes) library(yardstick) data(penguins, package = "modeldata") penguins <- penguins |> na.omit() set.seed(122) in_train <- sample(1:nrow(penguins), 200) penguins_train <- penguins[ in_train,] penguins_test <- penguins[-in_train,] rec <- recipe(island ~ ., data = penguins_train) |> step_dummy(species, sex) |> step_normalize(all_predictors()) set.seed(3) fit <- brulee_multinomial_reg(rec, data = penguins_train, epochs = 5) fit predict(fit, penguins_test) |> bind_cols(penguins_test) |> conf_mat(island, .pred_class) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(recipes) library(yardstick) data(penguins, package = "modeldata") penguins <- penguins |> na.omit() set.seed(122) in_train <- sample(1:nrow(penguins), 200) penguins_train <- penguins[ in_train,] penguins_test <- penguins[-in_train,] rec <- recipe(island ~ ., data = penguins_train) |> step_dummy(species, sex) |> step_normalize(all_predictors()) set.seed(3) fit <- brulee_multinomial_reg(rec, data = penguins_train, epochs = 5) fit predict(fit, penguins_test) |> bind_cols(penguins_test) |> conf_mat(island, .pred_class) }
brulee_resnet() fits residual network models with skip connections.
brulee_resnet(x, ...) ## Default S3 method: brulee_resnet(x, ...) ## S3 method for class 'data.frame' brulee_resnet( x, y, epochs = 100L, hidden_units = 3L, bottleneck_units = hidden_units, residual_at = NULL, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_resnet( x, y, epochs = 100L, hidden_units = 3L, bottleneck_units = hidden_units, residual_at = NULL, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_resnet( formula, data, epochs = 100L, hidden_units = 3L, bottleneck_units = hidden_units, residual_at = NULL, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_resnet( x, data, epochs = 100L, hidden_units = 3L, bottleneck_units = hidden_units, residual_at = NULL, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... )brulee_resnet(x, ...) ## Default S3 method: brulee_resnet(x, ...) ## S3 method for class 'data.frame' brulee_resnet( x, y, epochs = 100L, hidden_units = 3L, bottleneck_units = hidden_units, residual_at = NULL, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_resnet( x, y, epochs = 100L, hidden_units = 3L, bottleneck_units = hidden_units, residual_at = NULL, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_resnet( formula, data, epochs = 100L, hidden_units = 3L, bottleneck_units = hidden_units, residual_at = NULL, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_resnet( x, data, epochs = 100L, hidden_units = 3L, bottleneck_units = hidden_units, residual_at = NULL, activation = "relu", penalty = 0.001, mixture = 0, dropout = 0, validation = 0.1, optimizer = "ADAMw", learn_rate = 0.01, rate_schedule = "none", momentum = 0, batch_size = NULL, class_weights = NULL, stop_iter = 5, grad_value_clip = 5, grad_norm_clip = 5, verbose = FALSE, device = NULL, ... )
x |
Depending on the context:
The predictor data should be standardized (e.g. centered or scaled). |
... |
Options to pass to the learning rate schedulers via
|
y |
When
|
epochs |
An integer for the number of epochs of training. |
|
An integer vector specifying the number of hidden units in each layer. The length of this vector determines the number of layers. Each value must be >= 1. |
|
bottleneck_units |
An integer vector specifying the intermediate dimension
within each layer. Must have the same length as |
residual_at |
An integer vector specifying which layer indices should
have residual (skip) connections. For example, |
activation |
A character vector for the activation function (such as
"relu", "tanh", "sigmoid", and so on). See |
penalty |
The amount of weight decay (i.e., L2 regularization). |
mixture |
Proportion of Lasso Penalty (type: double, default: 0.0). A
value of mixture = 1 corresponds to a pure lasso model, while mixture = 0
indicates ridge regression (a.k.a weight decay). Must be zero for
optimizers |
dropout |
The proportion of parameters set to zero. |
validation |
The proportion of the data randomly assigned to a validation set. |
optimizer |
The method used in the optimization procedure. Possible choices
are |
learn_rate |
A positive number that controls the initial rapidity that the model moves along the descent path. Values around 0.1 or less are typical. |
rate_schedule |
A single character value for how the learning rate
should change as the optimization proceeds. Possible values are
|
momentum |
A positive number usually on |
batch_size |
An integer for the number of training set points in each
batch. ( |
class_weights |
Numeric class weights (classification only). The value can be:
|
stop_iter |
A non-negative integer for how many iterations with no improvement before stopping. |
grad_norm_clip, grad_value_clip
|
Two numeric values, possibly |
verbose |
A logical that prints out the iteration history. |
device |
A single character string for the device to train on (e.g.,
|
formula |
A formula specifying the outcome term(s) on the left-hand side, and the predictor term(s) on the right-hand side. |
data |
When a recipe or formula is used,
|
This function fits residual network (ResNet) models for regression (when the outcome is a number) or classification (a factor). ResNets use skip connections that add the input of a block to its output, allowing gradients to flow more easily through deep networks. For regression, the mean squared error is optimized and cross-entropy is the loss function for classification.
The network consists of a sequence of layers, each with batch normalization,
two linear transformations (with an intermediate bottleneck dimension), and
activation functions. Residual (skip) connections can be placed at specified
layers via the residual_at parameter.
Each layer follows this pattern:
Batch normalization (input dimension)
Linear transformation (input dimension -> bottleneck_units[i])
Activation function (ReLU by default)
Dropout (if specified)
Linear transformation (bottleneck_units[i] -> hidden_units[i])
Dropout (if specified)
When a residual connection is specified at layer i via residual_at, the
output of layer i is added to the input from the start of that residual
block. If dimensions don't match, a linear projection is automatically added.
The residual_at parameter defines where skip connections occur:
residual_at = 3 creates one block spanning layers 1-3
residual_at = c(2, 4) creates two blocks: layers 1-2 and layers 3-4
residual_at = NULL (default) places a skip connection at every layer
residual_at = integer(0) creates no residual connections (a purely
feed-forward model)
The learning rate can be set to constant (the default) or dynamically set
via a learning rate scheduler (via the rate_schedule). Using
rate_schedule = 'none' uses the learn_rate argument. Otherwise, any
arguments to the schedulers can be passed via ....
When the outcome is a number, the function internally standardizes the outcome data to have mean zero and a standard deviation of one. The prediction function creates predictions on the original scale.
By default, training halts when the validation loss increases for at least
step_iter iterations. If validation = 0 the training set loss is used.
The predictors data should all be numeric and encoded in the same units (e.g. standardized to the same range or distribution). If there are factor predictors, use a recipe or formula to create indicator variables (or some other method) to make them numeric. Predictors should be in the same units before training.
The model objects are saved for each epoch so that the number of epochs can
be efficiently tuned. Both the coef() and predict() methods for this
model have an epoch argument (which defaults to the epoch with the best
loss value).
The use of the L1 penalty (a.k.a. the lasso penalty) does not force parameters to be strictly zero (as it does in packages such as glmnet). The zeroing out of parameters is a specific feature the optimization method used in those packages.
A brulee_resnet object with elements:
models_obj: a serialized raw vector for the torch module.
estimates: a list of matrices with the model parameter estimates per
epoch.
best_epoch: an integer for the epoch with the smallest loss.
loss: A vector of loss values (MSE for regression, negative log-
likelihood for classification) at each epoch.
dim: A list of data dimensions.
y_stats: A list of summary statistics for numeric outcomes.
parameters: A list of some tuning parameter values.
blueprint: The hardhat blueprint data.
He, K., Zhang, X., Ren, S., & Sun, J. (2016). Deep residual learning for image recognition. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 770-778).
He, K., Zhang, X., Ren, S., & Sun, J. (2016). Identity mappings in deep residual networks. In European conference on computer vision (pp. 630-645). Springer, Cham.
Gorishniy, Y., Rubachev, I., Khrulkov, V., & Babenko, A. (2021). Revisiting deep learning models for tabular data. Advances in neural information processing systems, 34, 18932-18943.
Sandler, M., Howard, A., Zhu, M., Zhmoginov, A., & Chen, L. C. (2018). Mobilenetv2: Inverted residuals and linear bottlenecks. In Proceedings of the IEEE conference on computer vision and pattern recognition (pp. 4510-4520).
predict.brulee_resnet(), coef.brulee_resnet(),
autoplot.brulee_resnet()
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { ## ----------------------------------------------------------------------------- # regression examples (increase # epochs to get better results) data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(122) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_resnet(ames_rec, data = ames_train, hidden_units = c(20, 10), bottleneck_units = c(15, 8), residual_at = 2, epochs = 50, batch_size = 32) fit summary(fit) autoplot(fit) library(yardstick) predict(fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) # ------------------------------------------------------------------------------ # classification library(dplyr) data("parabolic", package = "modeldata") set.seed(1) in_train <- sample(1:nrow(parabolic), 300) parabolic_tr <- parabolic[ in_train,] parabolic_te <- parabolic[-in_train,] set.seed(2) cls_fit <- brulee_resnet(class ~ ., data = parabolic_tr, hidden_units = c(8, 5), bottleneck_units = c(6, 4), residual_at = 1:2, epochs = 200L, learn_rate = 0.1, activation = "elu", penalty = 0.1, batch_size = 2^8) autoplot(cls_fit) predict(cls_fit, parabolic_te, type = "prob") |> bind_cols(parabolic_te) |> roc_auc(class, .pred_Class1) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { ## ----------------------------------------------------------------------------- # regression examples (increase # epochs to get better results) data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(122) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_resnet(ames_rec, data = ames_train, hidden_units = c(20, 10), bottleneck_units = c(15, 8), residual_at = 2, epochs = 50, batch_size = 32) fit summary(fit) autoplot(fit) library(yardstick) predict(fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) # ------------------------------------------------------------------------------ # classification library(dplyr) data("parabolic", package = "modeldata") set.seed(1) in_train <- sample(1:nrow(parabolic), 300) parabolic_tr <- parabolic[ in_train,] parabolic_te <- parabolic[-in_train,] set.seed(2) cls_fit <- brulee_resnet(class ~ ., data = parabolic_tr, hidden_units = c(8, 5), bottleneck_units = c(6, 4), residual_at = 1:2, epochs = 200L, learn_rate = 0.1, activation = "elu", penalty = 0.1, batch_size = 2^8) autoplot(cls_fit) predict(cls_fit, parabolic_te, type = "prob") |> bind_cols(parabolic_te) |> roc_auc(class, .pred_Class1) }
brulee_rln() fits a single-hidden-layer neural network where each weight
learns its own adaptive regularization coefficient.
brulee_rln(x, ...) ## Default S3 method: brulee_rln(x, ...) ## S3 method for class 'data.frame' brulee_rln( x, y, epochs = 100L, hidden_units = 5L, penalty_type = "L1", penalty_average = 1e-10, step_rate = 1e+06, activation = "relu", validation = 0.1, optimizer = "ADAMw", learn_rate = 0.001, rate_schedule = "none", momentum = 0, batch_size = NULL, stop_iter = 20, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_rln( x, y, epochs = 100L, hidden_units = 5L, penalty_type = "L1", penalty_average = 1e-10, step_rate = 1e+06, activation = "relu", validation = 0.1, optimizer = "ADAMw", learn_rate = 0.001, rate_schedule = "none", momentum = 0, batch_size = NULL, stop_iter = 20, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_rln( formula, data, epochs = 100L, hidden_units = 5L, penalty_type = "L1", penalty_average = 1e-10, step_rate = 1e+06, activation = "relu", validation = 0.1, optimizer = "ADAMw", learn_rate = 0.001, rate_schedule = "none", momentum = 0, batch_size = NULL, stop_iter = 20, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_rln( x, data, epochs = 100L, hidden_units = 5L, penalty_type = "L1", penalty_average = 1e-10, step_rate = 1e+06, activation = "relu", validation = 0.1, optimizer = "ADAMw", learn_rate = 0.001, rate_schedule = "none", momentum = 0, batch_size = NULL, stop_iter = 20, verbose = FALSE, device = NULL, ... )brulee_rln(x, ...) ## Default S3 method: brulee_rln(x, ...) ## S3 method for class 'data.frame' brulee_rln( x, y, epochs = 100L, hidden_units = 5L, penalty_type = "L1", penalty_average = 1e-10, step_rate = 1e+06, activation = "relu", validation = 0.1, optimizer = "ADAMw", learn_rate = 0.001, rate_schedule = "none", momentum = 0, batch_size = NULL, stop_iter = 20, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'matrix' brulee_rln( x, y, epochs = 100L, hidden_units = 5L, penalty_type = "L1", penalty_average = 1e-10, step_rate = 1e+06, activation = "relu", validation = 0.1, optimizer = "ADAMw", learn_rate = 0.001, rate_schedule = "none", momentum = 0, batch_size = NULL, stop_iter = 20, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'formula' brulee_rln( formula, data, epochs = 100L, hidden_units = 5L, penalty_type = "L1", penalty_average = 1e-10, step_rate = 1e+06, activation = "relu", validation = 0.1, optimizer = "ADAMw", learn_rate = 0.001, rate_schedule = "none", momentum = 0, batch_size = NULL, stop_iter = 20, verbose = FALSE, device = NULL, ... ) ## S3 method for class 'recipe' brulee_rln( x, data, epochs = 100L, hidden_units = 5L, penalty_type = "L1", penalty_average = 1e-10, step_rate = 1e+06, activation = "relu", validation = 0.1, optimizer = "ADAMw", learn_rate = 0.001, rate_schedule = "none", momentum = 0, batch_size = NULL, stop_iter = 20, verbose = FALSE, device = NULL, ... )
x |
Depending on the context:
The predictor data should be standardized (e.g. centered or scaled). |
... |
Options to pass to the learning rate schedulers via
|
y |
When
|
epochs |
An integer for the number of epochs of training. |
|
An integer for the number of units in the single hidden layer. Must be >= 1. |
|
penalty_type |
A string for the regularization norm: |
penalty_average |
A positive numeric value for the target geometric mean
of the per-weight regularization coefficients (Theta in Shavitt and Segal
(2018)), on the natural scale. Converted to log10 scale internally. Default
is |
step_rate |
A positive numeric value for the step size used to update
the per-weight regularization coefficients (nu in Shavitt and Segal (2018)),
on the natural scale. Converted to log10 scale internally; the multiplier
applied is |
activation |
A character vector for the activation function (such as
"relu", "tanh", "sigmoid", and so on). See |
validation |
The proportion of the data randomly assigned to a validation set. |
optimizer |
The method used in the optimization procedure. Possible choices
are |
learn_rate |
A positive number that controls the initial rapidity that the model moves along the descent path. Values around 0.1 or less are typical. |
rate_schedule |
A single character value for how the learning rate
should change as the optimization proceeds. Possible values are
|
momentum |
A positive number usually on |
batch_size |
An integer for the number of training set points in each
batch. ( |
stop_iter |
A non-negative integer for how many iterations with no improvement before stopping. |
verbose |
A logical that prints out the iteration history. |
device |
A single character string for the device to train on (e.g.,
|
formula |
A formula specifying the outcome term(s) on the left-hand side, and the predictor term(s) on the right-hand side. |
data |
When a recipe or formula is used,
|
This function fits Regularization Learning Network (RLN) models for
regression (numeric outcomes only). Unlike standard regularization, which
applies a single global penalty, RLN learns a separate regularization
coefficient for each weight in the hidden layer. After each gradient step,
the per-weight coefficients (lambdas) are updated and projected to keep
their mean at log10(penalty_average) * log(10).
RLNs are designed for tabular datasets where interpretability matters. The per-weight regularization tends to produce very sparse networks. The original paper reports eliminating up to ~99.8% of network edges and ~82% of input features. This sparsity makes it easier to identify which inputs the network considers important, and the resulting models are competitive with gradient boosted trees. The best results in the paper are achieved by ensembling RLNs with gradient boosting tree ensembles.
The network is a single-hidden-layer MLP:
Linear transformation (predictors -> hidden_units)
Activation function
Linear transformation (hidden_units -> 1 output)
Weights are initialized with Xavier normal initialization.
After each optimizer step, the per-weight regularization coefficients are
updated using the gradient of the Counterfactual Loss with respect to the
coefficients, then projected onto a simplex so that mean(lambda) == log10(penalty_average) * log(10).
The ADAMw optimizer is the default.
The outcome is internally standardized to have mean zero and standard deviation one. Predictions are returned on the original scale.
By default, training halts when the validation loss increases for at least
stop_iter consecutive iterations. If validation = 0 the training set
loss is used. The default for stop_iter is higher for RLN than for other
brulee models (20 vs 5) because the sparsification process takes
approximately 10-20 epochs to stabilize (Shavitt & Segal, 2018); stopping
too early prevents the per-weight regularization from taking effect.
Predictors should all be numeric and on comparable scales. Categorical predictors must be converted to dummy variables.
Model parameters are saved each epoch so that epoch can be tuned
efficiently via the epoch argument of predict.brulee_rln() and
coef.brulee_rln().
A brulee_rln object with elements:
model_obj: a serialized raw vector for the torch module.
estimates: a list of model parameter matrices per epoch.
best_epoch: an integer for the epoch with the smallest loss.
loss: a numeric vector of loss values (scaled MSE) at each epoch.
dims: a list of data dimensions.
y_stats: a list of mean and standard deviation for the outcome.
parameters: a list of tuning parameter values.
device: a character string for the device used during training.
blueprint: the hardhat blueprint data.
Shavitt, I., & Segal, E. (2018). Regularization learning networks: Deep learning for tabular datasets. In Advances in neural information processing systems (pp. 1379-1389).
predict.brulee_rln(), coef.brulee_rln(),
autoplot.brulee_rln()
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(122) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_rln(ames_rec, data = ames_train, hidden_units = 20L, epochs = 50L) fit autoplot(fit) library(yardstick) predict(fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(122) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_rln(ames_rec, data = ames_train, hidden_units = 20L, epochs = 50L) fit autoplot(fit) library(yardstick) predict(fit, ames_test) |> bind_cols(ames_test) |> rmse(Sale_Price, .pred) }
Plot model loss over epochs
## S3 method for class 'brulee_mlp' autoplot(object, ...) ## S3 method for class 'brulee_logistic_reg' autoplot(object, ...) ## S3 method for class 'brulee_multinomial_reg' autoplot(object, ...) ## S3 method for class 'brulee_linear_reg' autoplot(object, ...) ## S3 method for class 'brulee_resnet' autoplot(object, ...) ## S3 method for class 'brulee_auto_int' autoplot(object, ...) ## S3 method for class 'brulee_rln' autoplot(object, ...)## S3 method for class 'brulee_mlp' autoplot(object, ...) ## S3 method for class 'brulee_logistic_reg' autoplot(object, ...) ## S3 method for class 'brulee_multinomial_reg' autoplot(object, ...) ## S3 method for class 'brulee_linear_reg' autoplot(object, ...) ## S3 method for class 'brulee_resnet' autoplot(object, ...) ## S3 method for class 'brulee_auto_int' autoplot(object, ...) ## S3 method for class 'brulee_rln' autoplot(object, ...)
object |
A |
... |
Not currently used |
This function plots the loss function across the available epochs. A vertical line shows the epoch with the best loss value.
A ggplot object.
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(ggplot2) library(recipes) theme_set(theme_bw()) data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_mlp(ames_rec, data = ames_train, epochs = 50, batch_size = 32) autoplot(fit) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(ggplot2) library(recipes) theme_set(theme_bw()) data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_mlp(ames_rec, data = ames_train, epochs = 50, batch_size = 32) autoplot(fit) }
Extract Model Coefficients
## S3 method for class 'brulee_logistic_reg' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_linear_reg' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_mlp' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_multinomial_reg' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_resnet' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_rln' coef(object, epoch = NULL, ...)## S3 method for class 'brulee_logistic_reg' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_linear_reg' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_mlp' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_multinomial_reg' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_resnet' coef(object, epoch = NULL, ...) ## S3 method for class 'brulee_rln' coef(object, epoch = NULL, ...)
object |
A model fit from brulee. |
epoch |
A single integer for the training iteration. If left |
... |
Not currently used. |
For logistic/linear regression, a named vector. For neural networks, a list of arrays.
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_linear_reg(ames_rec, data = ames_train, epochs = 50) coef(fit) coef(fit, epoch = 1) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_linear_reg(ames_rec, data = ames_train, epochs = 50) coef(fit) coef(fit, epoch = 1) }
For an x/y interface, matrix_to_dataset() converts the data to proper
encodings then formats the results for consumption by torch.
matrix_to_dataset(x, y, device = NULL)matrix_to_dataset(x, y, device = NULL)
x |
A numeric matrix of predictors. |
y |
A vector. If regression than |
device |
A single character string for the device to use (e.g., |
Missing values should be removed before passing data to this function.
An R6 index sampler object with classes "training_set", "dataset", and "R6".
if (torch::torch_is_installed()) { matrix_to_dataset(as.matrix(mtcars[, -1]), mtcars$mpg) }if (torch::torch_is_installed()) { matrix_to_dataset(as.matrix(mtcars[, -1]), mtcars$mpg) }
brulee_auto_int
Predict from a brulee_auto_int
## S3 method for class 'brulee_auto_int' predict(object, new_data, type = NULL, epoch = NULL, ...)## S3 method for class 'brulee_auto_int' predict(object, new_data, type = NULL, epoch = NULL, ...)
object |
A |
new_data |
A data frame or matrix of new predictors. |
type |
A single character. The type of predictions to generate. Valid options are:
|
epoch |
An integer for the epoch to make predictions. If this value
is larger than the maximum number that was fit, a warning is issued and the
parameters from the last epoch are used. If left |
... |
Not used, but required for extensibility. |
A tibble of predictions. The number of rows in the tibble is guaranteed
to be the same as the number of rows in new_data.
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { set.seed(87261) tr_data <- modeldata::sim_classification(500) te_data <- modeldata::sim_classification(50) set.seed(2) fit <- brulee_auto_int(class ~ ., data = tr_data, epochs = 50L, batch_size = 64L, stop_iter = 10L, hidden_units = 5, hidden_activations = "relu", learn_rate = 0.01, penalty = 0.01) fit autoplot(fit) predict(fit, te_data) predict(fit, te_data, type = "prob") }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { set.seed(87261) tr_data <- modeldata::sim_classification(500) te_data <- modeldata::sim_classification(50) set.seed(2) fit <- brulee_auto_int(class ~ ., data = tr_data, epochs = 50L, batch_size = 64L, stop_iter = 10L, hidden_units = 5, hidden_activations = "relu", learn_rate = 0.01, penalty = 0.01) fit autoplot(fit) predict(fit, te_data) predict(fit, te_data, type = "prob") }
brulee_linear_reg
Predict from a brulee_linear_reg
## S3 method for class 'brulee_linear_reg' predict(object, new_data, type = NULL, epoch = NULL, ...)## S3 method for class 'brulee_linear_reg' predict(object, new_data, type = NULL, epoch = NULL, ...)
object |
A |
new_data |
A data frame or matrix of new predictors. |
type |
A single character. The type of predictions to generate. Valid options are:
|
epoch |
An integer for the epoch to make predictions. If this value
is larger than the maximum number that was fit, a warning is issued and the
parameters from the last epoch are used. If left |
... |
Not used, but required for extensibility. |
A tibble of predictions. The number of rows in the tibble is guaranteed
to be the same as the number of rows in new_data.
if (torch::torch_is_installed() & rlang::is_installed("recipes")) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_linear_reg(ames_rec, data = ames_train, epochs = 50) predict(fit, ames_test) }if (torch::torch_is_installed() & rlang::is_installed("recipes")) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_linear_reg(ames_rec, data = ames_train, epochs = 50) predict(fit, ames_test) }
brulee_logistic_reg
Predict from a brulee_logistic_reg
## S3 method for class 'brulee_logistic_reg' predict(object, new_data, type = NULL, epoch = NULL, ...)## S3 method for class 'brulee_logistic_reg' predict(object, new_data, type = NULL, epoch = NULL, ...)
object |
A |
new_data |
A data frame or matrix of new predictors. |
type |
A single character. The type of predictions to generate. Valid options are:
|
epoch |
An integer for the epoch to make predictions. If this value
is larger than the maximum number that was fit, a warning is issued and the
parameters from the last epoch are used. If left |
... |
Not used, but required for extensibility. |
A tibble of predictions. The number of rows in the tibble is guaranteed
to be the same as the number of rows in new_data.
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(recipes) library(yardstick) data(penguins, package = "modeldata") penguins <- penguins |> na.omit() set.seed(122) in_train <- sample(1:nrow(penguins), 200) penguins_train <- penguins[ in_train,] penguins_test <- penguins[-in_train,] rec <- recipe(sex ~ ., data = penguins_train) |> step_dummy(all_nominal_predictors()) |> step_normalize(all_numeric_predictors()) set.seed(3) fit <- brulee_logistic_reg(rec, data = penguins_train, epochs = 5) fit predict(fit, penguins_test) predict(fit, penguins_test, type = "prob") |> bind_cols(penguins_test) |> roc_curve(sex, .pred_female) |> autoplot() }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(recipes) library(yardstick) data(penguins, package = "modeldata") penguins <- penguins |> na.omit() set.seed(122) in_train <- sample(1:nrow(penguins), 200) penguins_train <- penguins[ in_train,] penguins_test <- penguins[-in_train,] rec <- recipe(sex ~ ., data = penguins_train) |> step_dummy(all_nominal_predictors()) |> step_normalize(all_numeric_predictors()) set.seed(3) fit <- brulee_logistic_reg(rec, data = penguins_train, epochs = 5) fit predict(fit, penguins_test) predict(fit, penguins_test, type = "prob") |> bind_cols(penguins_test) |> roc_curve(sex, .pred_female) |> autoplot() }
brulee_mlp
Predict from a brulee_mlp
## S3 method for class 'brulee_mlp' predict(object, new_data, type = NULL, epoch = NULL, ...)## S3 method for class 'brulee_mlp' predict(object, new_data, type = NULL, epoch = NULL, ...)
object |
A |
new_data |
A data frame or matrix of new predictors. |
type |
A single character. The type of predictions to generate. Valid options are:
|
epoch |
An integer for the epoch to make predictions. If this value
is larger than the maximum number that was fit, a warning is issued and the
parameters from the last epoch are used. If left |
... |
Not used, but required for extensibility. |
A tibble of predictions. The number of rows in the tibble is guaranteed
to be the same as the number of rows in new_data.
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { # regression example: data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_mlp(ames_rec, data = ames_train, epochs = 50, batch_size = 32) predict(fit, ames_test) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { # regression example: data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_mlp(ames_rec, data = ames_train, epochs = 50, batch_size = 32) predict(fit, ames_test) }
brulee_multinomial_reg
Predict from a brulee_multinomial_reg
## S3 method for class 'brulee_multinomial_reg' predict(object, new_data, type = NULL, epoch = NULL, ...)## S3 method for class 'brulee_multinomial_reg' predict(object, new_data, type = NULL, epoch = NULL, ...)
object |
A |
new_data |
A data frame or matrix of new predictors. |
type |
A single character. The type of predictions to generate. Valid options are:
|
epoch |
An integer for the epoch to make predictions. If this value
is larger than the maximum number that was fit, a warning is issued and the
parameters from the last epoch are used. If left |
... |
Not used, but required for extensibility. |
A tibble of predictions. The number of rows in the tibble is guaranteed
to be the same as the number of rows in new_data.
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(recipes) library(yardstick) data(penguins, package = "modeldata") penguins <- penguins |> na.omit() set.seed(122) in_train <- sample(1:nrow(penguins), 200) penguins_train <- penguins[ in_train,] penguins_test <- penguins[-in_train,] rec <- recipe(island ~ ., data = penguins_train) |> step_dummy(species, sex) |> step_normalize(all_numeric_predictors()) set.seed(3) fit <- brulee_multinomial_reg(rec, data = penguins_train, epochs = 5) fit predict(fit, penguins_test) |> bind_cols(penguins_test) |> conf_mat(island, .pred_class) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "yardstick", "modeldata"))) { library(recipes) library(yardstick) data(penguins, package = "modeldata") penguins <- penguins |> na.omit() set.seed(122) in_train <- sample(1:nrow(penguins), 200) penguins_train <- penguins[ in_train,] penguins_test <- penguins[-in_train,] rec <- recipe(island ~ ., data = penguins_train) |> step_dummy(species, sex) |> step_normalize(all_numeric_predictors()) set.seed(3) fit <- brulee_multinomial_reg(rec, data = penguins_train, epochs = 5) fit predict(fit, penguins_test) |> bind_cols(penguins_test) |> conf_mat(island, .pred_class) }
brulee_resnet
Predict from a brulee_resnet
## S3 method for class 'brulee_resnet' predict(object, new_data, type = NULL, epoch = NULL, ...)## S3 method for class 'brulee_resnet' predict(object, new_data, type = NULL, epoch = NULL, ...)
object |
A |
new_data |
A data frame or matrix of new predictors. |
type |
A single character. The type of predictions to generate. Valid options are:
|
epoch |
An integer for the epoch to make predictions. If this value
is larger than the maximum number that was fit, a warning is issued and the
parameters from the last epoch are used. If left |
... |
Not used, but required for extensibility. |
A tibble of predictions. The number of rows in the tibble is guaranteed
to be the same as the number of rows in new_data.
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { # regression example: data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_resnet(ames_rec, data = ames_train, hidden_units = 2, num_layers = 2, bottleneck_units = 10, epochs = 50, batch_size = 32) predict(fit, ames_test) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { # regression example: data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] # Using recipe library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_resnet(ames_rec, data = ames_train, hidden_units = 2, num_layers = 2, bottleneck_units = 10, epochs = 50, batch_size = 32) predict(fit, ames_test) }
brulee_rln
Predict from a brulee_rln
## S3 method for class 'brulee_rln' predict(object, new_data, type = NULL, epoch = NULL, ...)## S3 method for class 'brulee_rln' predict(object, new_data, type = NULL, epoch = NULL, ...)
object |
A |
new_data |
A data frame or matrix of new predictors. |
type |
A single character. The only valid option is |
epoch |
An integer for the epoch to make predictions. If larger than
the number of epochs fit, a warning is issued and the last epoch is used.
If |
... |
Not used, but required for extensibility. |
A tibble of predictions with the same number of rows as new_data.
if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_rln(ames_rec, data = ames_train, hidden_units = 20L, epochs = 30L) predict(fit, ames_test) }if (torch::torch_is_installed() & rlang::is_installed(c("recipes", "modeldata"))) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) in_train <- sample(1:nrow(ames), 2000) ames_train <- ames[ in_train,] ames_test <- ames[-in_train,] library(recipes) ames_rec <- recipe(Sale_Price ~ Longitude + Latitude, data = ames_train) |> step_normalize(all_numeric_predictors()) set.seed(2) fit <- brulee_rln(ames_rec, data = ames_train, hidden_units = 20L, epochs = 30L) predict(fit, ames_test) }
Learning rate schedulers alter the learning rate to adjust as training
proceeds. In most cases, the learning rate decreases as epochs increase.
The schedule_*() functions are individual schedulers and
set_learn_rate() is a general interface.
schedule_decay_time(epoch, initial = 0.1, decay = 1) schedule_decay_expo(epoch, initial = 0.1, decay = 1) schedule_step(epoch, initial = 0.1, reduction = 1/2, steps = 5) schedule_cyclic(epoch, initial = 0.001, largest = 0.1, step_size = 5) set_learn_rate(epoch, learn_rate, type = "none", ...)schedule_decay_time(epoch, initial = 0.1, decay = 1) schedule_decay_expo(epoch, initial = 0.1, decay = 1) schedule_step(epoch, initial = 0.1, reduction = 1/2, steps = 5) schedule_cyclic(epoch, initial = 0.001, largest = 0.1, step_size = 5) set_learn_rate(epoch, learn_rate, type = "none", ...)
epoch |
An integer for the number of training epochs (zero being the initial value), |
initial |
A positive numeric value for the starting learning rate. |
decay |
A positive numeric constant for decreasing the rate (see Details below). |
reduction |
A positive numeric constant stating the proportional decrease
in the learning rate occurring at every |
steps |
The number of epochs before the learning rate changes. |
largest |
The maximum learning rate in the cycle. |
step_size |
The half-length of a cycle. |
learn_rate |
A constant learning rate (when no scheduler is used), |
type |
A single character value for the type of scheduler. Possible values are: "decay_time", "decay_expo", "none", "cyclic", and "step". |
... |
Arguments to pass to the individual scheduler functions (e.g.
|
The details for how the schedulers change the rates:
schedule_decay_time():
schedule_decay_expo():
schedule_step():
schedule_cyclic(): ,
, and
A numeric value for the updated learning rate.
if (rlang::is_installed("purrr")) { library(ggplot2) library(dplyr) library(purrr) iters <- 0:50 bind_rows( tibble(epoch = iters, rate = map_dbl(iters, schedule_decay_time), type = "decay_time"), tibble(epoch = iters, rate = map_dbl(iters, schedule_decay_expo), type = "decay_expo"), tibble(epoch = iters, rate = map_dbl(iters, schedule_step), type = "step"), tibble(epoch = iters, rate = map_dbl(iters, schedule_cyclic), type = "cyclic") ) |> ggplot(aes(epoch, rate)) + geom_line() + facet_wrap(~ type) }if (rlang::is_installed("purrr")) { library(ggplot2) library(dplyr) library(purrr) iters <- 0:50 bind_rows( tibble(epoch = iters, rate = map_dbl(iters, schedule_decay_time), type = "decay_time"), tibble(epoch = iters, rate = map_dbl(iters, schedule_decay_expo), type = "decay_expo"), tibble(epoch = iters, rate = map_dbl(iters, schedule_step), type = "step"), tibble(epoch = iters, rate = map_dbl(iters, schedule_cyclic), type = "cyclic") ) |> ggplot(aes(epoch, rate)) + geom_line() + facet_wrap(~ type) }
summary() methods brulee neural network models print a
layer-by-layer description of the fitted torch module: each component's
type, shape, and parameter count, followed by the total parameter count.
For brulee_resnet, residual (skip) connections and their projection
layers are shown at the block boundaries where they apply.
## S3 method for class 'brulee_mlp' summary(object, ...) ## S3 method for class 'brulee_resnet' summary(object, ...) ## S3 method for class 'brulee_rln' summary(object, ...) ## S3 method for class 'brulee_auto_int' summary(object, ...)## S3 method for class 'brulee_mlp' summary(object, ...) ## S3 method for class 'brulee_resnet' summary(object, ...) ## S3 method for class 'brulee_rln' summary(object, ...) ## S3 method for class 'brulee_auto_int' summary(object, ...)
object |
A |
... |
Not used. |
The model object, invisibly. Called for its side effect of printing the architecture.
if (torch::torch_is_installed() & rlang::is_installed("modeldata")) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) fit <- brulee_resnet(Sale_Price ~ Longitude + Latitude, data = ames, hidden_units = c(8, 4), bottleneck_units = c(6, 3), residual_at = 2, epochs = 3) summary(fit) }if (torch::torch_is_installed() & rlang::is_installed("modeldata")) { data(ames, package = "modeldata") ames$Sale_Price <- log10(ames$Sale_Price) set.seed(1) fit <- brulee_resnet(Sale_Price ~ Longitude + Latitude, data = ames, hidden_units = c(8, 4), bottleneck_units = c(6, 3), residual_at = 2, epochs = 3) summary(fit) }
There are ways to speed up or slow down model training. Here are some notes.
GPUs can perform calculations very fast, sometimes faster than the overhead of a high-level interface such as brulee. GPU utilization might be lower than expected because the model is not very large (i.e., with millions of parameters) and/or because the batch size is small.
For the latter, here is an example of a training set with 1K samples, one single hidden layer with 50 units, 200 epochs, and used ADAMw optimizer:
(CPU/CUDA)
batch_size CPU elapsed CUDA elapsed speedup
128 90.09s 111.54s 0.81x
512 26.22s 28.61s 0.92x
2048 11.07s 8.31s 1.33x
8192 4.42s 3.57s 1.24x
As batch sizes become larger, the GPU has a better chance of reducing training time.
Some optimizers are faster than others. Although we use
torch::optim_adamw() directly, it can be much slower than others. For one
benchmark:
optimizer CPU elapsed CUDA elapsed
ADAMw 66.22s 84.42s
SGD 30.12s 30.83s