-
Notifications
You must be signed in to change notification settings - Fork 313
/
RepeaterCriterion.lua
62 lines (52 loc) · 1.72 KB
/
RepeaterCriterion.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
------------------------------------------------------------------------
--[[ RepeaterCriterion ]]--
-- Applies a criterion to each of the inputs in a Table using the
-- same target (the target is repeated).
-- Useful for nn.Repeater and nn.Sequencer.
------------------------------------------------------------------------
assert(not nn.RepeaterCriterion, "update nnx package : luarocks install nnx")
local RepeaterCriterion, parent = torch.class('nn.RepeaterCriterion', 'nn.Criterion')
function RepeaterCriterion:__init(criterion)
parent.__init(self)
self.criterion = criterion
self.gradInput = {}
self.clones = {}
end
RepeaterCriterion.getStepCriterion = nn.SequencerCriterion.getStepCriterion
function RepeaterCriterion:forward(input, target)
self.output = 0
local nStep
if torch.isTensor(input) then
nStep = input:size(1)
else
nStep = #input
end
for i=1,nStep do
local criterion = self:getStepCriterion(i)
self.output = self.output + criterion:forward(input[i], target)
end
return self.output
end
function RepeaterCriterion:backward(input, target)
self.gradInput = {}
if torch.isTensor(input) then
nStep = input:size(1)
else
nStep = #input
end
local tableGradInput = {}
for i=1,nStep do
local criterion = self:getStepCriterion(i)
tableGradInput[i] = criterion:backward(input[i], target)
end
if torch.isTensor(input) then
self.gradInput = tableGradInput[1].new()
self.gradInput:resize(nStep, unpack(tableGradInput[1]:size():totable()))
for step=1,nStep do
self.gradInput[step]:copy(tableGradInput[step])
end
else
self.gradInput = tableGradInput
end
return self.gradInput
end