forked from clementfarabet/lua---nnx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sqrt.lua
34 lines (27 loc) · 819 Bytes
/
Sqrt.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
local Sqrt, parent = torch.class('nn.Sqrt','nn.Module')
function Sqrt:__init(args)
parent.__init(self)
if args then
error(xlua.usage('nn.Sqrt',
'a simple component-wise mapping: sqrt()',
'sq = nn.Sqrt()\n'..
'sqrt = sq:forward(sometensor)',
{type='nil', help='no arg required'}))
end
end
function Sqrt:forward(input)
self.output:resizeAs(input):copy(input)
self.output:sqrt()
return self.output
end
function Sqrt:backward(input, gradOutput)
self.gradInput:resizeAs(input):copy(gradOutput)
self.gradInput:cdiv(self.output):mul(0.5)
return self.gradInput
end
function Sqrt:write(file)
parent.write(self,file)
end
function Sqrt:read(file)
parent.read(self,file)
end