-
Notifications
You must be signed in to change notification settings - Fork 17
/
AbstractSequencerCriterion.lua
54 lines (50 loc) · 1.95 KB
/
AbstractSequencerCriterion.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
------------------------------------------------------------------------
--[[ AbstractSequencerCriterion ]]--
-- Inherited by SequencerCriterion and RepeaterCriterion
-- WARNING : assumes that the decorated criterion is stateless, i.e.
-- the backward doesn't need to be preceded by a commensurate forward.
------------------------------------------------------------------------
local AbstractSequencerCriterion, parent = torch.class('nn.AbstractSequencerCriterion', 'nn.Criterion')
function AbstractSequencerCriterion:__init(criterion, sizeAverage)
parent.__init(self)
if torch.isTypeOf(criterion, 'nn.ModuleCriterion') then
error(torch.type(self).." shouldn't decorate a ModuleCriterion. "..
"Instead, try the other way around : "..
"ModuleCriterion decorates a ".. torch.type(self) .. ". "..
"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 = {criterion}
end
function AbstractSequencerCriterion:getStepCriterion(step)
assert(step, "expecting step at arg 1")
local criterion = self.clones[step]
if not criterion then
criterion = self.clones[1]:clone()
self.clones[step] = criterion
end
return criterion
end
function AbstractSequencerCriterion:setZeroMask(zeroMask)
if zeroMask == false then
for k,stepcriterion in pairs(self.clones) do
stepcriterion:setZeroMask(zeroMask)
end
else
assert(zeroMask:dim() >= 2, "Expecting dim >= 2 for zeroMask. For example, seqlen x batchsize")
for step=1,zeroMask:size(1) do
local stepcriterion = self:getStepCriterion(step)
stepcriterion:setZeroMask(zeroMask[step])
end
end
end
function AbstractSequencerCriterion:type(type, typecache)
for key, clone in pairs(self.clones) do
clone:type(type, typecache)
end
return parent.type(self, type, typecache)
end