在PyTorch中矩阵和向量之间的加减运算

8
我希望在pytorch中进行矩阵和向量的加减乘除运算。如何以良好的性能完成此操作?我尝试使用expand函数,但速度非常慢(我使用的是大矩阵和小向量)。
a = torch.rand(2,3)
print(a)
 0.7420  0.2990  0.3896
 0.0715  0.6719  0.0602
[torch.FloatTensor of size 2x3]
b = torch.rand(2)
print(b)
 0.3773
 0.6757
[torch.FloatTensor of size 2]
a.add(b)
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3066, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-17-a1cb1b03d031>", line 1, in <module>
    a.add(b)
RuntimeError: inconsistent tensor size, expected r_ [2 x 3], t [2 x 3] and src [2] to have the same number of elements, but got 6, 6 and 2 elements respectively at c:\miniconda2\conda-bld\pytorch-cpu_1519449358620\work\torch\lib\th\generic/THTensorMath.c:1021

预期结果:

 0.7420-0.3773  0.2990-0.3773  0.3896-0.3773
 0.0715-0.6757  0.6719-0.6757  0.0602-0.6757
3个回答

7
为了使用广播,您需要将张量b的维度提升为二维,因为张量a是二维的。在此过程中,您需要促进张量b的维度。
In [43]: a
Out[43]: 
tensor([[ 0.9455,  0.2088,  0.1070],
        [ 0.0823,  0.6509,  0.1171]])

In [44]: b
Out[44]: tensor([ 0.4321,  0.8250])

# subtraction    
In [46]: a - b[:, None]
Out[46]: 
tensor([[ 0.5134, -0.2234, -0.3252],
        [-0.7427, -0.1741, -0.7079]])

# alternative way to do subtraction
In [47]: a.sub(b[:, None])
Out[47]: 
tensor([[ 0.5134, -0.2234, -0.3252],
        [-0.7427, -0.1741, -0.7079]])

# yet another approach
In [48]: torch.sub(a, b[:, None])
Out[48]: 
tensor([[ 0.5134, -0.2234, -0.3252],
        [-0.7427, -0.1741, -0.7079]])

其他操作(+*)可以类比地进行。
就性能而言,似乎没有使用一种方法优于其他方法的优势。只需使用其中任何一种方法即可。
In [49]: a = torch.rand(2000, 3000)
In [50]: b = torch.rand(2000)

In [51]: %timeit torch.sub(a, b[:, None])
2.4 ms ± 8.31 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [52]: %timeit a.sub(b[:, None])
2.4 ms ± 6.94 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In [53]: %timeit a - b[:, None]
2.4 ms ± 12 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

1

你尝试过使用.unsqueeze_()方法吗?你可以使用.unsqueeze_()方法就地向张量中添加一个维度。我相信这样会更快。作为参数,你需要传递要扩展的轴索引。

a = torch.rand(2,3)
print(a)
"""Output 
0.9323  0.9162  0.9505
0.9430  0.6184  0.3671
[torch.FloatTensor of size 2x3]"""

b = torch.rand(2)
print(b)
"""Output
0.4723
0.9345
[torch.FloatTensor of size 2]"""

b.unsqueeze_(1)
"""Output
0.4723
0.9345
torch.FloatTensor of size 2x1]"""

a.add(b)
"""Output
1.4046  1.3885  1.4228
1.8775  1.5528  1.3016
[torch.FloatTensor of size 2x3]"""

0

重塑怎么样?

In [2]: a = torch.rand(2,3)
   ...: 

In [3]: a
Out[3]: 
tensor([[ 0.2207,  0.1203,  0.7908],
        [ 0.6702,  0.7557,  0.0708]])

In [4]: b = torch.rand(2)
   ...: 

In [5]: b
Out[5]: tensor([ 0.5953,  0.7925])

In [6]: a.add(b.reshape(-1,1))
Out[6]: 
tensor([[ 0.8160,  0.7156,  1.3861],
        [ 1.4627,  1.5481,  0.8632]])

同时也要检查广播相关内容:https://pytorch.org/docs/stable/notes/broadcasting.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接