假设我有 4 批 5x3 矩阵。所以这些张量的维度是 4x5x3。我如何对每个批次中的每个矩阵进行转置。那么将其转换为 4x3x5 吗?
为了性能,我将在这里放弃一些基准。使用 OP 的答案中提出的相同张量。
In[2]: import torch
In[3]: x = torch.randn(2, 3, 5)
In[4]: x.size()
Out[4]: torch.Size([2, 3, 5])
In[5]: %timeit x.permute(1, 0, 2)
1.03 µs ± 41.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In[6]: %timeit torch.transpose(x, 0, 1)
892 ns ± 9.61 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
In[7]: torch.transpose(x, 0, 1).equal(x.permute(1, 0, 2))
Out[7]: True
Run Code Online (Sandbox Code Playgroud)
显然它torch.transpose
更快,因此建议尽可能使用它。
归档时间: |
|
查看次数: |
3592 次 |
最近记录: |