blob: 7f7b5249054a7db9db9b3a426011462ebe09555b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
local DotProduct, parent = torch.class('nn.DotProduct', 'nn.Module')
function DotProduct:__init()
parent.__init(self)
self.gradInput = {torch.Tensor(), torch.Tensor()}
self.output=torch.Tensor(1)
end
function DotProduct:updateOutput(input,y)
self.output[1] = input[1]:dot(input[2])
return self.output
end
function DotProduct:updateGradInput(input, gradOutput)
local v1 = input[1]
local v2 = input[2]
local gw1=self.gradInput[1];
local gw2=self.gradInput[2];
gw1:resizeAs(v1)
gw2:resizeAs(v2)
gw1:copy( v2)
gw1:mul(gradOutput[1])
gw2:copy( v1)
gw2:mul(gradOutput[1])
return self.gradInput
end
function DotProduct:type(type)
for i, tensor in ipairs(self.gradInput) do
self.gradInput[i] = tensor:type(type)
end
return parent.type(self, type)
end
|