keras_fit: Fit a keras model

View source: R/models.R

keras_fitR Documentation

Fit a keras model

Description

Learn the weight and bias values for am model given training data. Model must be compiled first. The model is modified in place.

Usage

keras_fit(model, x, y, batch_size = 32, epochs = 10, verbose = 1,
  callbacks = NULL, validation_split = 0, validation_data = NULL,
  shuffle = TRUE, class_weight = NULL, sample_weight = NULL,
  initial_epoch = 0)

Arguments

model

a keras model object, for example created with Sequential()

x

numeric matrix of input data

y

a numeric matrix or numeric vector containing labels.

batch_size

integer. Number of samples per gradient update.

epochs

integer, the number of epochs to train the model.

verbose

0 for no logging to stdout, 1 for progress bar logging, 2 for one log line per epoch.

callbacks

list of callbacks to apply during training. See EarlyStopping() or TensorBoard() for examples.

validation_split

numeric (0 < x < 1). Fraction of the data to use as held-out validation data.

validation_data

list(x_val, y_val) or list(x_val, y_val, val_sample_weights) to be used as held-out validation data. Will override validation_split.

shuffle

boolean or string (for batch). Whether to shuffle the samples at each epoch. batch is a special option for dealing with the limitations of HDF5 data; it shuffles in batch-sized chunks.

class_weight

dictionary mapping classes to a weight value, used for scaling the loss function (during training only).

sample_weight

numeric array of weights for the training samples

initial_epoch

epoch at which to start training

Author(s)

Taylor B. Arnold, taylor.arnold@acm.org

References

Chollet, Francois. 2015. Keras: Deep Learning library for Theano and TensorFlow.

See Also

keras_compile()

Other model functions: LoadSave, Predict, Sequential, keras_compile

Examples

if(keras_available()) {
  X_train <- matrix(rnorm(100 * 10), nrow = 100)
  Y_train <- to_categorical(matrix(sample(0:2, 100, TRUE), ncol = 1), 3)

  mod <- Sequential()
  mod$add(Dense(units = 50, input_shape = dim(X_train)[2]))
  mod$add(  Dropout(rate = 0.5))
  mod$add(Activation("relu"))
  mod$add(Dense(units = 3))
  mod$add(ActivityRegularization(l1 = 1))
  mod$add(Activation("softmax"))
  keras_compile(mod,  loss = 'categorical_crossentropy', optimizer = RMSprop())

  keras_fit(mod, X_train, Y_train, batch_size = 32, epochs = 5,
            verbose = 0, validation_split = 0.2)
  
  # You can also add layers directly as arguments to Sequential()

  mod <- Sequential(
    Dense(units = 50, input_shape = ncol(X_train)),
    Dropout(rate = 0.5),
    Activation("relu"),
    Dense(units = 3),
    ActivityRegularization(l1 = 1),
    Activation("softmax")
  )
  keras_compile(mod,  loss = 'categorical_crossentropy', optimizer = RMSprop())
  
  keras_fit(mod, X_train, Y_train, batch_size = 32, epochs = 5,
            verbose = 0, validation_split = 0.2)
  
}


kerasR documentation built on Aug. 17, 2022, 5:06 p.m.