简体   繁体   English

使用您自己保存的模型在Keras中转移学习

[英]Transfer learning in Keras with your own saved model

I have seen some examples of transfer learning where one can use pre-trained models from keras.application (Xception, VGG16, VGG19, ResNet50 etc) but what I want is to transfer the learning from the model I saved using model.save('model.h5') 我已经看到了转移学习的一些示例,其中可以使用来自keras.application的预训练模型(Xception,VGG16,VGG19,ResNet50等),但是我想要的是从我使用model.save(' model.h5' )

This is my current model: 这是我当前的模型:

model = Sequential()
model.add(Embedding(max_words, embedding_dim, input_length=maxlen))
model.add(LSTM(32))
model.add(Dropout(0.6))
model.add(Dense(2, activation='sigmoid'))

model.compile(optimizer='rmsprop', loss='binary_crossentropy',metrics=['acc'])

model.fit(sequences, labels, epochs=10, batch_size=32, validation_split=0.2)

Now, Instead of saying 现在,不用说

model_base = keras.applications.vgg16.VGG16(include_top=False, weights='imagenet')

I want to load the saved model probably with load_model('model.h5') and add it as a layer to my current model. 我想使用load_model('model.h5')加载保存的模型,并将其作为图层添加到当前模型中。

try this 尝试这个

model = Sequential()
model.add(Embedding(max_words, embedding_dim, input_length=maxlen))
model.add(LSTM(32))
model.add(Dropout(0.6))
model.add(Dense(2, activation='sigmoid'))

model.layers[0].set_weights([embedding_matrix])
model.layers[0].trainable = False

model.load_weights('model.h5')
model.compile(optimizer='rmsprop', loss='binary_crossentropy',metrics=['acc'])

model_base = model

Dont forget to remove your classifier 不要忘记删除您的分类器

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

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