内存中的numpy nd数组布局
numpy nd array layout in memory
转置 ndarry 不会改变内存中的实际布局
即在转置之后,ndarry 不再是 'C' 顺序
我为 numpy 任务编写了一个 cpp 扩展,因此如果给定维度的顺序与 cpp 期望的顺序不一致,我会在调用扩展之前使用 np.traspose,尽管实际内存布局没有按预期更改。
原创
x = np.random.rand(1, 10, 10, 6)
print('shape', x.shape, 'strides', x.strides)
output: shape (1, 10, 10, 6) strides (4800, 480, 48, 8)
现在转置数据期望形状转置,但大步前进仍处于递减顺序
x_t = np.transpose(x, [0, 3, 1, 2])
print('shape', x_t.shape, 'strides', x_t.strides)
shape (1, 6, 10, 10) strides (4800, 8, 480, 48)
即数据布局在内存中保持不变
我想转置以更改实际数据内存布局以实现高效数据循环
我发现的一种方法是这样使用 np.copy
:
x_t = np.transpose(x, [0, 3, 1, 2]).copy(order='C')
print('shape', x_t.shape, 'strides', x_t.strides)
shape (1, 6, 10, 10) strides (4800, 800, 80, 8)
转置 ndarry 不会改变内存中的实际布局 即在转置之后,ndarry 不再是 'C' 顺序
我为 numpy 任务编写了一个 cpp 扩展,因此如果给定维度的顺序与 cpp 期望的顺序不一致,我会在调用扩展之前使用 np.traspose,尽管实际内存布局没有按预期更改。
原创
x = np.random.rand(1, 10, 10, 6)
print('shape', x.shape, 'strides', x.strides)
output: shape (1, 10, 10, 6) strides (4800, 480, 48, 8)
现在转置数据期望形状转置,但大步前进仍处于递减顺序
x_t = np.transpose(x, [0, 3, 1, 2])
print('shape', x_t.shape, 'strides', x_t.strides)
shape (1, 6, 10, 10) strides (4800, 8, 480, 48)
即数据布局在内存中保持不变
我想转置以更改实际数据内存布局以实现高效数据循环
我发现的一种方法是这样使用 np.copy
:
x_t = np.transpose(x, [0, 3, 1, 2]).copy(order='C')
print('shape', x_t.shape, 'strides', x_t.strides)
shape (1, 6, 10, 10) strides (4800, 800, 80, 8)