gpt4 book ai didi

python - 值错误: A `Concatenate` layer requires inputs with matching shapes except for the concat axis

转载 作者:行者123 更新时间:2023-12-01 08:25:28 24 4
gpt4 key购买 nike

我正在尝试使用 Keras 功能 API 实现一种特殊类型的神经网络,如下所示:

enter image description here

但是我在连接层上遇到了问题:

ValueError: A "Concatenate" layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 160, 160, 384), (None, 160, 160, 48)]

注意:根据我的研究,我认为这个问题不重复,我已经看到 this question ,和this post (用 Google 翻译),但它们似乎不起作用(相反,它们使问题甚至稍微“更糟”)。

<小时/>

这是连接层之前的神经网络代码:

from keras.layers import Input, Dense, Conv2D, ZeroPadding2D, MaxPooling2D, BatchNormalization, concatenate
from keras.activations import relu
from keras.initializers import RandomUniform, Constant, TruncatedNormal

# Network 1, Layer 1
screenshot = Input(shape=(1280, 1280, 0), dtype='float32', name='screenshot')
# padded1 = ZeroPadding2D(padding=5, data_format=None)(screenshot)
conv1 = Conv2D(filters=96, kernel_size=11, strides=(4, 4), activation=relu, padding='same')(screenshot)
# conv1 = Conv2D(filters=96, kernel_size=11, strides=(4, 4), activation=relu, padding='same')(padded1)
pooling1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='same')(conv1)
normalized1 = BatchNormalization()(pooling1) # https://stats.stackexchange.com/questions/145768/importance-of-local-response-normalization-in-cnn

# Network 1, Layer 2

# padded2 = ZeroPadding2D(padding=2, data_format=None)(normalized1)
conv2 = Conv2D(filters=256, kernel_size=5, activation=relu, padding='same')(normalized1)
# conv2 = Conv2D(filters=256, kernel_size=5, activation=relu, padding='same')(padded2)
normalized2 = BatchNormalization()(conv2)
# padded3 = ZeroPadding2D(padding=1, data_format=None)(normalized2)
conv3 = Conv2D(filters=384, kernel_size=3, activation=relu, padding='same',
kernel_initializer=TruncatedNormal(stddev=0.01),
bias_initializer=Constant(value=0.1))(normalized2)
# conv3 = Conv2D(filters=384, kernel_size=3, activation=relu, padding='same',
# kernel_initializer=RandomUniform(stddev=0.1),
# bias_initializer=Constant(value=0.1))(padded3)

# Network 2, Layer 1

textmaps = Input(shape=(160, 160, 128), dtype='float32', name='textmaps')
txt_conv1 = Conv2D(filters=48, kernel_size=1, activation=relu, padding='same',
kernel_initializer=TruncatedNormal(stddev=0.01), bias_initializer=Constant(value=0.1))(textmaps)

# (Network 1 + Network 2), Layer 1

merged = concatenate([conv3, txt_conv1], axis=1)

这是解释器评估变量 conv3txt_conv1 的方式:

>>> conv3
<tf.Tensor 'conv2d_3/Relu:0' shape=(?, 160, 160, 384) dtype=float32>
>>> txt_conv1
<tf.Tensor 'conv2d_4/Relu:0' shape=(?, 160, 160, 48) dtype=float32>

这是解释器在将 image_data_format 设置为 channels_first 后评估 txt_conv1conv3 变量的方式:

>>> conv3
<tf.Tensor 'conv2d_3/Relu:0' shape=(?, 384, 160, 0) dtype=float32>
>>> txt_conv1
<tf.Tensor 'conv2d_4/Relu:0' shape=(?, 48, 160, 128) dtype=float32>

这两层都具有架构中实际未描述的形状。

<小时/>

有什么办法可以解决这个问题吗?也许我没有编写适当的代码(我是 Keras 新手)。

附注

我知道上面的代码没有组织,我只是测试。

谢谢!

最佳答案

您应该在连接层中将轴更改为 -1,因为要连接的两个张量的形状仅在最后一个维度上有所不同。所得张量的形状为 (?, 160, 160, 384 + 48)

关于python - 值错误: A `Concatenate` layer requires inputs with matching shapes except for the concat axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54280667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com