【问题标题】:Feed keras model input to the output layer将 keras 模型输入馈送到输出层
【发布时间】:2021-01-21 21:11:59
【问题描述】:

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

编辑:

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

【问题讨论】:

  • 你想做什么样的简单操作?你想使用跳过连接吗?
  • 是的,它就像一个跳过连接,但是该模型是预训练的,所以我宁愿避免重新创建架构,而只是替换最后一个上采样层(注意:这个模型将成为 GAN 架构上的生成器)

标签: python keras


【解决方案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的好文章

【讨论】:

    【解决方案2】:

    所以为了将来参考,我通过使用 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      • 2021-06-18
      • 2018-03-16
      • 2017-05-25
      • 1970-01-01
      • 2017-03-01
      • 2017-11-21
      相关资源
      最近更新 更多