Nag*_*S N 7 python keras tensorflow pre-trained-model
我想以顺序方式训练模型。那就是我想最初用一个简单的架构训练模型,一旦训练好,我想添加几个层并继续训练。可以在 Keras 中做到这一点吗?如果是这样,如何?
我试图修改模型架构。但是在我编译之前,这些更改都无效。一旦我编译,所有的权重都会重新初始化,我会丢失所有经过训练的信息。
我在 web 和 SO 中发现的所有问题要么是关于加载预训练模型并继续训练,要么是修改预训练模型的架构,然后仅对其进行测试。我没有找到与我的问题相关的任何内容。任何指针也受到高度赞赏。
PS:我在 tensorflow 2.0 包中使用 Keras。
小智 4
在不知道模型详细信息的情况下,以下代码片段可能会有所帮助:
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(...)
Run Code Online (Sandbox Code Playgroud)
基本上,您训练初始模型并保存它。然后再次重新加载它,并使用 API 将其与其他层包装在一起Model。如果您不熟悉API,可以在此处Model查看 Keras 文档(据我所知,Tensorflow.Keras 2.0 的 API 保持不变)。
请注意,您需要检查初始模型的最终层的输出形状是否与附加层兼容(例如,如果您只是进行特征提取,您可能希望从初始模型中删除最终的密集层)。
| 归档时间: |
|
| 查看次数: |
1335 次 |
| 最近记录: |