pub struct Variable<T: Float> { /* private fields */ }Expand description
A variable in the computation graph that wraps a Tensor and supports
reverse-mode automatic differentiation.
Variable<T> uses shared ownership (Rc<RefCell<...>>) so that the same
node can appear as a parent in multiple downstream operations. Cloning a
Variable is cheap — it just increments the reference count.
Implementations§
Source§impl<T: Float> Variable<T>
impl<T: Float> Variable<T>
Sourcepub fn new(data: Tensor<T>, requires_grad: bool) -> Self
pub fn new(data: Tensor<T>, requires_grad: bool) -> Self
Create a new leaf variable.
§Examples
let t = Tensor::<f64>::from_vec(vec![1.0, 2.0, 3.0], vec![3]).unwrap();
let v = Variable::new(t, true);
assert!(v.requires_grad());
assert_eq!(v.shape(), vec![3]);Sourcepub fn requires_grad(&self) -> bool
pub fn requires_grad(&self) -> bool
Whether this variable tracks gradients.
Sourcepub fn detach(&self) -> Self
pub fn detach(&self) -> Self
Detach from the computation graph, returning a new leaf variable with the same data but no graph history.
Sourcepub fn set_data(&self, data: Tensor<T>)
pub fn set_data(&self, data: Tensor<T>)
Replace the data tensor (used by optimizers and weight loading).
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Variable<T>
impl<T> !RefUnwindSafe for Variable<T>
impl<T> !Send for Variable<T>
impl<T> !Sync for Variable<T>
impl<T> Unpin for Variable<T>
impl<T> UnsafeUnpin for Variable<T>
impl<T> !UnwindSafe for Variable<T>
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