Keras: Continue in training after model.load()

Created on 4 Apr 2018  路  1Comment  路  Source: keras-team/keras

Struggling trying to find out how to continue in training sequential model after load_model() in different session.

I got model for time-series forecasting saved with model.save("model_original.h5", overwrite=True). Now I want to load it in different session with load_model("model_original.h5") and continue in training on the exactly the same data. Problem is, when I use model.fit(). It start to learn from the scratch. TensorFlow: '1.7.0' , Keras: '2.1.5'

def update_model(train, n_lag, n_batch, nb_epoch, model):
        X, y = train[:, :-n_seq], train[:, -n_seq:]
        X = X.reshape(X.shape[0], n_lag, n_features)            
        for i in range(updates):               
            model.fit(X, y,epoch=1,b_size=1,verbose=0, shuffle=False)
            model.reset_states()
        return model

model = load_model("model_original.h5")

score_original = model.evaluate(X, y, batch_size=1)
score_original: 0.6359964006149502

model_updated = update_model(train, n_lag, n_batch, n_epochs, model)

score_updated = model_updated.evaluate(X, y, batch_size=1)
score_updated:  0.004480584771153034

already tried to model.compile() after loading, no success.

Most helpful comment

The following code works perfectly fine for me.

model = load_model("model.h5")
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss 1:', score[0])
print('Test accuracy 1:', score[1])
model.fit(x_train, y_train,batch_size=batch_size,epochs=1,verbose=0)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss 2:', score[0])
print('Test accuracy 2:', score[1])
Test loss 1: 0.08189981982740374
Test accuracy 1: 0.9829
Test loss 2: 0.08721873364182843
Test accuracy 2: 0.9831

>All comments

The following code works perfectly fine for me.

model = load_model("model.h5")
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss 1:', score[0])
print('Test accuracy 1:', score[1])
model.fit(x_train, y_train,batch_size=batch_size,epochs=1,verbose=0)
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss 2:', score[0])
print('Test accuracy 2:', score[1])
Test loss 1: 0.08189981982740374
Test accuracy 1: 0.9829
Test loss 2: 0.08721873364182843
Test accuracy 2: 0.9831
Was this page helpful?
0 / 5 - 0 ratings

Related issues

harishkrishnav picture harishkrishnav  路  3Comments

fredtcaroli picture fredtcaroli  路  3Comments

amityaffliction picture amityaffliction  路  3Comments

vinayakumarr picture vinayakumarr  路  3Comments

NancyZxll picture NancyZxll  路  3Comments