Class: Chainer::Chain

Inherits:
Link
  • Object
show all
Defined in:
lib/chainer/link.rb

Direct Known Subclasses

Links::Model::Classifier

Instance Attribute Summary

Attributes inherited from Link

#name

Instance Method Summary collapse

Methods inherited from Link

#cleargrads, #init_scope, #register_persistent, #within_init_scope

Constructor Details

#initializeChain

Returns a new instance of Chain.



107
108
109
110
# File 'lib/chainer/link.rb', line 107

def initialize
  super
  @children = []
end

Instance Method Details

#del_attr(name) ⇒ Object



123
124
125
126
# File 'lib/chainer/link.rb', line 123

def del_attr(name)
  @children.delete(name)
  super
end

Yields:

  • ('/', _self)

Yield Parameters:



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/chainer/link.rb', line 153

def namedlinks(skipself: false)
  yield('/' , self) unless skipself
  d = self.instance_variables.each_with_object({}) { |sym, h| h[sym] = self.instance_variable_get(sym) }
  @children.each do |name|
    child = d[name.to_sym]
    prefix = '/' + name.to_s
    yield(prefix, child)
    d[name].namedlinks(skipself: true) do |path, link|
      yield(prefix + path, link)
    end
  end
end

#namedparams(include_uninit: true) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/chainer/link.rb', line 140

def namedparams(include_uninit: true)
  super(include_uninit: include_uninit) do |param|
    yield param
  end

  @children.each do |name|
    prefix = "/#{name}"
    self.instance_variable_get(name).namedparams(include_uninit: include_uninit) do |(path, param)|
      yield [prefix + path, param]
    end
  end
end

#params(include_uninit: true) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chainer/link.rb', line 128

def params(include_uninit: true)
  super(include_uninit: include_uninit) do |param|
    yield param
  end

  @children.each do |name|
    self.instance_variable_get(name).params(include_uninit: include_uninit) do |param|
      yield param
    end
  end
end

#serialize(serializer) ⇒ Object



166
167
168
169
170
171
172
# File 'lib/chainer/link.rb', line 166

def serialize(serializer)
  super(serializer)
  d = self.instance_variables.each_with_object({}) { |sym, h| h[sym] = self.instance_variable_get(sym) }
  @children.each do |name|
    d[name].serialize(serializer[name.to_s])
  end
end

#set_attr(name, value) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/chainer/link.rb', line 112

def set_attr(name, value)
  if within_init_scope && value.kind_of?(Chainer::Link)
    if self.respond_to?(name)
      raise TypeError, "cannot register a new link #{name}: attribute exists"
    end
    value.name = name
    @children << name
  end
  super
end