forked from torch/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpatialConvolutionCUDA.lua
71 lines (60 loc) · 2.38 KB
/
SpatialConvolutionCUDA.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
local SpatialConvolutionCUDA, parent = torch.class('nn.SpatialConvolutionCUDA', 'nn.Module')
function SpatialConvolutionCUDA:__init(nInputPlane, nOutputPlane, kW, kH, dW, dH, padding, partialSum)
parent.__init(self)
dW = dW or 1
dH = dH or 1
partialSum = partialSum or 0
self.nInputPlane = nInputPlane
self.nOutputPlane = nOutputPlane
self.kW = kW
self.kH = kH
self.dW = dW
self.dH = dH
self.padding = padding or 0
self.partialSum = partialSum
self.weight = torch.Tensor(nInputPlane, kH, kW, nOutputPlane)
self.bias = torch.Tensor(nOutputPlane)
self.gradWeightPartial = torch.Tensor(nInputPlane, kH, kW, nOutputPlane)
self.gradWeight = torch.Tensor(nInputPlane, kH, kW, nOutputPlane)
self.gradBias = torch.Tensor(nOutputPlane)
self:reset()
end
function SpatialConvolutionCUDA:reset(stdv)
if stdv then
stdv = stdv * math.sqrt(3)
else
stdv = 1/math.sqrt(self.kW*self.kH*self.nInputPlane)
end
self.weight:uniform(-stdv, stdv)
self.bias:uniform(-stdv, stdv)
end
function SpatialConvolutionCUDA:updateOutput(input)
input.nn.SpatialConvolutionCUDA_updateOutput(self, input)
local biasrep = self.bias:new():resize(self.bias:size(1),1,1,1):expandAs(self.output)
self.biasrepc = self.biasrepc or biasrep.new()
self.biasrepc:resizeAs(self.output):copy(biasrep)
self.output:add(self.biasrepc)
return self.output
end
function SpatialConvolutionCUDA:updateGradInput(input, gradOutput)
input.nn.SpatialConvolutionCUDA_updateGradInput(self, input, gradOutput)
return self.gradInput
end
function SpatialConvolutionCUDA:accGradParameters(input, gradOutput, scale)
scale = scale or 1
input.nn.SpatialConvolutionCUDA_accGradParameters(self, input, gradOutput, scale)
if self.partialSum > 0 then
self.gradWeight:add(self.gradWeightPartial:sum(1):resizeAs(self.gradWeight))
end
local sums = gradOutput:new():resize(gradOutput:size(1), gradOutput:size(2)*gradOutput:size(3)*gradOutput:size(4)):sum(2)
self.gradBias:add(scale, sums)
end
-- this routine copies weight+bias from a regular SpatialConvolution module
function SpatialConvolutionCUDA:copy(sc)
local weight = sc.weight:clone()
weight:resize(sc.nOutputPlane, sc.nInputPlane * sc.kH * sc.kW)
weight = weight:t():contiguous()
weight:resize(sc.nInputPlane, sc.kH, sc.kW, sc.nOutputPlane)
self.weight:copy(weight)
self.bias:copy(sc.bias)
end