Tensor

Struct Tensor 

Source
pub struct Tensor {
    pub value: Array1<f64>,
    pub grad: Option<Array1<f64>>,
}
Expand description

Represents a basic implementation of a tensor.

Fields§

§value: Array1<f64>§grad: Option<Array1<f64>>

Implementations§

Source§

impl Tensor

Source

pub fn new(value: Array1<f64>) -> Tensor

Creates a new Tensor with the given value.

§Arguments
  • value - The array representing the value of the tensor.
§Returns

(Tensor): A new Tensor instance.

§Examples
use ndarray::{array, Array1};
use tinygrad::Tensor;

let value = array![1.0, 2.0, 3.0];
let tensor = Tensor::new(value);

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Tensor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl TensorTrait for Tensor

Source§

fn forward(&self, _ctx: &mut Context, _inputs: Vec<ArrayView1<'_, f64>>) -> f64

Implements the forward pass for the Tensor.

§Arguments
  • _ctx - A mutable reference to the computation context (not used in this example).
  • _inputs - A vector of ArrayView1 representing the input values (not used in this example).
§Returns

(f64): The result of the forward pass (not implemented in this example, returns 0.0).

Source§

fn backward(&self, _ctx: &mut Context, _grad_output: ArrayView1<'_, f64>)

Implements the backward pass for the Tensor.

§Arguments
  • _ctx - A mutable reference to the computation context (not used in this example).
  • _grad_output - An ArrayView1 representing the gradient of the output.
§Examples
use ndarray::{array, Array1};
use tinygrad::{Tensor, Context, TensorTrait};

let mut ctx = Context::new();
let tensor = Tensor::new(array![1.0, 2.0, 3.0]);
tensor.backward(&mut ctx, array![1.0, 1.0, 1.0].view());
Source§

fn get_value(&self) -> ArrayView1<'_, f64>

Returns the value of the Tensor.

§Returns

(ArrayView1<f64>): The view of the array representing the value of the tensor.

§Examples
use ndarray::{array, Array1};
use tinygrad::{Tensor, TensorTrait};

let tensor = Tensor::new(array![1.0, 2.0, 3.0]);
let value = tensor.get_value();
Source§

fn get_grad(&self) -> Option<Array1<f64>>

Returns the gradient of the Tensor if available.

§Returns

(Option<Gradient>): The optional gradient of the tensor.

§Examples
use ndarray::{array, Array1};
use tinygrad::{Tensor, TensorTrait};

let tensor = Tensor::new(array![1.0, 2.0, 3.0]);
let grad = tensor.get_grad();

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.