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

How to revert keras sequential model to a state previous to build?

我想重复使用相同的模型架构但使用不同的数据集,即以编程方式将输入层更改为不同的形状,并在需要时重置模型参数。

类似于

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

有人知道执行此操作的干净方法吗?

您可以创建一个 backbone 模型并重复使用它来构建任意多个具有不同输入层的模型,backbone 模型的参数对于您创建的所有新模型都将保持不变,如果要重置参数,请构建新的 backbone 模型,示例代码在这里:

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
_________________________________________________________________