Method: Chainer::UpdateRule#serialize

Defined in:
lib/chainer/optimizer.rb

#serialize(serializer) ⇒ Object

Serializes the update rule state. Be careful that this method only saves/loads the state of the update rule. The parameters of the target link is not saved/loaded by this method, and so you need to serialize the target link separately if you want to fully recover the training state including parameters.

Parameters:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chainer/optimizer.rb', line 107

def serialize(serializer)
  if @state.nil?
    if serializer.is_a?(Chainer::Deserializer)
      # try to initialize the state to retrieve state entries
      @state = {}
      self_copy = self.dup
      # TODO(sonots): pass device from outside
      xm = Chainer::Device.default.xm
      arr = xm::SFloat.new(1)
      self_copy.init_state(Chainer::Variable.new(arr, grad: arr))
      @state.keys.each do |key|
        @state[key] = serializer.(key.to_s, nil)
      end
    end
  else
    @state.each do |key, val|
      @state[key] = serializer.(key.to_s, val)
    end
  end                                                                                 
end