Class: Chainer::VariableNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variable:, name:, grad: nil) ⇒ 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:, grad: nil)
  @variable = WeakRef.new(variable)
  @creator = nil
  @data = nil
  @rank = 0
  @name = name
  @requires_grad = variable.requires_grad

  set_data_type(variable.data)

  @grad = grad
end

Instance Attribute Details

#creatorObject

Returns the value of attribute creator.



4
5
6
# File 'lib/chainer/variable_node.rb', line 4

def creator
  @creator
end

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/chainer/variable_node.rb', line 4

def data
  @data
end

#dtypeObject (readonly)

Returns the value of attribute dtype.



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

def dtype
  @dtype
end

#gradObject

Returns the value of attribute grad.



4
5
6
# File 'lib/chainer/variable_node.rb', line 4

def grad
  @grad
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/chainer/variable_node.rb', line 4

def name
  @name
end

#rankObject

Returns the value of attribute rank.



4
5
6
# File 'lib/chainer/variable_node.rb', line 4

def rank
  @rank
end

#requires_gradObject

Returns the value of attribute requires_grad.



4
5
6
# File 'lib/chainer/variable_node.rb', line 4

def requires_grad
  @requires_grad
end

#shapeObject (readonly)

Returns the value of attribute shape.



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

def shape
  @shape
end

#variableObject

Returns the value of attribute variable.



4
5
6
# File 'lib/chainer/variable_node.rb', line 4

def variable
  @variable
end

Instance Method Details

#labelObject



36
37
38
39
40
41
42
# File 'lib/chainer/variable_node.rb', line 36

def label
  if @shape.nil? || @shape.empty?
    @dtype.to_s
  else
    "(#{@shape.join(', ')}), #{@dtype.to_s}"
  end
end

#retain_dataObject



48
49
50
51
52
53
54
# File 'lib/chainer/variable_node.rb', line 48

def retain_data
  if @variable.nil?
    raise "cannot retain variable data: the variable has been already released"
  else
    @variable.data
  end
end

#set_data_type(data) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/chainer/variable_node.rb', line 56

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



66
67
68
69
# File 'lib/chainer/variable_node.rb', line 66

def set_grad_with_check(g, func, var)
  Utils::Variable.check_grad_type(func, var, g)
  @grad = g
end

#unchainObject



44
45
46
# File 'lib/chainer/variable_node.rb', line 44

def unchain
  @creator = nil
end