简体   繁体   English

合并多个keras max池层

[英]merge multiple keras max pooling layers

I am new to keras. 我是新来的喀拉拉邦。

My goal is to have total of 4 max pooling layers. 我的目标是共有4个最大池化层。 All of them take same input with shape (N, 256). 它们全部采用相同的形状(N,256)输入。 The first layer does global max pooling and give 1 output. 第一层进行全局最大池化并给出1个输出。 The second layer with N / 2 pooling size and N / 2 stride, gives 2 outputs. 具有N / 2合并大小和N / 2步幅的第二层提供2个输出。 The third gives 4 outputs and the fourth gives 8 outputs. 第三个给出4个输出,第四个给出8个输出。 Here is my code. 这是我的代码。

    test_x = np.random.rand(N, 256, 1)

    model = Sequential()

    input1 = Input(shape=test_x.shape, name='input1')
    input2 = Input(shape=test_x.shape, name='input2')
    input3 = Input(shape=test_x.shape, name='input3')
    input4 = Input(shape=test_x.shape, name='input4')

    max1 = MaxPooling2D(pool_size=(N, 256), strides=N)(input1)
    max2 = MaxPooling2D(pool_size=(N / 2, 256), strides=N / 2)(input2)
    max3 = MaxPooling2D(pool_size=(N / 4, 256), strides=N / 4)(input3)
    max4 = MaxPooling2D(pool_size=(N / 8, 256), strides=N / 8)(input4)

    mrg = Merge(mode='concat')([max1, max2, max3, max4])

After creating 4 max pooling layers, I try to merge them together, but keras gives this error. 创建4个最大池化层后,我尝试将它们合并在一起,但是keras会出现此错误。

ValueError: Dimension 1 in both shapes must be equal, but are 4 and 8 for 'merge_1/concat' (op: 'ConcatV2') with input shapes: [?,1,1,1], [?,2,1,1], [?,4,1,1], [?,8,1,1], [] and with computed input tensors: input[4] = <3>. ValueError:两个形状中的尺寸1必须相等,但对于输入形状为[?,1,1,1],[?, 2,1,'merge_1 / concat'(op:'ConcatV2')的尺寸4和8 1],[?, 4,1,1],[?, 8,1,1],[],并具有计算的输入张量:input [4] = <3>。

How can I solve this issue? 我该如何解决这个问题? Is merging the correct way to achieve my goal in keras? 合并正确的方法以实现我在喀拉拉邦的目标吗?

For concatenation, all dimensions must have the same number of elements, except for the concat dimension itself. 对于串联,除concat尺寸本身外,所有尺寸都必须具有相同数量的元素。

As you can see, your results have shape: 如您所见,您的结果具有以下形状:

(?, 1, 1, 1)    
(?, 2, 1, 1)    
(?, 4, 1, 1)    
(?, 8, 1, 1)    

Naturally, the only possible way to concatenate them is in the second axis (axis=1) 自然,连接它们的唯一可能方法是在第二个轴(axis = 1)中

mrg = Concatenate(axis=1)([max1,max2,max3,max4])

But notice that (unless you have specific reasons for that and know exaclty what you're doing) this will result in a very weird image, since you're concatenating in a spatial dimension, not in a channel dimension. 但是请注意(除非您有特定的原因并且知道自己在做什么),这将导致图像非常奇怪,因为您是在空间维度上进行合并,而不是在渠道维度上进行合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM