Keras输出形状具有额外的尺寸 [英] Keras output shape has an extra dimension

查看:77
本文介绍了Keras输出形状具有额外的尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个神经网络,可以接收500px x 500px的RGB彩色图像,并且还将输出相同尺寸的另一幅图像.

I have a neural network that takes in an RGB colour image of 500px by 500px and will also output another image of the same dimensions.

这是我的网络结构:

Generative_Model = Sequential([ 

   Conv2D(32, (6, 6), padding="same", name="generative", input_shape=(500,500, 3), data_format="channels_last")  

   PReLU(alpha_initializer='zeros'), 

   Conv2D(3, (3, 3), padding="same"), 
   PReLU(alpha_initializer='zeros', name="outp1"), 

])

我遇到的问题是出来的尺寸是[None,500,500,3] 尽管我原本以为他们会是[500,500,3].我不确定额外维度来自何处.

The problem I'm having is that the dimensions coming out are [None, 500, 500, 3] though I was expecting them to be [500, 500, 3]. I'm not sure where the extra dimension is coming from.

重要的是,在离开网络之前,必须先删除尺寸,因为它会进入第二个对抗网络.

It's important that the dimensions are removed before leaving the Network as this feeds into a second adversarial network.

这是我打印model.summary()时得到的:

我尝试在末尾添加重塑形状以强制网络降低最后一个尺寸,但是由于输出形状似乎保持不变,因此它似乎无法正常工作.

I've tried adding a reshape at the end to force the network to drop the last dimension but it doesn't appear to be working as the output shape appears to remain the same.

推荐答案

在聊天中与@Dodge交谈时,他指出了以下文档:

Whilst speaking to @Dodge in chat he pointed me to the following docs:

https://www.tensorflow.org/api_docs/python /tf/keras/layers/Reshape

,其中指出其他无"来自批次长度.我需要将第一个网络的输出馈入第二个网络的输出中,而第二个网络的输出预计将不具有批处理维度,因此我在第一个网络的外部使用了重塑来删除了该网络,如下所示:

which states that the additional None comes from the batch length. I needed to feed the output of the first network into the output of a second which expected not to have the batch dimension so I removed this using a reshape outside of the first network like so:

#Adversierial network which is comprised of a generator network and a discriminator network.
self.model = Sequential([
   Gen_Input, # Generator Network
   Reshape((500, 500, 3), input_shape=(500, 500, 3)),
   discriminative_model.Input # Discriminator Network
        ])

这使我能够从图形内部重塑输出.

This allowed me to reshape the output from inside the graph.

这篇关于Keras输出形状具有额外的尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆