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

Call a function without storing internal results.

On a forward propagation Chainer stores all internal results of Function on a computational graph as they are required on backward-propagation. These results consume too much memory when the internal results are too large. This method forgets such internal results on forward propagation, and still supports back-propagation with recalculation.

In a forward propagation, this method calls a given function with given variables without creating a computational graph. That means, no internal results are stored. In a backward propagation this method calls the given function again to create a computational graph to execute back-propagation.

This method reduces internal memory usage. Instead it requires more calculation time as it calls the function twice.


Let f be a function defined as:

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

and, x and y be Variable:

>>> 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 internal result x + y is stored in memory. Instead if you call f with forget():

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

internal x + y is forgotten.


The method does not support functions behaving randomly, such as dropout() and negative_sampling(). It is because first results of these function differ from the second one.

  • 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.

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

Return type: