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