-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathSequencerCriterion.lua
108 lines (96 loc) · 3.54 KB
/
SequencerCriterion.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
------------------------------------------------------------------------
--[[ SequencerCriterion ]]--
-- Applies a criterion to each of the inputs and targets in the
-- corresponding input and target Tables.
-- Useful for nn.Repeater and nn.Sequencer.
-- WARNING : assumes that the decorated criterion is stateless, i.e.
-- the backward doesn't need to be preceded by a commensurate forward.
------------------------------------------------------------------------
local SequencerCriterion, parent = torch.class('nn.SequencerCriterion', 'nn.Criterion')
function SequencerCriterion:__init(criterion, sizeAverage)
parent.__init(self)
self.criterion = criterion
if torch.isTypeOf(criterion, 'nn.ModuleCriterion') then
error("SequencerCriterion shouldn't decorate a ModuleCriterion. "..
"Instead, try the other way around : "..
"ModuleCriterion decorates a SequencerCriterion. "..
"Its modules can also be similarly decorated with a Sequencer.")
end
if sizeAverage ~= nil then
self.sizeAverage = sizeAverage
else
self.sizeAverage = false
end
self.clones = {}
self.gradInput = {}
end
function SequencerCriterion:getStepCriterion(step)
assert(step, "expecting step at arg 1")
local criterion = self.clones[step]
if not criterion then
criterion = self.criterion:clone()
self.clones[step] = criterion
end
return criterion
end
function SequencerCriterion:updateOutput(input, target)
self.output = 0
local nStep
if torch.isTensor(input) then
assert(torch.isTensor(target), "expecting target Tensor since input is a Tensor")
assert(target:size(1) == input:size(1), "target should have as many elements as input")
nStep = input:size(1)
else
assert(torch.type(target) == 'table', "expecting target table")
assert(#target == #input, "target should have as many elements as input")
nStep = #input
end
for i=1,nStep do
local criterion = self:getStepCriterion(i)
self.output = self.output + criterion:forward(input[i], target[i])
end
if self.sizeAverage then
self.output = self.output / nStep
end
return self.output
end
function SequencerCriterion:updateGradInput(input, target)
self.gradInput = {}
local nStep
if torch.isTensor(input) then
assert(torch.isTensor(target), "expecting target Tensor since input is a Tensor")
assert(target:size(1) == input:size(1), "target should have as many elements as input")
nStep = input:size(1)
else
assert(torch.type(target) == 'table', "expecting gradOutput table")
assert(#target == #input, "target should have as many elements as input")
nStep = #input
end
local tableGradInput = {}
for i=1,nStep do
local criterion = self:getStepCriterion(i)
tableGradInput[i] = criterion:backward(input[i], target[i])
if self.sizeAverage then
local function table_div(output, scalar)
if torch.type(output) == 'table' then
for j=1,#output do
table_div(output[j], scalar)
end
else
output:div(scalar)
end
end
table_div(tableGradInput[i], nStep)
end
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