Class: Chainer::FunctionAdapter

Inherits:
FunctionNode show all
Defined in:
lib/chainer/function.rb

Instance Attribute Summary collapse

Attributes inherited from FunctionNode

#inputs, #outputs, #rank

Instance Method Summary collapse

Methods inherited from FunctionNode

#apply, #backward_accumulate, #forward_cpu, #get_retained_inputs, #get_retained_outputs, #output_data, #retain_inputs, #retain_outputs, #unchain

Constructor Details

#initialize(function) ⇒ FunctionAdapter

Returns a new instance of FunctionAdapter.



101
102
103
104
105
# File 'lib/chainer/function.rb', line 101

def initialize(function)
  super()
  @weak_function = WeakRef.new(function)
  function.owned_node = self
end

Instance Attribute Details

#functionObject

Returns the value of attribute function.



99
100
101
# File 'lib/chainer/function.rb', line 99

def function
  @function
end

#weak_functionObject

Returns the value of attribute weak_function.



99
100
101
# File 'lib/chainer/function.rb', line 99

def weak_function
  @weak_function
end

Instance Method Details

#backward(target_input_indexes, grad_outputs) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/chainer/function.rb', line 124

def backward(target_input_indexes, grad_outputs)
  in_data = @inputs.map { |input| input.data }
  grad_out_data = grad_outputs.map { |grad| grad.nil? ? nil : grad.data }

  gxs = @function.backward(in_data, grad_out_data)
  ret = []
  target_input_indexes.each do |i|
    if gxs[i].nil?
      g = nil
    else
      g = Chainer::Variable.new(gxs[i])
      g.node.old_style_grad_generator = @function.label
    end
    ret << g
  end

  ret
end

#forward(inputs) ⇒ Object



119
120
121
122
# File 'lib/chainer/function.rb', line 119

def forward(inputs)
  retain_inputs(inputs.size.times.to_a)
  @function.forward(inputs)
end

#labelObject



115
116
117
# File 'lib/chainer/function.rb', line 115

def label
  @function.label
end