我在Keras有一个预先训练好的模型。我想训练另一个模型,其中模型的输出是已训练模型的输入,并且已训练模型的输出用于未训练模型的损失函数。就像这样
in_a + mod_b(some kind of feedback from B here) --> Model A --> out_a --> Model B --> out_b
error = (in_a - out_b)**2然后使用这个错误来训练模型A。在这个系统中,in_a可以被视为一个常量,也有一个反馈回路
在keras或tensorflow中如何做到这一点?
发布于 2017-03-27 00:59:06
这是一个想法。构建模型A直到输出层,我们假设输出层与模型B的输入层兼容。另外,假设您使用预先训练好的VGG16作为模型B。您将使用预先训练好的权重加载模型:
from keras.applications.vgg16 import VGG16
# Model A is trainable
x = Input(shape=(32,))
x_d = Dense(10)(x)
model_a_out = Dense(10)(x_d)
# Model B
model_b = VGG16(weights='imagenet', include_top=True)
# Freeze Model B
for layer in model_b.layers:
layer.trainable = False
# Set input to Model B as output from A
model_b.input = model_a_out
# Train as usual
model_b.compile... and model_b.fit ...另一种方法是首先构建A,然后:
for layer in model_b.layers:
new_layer = layer
new_layer.trainable = False
model_a.add(new_layer)请查看Keras的Applications页面以获取一些想法。
https://stackoverflow.com/questions/43027764
复制相似问题