Path: blob/master/examples/keras_recipes/md/trainer_pattern.md
3508 views
Trainer pattern
Author: nkovela1
Date created: 2022/09/19
Last modified: 2022/09/26
Description: Guide on how to share a custom training step across multiple Keras models.
Introduction
This example shows how to create a custom training step using the "Trainer pattern", which can then be shared across multiple Keras models. This pattern overrides the train_step()
method of the keras.Model
class, allowing for training loops beyond plain supervised learning.
The Trainer pattern can also easily be adapted to more complex models with larger custom training steps, such as this end-to-end GAN model, by putting the custom training step in the Trainer class definition.
Setup
Define the Trainer class
A custom training and evaluation step can be created by overriding the train_step()
and test_step()
method of a Model
subclass:
Define multiple models to share the custom training step
Let's define two different models that can share our Trainer class and its custom train_step()
:
Create Trainer class objects from the models
Compile and fit the models to the MNIST dataset
<keras.src.callbacks.history.History at 0x7efe405fe560>