在不使用 reshape 的情况下重塑 n 维数组的视图
reshaping a view of a n-dimensional array without using reshape
tl;dr 我可以在不使用 numpy.reshape 的情况下将 numpy 数组的视图从 5x5x5x3x3x3 重塑为 125x1x1x3x3x3 吗?
我想对一个卷(大小为 MxMxM)执行滑动 window 操作(步幅不同)。可以使用 numpy.lib.stride_tricks.as_strided
, as previously suggested by Benjamin and Eickenberg, and demonstrated in the below code snippet, which uses a helper method from skimage 生成滑动 window 数组,该数组使用 as_strided
.
此辅助方法的输出为我提供了 NxNxNxnxnxn 的形状,但我更希望该形状为 N^3x1xnxnxn。虽然我可以使用 np.reshape 来实现这一点,但如果音量变大 (> 100x100x100),np.reshape 会很慢,我不确定为什么。我以为我可以使用 as_stride 来重塑输出,但是 numpy 崩溃了(下面的代码片段)。关于如何在不使用 np.reshape 的情况下将辅助方法的输出视图视为 N**3x1xnxnxn 的任何想法?
import numpy as np
import skimage
l = 15
s = 3
X = np.ones((l,l,l))
print('actual shape',X.shape)
view = skimage.util.shape.view_as_blocks(X,(s,s,s))
print('original view',view.shape)
new_shape = ((l/s)**3,1,1,s,s,s)
print('new view',new_shape)
view_correct = view.reshape(new_shape)
print(view_correct.shape)
print('coord:','124,0,0,2,2,2','value:',view_correct[124,0,0,2,2,2])
view_incorrect = np.lib.stride_tricks.as_strided(view, shape=new_shape)
print(view_incorrect.shape)
print('coord:','124,0,0,2,2,2','value:',view_incorrect[124,0,0,2,2,2])
我从 view_as_blocks
中拿了一个例子,并尝试了你的 reshape 风格:
A = np.arange(4*4).reshape(4,4)
B = view_as_blocks(A, block_shape=(2, 2))
print(A.__array_interface__)
print(B.__array_interface__)
C = B.reshape((2*2,2,2))
print(C.__array_interface__)
生产:
{'typestr': '<i4', 'data': (153226600, False), 'shape': (4, 4),
'descr': [('', '<i4')], 'version': 3, 'strides': None}
{'typestr': '<i4', 'data': (153226600, False), 'shape': (2, 2, 2, 2),
'descr': [('', '<i4')], 'version': 3, 'strides': (32, 8, 16, 4)}
{'typestr': '<i4', 'data': (150895960, False), 'shape': (4, 2, 2),
'descr': [('', '<i4')], 'version': 3, 'strides': None}
A
和B
的data
指针相同; B
是对 A
的看法。
但是C
的指针不同。它是一个副本。这就解释了为什么你的情况需要这么长时间。
让我们做点不同的事情:
A = np.arange(4*4).reshape(4,4)
B = view_as_blocks(A, block_shape=(2, 2))
print(A.__array_interface__)
print(B.__array_interface__)
C = B.reshape((2*2,1,2,2))
print(C.__array_interface__)
D = as_strided(B, shape=(2*2,1,2,2))
print(D.__array_interface__)
print(B[1,1,:,:])
print(C[3,0,:,:])
print(D[3,0,:,:])
生产
1254:~/mypy$ python3 skshape.py
{'strides': None, 'typestr': '<i4', 'version': 3,
'data': (154278744, False), 'shape': (4, 4), 'descr': [('', '<i4')]}
{'strides': (32, 8, 16, 4), 'typestr': '<i4', 'version': 3,
'data': (154278744, False), 'shape': (2, 2, 2, 2), 'descr': [('', '<i4')]}
{'strides': None, 'typestr': '<i4', 'version': 3,
'data': (155705400, False), 'shape': (4, 1, 2, 2), 'descr': [('', '<i4')]}
{'strides': (32, 8, 16, 4), 'typestr': '<i4', 'version': 3,
'data': (154278744, False), 'shape': (4, 1, 2, 2), 'descr': [('', '<i4')]}
[[10 11]
[14 15]]
[[10 11]
[14 15]]
[[ 154561960 -1217783696]
[ 48 3905]]
整形再次创建一个副本。第二个 as_strides
returns 一个观点,但是步幅搞砸了。它正在查看原始数据缓冲区之外的内存(这就是为什么自己大步前进是危险的部分原因)。
在我的示例中,查看每个块的第一个角值
print(B[:,:,0,0])
print(C[:,0,0,0])
[[ 0 2]
[ 8 10]]
[ 0 2 8 10]
对于B
,行增加8,列增加2;这反映在 (32,8)
(4*8,4*2) 中。
但在 C
中,步骤是 (2,6,2) - 跨步不能做到这一点。
由此我得出结论,如果没有副本,则无法进行重塑。
tl;dr 我可以在不使用 numpy.reshape 的情况下将 numpy 数组的视图从 5x5x5x3x3x3 重塑为 125x1x1x3x3x3 吗?
我想对一个卷(大小为 MxMxM)执行滑动 window 操作(步幅不同)。可以使用 numpy.lib.stride_tricks.as_strided
, as previously suggested by Benjamin and Eickenberg, and demonstrated in the below code snippet, which uses a helper method from skimage 生成滑动 window 数组,该数组使用 as_strided
.
此辅助方法的输出为我提供了 NxNxNxnxnxn 的形状,但我更希望该形状为 N^3x1xnxnxn。虽然我可以使用 np.reshape 来实现这一点,但如果音量变大 (> 100x100x100),np.reshape 会很慢,我不确定为什么。我以为我可以使用 as_stride 来重塑输出,但是 numpy 崩溃了(下面的代码片段)。关于如何在不使用 np.reshape 的情况下将辅助方法的输出视图视为 N**3x1xnxnxn 的任何想法?
import numpy as np
import skimage
l = 15
s = 3
X = np.ones((l,l,l))
print('actual shape',X.shape)
view = skimage.util.shape.view_as_blocks(X,(s,s,s))
print('original view',view.shape)
new_shape = ((l/s)**3,1,1,s,s,s)
print('new view',new_shape)
view_correct = view.reshape(new_shape)
print(view_correct.shape)
print('coord:','124,0,0,2,2,2','value:',view_correct[124,0,0,2,2,2])
view_incorrect = np.lib.stride_tricks.as_strided(view, shape=new_shape)
print(view_incorrect.shape)
print('coord:','124,0,0,2,2,2','value:',view_incorrect[124,0,0,2,2,2])
我从 view_as_blocks
中拿了一个例子,并尝试了你的 reshape 风格:
A = np.arange(4*4).reshape(4,4)
B = view_as_blocks(A, block_shape=(2, 2))
print(A.__array_interface__)
print(B.__array_interface__)
C = B.reshape((2*2,2,2))
print(C.__array_interface__)
生产:
{'typestr': '<i4', 'data': (153226600, False), 'shape': (4, 4),
'descr': [('', '<i4')], 'version': 3, 'strides': None}
{'typestr': '<i4', 'data': (153226600, False), 'shape': (2, 2, 2, 2),
'descr': [('', '<i4')], 'version': 3, 'strides': (32, 8, 16, 4)}
{'typestr': '<i4', 'data': (150895960, False), 'shape': (4, 2, 2),
'descr': [('', '<i4')], 'version': 3, 'strides': None}
A
和B
的data
指针相同; B
是对 A
的看法。
但是C
的指针不同。它是一个副本。这就解释了为什么你的情况需要这么长时间。
让我们做点不同的事情:
A = np.arange(4*4).reshape(4,4)
B = view_as_blocks(A, block_shape=(2, 2))
print(A.__array_interface__)
print(B.__array_interface__)
C = B.reshape((2*2,1,2,2))
print(C.__array_interface__)
D = as_strided(B, shape=(2*2,1,2,2))
print(D.__array_interface__)
print(B[1,1,:,:])
print(C[3,0,:,:])
print(D[3,0,:,:])
生产
1254:~/mypy$ python3 skshape.py
{'strides': None, 'typestr': '<i4', 'version': 3,
'data': (154278744, False), 'shape': (4, 4), 'descr': [('', '<i4')]}
{'strides': (32, 8, 16, 4), 'typestr': '<i4', 'version': 3,
'data': (154278744, False), 'shape': (2, 2, 2, 2), 'descr': [('', '<i4')]}
{'strides': None, 'typestr': '<i4', 'version': 3,
'data': (155705400, False), 'shape': (4, 1, 2, 2), 'descr': [('', '<i4')]}
{'strides': (32, 8, 16, 4), 'typestr': '<i4', 'version': 3,
'data': (154278744, False), 'shape': (4, 1, 2, 2), 'descr': [('', '<i4')]}
[[10 11]
[14 15]]
[[10 11]
[14 15]]
[[ 154561960 -1217783696]
[ 48 3905]]
整形再次创建一个副本。第二个 as_strides
returns 一个观点,但是步幅搞砸了。它正在查看原始数据缓冲区之外的内存(这就是为什么自己大步前进是危险的部分原因)。
在我的示例中,查看每个块的第一个角值
print(B[:,:,0,0])
print(C[:,0,0,0])
[[ 0 2]
[ 8 10]]
[ 0 2 8 10]
对于B
,行增加8,列增加2;这反映在 (32,8)
(4*8,4*2) 中。
但在 C
中,步骤是 (2,6,2) - 跨步不能做到这一点。
由此我得出结论,如果没有副本,则无法进行重塑。