# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Accessing Model Training History in Keras
# * It records training metrics for each epoch
# * we can use data collected in the history object to create plots.The plots can provide an indication of useful things about the training of model such as:
#     * It's speed of convergence over epochs(slope)
#     * Whether the model may have already converged(plateau of the line)
#     * Whether the model may be over-learning the training data

# +
# we can list all metrics collected in history object
# -

# ## 1. Visualizing Model Training History in Keras
# * A plot of accuracy on the training and validation datasets over training epochs.
# * A plot of loss on the training and validation datasets over training epochs.

# +
# Visualize training history
from keras.models import Sequential
from keras.layers import Dense
import matplotlib.pyplot as plt
import numpy as np

# fix random seed for reproducibility
seed = 7
np.random.seed(seed)

# load pima indians dataset
dataset = np.loadtxt("pima-indians-diabetes.data.csv",delimiter=",")

# split into input(X) and Output(Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]

# create model
model = Sequential()
model.add(Dense(12,input_dim=8,activation='relu'))
model.add(Dense(8,activation='relu'))
model.add(Dense(1,activation='sigmoid'))

# compile model
model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])

# Fit the model
history = model.fit(X,Y,validation_split=0.33,epochs=150,batch_size=10,verbose=0)

# list all data in history
print(history.history.keys())

# summarize history for accuracy
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train','test'],loc='upper left')
plt.savefig('accuracy')
plt.show()


# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train','test'],loc='upper left')
plt.savefig('loss')
plt.show()
# -

# # summary
# * Discovered the importance of collecting and reviewing metrics during the training of deep learning models.



StarcoderdataJupyterScriptsDedupFiltered