I use the Keras library to create a neural network. I have an iPython Notebook for downloading training data, initializing the network, and βmatchingβ the weights of the neural network. Finally, I save the scales using the save_weights () method. Code 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 output 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')
The problem is that apparently the load_weights method is not enough to have a functional model. I get an error message:
--------------------------------------------------------------------------- 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? Many thanks.