Tensorflow值错误:操作数无法与形状(5、5、160)(19、19、80)一起广播

2024-04-26 22:23:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个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, 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_layers])


Tags: inputsizemodellayersblockactivationkernelfilters
1条回答
网友
1楼 · 发布于 2024-04-26 22:23:40

如果使用kernel_size>;1和strides>;1输出的维度将小于输入的维度

例如:

Conv2D(filters=6, kernel_size=5, stride=2)

将获取维度(32,32,1)的输入,并给出维度(28,28,6)的输出。 如果您试图将其添加到ResNet样式的快捷方式块中,这会导致出现问题,因为不清楚如何添加到不同维度的张量

有几种方法可以解决这个问题

  • 不要减少卷积的维数(保持步幅=1)
  • 通过使用1x1卷积内核,使用与Conv2D中使用的步幅相同的步幅,减小快捷块的大小
  • 将快捷方式块的输出通道数更改为 与Conv2D中的筛选器数相同

相关问题 更多 >