-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMul.lua
More file actions
68 lines (60 loc) · 2 KB
/
CMul.lua
File metadata and controls
68 lines (60 loc) · 2 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
local CMul, parent = torch.class('nn.CMul', 'nn.Module')
function CMul:__init(inputSize)
parent.__init(self)
self.weight = torch.Tensor(inputSize)
self.gradWeight = torch.Tensor(inputSize)
-- state
-- self.gradInput:resize(inputSize)
self.output:resize(inputSize)
self:reset()
end
function CMul:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1./math.sqrt(self.weight:size(1))
end
self.weight:uniform(-stdv,stdv)
end
function CMul:updateOutput(input)
self.output:resizeAs(input):copy(input)
if input:nElement() == self.weight:nElement() then
self.output:view(-1):cmul(self.weight:view(-1));
else
if input:isSameSizeAs(self.weight) then
self.output:cmul(self.weight)
else
local batchSize = input:size(1)
self.output:view(batchSize, -1):cmul(self.weight:view(1,-1):expandAs(input:view(batchSize, -1)))
end
end
return self.output
end
function CMul:updateGradInput(input, gradOutput)
if self.gradInput then
local nElement = self.gradInput:nElement()
self.gradInput:resizeAs(input)
self.gradInput:zero()
if self.weight:nElement() == gradOutput:nElement() then
self.gradInput:addcmul(1, self.weight, gradOutput)
else
local gradOutput = gradOutput:view(input:size(1), -1)
local gradInput = self.gradInput:view(input:size(1), -1)
gradInput:addcmul(1, self.weight:view(1,-1):expandAs(gradOutput), gradOutput)
end
return self.gradInput
end
end
function CMul:accGradParameters(input, gradOutput, scale)
if self.weight:nElement() == gradOutput:nElement() then
self.gradWeight:addcmul(scale or 1, input, gradOutput)
else
local batchSize = input:size(1)
local input = input:view(batchSize, -1)
local gradOutput = gradOutput:view(batchSize, -1)
local gradWeight = self.gradWeight:view(1, -1)
for i=1,batchSize do
gradWeight:addcmul(scale or 1, input[i], gradOutput[i])
end
end
end