class Chainer::Link

Attributes

name[RW]

Public Class Methods

new() click to toggle source
# File lib/chainer/link.rb, line 5
def initialize
  @params = []
  @persistent = []
  @within_init_scope = false
  @name = nil
end

Public Instance Methods

cleargrads() click to toggle source
# File lib/chainer/link.rb, line 44
def cleargrads
  params do |param|
    param.cleargrad
  end
end
del_attr(name) click to toggle source
# File lib/chainer/link.rb, line 38
def del_attr(name)
  @params.delete(name)
  @persistent.delete(name)
  self.remove_instance_variable(name)
end
init_scope() { || ... } click to toggle source
# File lib/chainer/link.rb, line 16
def init_scope
  old_flag = self.within_init_scope
  @within_init_scope = true

  begin
    yield
    self.instance_variables.each do |name|
      set_attr(name, self.instance_variable_get(name))
    end
  ensure
    @within_init_scope = old_flag
  end
end
namedparams(include_uninit: true) { |'/' + name, instance_variable_get| ... } click to toggle source
# File lib/chainer/link.rb, line 70
def namedparams(include_uninit: true)
  @params.each do |name|
    if include_uninit || self.instance_variable_get(name).data
      yield ['/' + name.to_s, self.instance_variable_get(name)]
    end
  end
end
params(include_uninit: true) { |instance_variable_get| ... } click to toggle source
# File lib/chainer/link.rb, line 61
def params(include_uninit: true)
  @params.map do |name|
    data = self.instance_variable_get(name).data
    if include_uninit || data
      yield self.instance_variable_get(name)
    end
  end
end
register_persistent(name) click to toggle source

Registers an attribute of a given name as a persistent value. This is a convenient method to register an existing attribute as a persistent value. If `name` has been already registered as a parameter, this method removes it from the list of parameter names and re-registers it as a persistent value.

@param [string] name Name of the attribute to be registered.

# File lib/chainer/link.rb, line 56
def register_persistent(name)
  @persistent << name
  @params.delete(name)
end
serialize(serializer) click to toggle source
# File lib/chainer/link.rb, line 82
def serialize(serializer)
  # TODO(sonots): pass device from outside
  xm = Chainer::Device.default.xm
  d = self.instance_variables.each_with_object({}) { |sym, h| h[sym] = self.instance_variable_get(sym) }
  @params.each do |name|
    param = d[name]
    data = serializer.(name.to_s, param.data)
    if param.data.nil? && !data.nil?
      # Initialize the parameter here
      param.init(data.shape)
      if Chainer.array?(param.data)
        param.data.store(data)
      else
        param.data.set(xm::NArray.cast(data))
      end
    end
  end

  @persistent.each do |name|
    d[name] = serializer.(name.to_s, d[name])
  end
end
set_attr(name, value) click to toggle source
# File lib/chainer/link.rb, line 30
def set_attr(name, value)
  if within_init_scope && value.kind_of?(Chainer::Parameter)
    value.name = name
    @params << name
    @persistent.delete(name)
  end
end
within_init_scope() click to toggle source
# File lib/chainer/link.rb, line 12
def within_init_scope
  @within_init_scope || false
end