pub struct GradStore<D: Device>(/* private fields */);Expand description
Maps tensor ids to their accumulated gradients during a backward pass.
Gradients are always Float-kind tensors on the same device.
Implementations§
Source§impl<D: Device> GradStore<D>
impl<D: Device> GradStore<D>
pub fn new() -> Self
pub fn get(&self, tensor: &Tensor<D, Float>) -> Option<&Tensor<D, Float>>
pub fn get_by_id(&self, id: TensorId) -> Option<&Tensor<D, Float>>
pub fn remove(&mut self, tensor: &Tensor<D, Float>) -> Option<Tensor<D, Float>>
pub fn insert( &mut self, tensor: &Tensor<D, Float>, grad: Tensor<D, Float>, ) -> Option<Tensor<D, Float>>
Sourcepub fn or_insert(
&mut self,
tensor: &Tensor<D, Float>,
) -> Result<&mut Tensor<D, Float>>
pub fn or_insert( &mut self, tensor: &Tensor<D, Float>, ) -> Result<&mut Tensor<D, Float>>
Get the gradient accumulator for tensor, inserting a zeros tensor of the
same shape/dtype if absent.
pub fn get_ids(&self) -> impl Iterator<Item = &TensorId>
pub fn tensors(&self) -> impl Iterator<Item = &Tensor<D, Float>>
pub fn iter(&self) -> Iter<'_, TensorId, Tensor<D, Float>> ⓘ
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn global_norm(&self) -> Result<f64>
pub fn global_norm(&self) -> Result<f64>
Global L2 norm of all gradients: sqrt(sum ||g||^2).
Sourcepub fn clip_grad_norm(&mut self, max_norm: f64) -> Result<f64>
pub fn clip_grad_norm(&mut self, max_norm: f64) -> Result<f64>
Clip all gradients by their global norm: if norm > max_norm, scale
every gradient by max_norm / norm. Returns the (pre-clip) norm.
pub fn clear(&mut self)
Trait Implementations§
Auto Trait Implementations§
impl<D> Freeze for GradStore<D>
impl<D> RefUnwindSafe for GradStore<D>
impl<D> Send for GradStore<D>
impl<D> Sync for GradStore<D>
impl<D> Unpin for GradStore<D>
impl<D> UnsafeUnpin for GradStore<D>
impl<D> UnwindSafe for GradStore<D>
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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