Python Tensorflow值错误:操作数无法与形状(5、5、160)(19、19、80)一起广播
我正在创建一个CNN,第一个隐藏层的大小为80,其余的conv层的大小为160,最后一个隐藏层的大小为128。但我不断地遇到一条错误消息,我真的不知道它是什么意思。输入数据的形状是(80,80,1),这是我输入到神经网络的 以下是创建CNN的代码:Python Tensorflow值错误:操作数无法与形状(5、5、160)(19、19、80)一起广播,python,tensorflow,machine-learning,artificial-intelligence,conv-neural-network,Python,Tensorflow,Machine Learning,Artificial Intelligence,Conv Neural Network,我正在创建一个CNN,第一个隐藏层的大小为80,其余的conv层的大小为160,最后一个隐藏层的大小为128。但我不断地遇到一条错误消息,我真的不知道它是什么意思。输入数据的形状是(80,80,1),这是我输入到神经网络的 以下是创建CNN的代码: if start_model is not None: model = load_model(start_model) else: def res_net_block(input_layers, con
if start_model is not None:
model = load_model(start_model)
else:
def res_net_block(input_layers, conv_size, hm_filters, hm_strides):
x = Conv2D(conv_size, kernel_size=hm_filters, strides=hm_strides, activation="relu", padding="same")(input_layers)
x = BatchNormalization()(x)
x = Conv2D(conv_size, kernel_size=hm_filters, strides=hm_strides, activation=None, padding="same")(x)
x = Add()([x, input_layers]) # Creates resnet block
x = Activation("relu")(x)
return x
input = keras.Input(i_shape)
x = Conv2D(80, kernel_size=8, strides=4, activation="relu")(input)
x = BatchNormalization()(x)
for i in range(3):
x = res_net_block(x, 160, 4, 2)
x = Conv2D(160, kernel_size=4, strides=2, activation="relu")(x)
x = BatchNormalization()(x)
x = Flatten(input_shape=(np.prod(window_size), 1, 1))(x)
x = Dense(128, activation="relu")(x)
output = Dense(action_space_size, activation="linear")(x)
model = keras.Model(input, output)
model.compile(optimizer=Adam(lr=0.01), loss="mse", metrics=["accuracy"])
顺便说一句,错误消息位于代码中的
x=Add()([x,input\u layers])
如果使用kernel\u size
>1和strips
>1进行卷积,则输出的维度将小于输入
例如:
Conv2D(filters=6, kernel_size=5, stride=2)
将获取维度(32,32,1)
的输入,并给出维度(28,28,6)
的输出。
如果尝试将其添加到样式快捷方式块中,则会出现问题,因为不清楚如何添加到不同维度的张量
有几种方法可以解决这个问题
- 不要减少卷积的维数(保持步幅=1)
- 使用1x1卷积内核减小快捷方式块的大小,其步长与
Conv2D
- 将快捷方式块的输出通道数更改为
与
Conv2D