pub struct Gradients { /* private fields */ }Expand description
Move-only accumulated gradient bundle backed by one allocation group.
§Examples
use tenferro_ad::{EagerRuntime, EagerTensor, Tensor};
use tenferro_cpu::CpuBackend;
let ctx = EagerRuntime::with_cpu_backend(CpuBackend::new())?;
let x = EagerTensor::requires_grad_in(
Tensor::from_vec_col_major(vec![2], vec![2.0_f64, 3.0])?,
ctx,
)?;
let loss = x.mul(&x)?.reduce_sum(Some(&[0]))?;
let gradients = loss.backward()?;
assert!(!gradients.is_empty());Implementations§
Source§impl Gradients
impl Gradients
Sourcepub fn grad(&self, key: &ValueKey<StdTensorOp>) -> Option<TensorView<'_>>
pub fn grad(&self, key: &ValueKey<StdTensorOp>) -> Option<TensorView<'_>>
Borrow one gradient view by its local value key.
Sourcepub fn take_grad(
&mut self,
key: &ValueKey<StdTensorOp>,
) -> Result<Option<Tensor>>
pub fn take_grad( &mut self, key: &ValueKey<StdTensorOp>, ) -> Result<Option<Tensor>>
Consume one gradient owner while leaving the bundle unchanged on failure.
§Errors
Returns tenferro_tensor::Error::RuntimeState when the descriptor is
invalid or its allocation is aliased. A missing key is reported as
Ok(None).
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Gradients
impl !RefUnwindSafe for Gradients
impl !UnwindSafe for Gradients
impl Send for Gradients
impl Sync for Gradients
impl Unpin for Gradients
impl UnsafeUnpin for Gradients
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more