-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNilModule.lua
43 lines (33 loc) · 1.16 KB
/
NilModule.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
require 'nn'
require 'myutils'
require 'strict'
--------------------------------------- NilModule---------------------------------------
-- Creates a module that returns zeros or nil as output and zeros as gradInput.
-- This is useful when combined with the module ParallelTable in case one wishes to
-- ignore a certain element in the table.
local NilModule, parent = torch.class('nn.NilModule', 'nn.Module')
function NilModule:__init(oSize)
parent.__init(self)
if oSize == nil then
self.output = nil
else
self.output = torch.Tensor(torch.LongStorage(oSize)):zero()
end
self.gradInput = nil
end
function NilModule:updateOutput(input)
assert(input ~= nil)
return self.output
end
function NilModule:updateGradInput(input, gradOutput)
assert(input ~= nil)
-- input was nothing so gradInput will be zeros in the same form and size as input
if (self.gradInput == nil) then
self.gradInput = funcOnTensors(input, function (x) return x*0 end)
end
return self.gradInput
end
function NilModule:type(type, tensorCache)
parent.type(self, type, tensorCache)
self.gradInput = nil
end