-
Notifications
You must be signed in to change notification settings - Fork 0
/
PReLU.lua
52 lines (47 loc) · 1.38 KB
/
PReLU.lua
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
local PReLU, parent = torch.class('nn.PReLU','nn.Module')
function PReLU:__init(nOutputPlane)
parent.__init(self)
-- if no argument provided, use shared model (weight is scalar)
self.nOutputPlane = nOutputPlane or 0
self.weight = torch.Tensor(nOutputPlane or 1):fill(0.25)
self.gradWeight = torch.Tensor(nOutputPlane or 1)
end
function PReLU:updateOutput(input)
input.THNN.PReLU_updateOutput(
input:cdata(),
self.output:cdata(),
self.weight:cdata(),
self.nOutputPlane
)
return self.output
end
function PReLU:updateGradInput(input, gradOutput)
input.THNN.PReLU_updateGradInput(
input:cdata(),
gradOutput:cdata(),
self.gradInput:cdata(),
self.weight:cdata(),
self.nOutputPlane
)
return self.gradInput
end
function PReLU:accGradParameters(input, gradOutput, scale)
self.gradWeightBuf = self.gradWeightBuf or input.new()
self.gradWeightBuf2 = self.gradWeightBuf2 or input.new()
input.THNN.PReLU_accGradParameters(
input:cdata(),
gradOutput:cdata(),
self.gradInput:cdata(),
self.weight:cdata(),
self.gradWeight:cdata(),
self.gradWeightBuf:cdata(),
self.gradWeightBuf2:cdata(),
self.nOutputPlane,
scale or 1
)
return self.gradWeight
end
function PReLU:clearState()
nn.utils.clear(self, 'gradWeightBuf', 'gradWeightBuf2')
return parent.clearState(self)
end