Esercizio 
Si completi l’esercizio sviluppato nella lezione 65 (riportato in calce) inserendo una callback di tipo ModelCheckpoint e stampando un grafico relativo all’evoluzione della loss e dell’accuratezza durante il processo di addestramento.
Fornire un notebook Python contenente il codice sorgente ed il relativo output.

--
Lezione 65
from sklearn import datasets
from pandas import DataFrame
from matplotlib import pyplot

X, Y = datasets.make_moons(n_samples=5000, shuffle=True, noise=0.1)

print(X)
print(Y)

df=DataFrame(dict(x=X[:,0], y=X[:,1], label=Y))
colors = {0:'red', 1:'blue'}
fig, ax = pyplot.subplots(1)
grouped = df.groupby('label')
for key, group in grouped:
    group.plot(ax=ax, kind='scatter', x='x', y='y', label=key, color=colors[key])

X.shape

Y.shape

X_train, X_test, Y_train, Y_test = X[:4000], X[4000:], Y[:4000], Y[4000:]

from tensorflow.keras.models import *
from tensorflow.keras.layers import *

model = Sequential()
model.add(Dense(50, activation="relu", input_shape=[2]))
model.add(Dense(30, activation="relu"))
model.add(Dense(20, activation="relu"))
model.add(Dense(1, activation="sigmoid"))

model.summary()

hidden1 = model.layers[1]
weights, biases = hidden1.get_weights()
weights # sono inizializzati con numeri casuali

biases

# compilazione del modello
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])

history = model.fit(X_train, Y_train, batch_size=200, epochs=30)

prediction = model.predict([[1.5, 1.7]])
print('Predizione:', prediction)

prediction = model.predict([[5.5, 1.7]])
print('Predizione:', prediction)

from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score

prediction = model.predict(X_test)>0.5

print('Confusion Matrix:\n',confusion_matrix(Y_test, prediction))
print('Accuracy:',accuracy_score(Y_test, prediction))
print('Precision:',precision_score(Y_test, prediction))
print('Recall:',recall_score(Y_test, prediction))
print('F1:',f1_score(Y_test, prediction))


--

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

white = pd.read_csv("white.csv", sep =',')
red = pd.read_csv("red.csv", sep =',')

white.describe()

red.describe()

red['type'] = 1
white['type'] = 0

wines = red.append(white, ignore_index = True)

from sklearn.model_selection import train_test_split

X = wines.iloc[:, 0:11]
Y = np.ravel(wines.type)

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.34, random_state = 45)

X.shape

from tensorflow.keras.models import *
from tensorflow.keras.layers import *

model = Sequential()
model.add(Dense(20, activation="relu", input_shape=[11]))
model.add(Dense(20, activation="relu"))
model.add(Dense(10, activation="relu"))
model.add(Dense(1, activation="sigmoid"))

model.summary()

from tensorflow.keras import callbacks

checkpoint_cb = callbacks.ModelCheckpoint("model.h5")

# compilazione del modello
model.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])

# il modello corrente sarà salvato alla fine di ogni epoca
history = model.fit(X_train, Y_train, batch_size=200, epochs=30, callbacks=[checkpoint_cb])

import matplotlib
import pandas as pd

df=pd.DataFrame(history.history)
df.plot()

model = load_model("model.h5")

from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score, f1_score

prediction = model.predict(X_test)>0.5

print('Confusion Matrix:\n',confusion_matrix(Y_test, prediction))
print('Accuracy:',accuracy_score(Y_test, prediction))
print('Precision:',precision_score(Y_test, prediction))
print('Recall:',recall_score(Y_test, prediction))
print('F1:',f1_score(Y_test, prediction))