简体   繁体   English

堆叠 ndarrays 的最快方法

[英]fastest way to stack ndarrays

Gist要旨

Basically I want to perform an increase in dimension of two axes on a n-dimensonal tensor.基本上我想在 n 维张量上增加两个轴的维度。 For some reason this operation seems very slow on bigger tensors.出于某种原因,此操作在较大的张量上似乎非常慢。 If someone can give me a reason or better method I'd be very happy.如果有人能给我一个理由或更好的方法,我会很高兴。

Goal目标

Going from (4, 8, 8, 4, 4, 4, 4, 4, 16, 8, 4, 4, 1) to (4, 32, 8, 4, 4, 4, 4, 4, 4, 8, 4, 4, 1) takes roughly 170 second .(4, 8, 8, 4, 4, 4, 4, 4, 16, 8, 4, 4, 1)(4, 32, 8, 4, 4, 4, 4, 4, 4, 8, 4, 4, 1)大约需要170 second I'd like to improve on that.我想对此进行改进。 Below is an example, finding the correct indices is not necessary here.下面是一个例子,这里不需要找到正确的索引。

Example Code示例代码

Increase dimension (0,2) of tensor增加tensor的维度(0,2)

tensor = np.arange(16).reshape(2,2,4,1)
I = np.identity(4)

I tried 3 different methods:我尝试了 3 种不同的方法:

np.kron

indices = [1,3,0,2]
result = np.kron(
            I, tensor.transpose(indices)
        ).transpose(np.argsort(indices))
print(result.shape) # should be (8,2,16,1)

manual stacking

col = []
for i in range(4):
    row  = [np.zeros_like(tensor)]*4
    row[i]=tensor
    col.append(a)
result = np.array(col).transpose(0,2,3,1,4,5).reshape(8,2,16,1)
print(result.shape) # should be (8,2,16,1)

np.einsum

result =np.einsum("ij, abcd -> iabjcd", I, tensor).reshape(8,2,16,1)
print(result.shape) # should be (8,2,16,1)

Results结果

On my machine they performed the following (on the big example with complex entries):他们在我的机器上执行了以下操作(在具有复杂条目的大示例中):

  1. np.einsum ~ 170s np.einsum ~ 170s
  2. manual stacking ~ 185s manual stacking ~ 185s
  3. np.kron ~ 580s np.kron ~ 580s

As Jérôme pointed out:正如杰罗姆指出的那样:

all your operations seems to involve a transposition which is known to be very expensive on modern hardware.您的所有操作似乎都涉及换位,这在现代硬件上众所周知是非常昂贵的。

I reworked my algorithm to not rely on the dimensional increase by doing certain preprocessing steps.我通过执行某些预处理步骤重新设计了我的算法,使其不依赖于维数增加。 This indeed speeds up the overall process substantially.这确实大大加快了整个过程。

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

相关问题 比较Numpy ndarrays的最快方法 - Fastest way to compare Numpy ndarrays 堆叠CSV文件的最快方法 - Fastest way to stack CSV files 在循环中堆叠 numpy arrays 的最快方法是什么? - What is the fastest way to stack numpy arrays in a loop? 制作 ndarray 的 numpy 数组的正确方法 - Proper way to make numpy array of ndarrays 在新维度上使用 np.stack 在循环中堆叠 ndarrays(创建一个 ndarrays 数组) - Stacking ndarrays in a loop using np.stack over the new dimension (creating an array of ndarrays) 在大型TIFF堆栈ndarray上对numpy / scipy样条进行优化吗? - Optimizations to be had for numpy/scipy splines over large TIFF stack ndarrays? 将许多具有相同列的数据帧垂直堆叠在一起的最快方法是什么? - What's the fastest way to vertically stack a lot of dataframes with same columns together? 在两个 Ndarray 之间执行广播二进制操作的 Pythonic 方式 - Pythonic way to perform broadcasting binary operations between two Ndarrays 有没有一种很好的方法来切片Python numpy ndarrays而无需检查边界符号? - Is there a nice way to slice Python numpy ndarrays without checking sign of the bounds? 是否可以更快地将ndarray的值“分配”到基于赋值的其他ndarray中? - Faster way to “distribute” values of an ndarray into other ndarrays based on assignments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM