class Chainer::VariableNode

Attributes

creator_node[RW]
data[R]
dtype[R]
name[RW]
old_style_grad_generator[RW]
rank[RW]
requires_grad[RW]
shape[R]
variable[RW]

Public Class Methods

new(variable: , name:) click to toggle source
# File lib/chainer/variable_node.rb, line 6
def initialize(variable: , name:)
  @variable = WeakRef.new(variable)
  @creator_node = nil
  @data = nil
  @rank = 0
  @name = name
  @requires_grad = variable.requires_grad

  @old_style_grad_generator = nil

  set_data_type(variable.data)
end

Public Instance Methods

check_old_style_gradient() click to toggle source
# File lib/chainer/variable_node.rb, line 119
def check_old_style_gradient
  if @old_style_grad_generator
    raise RuntimeError, "cannot twice-differentiate an old style Function #{@old_style_grad_generator}"
  end
end
creator() click to toggle source
# File lib/chainer/variable_node.rb, line 19
def creator
  node = @creator_node
  if node.nil?
    return nil
  end

  if node.is_a?(Chainer::FunctionAdapter)
    return node.function
  end
  node
end
creator=(func) click to toggle source
# File lib/chainer/variable_node.rb, line 31
def creator=(func)
  self.creator_node = func
end
creator_node=(func) click to toggle source
# File lib/chainer/variable_node.rb, line 35
def creator_node=(func)
  func = func.node if func.is_a?(Chainer::Function)
  @creator_node = func
  unless func.nil?
    @rank = func.rank + 1
  end
end
data=(data) click to toggle source
# File lib/chainer/variable_node.rb, line 43
def data=(data)
  @data = data
  set_data_type(data)
end
get_variable() click to toggle source

Returns the corresponding :class:`Variable` object.

@return [Chainer::Variable] The variable object that refers this node.

# File lib/chainer/variable_node.rb, line 71
def get_variable
  var = @variable
  # workaround: check weakref_alive?, because weakref sometimes delegates references by GC
  return var.__getobj__ if !var.nil? && var.weakref_alive?

  var = Chainer::Variable.new(@data, name: @name, requires_grad: @requires_grad)
  var.node = self
  var
end
grad() click to toggle source

Gradient array of the corresponding variable.

# File lib/chainer/variable_node.rb, line 49
def grad
  var = get_variable
  var.nil? ? nil : var.grad
end
grad_var() click to toggle source

Gradient variable of the corresponding variable.<Paste>

# File lib/chainer/variable_node.rb, line 55
def grad_var
                    var  = get_variable
  var.nil? ? nil : var.grad_var
end
label() click to toggle source
# File lib/chainer/variable_node.rb, line 60
def label
  if @shape.nil? || @shape.empty?
    @dtype.to_s
  else
    "(#{@shape.join(', ')}), #{@dtype.to_s}"
  end
end
retain_data() click to toggle source
# File lib/chainer/variable_node.rb, line 96
def retain_data
  if @variable.nil?
    raise "cannot retain variable data: the variable has been already released"
  else
    @variable.data
  end
end
set_creator(creator) click to toggle source
# File lib/chainer/variable_node.rb, line 81
def set_creator(creator)
  self.creator = creator
end
set_creator_node(creator_node) click to toggle source

Sets a `FunctionNode` object that created this node.

@param [Chainer::FunctionNode] creator_node Function node that has this variable as an output.

# File lib/chainer/variable_node.rb, line 88
def set_creator_node(creator_node)
  self.creator_node = creator_node
end
set_data_type(data) click to toggle source
# File lib/chainer/variable_node.rb, line 104
def set_data_type(data)
  if data.nil?
    @dtype = nil
    @shape = nil
  else
    @dtype = data.class
    @shape = data.shape
  end
end
set_grad_with_check(g, func, var) click to toggle source
# File lib/chainer/variable_node.rb, line 114
def set_grad_with_check(g, func, var)
  Utils::Variable.check_grad_type(func, var, g)
  @grad = g
end
unchain() click to toggle source
# File lib/chainer/variable_node.rb, line 92
def unchain
  self.creator_node = nil
end