Saturday, May 14, 2022

Tensorflow: Save and Load Model to retrain

 This demo program show how to save a trained model and then load it for retraining. I splitted the original file into 2 which I used in this post "Predictive Model: Customer Subsciption (Continue/Discontinue) with Tensorflow". 


The output:

Orignal Model Accuracy result:


Loaded Model Accuracy result:


The code:

First program:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import pandas as pd
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense, Flatten
from sklearn.metrics import accuracy_score
from sklearn.metrics import ConfusionMatrixDisplay, classification_report, confusion_matrix
from matplotlib import style
import tensorflow as tf
from tensorflow import keras


style.use('classic')
df = pd.read_csv('cont_subs.csv')
X = pd.get_dummies(df.drop(['cont_subs', 'Customer ID'], axis=1))
y = df['cont_subs'].apply(lambda x: 1 if x=='Yes' else 0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.15)
X.columns

y_train.head()

model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=len(X_train.columns)))
model.add(Dense(units=128, activation='relu'))
model.add(Dense(units=128, activation='relu'))
model.add(Dense(units=1, activation='sigmoid'))

checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

# Create a callback that saves the model's weights
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                 save_weights_only=True,
                                                 verbose=1)

model.compile(loss='binary_crossentropy', optimizer='sgd', metrics='accuracy')

model.fit(X_train, y_train, epochs=256, batch_size=32, callbacks=[cp_callback])

y_hat = model.predict(X_test)
y_hat = [0 if val < 0.5 else 1 for val in y_hat]

print('Model Accuracy' + str(accuracy_score(y_test, y_hat)))

# Display the model's architecture
model.summary()

cm = confusion_matrix(y_test, y_hat)
disp = ConfusionMatrixDisplay(confusion_matrix = cm)
disp.plot()

model.save('my_model')

 

The Loaded Model Program:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import os
import pandas as pd
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense, Flatten
from sklearn.metrics import accuracy_score
from sklearn.metrics import ConfusionMatrixDisplay, classification_report, confusion_matrix
from matplotlib import style
import tensorflow as tf
from tensorflow import keras


style.use('classic')
df = pd.read_csv('cont_subs1.csv')
X = pd.get_dummies(df.drop(['cont_subs', 'Customer ID'], axis=1))
y = df['cont_subs'].apply(lambda x: 1 if x=='Yes' else 0)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.15)
X.columns
y_train.head()

new_model = tf.keras.models.load_model('my_model')

# Check its architecture
new_model.summary()
new_model = Sequential()
new_model.add(Dense(units=64, activation='relu', input_dim=len(X_train.columns)))
new_model.add(Dense(units=128, activation='relu'))
new_model.add(Dense(units=1, activation='sigmoid'))

checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

# Create a callback that saves the model's weights
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                 save_weights_only=True,
                                                 verbose=1)

new_model.compile(loss='binary_crossentropy', optimizer='sgd', metrics='accuracy')
new_model.fit(X_train, y_train, epochs=256, batch_size=32, callbacks=[cp_callback])
new_model.evaluate(X_train, y_train, verbose=2)
y_hat = new_model.predict(X_test)
y_hat = [0 if val < 0.5 else 1 for val in y_hat]

#accuracy_score(y_test, y_hat)
print('Loaded Model Accuracy' + str(accuracy_score(y_test, y_hat))) 


model.save('my_model')

new_model.summary()

No comments:

Post a Comment