pub struct Tensor {
pub data: Array,
pub grad: Option<Rc<RefCell<Array>>>,
pub requires_grad: bool,
pub compute_node: Option<Rc<ComputeNode>>,
pub is_leaf: bool,
}Expand description
Tensor con soporte para autograd
Fields§
§data: ArrayDatos del tensor
grad: Option<Rc<RefCell<Array>>>Gradiente acumulado (dL/d(este tensor))
requires_grad: boolSi este tensor requiere gradientes
compute_node: Option<Rc<ComputeNode>>Nodo del compute graph (None para leaf tensors)
is_leaf: boolSi es un leaf tensor (parámetro del modelo)
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn cross_entropy_loss(&self, target: &Tensor) -> Result<Self>
pub fn cross_entropy_loss(&self, target: &Tensor) -> Result<Self>
Cross-entropy loss con softmax integrado (Batch-aware)
Sourcepub fn flatten(&self, start_dim: usize, end_dim: usize) -> Result<Self>
pub fn flatten(&self, start_dim: usize, end_dim: usize) -> Result<Self>
Flatten: flatten(start, end)
Sourcepub fn conv1d(
&self,
weight: &Tensor,
bias: Option<&Tensor>,
stride: usize,
padding: usize,
) -> Result<Self>
pub fn conv1d( &self, weight: &Tensor, bias: Option<&Tensor>, stride: usize, padding: usize, ) -> Result<Self>
Conv1D
Source§impl Tensor
impl Tensor
Sourcepub fn new(data: Array, requires_grad: bool) -> Self
pub fn new(data: Array, requires_grad: bool) -> Self
Crea un nuevo tensor desde un Array
§Argumentos
data: Array con los datosrequires_grad: Si se deben calcular gradientes para este tensor
Sourcepub fn from_operation(
data: Array,
compute_node: ComputeNode,
requires_grad: bool,
) -> Self
pub fn from_operation( data: Array, compute_node: ComputeNode, requires_grad: bool, ) -> Self
Crea tensor desde resultado de operación
Sourcepub fn set_gradient(&self, grad: Array) -> Result<()>
pub fn set_gradient(&self, grad: Array) -> Result<()>
Establece el gradiente manualmente
Sourcepub fn accumulate_gradient(&self, incoming_grad: &Array) -> Result<()>
pub fn accumulate_gradient(&self, incoming_grad: &Array) -> Result<()>
Acumula gradiente (grad += incoming_grad)
Sourcepub fn detach(&self) -> Self
pub fn detach(&self) -> Self
Desconecta del compute graph (detach) Retorna un nuevo tensor sin gradientes
Sourcepub fn backward(&self) -> Result<()>
pub fn backward(&self) -> Result<()>
Backpropagation - Calcula gradientes desde este tensor hacia las hojas
Este es el algoritmo principal de autograd:
- Inicializa grad = 1.0 para este tensor (dL/dL = 1)
- Recorre el graph en orden topológico inverso
- Para cada operación, llama su backward_fn
- Acumula gradientes en los inputs
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Tensor
impl !RefUnwindSafe for Tensor
impl !Send for Tensor
impl !Sync for Tensor
impl Unpin for Tensor
impl !UnwindSafe for Tensor
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
🔬This is a nightly-only experimental API. (
clone_to_uninit)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