修改经过训练的模型架构并继续训练Keras



我想以顺序的方式训练模型。也就是说,我想最初用一个简单的体系结构来训练模型,一旦训练完成,我想添加几个层并继续训练。在喀拉拉邦有可能做到这一点吗?如果是,如何?

我试图修改模型体系结构。但在我编译之前,这些更改是无效的。一旦我编译,所有的权重都会被重新初始化,并且我会丢失所有经过训练的信息。

我在web和SO中发现的所有问题要么是关于加载预训练模型并继续训练,要么是修改预训练模型的架构并只测试它。我没有发现任何与我的问题相关的问题。任何指示也将受到高度赞赏。

附言:我在tensorflow 2.0包中使用Keras。

在不了解模型细节的情况下,以下片段可能会有所帮助:

from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Input
# Train your initial model
def get_initial_model():
...
return model
model = get_initial_model()
model.fit(...)
model.save_weights('initial_model_weights.h5')
# Use Model API to create another model, built on your initial model
initial_model = get_initial_model()
initial_model.load_weights('initial_model_weights.h5')
nn_input = Input(...)
x = initial_model(nn_input)
x = Dense(...)(x)  # This is the additional layer, connected to your initial model
nn_output = Dense(...)(x)
# Combine your model
full_model = Model(inputs=nn_input, outputs=nn_output)
# Compile and train as usual
full_model.compile(...)
full_model.fit(...)

基本上,您训练初始模型,保存它。然后再次重新加载它,并使用ModelAPI将其与附加层包装在一起。如果你不熟悉ModelAPI,你可以在这里查看Keras文档(afaik API对Tensorflow.Caras 2.0保持不变(

请注意,您需要检查初始模型的最终层的输出形状是否与其他层兼容(例如,如果您只是在进行特征提取,则可能希望从初始模型中删除最终的密集层(。

最新更新