Wie multiplizieren eine matrix durch einen Vektor in PyTorch

Ich bin Herumspielen mit PyTorch mit dem Ziel, Sie zu lernen, und ich habe eine sehr dumme Frage: wie kann ich das multiplizieren einer matrix durch einen Vektor?

Hier ist, was ich versucht habe:

>>> import torch
>>> a = torch.rand(4,4)
>>> a

 0.3162  0.4434  0.9318  0.8752
 0.0129  0.8609  0.6402  0.2396
 0.5720  0.7262  0.7443  0.0425
 0.4561  0.1725  0.4390  0.8770
[torch.FloatTensor of size 4x4]

>>> b = torch.rand(4)
>>> b

 0.1813
 0.7090
 0.0329
 0.7591
[torch.FloatTensor of size 4]

>>> a.mm(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: invalid argument 2: dimension 1 out of range of 1D tensor at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensor.c:24
>>> a.mm(b.t())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: t() expects a 2D tensor, but self is 1D
>>> b.mm(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: matrices expected, got 1D, 2D tensors at /Users/soumith/code/builder/wheel/pytorch-src/torch/lib/TH/generic/THTensorMath.c:1288
>>> b.t().mm(a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: t() expects a 2D tensor, but self is 1D

Auf der anderen Seite, wenn ich

>>> b = torch.rand(4,2)

dann mein Erster Versuch, a.mm(b), funktioniert gut. So, das problem ist nur, dass ich multipliziere einen Vektor anstatt einer matrix --- aber wie kann ich dies tun?

InformationsquelleAutor Nathaniel | 2017-12-18
Schreibe einen Kommentar