Model Pipeline - Custom callbacks
This pipeline shows how a neural network trains on data while using a custom callback to monitor and print training progress in a friendly way.
This pipeline shows how a neural network trains on data while using a custom callback to monitor and print training progress in a friendly way.
Loss
0.7 |*
0.6 | *
0.5 | *
0.4 | *
0.3 | *
+---------
1 2 3 4 5 Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.65 | 0.60 | Model starts learning, loss is high, accuracy moderate |
| 2 | 0.50 | 0.72 | Loss decreases, accuracy improves |
| 3 | 0.40 | 0.80 | Model continues to improve |
| 4 | 0.35 | 0.85 | Loss lowers further, accuracy rises |
| 5 | 0.30 | 0.88 | Training converging well |
import tensorflow as tf
class MyCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
print(f"Epoch {epoch+1} ended with loss {logs['loss']:.2f}")
model = tf.keras.Sequential([tf.keras.layers.Dense(1)])
model.compile(optimizer='sgd', loss='mse')
model.fit([[1.0]], [[2.0]], epochs=2, callbacks=[MyCallback()])class StopCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
if logs['loss'] < 0.1:
self.model.stop_training = True
model.fit(x_train, y_train, epochs=10, callbacks=[StopCallback()])Pass the test and this pattern moves unknown