chainer.ChainList

class chainer.ChainList(*links)

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.

Methods

__getitem__(index)

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
__len__()

Returns the number of children.

__iter__()

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=<type 'numpy.float32'>, initializer=None)

Registers a parameter to the link.

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

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

can be replaced by the following assignment.

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

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

Parameters:
  • 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)

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.

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

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.
children()
cleargrads()

Clears all gradient arrays.

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

copy()
copyparams(link)
disable_update()

Disables update rules of all parameters under the link hierarchy.

This method sets the :attr:~chainer.UpdateRule.enabled` flag of the update rule of each parameter variable to False.

enable_update()

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.

init_scope(*args, **kwds)

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.

Example

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):
        super().__init__()
        with self.init_scope():
            self.W = chainer.Parameter(0, (10, 5))
            self.b = chainer.Parameter(0, (5,))
namedparams(include_uninit=True)
params(include_uninit=True)
register_persistent(name)

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.
serialize(serializer)
to_cpu()
to_gpu(device=None)
zerograds()

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.

Attributes

update_enabled

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

within_init_scope

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

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

xp

Array module for this link.

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