NumPy Array Reshaped但如何更改池的轴? [英] NumPy Array Reshaped but how to change axis for pooling?

查看:131
本文介绍了NumPy Array Reshaped但如何更改池的轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个8x8矩阵,如下所示:

I have a 8x8 matrix as follows:

[[ 0.3  0.3  0.3  0.3  0.3  0.5  0.1 -0.1]
 [ 0.1  0.1 -0.1  0.3  0.3 -0.1 -0.1 -0.5]
 [-0.1  0.1  0.3 -0.1  0.3 -0.1 -0.1 -0.1]
 [-0.1  0.1  0.5  0.3 -0.3 -0.1 -0.3 -0.1]
 [ 0.5  0.1 -0.1  0.1 -0.1 -0.1 -0.3 -0.5]
 [ 0.1 -0.1 -0.3 -0.5 -0.5 -0.1 -0.1 -0.3]
 [-0.5 -0.3 -0.3 -0.3 -0.1 -0.5 -0.1 -0.3]
 [-0.3 -0.3 -0.3 -0.3 -0.1 -0.1 -0.5 -0.3]]

我的窗口是2x2。我想做的是将四个数字(上下数字)汇集在一起​​。示例输出如下:

My window is 2x2. What I am trying to do is get four numbers together (up and down numbers) for pooling. Sample output looks like this:

    [[0.3 0.3
      0.1 0.1]

      [0.3 0.3
      -0.1 0.3]
       .......
       .......
      [-0.1 -0.3
       -0.5 -0.3]]

我使用的是 print arr.reshape(16,2,2 )我不明白的是如何为此要求设置轴。我的输出是:

What I using is print arr.reshape(16,2,2) What i can't understand is how to setup axis for this requirement. My output is:

[[[ 0.3  0.3]
  [ 0.3  0.3]]

 [[ 0.3  0.1]
  [ 0.5 -0.1]]

 [[ 0.1 -0.1]
  [ 0.1  0.3]]

 [[ 0.3 -0.1]
  [-0.1 -0.5]]

 [[-0.1  0.3]
  [ 0.1 -0.1]]

 [[ 0.3 -0.1]
  [-0.1 -0.1]]

 [[-0.1  0.5]
  [ 0.1  0.3]]

 [[-0.3 -0.3]
  [-0.1 -0.1]]

 [[ 0.5 -0.1]
  [ 0.1  0.1]]

 [[-0.1 -0.3]
  [-0.1 -0.5]]

 [[ 0.1 -0.3]
  [-0.1 -0.5]]

 [[-0.5 -0.1]
  [-0.1 -0.3]]

 [[-0.5 -0.3]
  [-0.3 -0.3]]

 [[-0.1 -0.1]
  [-0.5 -0.3]]

 [[-0.3 -0.3]
  [-0.3 -0.3]]

 [[-0.1 -0.5]
  [-0.1 -0.3]]]

请说明如何轴适用于这种情况。或者,如果它们是获得最大池化的更好方法,请提及。

Please explain how would axis be applied on this type of situation. Or if their is a better way to get max-pooling do mention.

注意:所有这些都是针对最大池化的。我在python上使用了NumPy,SciPy。

Note: All of this is for max-pooling. I am using NumPy, SciPy on python.

推荐答案

Reshape将两个轴分别拆分为两个,使得后者分割轴的长度与块尺寸相同。这将为我们提供一个 4D 数组。然后,沿着后面的轴(即该 4D 数组中的第二轴和第四轴)执行最大查找。

Reshape splitting each of the two axes into two each such that the latter of the split axes is of the same length as the block size. This would give us a 4D array. Then, perform maximum finding along those latter axes, which would be the second and fourth axes in that 4D array.

因此,只需-

m,n = a.shape
out = a.reshape(m//2,2,n//2,2).max(axis=(1,3))

示例运行-

In [50]: a
Out[50]: 
array([[87, 96, 46, 97, 25, 22, 13, 16],
       [65, 62, 68, 87, 52, 80, 26, 82],
       [27, 82, 50, 20, 11, 14, 94, 23],
       [86, 44, 17, 97, 17, 57, 76, 42],
       [47, 85, 30, 61, 55, 87, 11, 35],
       [36, 11, 29, 45, 16, 54, 40, 77],
       [38, 87, 94, 77, 53, 20, 46, 18],
       [86, 50, 17, 23, 91, 23, 25, 11]])

In [51]: m,n = a.shape

In [52]: a.reshape(m//2,2,n//2,2).max(axis=(1,3))
Out[52]: 
array([[96, 97, 80, 82],
       [86, 97, 57, 94],
       [85, 61, 87, 77],
       [87, 94, 91, 46]])

这篇关于NumPy Array Reshaped但如何更改池的轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆