safe_grad: Safely compute a TensorFlow gradient

View source: R/utils.R

safe_gradR Documentation

Safely compute a TensorFlow gradient

Description

Wraps tape$gradient() and returns tf$zeros_like(x) on error or when the result is a Python None.

Usage

safe_grad(tape, y, x)

Arguments

tape

A tf$GradientTape object.

y

The tensor to differentiate.

x

The variable with respect to which to differentiate.

Value

A TensorFlow tensor (gradient or zeros).


soilFlux documentation built on Dec. 4, 2026, 5:10 p.m.