我想制作一个由其他模型组成的模型,以支持各种训练策略。主要目标是:
input_shape
,几层和一个输出层我当然可以将其构建为一个大型模型,但是我有一组特定的训练实验,我希望我的复合模型能够支持:
使用伪代码:
def build_model_a(input_layer):
... build model a using input_layer arg as input...
model = Model(inputs=input_layer,
outputs=output_layer)
model.compile(...)
return model
input_model = build_input_model(args)
model_a = build_model_a(input_model.output)
model_b = build_model_b(input_model.output)
composite_model = Model(inputs=input_model.input,
outputs=[model_a.output,
model_b.output])
# train from input_model through model_a but don't change model_b weights
model_b.trainable = False
composite_model.fit(...)
# train from input_model through model_b but don't change model_a weights
model_a.trainable = False
model_b.trainable = True
composite_model.fit(...)
# train from input_model through model_a but don't change model_b weights
model_b.trainable = True
model_a.trainable = True
composite_model.fit(...)
# Train just input_model
input_model.fit(...)
# Eval through entire model
composite_model.predict(xs)
这是我目前的做法,导致以下错误:
AttributeError: 'Model' object has no attribute '_name'
这使我认为我没有正确处理此问题,因此希望能提供其他建议或参考。我相当有信心,可以使用Keras功能API做到这一点,但正在努力使其正确。
我也不清楚,是否允许对trainable = False
使用此方法。根据试运行的警告,我需要在更改可训练标志后重新编译这些模型...
答案 0 :(得分:0)
至少对于问题的第一部分,您可以使用功能性api将模型用作图层,而不是使用其输入,输出。像这样:
# creation of a model
def build_model_a():
... build model create your own input...
input_layer = Input(shape=i_shape)
model = Model(inputs=input_layer,
outputs=output_layer)
model.compile(...)
return model
# model creations
input_model = build_input_model(args)
model_a = build_model_a()
model_b = build_model_b()
#connect models
input_layer = Input(shape=i_shape)
middle = input_model(input_layer)
out_a = model_a(middle )
out_b = model_b(middle )
composite_model = Model(inputs=input_layer ,
outputs=[out_a ,
out_b ])
也许有人可以完成答案,或者您可以测试自己