Listeners
Training listeners — ScoreIterationListener, PerformanceListener, EvaluativeListener, CheckpointListener, and custom listeners
Listeners let you hook into events during MultiLayerNetwork or ComputationGraph training. Common uses include logging loss scores, measuring throughput, saving periodic checkpoints, and running evaluation on a test set. Listeners implement org.deeplearning4j.optimize.api.TrainingListener and are added with setListeners(...).
Attaching Listeners
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.deeplearning4j.optimize.listeners.PerformanceListener;
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
// One listener
model.setListeners(new ScoreIterationListener(10));
// Multiple listeners at once
model.setListeners(
new ScoreIterationListener(10),
new PerformanceListener(10, true)
);
// Add to a ComputationGraph the same way
ComputationGraph graph = new ComputationGraph(graphConf);
graph.init();
graph.setListeners(new ScoreIterationListener(1));ScoreIterationListener
Logs the current loss value to slf4j (INFO level) every N iterations. The "score" is the network's loss function value for the most recently processed minibatch.
Example output:
Constructor: ScoreIterationListener(int printIterations) — frequency in parameter update steps.
PerformanceListener
Reports training throughput (examples per second and minibatches per second) and optionally the current score. Useful for profiling training speed across hardware configurations.
Or use the simple constructor:
Example output:
EvaluativeListener
Runs a full evaluation pass on a held-out DataSetIterator every N iterations or every N epochs. The InvocationType controls whether frequency counts iterations or epochs.
The listener logs the result of model.evaluate(iterator) (for classification) or model.evaluateRegression(iterator) depending on the network configuration.
You can supply a callback to receive the IEvaluation result:
CheckpointListener
Periodically saves the model to disk during training. The three trigger types (epochs, iterations, time) can be combined freely.
CheckpointListener Builder Reference
saveEveryNEpochs(int n)
Save after every n completed epochs.
saveEveryNIterations(int n)
Save after every n parameter update iterations.
saveEvery(long amount, TimeUnit unit)
Save when the specified wall-clock time has elapsed since training started.
saveEvery(long amount, TimeUnit unit, boolean sinceLast)
If sinceLast=true, reset timer after each save.
keepAll()
Never delete checkpoint files.
keepLast(int n)
Keep only the most recent n checkpoint files; older ones are deleted.
keepLastAndEvery(int nLast, int every)
Keep the most recent nLast files plus every every-th checkpoint.
Restoring from a Checkpoint
CollectScoresIterationListener
Stores the loss score at each iteration (or every N iterations) in memory for later programmatic access or export to a file.
TimeIterationListener
Estimates and logs the remaining training time and projected finish time. Requires the total number of iterations (across all epochs) to be specified upfront.
Output each iteration:
ComposableIterationListener
Groups multiple IterationListener instances into a single listener. Useful when you need to wrap listeners in a context that accepts only one.
ParamAndGradientIterationListener
Logs statistics (mean, min, max, mean absolute value) for all parameters and gradients at each iteration. Text-based alternative to the UI histogram when training on remote machines.
Custom Listeners
Implement TrainingListener (or extend BaseTrainingListener for default no-op implementations of unused methods):
The full TrainingListener interface also exposes:
onForwardPass(Model, List<INDArray>)
After each forward pass (activations available)
onBackwardPass(Model)
After each backward pass (gradients available)
onGradientCalculation(Model)
After gradient computation, before parameter update
iterationDone(Model, int, int)
After each parameter update (iteration, epoch)
onEpochStart(Model)
Before the first iteration of each epoch
onEpochEnd(Model)
After the last iteration of each epoch
API Reference
TrainingListener (interface)
org.deeplearning4j.optimize.api
BaseTrainingListener
org.deeplearning4j.optimize.api
ScoreIterationListener
org.deeplearning4j.optimize.listeners
PerformanceListener
org.deeplearning4j.optimize.listeners
EvaluativeListener
org.deeplearning4j.optimize.listeners
CheckpointListener
org.deeplearning4j.optimize.listeners
CollectScoresIterationListener
org.deeplearning4j.optimize.listeners
TimeIterationListener
org.deeplearning4j.optimize.listeners
ComposableIterationListener
org.deeplearning4j.optimize.listeners
ParamAndGradientIterationListener
org.deeplearning4j.optimize.listeners
Last updated
Was this helpful?