[英]fastest way to stack ndarrays
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.
如果有人能给我一个理由或更好的方法,我会很高兴。
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.
下面是一个例子,这里不需要找到正确的索引。
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)
On my machine they performed the following (on the big example with complex entries):他们在我的机器上执行了以下操作(在具有复杂条目的大示例中):
np.einsum
~ 170s
np.einsum
~ 170s
manual stacking
~ 185s
manual stacking
~ 185s
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.