Skip to main content

no_grad

Function no_grad 

Source
pub fn no_grad<F, R>(f: F) -> R
where F: FnOnce() -> R,
Expand description

Execute a closure without gradient tracking.

Useful for inference or when gradients are not needed.

§Example

use aprender::autograd::{Tensor, no_grad};

let x = Tensor::from_slice(&[1.0, 2.0]).requires_grad();

// No gradients computed inside this block
let y = no_grad(|| {
    x.mul(&x).sum()
});

assert!(y.grad().is_none());