class Chainer::ChainList
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.
Attributes
children[R]
Public Class Methods
new(*links)
click to toggle source
Calls superclass method
Chainer::Link::new
# File lib/chainer/link.rb, line 188 def initialize(*links) super() @children = [] links.each do |link| add_link(link) end end
Public Instance Methods
<<(link)
click to toggle source
# File lib/chainer/link.rb, line 216 def <<(link) add_link(link) end
[](index)
click to toggle source
# File lib/chainer/link.rb, line 204 def [](index) @children[index] end
add_link(link)
click to toggle source
# File lib/chainer/link.rb, line 220 def add_link(link) link.name = @children.size.to_s @children << link end
each(&block)
click to toggle source
# File lib/chainer/link.rb, line 208 def each(&block) @children.each(&block) end
links(skipself: false) { |self| ... }
click to toggle source
# 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
namedlinks(skipself: false) { |'/', self| ... }
click to toggle source
# 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) { |ret| ... }
click to toggle source
Calls superclass method
Chainer::Link#namedparams
# 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) { |param| ... }
click to toggle source
Calls superclass method
Chainer::Link#params
# 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)
click to toggle source
Calls superclass method
Chainer::Link#serialize
# 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)
click to toggle source
Calls superclass method
Chainer::Link#set_attr
# 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
size()
click to toggle source
# File lib/chainer/link.rb, line 212 def size @children.size end