Class: Chainer::VariableNode
- Inherits:
-
Object
- Object
- Chainer::VariableNode
- Defined in:
- lib/chainer/variable_node.rb
Instance Attribute Summary collapse
-
#creator_node ⇒ Object
Returns the value of attribute creator_node.
-
#data ⇒ Object
Returns the value of attribute data.
-
#dtype ⇒ Object
readonly
Returns the value of attribute dtype.
-
#name ⇒ Object
Returns the value of attribute name.
-
#old_style_grad_generator ⇒ Object
Returns the value of attribute old_style_grad_generator.
-
#rank ⇒ Object
Returns the value of attribute rank.
-
#requires_grad ⇒ Object
Returns the value of attribute requires_grad.
-
#shape ⇒ Object
readonly
Returns the value of attribute shape.
-
#variable ⇒ Object
Returns the value of attribute variable.
Instance Method Summary collapse
- #check_old_style_gradient ⇒ Object
- #creator ⇒ Object
- #creator=(func) ⇒ Object
-
#get_variable ⇒ Chainer::Variable
Returns the corresponding :class:‘Variable` object.
-
#grad ⇒ Object
Gradient array of the corresponding variable.
-
#grad_var ⇒ Object
Gradient variable of the corresponding variable.<Paste>.
-
#initialize(variable:, name:) ⇒ VariableNode
constructor
A new instance of VariableNode.
- #label ⇒ Object
- #retain_data ⇒ Object
- #set_creator(creator) ⇒ Object
-
#set_creator_node(creator_node) ⇒ Object
Sets a ‘FunctionNode` object that created this node.
- #set_data_type(data) ⇒ Object
- #set_grad_with_check(g, func, var) ⇒ Object
- #unchain ⇒ Object
Constructor Details
#initialize(variable:, name:) ⇒ VariableNode
Returns a new instance of VariableNode.
6 7 8 9 10 11 12 13 14 15 16 17 |
# 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 |
Instance Attribute Details
#creator_node ⇒ Object
Returns the value of attribute creator_node.
4 5 6 |
# File 'lib/chainer/variable_node.rb', line 4 def creator_node @creator_node end |
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/chainer/variable_node.rb', line 3 def data @data end |
#dtype ⇒ Object (readonly)
Returns the value of attribute dtype.
3 4 5 |
# File 'lib/chainer/variable_node.rb', line 3 def dtype @dtype end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/chainer/variable_node.rb', line 4 def name @name end |
#old_style_grad_generator ⇒ Object
Returns the value of attribute old_style_grad_generator.
4 5 6 |
# File 'lib/chainer/variable_node.rb', line 4 def old_style_grad_generator @old_style_grad_generator end |
#rank ⇒ Object
Returns the value of attribute rank.
4 5 6 |
# File 'lib/chainer/variable_node.rb', line 4 def rank @rank end |
#requires_grad ⇒ Object
Returns the value of attribute requires_grad.
4 5 6 |
# File 'lib/chainer/variable_node.rb', line 4 def requires_grad @requires_grad end |
#shape ⇒ Object (readonly)
Returns the value of attribute shape.
3 4 5 |
# File 'lib/chainer/variable_node.rb', line 3 def shape @shape end |
#variable ⇒ Object
Returns the value of attribute variable.
4 5 6 |
# File 'lib/chainer/variable_node.rb', line 4 def variable @variable end |
Instance Method Details
#check_old_style_gradient ⇒ Object
119 120 121 122 123 |
# 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 ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
31 32 33 |
# File 'lib/chainer/variable_node.rb', line 31 def creator=(func) self.creator_node = func end |
#get_variable ⇒ Chainer::Variable
Returns the corresponding :class:‘Variable` object.
71 72 73 74 75 76 77 78 79 |
# 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 ⇒ Object
Gradient array of the corresponding variable.
49 50 51 52 |
# File 'lib/chainer/variable_node.rb', line 49 def grad var = get_variable var.nil? ? nil : var.grad end |
#grad_var ⇒ Object
Gradient variable of the corresponding variable.<Paste>
55 56 57 58 |
# File 'lib/chainer/variable_node.rb', line 55 def grad_var var = get_variable var.nil? ? nil : var.grad_var end |
#label ⇒ Object
60 61 62 63 64 65 66 |
# 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 ⇒ Object
96 97 98 99 100 101 102 |
# 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) ⇒ Object
81 82 83 |
# File 'lib/chainer/variable_node.rb', line 81 def set_creator(creator) self.creator = creator end |
#set_creator_node(creator_node) ⇒ Object
Sets a ‘FunctionNode` object that created this node.
88 89 90 |
# File 'lib/chainer/variable_node.rb', line 88 def set_creator_node(creator_node) self.creator_node = creator_node end |
#set_data_type(data) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# 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) ⇒ Object
114 115 116 117 |
# 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 ⇒ Object
92 93 94 |
# File 'lib/chainer/variable_node.rb', line 92 def unchain self.creator_node = nil end |