Class: Chainer::Link

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLink

Returns a new instance of Link.



5
6
7
8
9
10
# File 'lib/chainer/link.rb', line 5

def initialize
  @params = []
  @persistent = []
  @within_init_scope = false
  @name = nil
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/chainer/link.rb', line 3

def name
  @name
end

Instance Method Details

#cleargradsObject



44
45
46
47
48
# File 'lib/chainer/link.rb', line 44

def cleargrads
  params do |param|
    param.cleargrad
  end
end

#del_attr(name) ⇒ Object



38
39
40
41
42
# File 'lib/chainer/link.rb', line 38

def del_attr(name)
  @params.delete(name)
  @persistent.delete(name)
  self.remove_instance_variable(name)
end

#init_scopeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
# 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

Yields:

  • ('/', _self)

Yield Parameters:

  • _self (Chainer::Link)

    the object that the method was called on



78
79
80
# File 'lib/chainer/link.rb', line 78

def namedlinks(skipself: false)
  yield('/', self) unless skipself
end

#namedparams(include_uninit: true) ⇒ Object



70
71
72
73
74
75
76
# 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) ⇒ Object



61
62
63
64
65
66
67
68
# 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) ⇒ Object

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.

Parameters:

  • name (string)

    Name of the attribute to be registered.



56
57
58
59
# File 'lib/chainer/link.rb', line 56

def register_persistent(name)
  @persistent << name
  @params.delete(name)
end

#serialize(serializer) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object



30
31
32
33
34
35
36
# 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_scopeObject



12
13
14
# File 'lib/chainer/link.rb', line 12

def within_init_scope
  @within_init_scope || false
end