Keras load weights of a neural network / error when predicting

Julian picture Julian · Nov 2, 2015 · Viewed 12.7k times · Source

I'm using the Keras library to create a neural network. I have a iPython Notebook in order to load the training data, initializing the network and "fit" the weights of the neural network. Finally, I save the weights using the save_weights() method. Code is below :

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
from keras.regularizers import l2
from keras.callbacks import History

[...]

input_size = data_X.shape[1]
output_size = data_Y.shape[1]
hidden_size = 100
learning_rate = 0.01
num_epochs = 100
batch_size = 75

model = Sequential()
model.add(Dense(hidden_size, input_dim=input_size, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.2))
model.add(Dense(hidden_size))
model.add(Activation('tanh'))
model.add(Dropout(0.2))
model.add(Dense(output_size))
model.add(Activation('tanh'))

sgd = SGD(lr=learning_rate, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='mse', optimizer=sgd)

model.fit(X_NN_part1, Y_NN_part1, batch_size=batch_size, nb_epoch=num_epochs, validation_data=(X_NN_part2, Y_NN_part2), callbacks=[history])

y_pred = model.predict(X_NN_part2) # works well

model.save_weights('keras_w')

Then, in another iPython Notebook, I just want to use these weights and predict some outputs values given inputs. I initialize the same neural network, and then load the weights.

# same headers
input_size = 37
output_size = 40
hidden_size = 100

model = Sequential()
model.add(Dense(hidden_size, input_dim=input_size, init='uniform'))
model.add(Activation('tanh'))
model.add(Dropout(0.2))
model.add(Dense(hidden_size))
model.add(Activation('tanh'))
model.add(Dropout(0.2))
model.add(Dense(output_size))
model.add(Activation('tanh'))

model.load_weights('keras_w') 
#no error until here

y_pred = model.predict(X_nn)

The problem is that apparently, the load_weights method is not enough to have a functional model. I'm getting an error :

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-e6d32bc0d547> in <module>()
  1 
----> 2 y_pred = model.predict(X_nn)
C:\XXXXXXX\Local\Continuum\Anaconda\lib\site-packages\keras\models.pyc in predict(self, X, batch_size, verbose)
491     def predict(self, X, batch_size=128, verbose=0):
492         X = standardize_X(X)
--> 493         return self._predict_loop(self._predict, X, batch_size, verbose)[0]
494 
495     def predict_proba(self, X, batch_size=128, verbose=1):

AttributeError: 'Sequential' object has no attribute '_predict'

Any idea? Thanks a lot.

Answer

Daniel Renshaw picture Daniel Renshaw · Nov 2, 2015

You need to call model.compile. This can be done either before or after the model.load_weights call but must be after the model architecture is specified and before the model.predict call.