将 keras 模型输入馈送到输出层

时间:2021-01-21 21:11:59

标签: python keras

所以我正在构建一个 keras 顺序模型,其中最后一个输出层是 Upsampling2D 层,我需要将输入图像提供给该输出层以执行简单的操作并返回输出,有什么想法吗?

编辑:

前面提到的模型是一个 GAN 模型的生成器,其中我需要将输入图像添加到生成器的输出中,然后再将其提供给鉴别器

2 个答案:

答案 0 :(得分:1)

1.您可以使用预训练模型的输入和预训练模型输出层之前的最后一层的输出定义一个主干模型

2.基于该主干模型,定义的新模型具有与预训练模型相同的新跳过连接和输出层

3.将新模型中输出层的权重设置为等于预训练模型中输出层的权重,使用:new_model.layers[-1].set_weights(pre_model.layers[-1].get_weights())

这是一篇关于 Adding Layers to the middle of a pre-trained network whithout invalidating the weights

的好文章

答案 1 :(得分:0)

所以为了将来参考,我通过使用 lambda 层解决了它,如下所示:

# z is the input I needed to use later on with the generator output to perform a certain function

generated_image = self.generator(z)        
generated_image_modified=tf.keras.layers.Concatenate()([generated_image,z])

# with x[...,variable_you_need_range] you can access the input we just concatenated in your train loop

lambd = tf.keras.layers.Lambda(lambda x: your_function(x[...,2:5],x[...,:2]))(generated_image_modified)

full_model = self.discriminator(lambd)
self.combined = Model(z,outputs = full_model)
相关问题