forked from nicholas-leonard/dp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.lua
222 lines (190 loc) · 5.49 KB
/
node.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
------------------------------------------------------------------------
--[[ Node ]]--
-- Abstract Class
-- Inherited by Model and Loss
-- Forward and backward propagates representations.
------------------------------------------------------------------------
local Node = torch.class("dp.Node")
Node.isNode = true
function Node:__init(config)
config = config or {}
assert(torch.type(config) == 'table' and not config[1],
"Constructor requires key-value arguments")
local default_type = torch.getdefaulttensortype()
local args, input_type, output_type, module_type, verbose
= xlua.unpack(
{config},
'Node',
'Forward and backward propagates representations.',
{arg='input_type', type='string', default=default_type,
help='type of input activation and gradient tensors'},
{arg='output_type', type='string', default=default_type,
help='type of output activation and gradient tensors'},
{arg='module_type', type='string', default=default_type,
help='type of modules used in this Node'},
{arg='verbose', type='boolean', default=true,
help='print verbose messages'}
)
self:inputType(input_type)
self:outputType(output_type)
self:moduleType(module_type)
self:zeroStatistics()
self:doneBatch()
self._verbose = verbose
end
function Node:setup(config)
assert(type(config) == 'table', "Setup requires key-value arguments")
local args, mediator, id = xlua.unpack(
{config},
'Node:setup',
'post-initialization setup method',
{arg='mediator', type='dp.Mediator',
help='allows Nodes to signal other object of events.'},
{arg='id', type='dp.ObjectID',
help='Uniquely identifies node.'}
)
self._mediator = mediator
self._id = id
mediator:subscribe("doneEpoch", self, "doneEpoch")
self._setup = true
end
function Node:id()
return self._id
end
function Node:name()
return self._id:name()
end
-- returns a report of the Node.
-- if statistics were being gathered, this is the time to report them.
-- Expect report to be called at least every epoch.
function Node:report()
end
-- zero statistics between epochs
function Node:zeroStatistics()
self._stats = {nSample=0}
self:_zeroStatistics()
end
function Node:_zeroStatistics()
end
-- should only be called by forward or evaluate (once per batch)
function Node:updateStatistics(carry)
self._stats.nSample = self._stats.nSample + carry:getObj('nSample')
self:_updateStatistics(carry)
end
function Node:_updateStatistics(carry)
end
function Node:doneBatch(...)
self:_doneBatch(...)
self.forwarded = false
self.backwarded = false
self.evaluated = false
end
function Node:_doneBatch(...)
end
function Node:doneEpoch(report, ...)
--zeros statistics between epochs
self:zeroStatistics()
end
function Node:clone()
local f = torch.MemoryFile("rw"):binary()
f:writeObject(self)
f:seek(1)
local clone = f:readObject()
f:close()
return clone
end
function Node:share(mlp, ...)
error"Not Implemented"
end
-- creates a clone with shared parameters
function Node:sharedClone()
error"Not Implemented"
end
-- shares parameters and statistics (use to share nodes between coroutines)
function Node:coroutineClone()
error"Not Implemented"
end
function Node:inputView(input_view)
if input_view then
assert(torch.type(input_view) == 'string')
self._input_view = input_view
end
return self._input_view
end
function Node:outputView(output_view)
if output_view then
assert(torch.type(output_view) == 'string')
self._output_view = output_view
end
return self._output_view
end
function Node:outputType(output_type)
if output_type then
assert(torch.type(output_type) == 'string')
self._output_type = output_type
end
return self._output_type
end
function Node:inputType(input_type)
if input_type then
assert(torch.type(input_type) == 'string')
self._input_type = input_type
end
return self._input_type
end
function Node:moduleType(module_type)
if module_type then
assert(torch.type(module_type) == 'string')
self._module_type = module_type
end
return self._module_type
end
function Node:verbose(verbose)
self._verbose = (verbose == nil) and true or verbose
end
function Node:silent()
self:verbose(false)
end
-- changes the type of internal variables inplace (same as nn)
-- returns self
function Node:type(new_type)
if new_type then
self:_type(new_type)
self:moduleType(new_type)
collectgarbage()
end
return self
end
-- this should only change the input, output, module or criteria
-- types if the internal module or criteria permits it.
-- for example, NLL only changes the input type to float or double.
function Node:_type(new_type)
end
function Node:float()
return self:type('torch.FloatTensor')
end
function Node:double()
return self:type('torch.DoubleTensor')
end
function Node:cuda()
return self:type('torch.CudaTensor')
end
function Node:int()
return self:type('torch.IntTensor')
end
function Node:long()
return self:type('torch.LongTensor')
end
--default is to call forward (only diff is 'evaluate' flag in carry)
function Node:_evaluate(carry)
return self:_forward(carry)
end
--[[
-- experimental (would allow for one chained RPC call for both backward forward)
function Node:flux(carry)
local output, carry = self:forward()
local input, carry = self._successor:flux{output, carry}
local input, carry = self:backward{input, carry}
return input, carry
end
--]]