This function delegates most work to keras3::fit.keras.src.models.model.Model()
and performs additional consistency
checks to make sure tf_compile_model()
was called with the appropriate options to support fitting the observations
y
as well as automatically converting y
to a n x 6 matrix needed by the compiled loss function.
Usage
# S3 method for reservr_keras_model
fit(
object,
x,
y,
batch_size = NULL,
epochs = 10,
verbose = getOption("keras.fit_verbose", default = 1),
callbacks = NULL,
view_metrics = getOption("keras.view_metrics", default = "auto"),
validation_split = 0,
validation_data = NULL,
shuffle = TRUE,
class_weight = NULL,
sample_weight = NULL,
initial_epoch = 0,
steps_per_epoch = NULL,
validation_steps = NULL,
...
)
Arguments
- object
A compiled
reservr_keras_model
as obtained bytf_compile_model()
.- x
A list of input tensors (predictors)
- y
A
trunc_obs
tibble of observed outcomes, or something convertible viaas_trunc_obs()
.- batch_size
Integer or
NULL
. Number of samples per gradient update. If unspecified,batch_size
will default to32
. Do not specify thebatch_size
if your data is in the form of TF Datasets or generators, (since they generate batches).- epochs
Integer. Number of epochs to train the model. An epoch is an iteration over the entire
x
andy
data provided (unless thesteps_per_epoch
flag is set to something other thanNULL
). Note that in conjunction withinitial_epoch
,epochs
is to be understood as "final epoch". The model is not trained for a number of iterations given byepochs
, but merely until the epoch of indexepochs
is reached.- verbose
"auto"
,0
,1
, or2
. Verbosity mode.0
= silent,1
= progress bar,2
= one line per epoch."auto"
becomes 1 for most cases,2
if in a knitr render or running on a distributed training server. Note that the progress bar is not particularly useful when logged to a file, soverbose=2
is recommended when not running interactively (e.g., in a production environment). Defaults to"auto"
.- callbacks
List of
Callback()
instances. List of callbacks to apply during training. Seecallback_*
.- view_metrics
View realtime plot of training metrics (by epoch). The default (
"auto"
) will display the plot when running within RStudio,metrics
were specified during modelcompile()
,epochs > 1
andverbose > 0
. Set the globaloptions(keras.view_metrics = )
option to establish a different default.- validation_split
Float between 0 and 1. Fraction of the training data to be used as validation data. The model will set apart this fraction of the training data, will not train on it, and will evaluate the loss and any model metrics on this data at the end of each epoch. The validation data is selected from the last samples in the
x
andy
data provided, before shuffling. This argument is not supported whenx
is a TF Dataset or generator. If bothvalidation_data
andvalidation_split
are provided,validation_data
will overridevalidation_split
.- validation_data
Data on which to evaluate the loss and any model metrics at the end of each epoch. The model will not be trained on this data. Thus, note the fact that the validation loss of data provided using
validation_split
orvalidation_data
is not affected by regularization layers like noise and dropout.validation_data
will overridevalidation_split
. It could be:A tuple
(x_val, y_val)
of arrays or tensors.A tuple
(x_val, y_val, val_sample_weights)
of arrays.A generator returning
(inputs, targets)
or(inputs, targets, sample_weights)
.
- shuffle
Boolean, whether to shuffle the training data before each epoch. This argument is ignored when
x
is a generator or a TF Dataset.- class_weight
Optional named list mapping class indices (integers, 0-based) to a weight (float) value, used for weighting the loss function (during training only). This can be useful to tell the model to "pay more attention" to samples from an under-represented class. When
class_weight
is specified and targets have a rank of 2 or greater, eithery
must be one-hot encoded, or an explicit final dimension of1
must be included for sparse class labels.- sample_weight
Optional array of weights for the training samples, used for weighting the loss function (during training only). You can either pass a flat (1D) array/vector with the same length as the input samples (1:1 mapping between weights and samples), or in the case of temporal data, you can pass a 2D array (matrix) with shape
(samples, sequence_length)
, to apply a different weight to every timestep of every sample. This argument is not supported whenx
is a TF Dataset or generator, instead provide the sample_weights as the third element ofx
. Note that sample weighting does not apply to metrics specified via themetrics
argument incompile()
. To apply sample weighting to your metrics, you can specify them via theweighted_metrics
incompile()
instead.- initial_epoch
Integer. Epoch at which to start training (useful for resuming a previous training run).
- steps_per_epoch
Integer or
NULL
. Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch. When training with input tensors such as backend-native tensors, the defaultNULL
is equal to the number of samples in your dataset divided by the batch size, or1
if that cannot be determined. Ifx
is a TF Dataset, andsteps_per_epoch
isNULL
, the epoch will run until the input dataset is exhausted. When passing an infinitely repeating dataset, you must specify thesteps_per_epoch
argument. Ifsteps_per_epoch = -1
the training will run indefinitely with an infinitely repeating dataset.- validation_steps
Only relevant if
validation_data
is provided. Total number of steps (batches of samples) to draw before stopping when performing validation at the end of every epoch. Ifvalidation_steps
isNULL
, validation will run until thevalidation_data
dataset is exhausted. In the case of an infinitely repeated dataset, it will run into an infinite loop. Ifvalidation_steps
is specified and only part of the dataset will be consumed, the evaluation will start from the beginning of the dataset at each epoch. This ensures that the same validation samples are used every time.- ...
Unused. If old arguments are supplied, an error message will be raised informing how to fix the issue.
Value
A history
object that contains all information collected during training.
The model object will be updated in-place as a side-effect.
Details
Additionally, the default batch_size
is min(nrow(y), 10000)
instead of keras default of 32
because the latter
is a very bad choice for fitting most distributions since the involved loss is much less stable than typical losses
used in machine learning, leading to divergence for small batch sizes.
Examples
dist <- dist_exponential()
params <- list(rate = 1.0)
N <- 100L
rand_input <- runif(N)
x <- dist$sample(N, with_params = params)
if (interactive()) {
tf_in <- keras3::layer_input(1L)
mod <- tf_compile_model(
inputs = list(tf_in),
intermediate_output = tf_in,
dist = dist,
optimizer = keras3::optimizer_adam(),
censoring = FALSE,
truncation = FALSE
)
tf_fit <- fit(
object = mod,
x = k_matrix(rand_input),
y = x,
epochs = 10L,
callbacks = list(
callback_debug_dist_gradients(mod, k_matrix(rand_input), x, keep_grads = TRUE)
)
)
}