Class: Chainer::ChainList

Inherits:
Link
  • Object
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.


188
189
190
191
192
193
194
195
# File 'lib/chainer/link.rb', line 188

def initialize(*links)
  super()
  @children = []

  links.each do |link|
    add_link(link)
  end
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.


186
187
188
# File 'lib/chainer/link.rb', line 186

def children
  @children
end

Instance Method Details

#<<(link) ⇒ Object


216
217
218
# File 'lib/chainer/link.rb', line 216

def <<(link)
  add_link(link)
end

#[](index) ⇒ Object


204
205
206
# File 'lib/chainer/link.rb', line 204

def [](index)
  @children[index]
end

220
221
222
223
# File 'lib/chainer/link.rb', line 220

def add_link(link)
  link.name = @children.size.to_s
  @children << link
end

#each(&block) ⇒ Object


208
209
210
# File 'lib/chainer/link.rb', line 208

def each(&block)
  @children.each(&block)
end

249
250
251
252
253
254
255
256
257
258
259
# File 'lib/chainer/link.rb', line 249

def links(skipself: false)
  unless skipself
    yield self
  end

  @children.each do |child|
    child.links do |link|
      yield link
    end
  end
end

261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/chainer/link.rb', line 261

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


237
238
239
240
241
242
243
244
245
246
247
# File 'lib/chainer/link.rb', line 237

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


225
226
227
228
229
230
231
232
233
234
235
# File 'lib/chainer/link.rb', line 225

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


281
282
283
284
285
286
# File 'lib/chainer/link.rb', line 281

def serialize(serializer)
  super
  @children.each_with_index do |child, idx|
    child.serialize(serializer[idx.to_s])
  end
end

#set_attr(name, value) ⇒ Object


197
198
199
200
201
202
# File 'lib/chainer/link.rb', line 197

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

#sizeObject


212
213
214
# File 'lib/chainer/link.rb', line 212

def size
  @children.size
end