Su JK
Su JK

Reputation: 171

How to continue training for a saved and then loaded Keras model?

Following the official keras documentation , I was able to save and load a model. Keras is using tensorflow as the backend.

However, is it possible to run more training for such saved and loaded models.

Following is the code borrowed from Link. Then edited.

In the following code, the model is trained for 75 epochs and saved then loaded again.

However, when I tried to train it further with more 75 epochs it seems model was not trained and I got the same result without any modifications.

# -*- coding: utf-8 -*-

from keras.models import Sequential
from keras.layers import Dense
from keras.models import model_from_json
import numpy
import os
# fix random seed for reproducibility
numpy.random.seed(7)
# load pima indians dataset
dataset = numpy.loadtxt("pima-indians-diabetes.txt", 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, kernel_initializer='uniform', activation='relu'))
model.add(Dense(8, kernel_initializer='uniform', activation='relu'))
model.add(Dense(1, kernel_initializer='uniform', activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Fit the model
model.fit(X, Y, epochs=75, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))

# serialize model to JSON
model_json = model.to_json()
with open("model.json", "w") as json_file: json_file.write(model_json)

# serialize weights to HDF5
model.save_weights("model.h5")
print("Saved model to disk")

# later...

# load json and create model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("model.h5")
print("Loaded model from disk")

# evaluate loaded model on test data
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
score = loaded_model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))


model.fit(X, Y, epochs=75, batch_size=10, verbose=0)
score = loaded_model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (loaded_model.metrics_names[1], score[1]*100))

Upvotes: 0

Views: 5893

Answers (1)

Fury
Fury

Reputation: 523

It looks like from that code that your evaluating the loaded_model twice, but your extra training is done on just the model. Instead of copying and pasting different variable names you could try something like this... I find it a little easier to keep track of. Also, add some white space to your code between comments, it will help keep things clear and organized.

# Save a model you have trained
model.save('trained_model.h5')

# Delete the model
del model

# Load the model
model = load_model('trained_model.h5')

# Train more on the loaded model
model.fit(data, labels, epochs, batch_size)

Upvotes: 1

Related Questions