pub struct GradientValue { /* private fields */ }Expand description
Read-only retained gradient value.
§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()?;
let gradient = x.grad()?.expect("tracked leaf has a gradient");
assert_eq!(gradient.shape(), &[2]);Implementations§
Source§impl GradientValue
impl GradientValue
Sourcepub fn value(&self) -> Result<ValueGuard<'_>>
pub fn value(&self) -> Result<ValueGuard<'_>>
Borrow the gradient’s value guard.
§Errors
Returns Error::RuntimeState when the retained gradient record is
unavailable or its allocation-group descriptor is invalid.
Sourcepub fn tensor_read(&self) -> Result<TensorRead<'_>>
pub fn tensor_read(&self) -> Result<TensorRead<'_>>
Borrow the gradient as a dtype-erased read target.
§Errors
Returns Error::RuntimeState when the retained gradient record or
its allocation-group descriptor is unavailable.
Sourcepub fn as_slice<T: TensorScalar>(&self) -> Result<&[T]>
pub fn as_slice<T: TensorScalar>(&self) -> Result<&[T]>
Borrow a compact host slice without downloading backend storage.
§Errors
Returns Error::RuntimeState when the retained value is unavailable,
tenferro_tensor::ValidationError::DTypeMismatch when T does
not match the gradient dtype, or tenferro_tensor::Error::HostAccess
when backend storage cannot be mapped as a host slice.
Sourcepub fn to_tensor(&self) -> Result<Tensor>
pub fn to_tensor(&self) -> Result<Tensor>
Explicitly copy a host-resident gradient into a standalone tensor.
§Errors
Returns Error::RuntimeState when the retained value or execution
session is unavailable, or a typed backend/host-access error when the
gradient cannot be materialized as a contiguous tensor.
Trait Implementations§
Source§impl Clone for GradientValue
impl Clone for GradientValue
Source§fn clone(&self) -> GradientValue
fn clone(&self) -> GradientValue
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for GradientValue
impl !UnwindSafe for GradientValue
impl Freeze for GradientValue
impl Send for GradientValue
impl Sync for GradientValue
impl Unpin for GradientValue
impl UnsafeUnpin for GradientValue
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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>
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