【问题标题】:Tensorflow ValueError: Operands could not be broadcast together with shapes (5, 5, 160) (19, 19, 80)Tensorflow ValueError:操作数无法与形状一起广播(5、5、160)(19、19、80)
【发布时间】:2020-06-19 20:35:43
【问题描述】:

我正在创建一个大小为 80 的 CNN,用于第一个隐藏层,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])

【问题讨论】:

    标签: python tensorflow machine-learning artificial-intelligence conv-neural-network


    【解决方案1】:

    如果您应用kernel_size > 1 和strides > 1 的卷积,则输出的维度将小于输入。

    例如:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-04-25
      • 2017-09-09
      • 2012-10-31
      • 2013-04-07
      • 2012-08-05
      • 2018-08-18
      • 2020-06-20
      • 2014-08-24
      • 2018-12-26
      相关资源
      最近更新 更多