pub struct Gradients<E, D: Storage<E>> { /* private fields */ }
Expand description
A generic container for keeping gradients of tensors keyed by the tensor’s UniqueId.
You can:
- Insert array values into it
- Remove entries
- Access references to arrays
- Access mutable references to arrays
Implementations§
source§impl<E, D: Storage<E>> Gradients<E, D>
impl<E, D: Storage<E>> Gradients<E, D>
sourcepub fn leaky() -> Self
pub fn leaky() -> Self
Creates a Gradients object without any leaf tensor ids. This will never drop gradients for temporary tensors.
This is why this method is called leaky
, because
it will keep gradients from previous passes if it is
used consecutively.
You should use crate::nn::ZeroGrads::alloc_grads, which will ensure non-leaf gradients are freed after backwards.
source§impl<E, D: Storage<E>> Gradients<E, D>
impl<E, D: Storage<E>> Gradients<E, D>
sourcepub fn get_or_alloc_mut<S: Shape>(
&mut self,
t: &impl Tensorlike<S, E, D>
) -> Result<&mut D::Vec, D::Err>
pub fn get_or_alloc_mut<S: Shape>( &mut self, t: &impl Tensorlike<S, E, D> ) -> Result<&mut D::Vec, D::Err>
Retrieves mutable gradient for t
, allocating one if it isn’t present.
sourcepub fn try_alloc_for<S: Shape>(
&mut self,
t: &impl Tensorlike<S, E, D>
) -> Result<(), D::Err>
pub fn try_alloc_for<S: Shape>( &mut self, t: &impl Tensorlike<S, E, D> ) -> Result<(), D::Err>
Inserts a gradient for t
sourcepub fn retain_leafs(&mut self, ids: &[UniqueId])
pub fn retain_leafs(&mut self, ids: &[UniqueId])
Drops all gradients except for the ids specified in the parameter.
sourcepub fn drop_non_leafs(&mut self)
pub fn drop_non_leafs(&mut self)
Keeps all gradients marked previously by Gradients::retain_leafs, and drops all others.
Trait Implementations§
Auto Trait Implementations§
impl<E, D> RefUnwindSafe for Gradients<E, D>where <D as Storage<E>>::Vec: RefUnwindSafe,
impl<E, D> Send for Gradients<E, D>
impl<E, D> Sync for Gradients<E, D>
impl<E, D> Unpin for Gradients<E, D>
impl<E, D> UnwindSafe for Gradients<E, D>where <D as Storage<E>>::Vec: RefUnwindSafe,
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