简体   繁体   English

将 keras model 输入馈送到 output 层

[英]Feed keras model input to the output layer

So I am building a keras sequential model in which the last output layer is an Upsampling2D layer & I need to feed the input image to that output layer to do a simple operation and return the output, any ideas? So I am building a keras sequential model in which the last output layer is an Upsampling2D layer & I need to feed the input image to that output layer to do a simple operation and return the output, any ideas?

EDIT:编辑:

The model mentioned before is the generator of a GAN model in which I need to add the input image to the output of the generator before feeding it to the discriminator前面提到的 model 是 GAN model 的生成器,其中我需要将输入图像添加到生成器的 output 中,然后再将其馈送到鉴别器

1.You can define a backbone model using inputs of pre-trained model and the outputs of the last layer before the output layer of pre-trained model 1.You can define a backbone model using inputs of pre-trained model and the outputs of the last layer before the output layer of pre-trained model

2.Base on that backbone model, defined new model have that new skip connection and the output layer as same as pre-trained model 2.Base on that backbone model, defined new model have that new skip connection and the output layer as same as pre-trained model

3.Set the weights of output layer in new model to equal to weights of output layer in pre-trained model, using: new_model.layers[-1].set_weights(pre_model.layers[-1].get_weights()) 3.Set the weights of output layer in new model to equal to weights of output layer in pre-trained model, using: new_model.layers[-1].set_weights(pre_model.layers[-1].get_weights())

Here is one good article about Adding Layers to the middle of a pre-trained network whithout invalidating the weights这是一篇关于在预训练网络中间添加层而不使权重无效的好文章

So for the future reference, I solved it by using lambda layers as follow:因此,为了将来参考,我通过使用 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Conv2d 层 output 作为 Keras model 的输入? - How to feed a Conv2d layer output as input for a Keras model? 如何将 keras LSTM 层的输出馈入输入层? - How to feed output of keras LSTM layer into input layer? 将输入输入到 tensorflow.keras model 的中间层 - Feed input to intermediate layer of tensorflow.keras model Keras:修改/重新排列输入层的元素以创建新的输入层并为新的输入层提供数据以创建新的输出 - Keras: Modify/Rearrange elements of Input Layer to create a new Input Layer and feed the new input Layer to create a new Output Keras Sequential 模型输入层 - Keras Sequential model input layer Keras:一次将多个输入输入到一个输入层,然后在 output 之前对结果求和 - Keras: feed multiple inputs to ONE input layer at a time and then sum results before the output 在训练TensorFlow模型(,?不是Keras模型)时,如何获取模型中间层(op)的输入输出? - During training the TensorFlow model(!!Not the Keras model), How to get the input and output of the intermediate layer(op) of the model? 模型输入必须来自 `tf.keras.Input` ...,它们不能是前一个非输入层的输出 - Model inputs must come from `tf.keras.Input` …, they cannot be the output of a previous non-Input layer 使用内部层的输出来拟合Keras模型? - Using the output of an internal layer to fit Keras model? 如何更改 keras 模型中密集层的输出? - How to change the output of a dense layer in a keras model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM