forked from torch/nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Replicate.lua
52 lines (49 loc) · 1.44 KB
/
Replicate.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
local Replicate, parent = torch.class('nn.Replicate','nn.Module')
function Replicate:__init(nf, dim)
parent.__init(self)
self.nfeatures = nf
self.dim = dim or 1
assert(self.dim > 0, "Can only replicate across positive integer dimensions.")
end
function Replicate:updateOutput(input)
self.dim = self.dim or 1 --backwards compatible
assert(
self.dim <= input:dim()+1,
"Not enough input dimensions to replicate along dimension " ..
tostring(self.dim) .. ".")
local sz = torch.LongStorage(input:dim()+1)
sz[self.dim] = self.nfeatures
for i = 1,input:dim() do
local offset = 0
if i >= self.dim then
offset = 1
end
sz[i+offset] = input:size(i)
end
local st = torch.LongStorage(input:dim()+1)
st[self.dim] = 0
for i = 1,input:dim() do
local offset = 0
if i >= self.dim then
offset = 1
end
st[i+offset] = input:stride(i)
end
self.output = input.new(input:storage(),input:storageOffset(),sz,st)
return self.output
end
function Replicate:updateGradInput(input, gradOutput)
self.gradInput:resizeAs(input):zero()
local sz = torch.LongStorage(input:dim()+1)
sz[self.dim] = 1
for i = 1,input:dim() do
local offset = 0
if i >= self.dim then
offset = 1
end
sz[i+offset] = input:size(i)
end
local gradInput = self.gradInput:view(sz)
gradInput:sum(gradOutput, self.dim)
return self.gradInput
end