有效地堆叠阵列/火炬张量的副本?

Ger*_*ult 4 arrays numpy pytorch tensor

我是 Python/Pytorch 用户。首先,在 numpy 中,假设我有一个大小为 LxL 的数组 M,并且我想要以下数组:A=(M,...,M) 大小,例如 NxLxL,是否有更优雅的/内存这样做的有效方法比:

A=np.array([M]*N) ?
Run Code Online (Sandbox Code Playgroud)

与火炬张量相同的问题!原因,现在,如果 M 是一个变量(torch.tensor),我必须这样做:

A=torch.autograd.Variable(torch.tensor(np.array([M]*N))) 
Run Code Online (Sandbox Code Playgroud)

这是丑陋的!

mex*_*mex 13

Note, that you need to decide whether you would like to allocate new memory for your expanded array or whether you simply require a new view of the existing memory of the original array.

In PyTorch, this distinction gives rise to the two methods expand() and repeat(). The former only creates a new view on the existing tensor where a dimension of size one is expanded to a larger size by setting the stride to 0. Any dimension of size 1 can be expanded to an arbitrary value without allocating new memory. In contrast, the latter copies the original data and allocates new memory.

In PyTorch, you can use expand() and repeat() as follows for your purposes:

import torch

L = 10
N = 20
A = torch.randn(L,L)
A.expand(N, L, L) # specifies new size
A.repeat(N,1,1) # specifies number of copies
Run Code Online (Sandbox Code Playgroud)

In Numpy, there are a multitude of ways to achieve what you did above in a more elegant and efficient manner. For your particular purpose, I would recommend np.tile() over np.repeat(), since np.repeat() is designed to operate on the particular elements of an array, while np.tile() is designed to operate on the entire array. Hence,

import numpy as np

L = 10
N = 20
A = np.random.rand(L,L)
np.tile(A,(N, 1, 1))
Run Code Online (Sandbox Code Playgroud)


hpa*_*ulj -1

输入numpy repeat速度更快:

np.repeat(M[None,...], N,0)
Run Code Online (Sandbox Code Playgroud)

我扩展了 的维度M,然后沿着新的维度重复。