forked from clementfarabet/nnop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpatialConvolutionMM.lua
81 lines (72 loc) · 2.51 KB
/
SpatialConvolutionMM.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
local SpatialConvolutionMM, parent = torch.class('nnop.SpatialConvolutionMM', 'nnop.Module')
function SpatialConvolutionMM:__init(nInputPlane, nOutputPlane, kW, kH, dW, dH, padW, padH)
parent.__init(self)
dW = dW or 1
dH = dH or 1
self.nInputPlane = nInputPlane
self.nOutputPlane = nOutputPlane
self.kW = kW
self.kH = kH
self.dW = dW
self.dH = dH
self.padW = padW or 0
self.padH = padH or self.padW
self.gradInput = {
torch.Tensor(),
torch.Tensor(),
torch.Tensor(),
}
self.finput = torch.Tensor()
self.fgradInput = torch.Tensor()
if nInputPlane and nOutputPlane and kW and kH then
self.parameterNodes = {
weight = nnop.Parameters(nOutputPlane, nInputPlane*kH*kW),
bias = nnop.Parameters(nOutputPlane),
}
end
end
function SpatialConvolutionMM:updateOutput(inputs)
local input = inputs[1]
local weight = inputs[2]
local bias = inputs[3]
nn.SpatialConvolutionMM.updateOutput({
weight=weight, bias=bias,
nInputPlane = self.nInputPlane, nOutputPlane = self.nOutputPlane,
kW = self.kW, kH = self.kH,
dW = self.dW, dH = self.dH,
padW = self.padW, padH = self.padH,
finput = self.finput, fgradInput = self.fgradInput,
output = self.output
}, input)
return self.output
end
function SpatialConvolutionMM:updateGradInput(inputs, gradOutput)
local input = inputs[1]
local weight = inputs[2]
local bias = inputs[3]
assert(weight:dim() == 2, 'weight must have 2 dims')
assert(weight:size(1) == bias:size(1), 'weight:size(1) == bias:size(1)')
nn.SpatialConvolutionMM.updateGradInput({
weight=weight, bias=bias,
nInputPlane = self.nInputPlane, nOutputPlane = self.nOutputPlane,
kW = self.kW, kH = self.kH,
dW = self.dW, dH = self.dH,
padW = self.padW, padH = self.padH,
finput = self.finput, fgradInput = self.fgradInput,
output = self.output,
gradInput = self.gradInput[1]
}, input, gradOutput)
self.gradInput[2]:resizeAs(weight):zero()
self.gradInput[3]:resizeAs(bias):zero()
nn.SpatialConvolutionMM.accGradParameters({
weight=weight, bias=bias,
nInputPlane = self.nInputPlane, nOutputPlane = self.nOutputPlane,
kW = self.kW, kH = self.kH,
dW = self.dW, dH = self.dH,
padW = self.padW, padH = self.padH,
finput = self.finput, fgradInput = self.fgradInput,
gradWeight=self.gradInput[2], gradBias=self.gradInput[3],
output=self.output,
}, input, gradOutput)
return self.gradInput
end