class chainer.ChainList(*links)[source]

Composable link with list-like interface.

This is another example of compositional link. Unlike Chain, this class can be used like a list of child links. Each child link is indexed by a non-negative integer, and it maintains the current number of registered child links. The add_link() method inserts a new link at the end of the list. It is useful to write a chain with arbitrary number of child links, e.g. an arbitrarily deep multi-layer perceptron.

Note that this class does not implement all methods of list.

Parameters:links – Initial child links.



Returns the child at given index.

Parameters:index (int) – Index of the child in the list.
Returns:The index-th child link.
Return type:Link

Returns the number of children.


Registers a child link and adds it to the tail of the list.

Parameters:link (Link) – The link object to be registered.
add_param(name, shape=None, dtype=<class 'numpy.float32'>, initializer=None)[source]

Registers a parameter to the link.

Deprecated since version v2.0.0: Assign a Parameter object directly to an attribute within init_scope() instead. For example, the following code

link.add_param('W', shape=(5, 3))

can be replaced by the following assignment.

with link.init_scope():
    link.W = chainer.Parameter(None, (5, 3))

The latter is easier for IDEs to keep track of the attribute’s type.

  • name (str) – Name of the parameter. This name is also used as the attribute name.
  • shape (int or tuple of ints) – Shape of the parameter array. If it is omitted, the parameter variable is left uninitialized.
  • dtype – Data type of the parameter array.
  • initializer – If it is not None, the data is initialized with the given initializer. If it is an array, the data is directly initialized by it. If it is callable, it is used as a weight initializer. Note that in these cases, dtype argument is ignored.
add_persistent(name, value)[source]

Registers a persistent value to the link.

The registered value is saved and loaded on serialization and deserialization. The value is set to an attribute of the link.

  • name (str) – Name of the persistent value. This name is also used for the attribute name.
  • value – Value to be registered.

Registers a child link and adds it to the tail of the list.

This is equivalent to add_link(). This method has been added to emulate the list interface.

Parameters:link (Link) – The link object to be regsitered.

Clears all gradient arrays.

This method should be called before the backward computation at every iteration of the optimization.


Disables update rules of all parameters under the link hierarchy.

This method sets the enabled flag of the update rule of each parameter variable to False.


Enables update rules of all parameters under the link hierarchy.

This method sets the enabled flag of the update rule of each parameter variable to True.


Creates an initialization scope.

This method returns a context manager object that enables registration of parameters (and links for Chain) by an assignment. A Parameter object can be automatically registered by assigning it to an attribute under this context manager.


In most cases, the parameter registration is done in the initializer method. Using the init_scope method, we can simply assign a Parameter object to register it to the link.

class MyLink(chainer.Link):
    def __init__(self):
        with self.init_scope():
            self.W = chainer.Parameter(0, (10, 5))
            self.b = chainer.Parameter(0, (5,))

Registers an attribute of a given name as a persistent value.

This is a convenient method to register an existing attribute as a persistent value. If name has been already registered as a parameter, this method removes it from the list of parameter names and re-registers it as a persistent value.

Parameters:name (str) – Name of the attribute to be registered.

Initializes all gradient arrays by zero.

This method can be used for the same purpose of cleargrads, but less efficient. This method is left for backward compatibility.

Deprecated since version v1.15: Use cleargrads() instead.



True if at least one parameter has an update rule enabled.


True if the current code is inside of an initialization scope.

See init_scope() for the details of the initialization scope.


Array module for this link.

Depending on which of CPU/GPU this link is on, this property returns numpy or cupy.