aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/lua-torch/nn/HardTanh.lua
blob: 07cfc62553aa66ebd4d17b3719c573cf62118937 (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
37
local HardTanh, parent = torch.class('nn.HardTanh', 'nn.Module')

function HardTanh:__init(min_value, max_value, inplace)
   parent.__init(self)
   self.min_val = min_value or -1
   self.max_val = max_value or 1
   self.inplace = inplace or false
   if (inplace and type(inplace) ~= 'boolean') then
      error('in-place flag must be boolean')
   end
   assert(self.max_val>self.min_val, 'max_value must be larger than min_value')
end

function HardTanh:updateOutput(input)
   self.min_val = self.min_val or -1
   self.max_val = self.max_val or 1
   input.THNN.HardTanh_updateOutput(
      input:cdata(),
      self.output:cdata(),
      self.min_val,
      self.max_val,
      self.inplace or false
   )
   return self.output
end

function HardTanh:updateGradInput(input, gradOutput)
   input.THNN.HardTanh_updateGradInput(
      input:cdata(),
      gradOutput:cdata(),
      self.gradInput:cdata(),
      self.min_val,
      self.max_val,
      self.inplace or false
   )
   return self.gradInput
end