chainer.variable.VariableNode

class chainer.variable.VariableNode(variable, name, **kwargs)[source]

Node in the backward computational graph representing a variable.

This object represents a variable node in a computational graph. The node is used in error backpropagation (a.k.a. backprop) to determine which gradient to be passed to each function.

A variable node is held by the corresponding Variable object, which is managed by users. Function objects that take the variable as an input also hold references to the variable node.

Note that the node does not hold a reference to the corresponding data array in general. The data array is actually accessible by the node in the following cases.

  1. If there exists a Variable object that holds a reference to the variable node, the variable node holds a weak reference to the variable object, and thus the data array is accessible via the weak reference.
  2. If retain_data() is called, the node holds a reference to the data array. It is mainly called by a function that needs the input or output data array in its backprop procedure. See Function.retain_inputs() and Function.retain_outputs() for more details.

Users usually do not need to touch this variable node object. The computational graph is automatically managed by Chainer, and any interface that is beneficial for users is also provided by Variable.

Parameters:
  • variable (Variable) – The corresponding variable object.
  • name (str) – Name of the variable node.
Variables:
  • dtype – Data type of the data array.
  • shape – Shape of the data array.
  • name (str) – Name of the variable node.

Methods

get_variable()[source]

Returns the corresponding Variable object.

VariableNode object holds a weak reference of the variable object. If the reference is alive, it is returned by this property. Otherwise, this property creates a new Variable object from this node object and returns it.

Returns:The variable object that refers this node.
Return type:Variable
retain_data()[source]

Lets the node hold a reference to the underlying data array.

This method gets the data array of the corresponding variable and keeps it. If the weak reference to the corresponding variable is dead, it raises an error.

set_creator(creator)[source]

Sets a Function object that created this node.

This method is equivalent to self.creator = creator. A FunctionNode object can also be passed.

Parameters:creator (Function or FunctionNode) – Function that has created this variable.
set_creator_node(creator_node)[source]

Sets a FunctionNode object that created this node.

This method is equivalent to self.creator_node = creator_node. A Function object can also be passed, in which case the Function.node attribute is used.

Parameters:creator_node (FunctionNode or Function) – Function node that has this variable as an output.
unchain()[source]

Deletes the reference to the creator of this variable node.

This method is equivalent to self.creator_node = None.

Attributes

creator

Function object that created this variable node.

When the function is implemented with the old-style API (i.e., it uses Function class), this property returns the Function object. The object is extracted from the FunctionAdapter object, so the returned object is not the function node, but instead the actual implementation of forward and backward procedures.

When the function is implemented with the new-style API (i.e., it uses FunctionNode class), this property returns the function node object. In this case, the returned object is same as creator_node.

Warning

As of v3.0.0, when the creator is an old-style function, the following code is invalid:

creator = v.creator
v.creator = None
...
v.creator = creator

The point is that FunctionNode objects are used as nodes in the computational graph instead of Function, and each Function object only holds a weak reference to the corresponding FunctionNode. Since creator returns the Function object, the FunctionNode object is not kept by preserving creator.

The above code should be fixed as follows.

creator_node = v.creator_node
v.creator_node = None
...
v.creator_node = creator_node
creator_node

Function node that has this variable as an output.

See FunctionNode for the definition of a function node.

data

Data array of the corresponding variable.

If the data is not available, it returns None.

grad

Gradient array of the corresponding variable.

If the variable is not available, it returns None.

grad_var

Gradient variable of the corresponding variable.

If the corresponding variable is not available, it return None.

label

Short text that represents the variable node.

rank
requires_grad

It indicates that grad will be set in backward calculation.