简体   繁体   English

Keras中模型的麻烦加载权重

[英]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. 我已经使用GloVe词嵌入训练了模型,并节省了模型的体系结构和权重。 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. 当我调用model2时,它失败了。 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. 我在create_model()函数中更改了前面的某些层,最终我想训练模型(使用之前保存的权重)并在测试集上进行预测。

Any help would be great! 任何帮助将是巨大的!

Edit: forgot to post portion in which the model is being compiled. 编辑:忘记发布正在其中编译模型的部分。 Added it in the function. 在功能中添加了它。

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? 我不理解您的代码,您创建的新Model不会compileload新模型,而是会删除您的模型吗?

  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 通过访问模型对象的属性/ print_summary您可以查看模型的体系结构

  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 每个权重都针对给定的体系结构进行了优化,不能确保使用来自另一个体系结构的预训练权重可以节省计算时间,但会增加过度拟合的风险

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM