2016-03-16 74 views
1

在Theano中,當我有形狀爲[A,B,C]的3D張量x和形狀爲[C,D]的2D張量y時,則theano.tensor.dot(x, y)返回3D張量形狀[A,B,D]。3D張量*火炬中的2D張量點

Torch中的等效操作是什麼? torch.dot似乎沒有這樣做,並且x * ytorch.mm抱怨他們想要一個二維張量爲兩個參數,並且torch.bmm想要二維參數的張量。

回答

2

由於@smhx建議,可能的解決方案是重複第二張量(有辦法做到這一點沒有內存分配),然後執行批矩陣矩陣產品:

function repeatNoCopy(tensor, k) 
    local tens_size = tensor:size():totable() 
    return torch.expand(tensor:view(1, unpack(tens_size)), k, unpack(tens_size)) 
end 

A = torch.rand(3, 2, 5) 
B = torch.rand(5, 4) 
B_rep = repeatNoCopy(B, 3) 

result = torch.bmm(A, B_rep) 

print(result) 
> [torch.DoubleTensor of size 3x2x4] 
1

您需要將y擴展爲[A,C,D]大小,然後使用torch.bmm。看看torch.expand或torch.repeatTensor的文檔。

+0

你能更具體?代碼如何看起來像?從文檔看來,repeatTensor似乎創建了一個副本?我不想那樣。 – Albert