Skip to contents

Resets the gradient of a value object (and all its ancestors in the computational graph) to zero. This is useful when reusing parameters across multiple optimization steps.

Usage

zero_grad(e)

# S3 method for class 'value'
zero_grad(e)

# S3 method for class 'list'
zero_grad(e)

# Default S3 method
zero_grad(e)

Arguments

e

A value object or list of value objects

Value

Invisibly returns the input (for chaining)

Details

After calling backward(), gradients accumulate in the computational graph. Before computing new gradients, you should reset them with zero_grad().

See also

Examples

x <- val(3)
y <- x^2
backward(y)
grad(x)      # 6
#> [1] 6
zero_grad(y)
grad(x)      # 0
#> [1] 0