1. Home
  2. /
  3. Keras
  4. /
  5. Custom Callback Example

Custom Callback Example - Keras Typing CST Test

Loading…

Custom Callback Example — Keras Code

Custom callback to print loss at each epoch.

from tensorflow import keras

class PrintLossCallback(keras.callbacks.Callback):
	def on_epoch_end(self, epoch, logs=None):
		print(f"Epoch {epoch+1}: loss = {logs['loss']}")

model = keras.Sequential([keras.layers.Dense(1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mse')
model.fit([1,2,3],[2,4,6], epochs=5, callbacks=[PrintLossCallback()])

Keras Language Guide

Keras is an open-source, high-level deep learning API written in Python. It provides a user-friendly interface for building and training neural networks, running on top of TensorFlow, Theano, or CNTK backends.

Primary Use Cases

  • ▸Image classification and object detection
  • ▸Natural language processing (NLP)
  • ▸Reinforcement learning prototypes
  • ▸Time series forecasting
  • ▸Educational purposes and research experiments

Notable Features

  • ▸High-level API for deep learning
  • ▸Seamless integration with TensorFlow
  • ▸Supports Sequential and Functional API models
  • ▸Built-in layers, optimizers, loss functions, and metrics
  • ▸GPU acceleration via backend frameworks

Origin & Creator

Keras was developed in 2015 by François Chollet, a Google engineer, to simplify deep learning model creation and experimentation.

Industrial Note

Keras is popular in industry and academia for rapid prototyping, experimentation, and deployment of neural networks due to its simplicity, modularity, and integration with TensorFlow.

More Keras Typing Exercises

Keras Simple Linear RegressionKeras Simple Neural NetworkKeras MNIST ClassifierKeras Convolutional Neural NetworkKeras LSTM ExampleKeras Autoencoder ExampleKeras Regression with Multiple InputsKeras Transfer Learning ExampleKeras GAN Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher