Class: Chainer::ChainList
- Inherits:
-
Link
- Object
- Link
- Chainer::ChainList
show all
- Defined in:
- lib/chainer/link.rb
Overview
Composable link with list-like interface.
This is another example of compositional link. Unlike :class:‘Chainer::Chain`, this class can be used like a list of child links. Each child link is indexed by a non-negative integer, and it maintains the current number of registered child links. The :meth:`add_link` method inserts a new link at the end of the list. It is useful to write a chain with arbitrary number of child links, e.g. an arbitrarily deep multi-layer perceptron.
Instance Attribute Summary collapse
Attributes inherited from Link
#name
Instance Method Summary
collapse
Methods inherited from Link
#cleargrads, #del_attr, #init_scope, #register_persistent, #within_init_scope
Constructor Details
#initialize(*links) ⇒ ChainList
Returns a new instance of ChainList.
186
187
188
189
190
191
192
193
|
# File 'lib/chainer/link.rb', line 186
def initialize(*links)
super()
@children = []
links.each do |link|
add_link(link)
end
end
|
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
184
185
186
|
# File 'lib/chainer/link.rb', line 184
def children
@children
end
|
Instance Method Details
#<<(link) ⇒ Object
214
215
216
|
# File 'lib/chainer/link.rb', line 214
def <<(link)
add_link(link)
end
|
#[](index) ⇒ Object
202
203
204
|
# File 'lib/chainer/link.rb', line 202
def [](index)
@children[index]
end
|
#add_link(link) ⇒ Object
218
219
220
221
|
# File 'lib/chainer/link.rb', line 218
def add_link(link)
link.name = @children.size.to_s
@children << link
end
|
#each(&block) ⇒ Object
206
207
208
|
# File 'lib/chainer/link.rb', line 206
def each(&block)
@children.each(&block)
end
|
#links(skipself: false) ⇒ Object
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/chainer/link.rb', line 247
def links(skipself: false)
unless skipself
yield self
end
@children.each do |child|
child.links do |link|
yield link
end
end
end
|
#namedlinks(skipself: false) ⇒ Object
259
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/chainer/link.rb', line 259
def namedlinks(skipself: false)
unless skipself
yield '/', self
end
@children.each_with_index do |child, idx|
prefix = "/#{idx}"
yield prefix, child
child.namedlinks(skipself: true) do |path, link|
yield [prefix + path, link]
end
end
end
|
#namedparams(include_uninit: true) ⇒ Object
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/chainer/link.rb', line 235
def namedparams(include_uninit: true)
super(include_uninit: include_uninit) do |ret|
yield ret
end
@children.each_with_index do |link, idx|
prefix = "/#{idx}"
link.namedparams(include_uninit: include_uninit) do |path, param|
yield [prefix + path, param]
end
end
end
|
#params(include_uninit: true) ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/chainer/link.rb', line 223
def params(include_uninit: true)
super(include_uninit: include_uninit) do |param|
yield param
end
@children.each do |link|
link.params(include_uninit: include_uninit) do |param|
yield param
end
end
end
|
#serialize(serializer) ⇒ Object
279
280
281
282
283
284
|
# File 'lib/chainer/link.rb', line 279
def serialize(serializer)
super
@children.each_with_index do |child, idx|
child.serialize(serializer[idx.to_s])
end
end
|
#set_attr(name, value) ⇒ Object
195
196
197
198
199
200
|
# File 'lib/chainer/link.rb', line 195
def set_attr(name, value)
if within_init_scope && value.kind_of?(Chainer::Link)
raise TypeError, 'cannot register a new link within a "with chainlist.init_scope:" block.'
end
super
end
|
#size ⇒ Object
210
211
212
|
# File 'lib/chainer/link.rb', line 210
def size
@children.size
end
|