liveBook · Manning

Fit method

As explained in Deep Learning with R, Third Edition

The fit method trains a neural network on supplied data by iteratively updating its parameters.

The fit method trains a compiled neural network by repeatedly processing mini-batches, calculating loss, and updating weights. Supply inputs and targets, choose the number of epochs and batch size, then inspect the returned training history. section 3.6.6 section 3.7

history <- model |> fit(
  inputs,  #1
  targets,  #2
  epochs = 5,  #3
  batch_size = 128  #4
)

A minimal R/Keras call passes training data, repeats training for five epochs, and uses batches of 128 examples for each update. section 3.6.6

Use the main arguments and outputs to control and inspect training:

  • Pass inputs and targets as R or NumPy arrays, or use a TensorFlow Dataset. section 3.6.6
  • Set epochs to control how many times the training loop iterates over the data. section 3.6.6
  • Set batch_size to control how many examples contribute to each weight-update gradient. section 3.6.6
  • Read the returned history object for per-epoch loss and metric values, or convert it to a data frame. section 3.6.6
  • Monitor loss and metrics on validation data to assess performance on inputs excluded from training. section 3.7

Customizing the training step

When the default supervised-learning workflow is too restrictive, override Model's train_step() and continue calling fit(). A custom step can run the forward pass, compute loss, obtain gradients, update trainable weights, update metrics, and return logged values. This preserves conveniences such as callbacks, performance optimizations, and distributed-training support while changing the learning algorithm. section 7.4 section 7.4.4

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains arguments, history, and the underlying forward-pass-gradient-update cycle, then defers fully novel algorithms to custom loops.
Examples
Uses R/Keras array training, epoch histories, validation monitoring, and custom TensorFlow, PyTorch, and JAX train_step implementations.
What it assumes
Assumes a model has been built and compiled with an optimizer, loss, and metrics before training begins.
What you can do after
Operate/configure a Keras training workflow and customize its per-batch algorithm without abandoning fit conveniences.
How it connects
Connects compiled models with optimizers, losses, metrics, validation data, callbacks, train_step, and custom training loops.

Also explained in

Compare how 2 books explain fit method →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.