Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a custom callback class that prints a message at the end of each epoch.
TensorFlow
import tensorflow as tf class MyCallback(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs=None): print('Epoch', epoch, 'has ended') callback = MyCallback() model.fit(x_train, y_train, epochs=5, callbacks=[[1]])
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the class name instead of the instance.
Passing an undefined variable.
✗ Incorrect
The instance of the custom callback class is named 'callback', so it should be passed inside the callbacks list.
2fill in blank
mediumComplete the code to stop training early when accuracy reaches 90%.
TensorFlow
import tensorflow as tf class EarlyStopCallback(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs=None): if logs.get('accuracy') and logs.get('accuracy') > [1]: print('Reached 90% accuracy, stopping training!') self.model.stop_training = True callback = EarlyStopCallback()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 90 instead of 0.9.
Using 1.0 which means 100% accuracy.
✗ Incorrect
Accuracy values are between 0 and 1, so 90% accuracy is 0.9.
3fill in blank
hardFix the error in the custom callback to correctly access validation loss after each epoch.
TensorFlow
import tensorflow as tf class ValLossCallback(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs=None): val_loss = logs.get([1]) print(f'Validation loss after epoch {epoch}:', val_loss) callback = ValLossCallback()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'validation_loss' which is incorrect.
Using 'val-loss' with a dash instead of underscore.
✗ Incorrect
The correct key for validation loss in logs is 'val_loss'.
4fill in blank
hardFill both blanks to create a callback that saves the model weights only when validation accuracy improves.
TensorFlow
import tensorflow as tf class SaveBestModel(tf.keras.callbacks.Callback): def __init__(self): super().__init__() self.best_acc = 0 def on_epoch_end(self, epoch, logs=None): current_acc = logs.get([1]) if current_acc and current_acc > self.best_acc: self.best_acc = current_acc self.model.save_weights([2]) callback = SaveBestModel()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'accuracy' instead of 'val_accuracy'.
Saving weights to a generic or unclear filename.
✗ Incorrect
Validation accuracy is accessed with 'val_accuracy', and the weights are saved to 'best_weights.h5'.
5fill in blank
hardFill all three blanks to create a callback that logs loss and accuracy to a file after each epoch.
TensorFlow
import tensorflow as tf class LogToFile(tf.keras.callbacks.Callback): def __init__(self, filename): super().__init__() self.filename = filename def on_epoch_end(self, epoch, logs=None): loss = logs.get([1]) acc = logs.get([2]) with open(self.filename, [3]) as f: f.write(f'Epoch {epoch}: loss={loss}, accuracy={acc}\n') callback = LogToFile('training_log.txt')
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'val_loss' or 'val_accuracy' instead of 'loss' and 'accuracy'.
Opening the file in write mode 'w' which overwrites previous logs.
✗ Incorrect
We get 'loss' and 'accuracy' from logs, and open the file in append mode 'a' to add logs after each epoch.
