<jupyter_start><jupyter_text>call back in keras<jupyter_code># dependencies
%matplotlib inline
import keras
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Flatten, Dense ,Activation

import numpy as np
from  matplotlib import pyplot as plt
from IPython.display import clear_output

# keras version 
keras.__version__
# data loding 
(X_train,y_train) , (X_test, y_test) = mnist.load_data()
# data preprocessing
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
X_train = X_train.reshape(-1,28,28,1).astype('float32')/255
X_test = X_test.reshape(-1,28,28,1).astype('float32')/255

#plot
class PlotLosses(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.i = 0
        self.x = []
        self.losses = []
        self.val_losses = []
        
        self.fig = plt.figure()
        
        self.logs = []

    def on_epoch_end(self, epoch, logs={}):
        
        self.logs.append(logs)
        self.x.append(self.i)
        self.losses.append(logs.get('loss'))
        self.val_losses.append(logs.get('val_loss'))
        self.i += 1
        
        clear_output(wait=True)
        plt.plot(self.x, self.losses, label="loss")
        plt.plot(self.x, self.val_losses, label="val_loss")
        plt.legend()
        plt.show();
        
plot_losses = PlotLosses()
# just a logistic regression to keep it fast and simple
model = Sequential()

model.add(Flatten(input_shape=(28,28,1)))
model.add(Dense(10))
model.add(Activation('softmax'))

model.compile(optimizer='rmsprop',
             loss = 'categorical_crossentropy',
             metrics = ['accuracy'])
# in this static view it is not shown ,
#                but the plot grows step by step

model.fit(X_train, y_train,
         epochs = 10,
         validation_data = (X_test, y_test),
         callbacks = [plot_losses],
         verbose = 1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12054933875890132, 'fraction_numerical': 0.017293997965412006, 'mean_word_length': 3.0142857142857142, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '649533', 'license': 'MIT', 'n_tokens_mistral': 705, 'n_tokens_neox': 681, 'n_words': 141, 'path': 'plot_learning _curve.ipynb', 'repo_name': 'adibyte95/ML-Competitions', 'size': '1966'}