rmahesh
rmahesh

Reputation: 749

Trouble Loading Weights of a model in Keras

I have trained a model using GloVe word embeddings and have saved the architecture and weights of the model. I want to make some small changes to the models network and train the model again. Here is my code:

#Load back model, change architecture, train, predict
from keras import regularizers
from keras import layers
from keras.models import load_model

def create_model():
  model = Sequential()
  model.add(Embedding(max_fatures, embed_dim,input_length = X_train.shape[1]))
  model.add(Bidirectional(LSTM(150, return_sequences=True, dropout= 0.1, recurrent_dropout=0.1)))
  model.add(GlobalMaxPool1D())
  model.add(Dense(50, activation="relu"))
  model.add(Dropout(0.1))
  model.add(Dense(6, activation="sigmoid"))

  #Load GloVe
  model.layers[0].set_weights([embedding_matrix])
  model.layers[0].trainable = False
  model = load_model('/content/model_num2.h5')
  model.fit(X_train,y_train, nb_epoch=2, batch_size=32, show_accuracy=True, validation_split=0.1, verbose=2)
  return(model)

model2 = create_model()

When I call model2, it is failing. The error message is:

ValueError: Cannot create group in read only mode.

I changed some of the layers ahead in the create_model() function, and I ultimately want to train the model (using the weights I previously saved) and predict on a testing set.

Any help would be great!

Edit: forgot to post portion in which the model is being compiled. Added it in the function.

Upvotes: 0

Views: 376

Answers (1)

sslloo
sslloo

Reputation: 521

I don't understand your code, Your create a new Model does not compile it and load a new model instead that would erase your model?

  1. As a matter of rule, you should rewrite your model from scratch, because as it is compiled, it is not mutable anymore. By accessing your Model objects attributes / print_summary you can have a view of the architecture of your model

  2. Each weights are optimized for a given architecture, it not sure that using pretrained weights from another architecture save computation time, it increases the risk of overfitting

Upvotes: 1

Related Questions