pub struct SafeVariable<F: Float + Send + Sync> {
pub id: VariableID,
pub env: Arc<SafeVariableEnvironment<F>>,
pub requires_grad: bool,
}Expand description
PyTorch-compatible Variable wrapper for ToRSh integration
This provides a PyTorch-style Variable interface that wraps the SciRS2 autograd system. Unlike the RefCell-based Variable, this is thread-safe and can be used globally.
Fields§
§id: VariableIDVariable ID in the environment
env: Arc<SafeVariableEnvironment<F>>Reference to the thread-safe environment
requires_grad: boolWhether this variable requires gradients
Implementations§
Source§impl<F: Float + Send + Sync> SafeVariable<F>
impl<F: Float + Send + Sync> SafeVariable<F>
Sourcepub fn new(
data: NdArray<F>,
env: Arc<SafeVariableEnvironment<F>>,
requires_grad: bool,
) -> Result<Self, Box<dyn Error + Send + Sync>>
pub fn new( data: NdArray<F>, env: Arc<SafeVariableEnvironment<F>>, requires_grad: bool, ) -> Result<Self, Box<dyn Error + Send + Sync>>
Create a new variable with gradient requirement
Sourcepub fn backward(&self) -> Result<(), Box<dyn Error + Send + Sync>>
pub fn backward(&self) -> Result<(), Box<dyn Error + Send + Sync>>
PyTorch-compatible backward() method
Sourcepub fn data(&self) -> Result<NdArray<F>, Box<dyn Error + Send + Sync>>
pub fn data(&self) -> Result<NdArray<F>, Box<dyn Error + Send + Sync>>
Get the current data (read-only)
Sourcepub fn requires_grad(&self) -> bool
pub fn requires_grad(&self) -> bool
Check if gradients are required
Sourcepub fn set_requires_grad(&mut self, requires_grad: bool)
pub fn set_requires_grad(&mut self, requires_grad: bool)
Set gradient requirement
Trait Implementations§
Source§impl<F: Float + Send + Sync> AutogradTensor<F> for SafeVariable<F>
impl<F: Float + Send + Sync> AutogradTensor<F> for SafeVariable<F>
Source§impl<F: Clone + Float + Send + Sync> Clone for SafeVariable<F>
impl<F: Clone + Float + Send + Sync> Clone for SafeVariable<F>
Source§fn clone(&self) -> SafeVariable<F>
fn clone(&self) -> SafeVariable<F>
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreimpl<F: Float + Send + Sync> Send for SafeVariable<F>
Implement Send + Sync for thread safety
impl<F: Float + Send + Sync> Sync for SafeVariable<F>
Auto Trait Implementations§
impl<F> Freeze for SafeVariable<F>
impl<F> RefUnwindSafe for SafeVariable<F>
impl<F> Unpin for SafeVariable<F>
impl<F> UnwindSafe for SafeVariable<F>
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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