我想在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 …Run Code Online (Sandbox Code Playgroud)