如何将keras序列模型恢复到构建之前的状态



我想重用相同的模型架构,但使用不同的数据集,也就是说,通过编程将输入层更改为不同的形状,并在需要时重置模型参数。

类似的东西

model = tf.keras.Sequential(
tf.keras.layers.Dense(2)
)
optimizer = tf.optimizers.Adam()
losses=[tf.keras.losses.mean_absolute_percentage_error]
model.compile(optimizer=optimizer, loss=losses)
model.build(input_shape=(None,2))
# ... train model and evaluate
model.unbuild() # this doesn't exist
model.build(input_shape=(None,3))
# ... train model and evaluate on different dataset

有人知道一种干净的方法吗?

您可以创建一个骨干模型,并重用它来构建不同输入层的任意多个模型,骨干模型的参数对于您创建的所有新模型都将保持不变,如果您想要重置参数,则构建新的骨干模型,示例代码如下:

import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np
input_shape_b = (16, )
# Backbone model
def build_backbone_model():
inputs_b = layers.Input(shape=input_shape_b)
h = layers.Dense(256, 'relu')(inputs_b)
outputs_b = layers.Dense(1, 'sigmoid')(h)
return models.Model(inputs_b, outputs_b, name="Backbone")

backbone_model = build_backbone_model()
backbone_model.summary()
def new_model_reuse_backbone(input_shape, name):
inputs = layers.Input(shape=input_shape)
h = layers.Dense(input_shape_b[0], 'relu')(inputs)
outputs = backbone_model(h)
return models.Model(inputs, outputs, name=name)
# Will use backbone model we defined before
new_model_0 = new_model_reuse_backbone((32, ), "new_model_0")
new_model_0.summary()
# Rebuild will reset backbone model's parameters
backbone_model = build_backbone_model()
new_model_1 = new_model_reuse_backbone((256, ), "new_model_1")
new_model_1.summary()

输出:

Model: "Backbone"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         [(None, 16)]              0
_________________________________________________________________
dense (Dense)                (None, 256)               4352
_________________________________________________________________
dense_1 (Dense)              (None, 1)                 257
=================================================================
Total params: 4,609
Trainable params: 4,609
Non-trainable params: 0
_________________________________________________________________
Model: "new_model_0"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_2 (InputLayer)         [(None, 32)]              0
_________________________________________________________________
dense_2 (Dense)              (None, 16)                528
_________________________________________________________________
Backbone (Functional)        (None, 1)                 4609
=================================================================
Total params: 5,137
Trainable params: 5,137
Non-trainable params: 0
_________________________________________________________________
Model: "new_model_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_3 (InputLayer)         [(None, 256)]             0
_________________________________________________________________
dense_3 (Dense)              (None, 16)                4112
_________________________________________________________________
Backbone (Functional)        (None, 1)                 4609
=================================================================
Total params: 8,721
Trainable params: 8,721
Non-trainable params: 0
_________________________________________________________________

相关内容

  • 没有找到相关文章

最新更新