pub struct Gradients<E: Unit, D: DeviceStorage> { /* 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: Unit, D: DeviceStorage> Gradients<E, D>
impl<E: Unit, D: DeviceStorage> 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: Unit, D: DeviceStorage> Gradients<E, D>
impl<E: Unit, D: DeviceStorage> Gradients<E, D>
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.