forked from torch/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Threshold.lua
38 lines (34 loc) · 1.13 KB
/
Threshold.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
local Threshold, parent = torch.class('nn.Threshold','nn.Module')
function Threshold:__init(th,v,ip)
parent.__init(self)
self.threshold = th or 1e-6
self.val = v or 0
if (th and type(th) ~= 'number') or (v and type(v) ~= 'number') then
error('nn.Threshold(threshold, value)')
end
-- default for inplace is false
self.inplace = ip or false
if (ip and type(ip) ~= 'boolean') then
error('in-place flag must be boolean')
end
self:validateParameters()
end
function Threshold:updateOutput(input)
self:validateParameters()
input.nn.Threshold_updateOutput(self, input)
return self.output
end
function Threshold:updateGradInput(input, gradOutput)
self:validateParameters()
input.nn.Threshold_updateGradInput(self, input, gradOutput)
return self.gradInput
end
function Threshold:validateParameters()
self.inplace = self.inplace or false -- backwards compatibility pre inplace
if self.inplace then
if self.val > self.threshold then
error('in-place processing requires value (' .. self.val ..
') not exceed threshold (' .. self.threshold .. ')')
end
end
end