chainer.functions.forget

chainer.functions.forget(func, *xs)[source]

Calls a function without storing intermediate results.

On a forward propagation, Chainer normally stores all intermediate results of VariableNodes on a computational graph as they are required on backward propagation. Sometimes these results consume too much memory. F.forget forgets such intermediate results on forward propagation, and still supports backpropagation with recalculation.

On a forward propagation, F.forget calls a given function with given variables without creating a computational graph. That means, no intermediate results are stored. On a backward propagation, F.forget calls the given function again to create a computational graph for backpropagation.

F.forget reduces internal memory usage, whereas it requires more calculation time as it calls the function twice.

Example

Let f be a function defined as:

>>> def f(a, b):
...   return a + b + a

and, x and y be Variables:

>>> x = chainer.Variable(np.random.uniform(-1, 1, 5).astype('f'))
>>> y = chainer.Variable(np.random.uniform(-1, 1, 5).astype('f'))

When z is calculated as z = f(x, y), its intermediate result x + y is stored in memory. Instead, if you call f with F.forget:

>>> z = F.forget(f, x, y)

intermediate x + y is forgotten.

Note

F.forget does not support functions which behave differently in multiple calls with the same inputs, such as F.dropout() and F.negative_sampling().

Note

In case input argument variables are of class numpy.ndarray or cupy.ndarray objects, arguments will automatically be converted to Variables. This conversion takes place to ensure that this function is included in the computational graph to enable backward computations.

Parameters:
  • func (callable) – A function to call. It needs to be called with Variable object(s) and to return a Variable object or a tuple of Variable objects.
  • xs (Variable) – Argument variables of the function.
Returns:

A variable func returns. If it returns a tuple, the method returns a tuple too.

Return type:

Variable