-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpatialConvolutionFast.lua
86 lines (68 loc) · 2.72 KB
/
SpatialConvolutionFast.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
82
83
84
85
86
local SpatialConvolutionFast, parent = torch.class('nn.SpatialConvolutionFast', 'nn.Module')
function SpatialConvolutionFast:__init(nInputPlane, nOutputPlane, kW, kH, dW, dH)
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.weight = torch.Tensor(nOutputPlane, nInputPlane*kH*kW)
self.bias = torch.Tensor(nOutputPlane)
self.gradWeight = torch.Tensor(nOutputPlane, nInputPlane*kH*kW)
self.gradBias = torch.Tensor(nOutputPlane)
self.finput = torch.Tensor()
self.fgradInput = torch.Tensor()
self:reset()
end
function SpatialConvolutionFast:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1/math.sqrt(self.kW*self.kH*self.nInputPlane)
end
self.weight:apply(function()
return torch.uniform(-stdv, stdv)
end)
self.bias:apply(function()
return torch.uniform(-stdv, stdv)
end)
end
function SpatialConvolutionFast:updateOutput(input)
input = input:unfold(2, self.kH, self.dH)
input = input:unfold(3, self.kW, self.dW)
input = input:transpose(2,4)
input = input:transpose(3,5)
self.finput:resize(self.kW*self.kH*self.nInputPlane, input:size(4)*input:size(5)):copy(input)
self.output:resize(self.nOutputPlane, input:size(4), input:size(5))
local output = input.new(self.output:storage(), 1, self.nOutputPlane, -1, input:size(4)*input:size(5), -1)
for b=1,self.bias:size(1) do
self.output[b]:fill(self.bias[b])
end
output:addmm(1, self.weight, self.finput)
return self.output
end
function SpatialConvolutionFast:updateGradInput(input, gradOutput)
if self.gradInput then
gradOutput = input.new(gradOutput:storage(), 1, gradOutput:size(1), -1, gradOutput:size(2)*gradOutput:size(3), -1)
self.fgradInput:resizeAs(self.finput):zero()
self.fgradInput:addmm(1, self.weight:t(), gradOutput)
self.gradInput:resizeAs(input):zero()
local gradInput = self.gradInput:unfold(2, self.kH, self.dH)
gradInput = gradInput:unfold(3, self.kW, self.dW)
gradInput = gradInput:transpose(2,4)
gradInput = gradInput:transpose(3,5)
gradInput:add(self.fgradInput)
return self.gradInput
end
end
function SpatialConvolutionFast:accGradParameters(input, gradOutput, scale)
scale = scale or 1
gradOutput = input.new(gradOutput:storage(), 1, gradOutput:size(1), -1, gradOutput:size(2)*gradOutput:size(3), -1)
self.gradWeight:addmm(scale, gradOutput, self.finput:t())
for b=1,self.bias:size(1) do
self.gradBias[b] = self.gradBias[b] + scale*gradOutput[b]:sum()
end
end