Matrix-vector multiply (handling batched data)
<SCODE># (batch x inp)
v = torch.randn(5, 15)
# (inp x output)
M = torch.randn(15, 20)
<ECODE> Compute: <SCODE># (batch x output)
out = torch.Tensor(5, 20)
for i, batch_v in enumerate(v):
    out[i] = (batch_v * M).t()
<ECODE>
emanjavacas
apaszke
apaszke
emanjavacas
I think these are the only Py3 versions that we support.
apaszke
I was wondering… Matrix batched-vector multiply is just matrix-matrix multiply, but what about the inverse situation: Batched-matrix vector multiply?
emanjavacas
No, there’s not. But you could do it by reshaping your vector to look like a matrix (no memory copy there, just stride tricks): <SCODE>M_batch = ... # batch x dim1 x dim2
v = ... # dim2
v_view = v.unsqueeze(0).expand(-1, len(v)).unsqueeze(2) # batch x dim2 x 1
output = M_batch.bmm(v_view) # batch x dim1 x 1
# optionally output.squeeze(2) to get batch x dim1 output
<ECODE>
apaszke
Alright, thanks for the info. Given efficient reshaping capabilities, all this operations can easily be implemented by the user.
emanjavacas
I got it to work passing the batch size instead of -1. <SCODE>v.unsqueeze(0).expand(M_batch.size(0), len(v)).unsqueeze(2)
<ECODE>
emanjavacas
apaszke