Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor {
    pub data: Vec<f32>,
    pub shape: Vec<usize>,
}
Expand description

Basic tensor operations for GNN computations

Fields§

§data: Vec<f32>

Flattened tensor data

§shape: Vec<usize>

Shape of the tensor (dimensions)

Implementations§

Source§

impl Tensor

Source

pub fn new(data: Vec<f32>, shape: Vec<usize>) -> Result<Self>

Create a new tensor from data and shape

§Arguments
  • data - Flattened tensor data
  • shape - Dimensions of the tensor
§Returns

A new Tensor instance

§Errors

Returns GnnError::InvalidShape if data length doesn’t match shape

Source

pub fn zeros(shape: &[usize]) -> Result<Self>

Create a zero-filled tensor with the given shape

§Arguments
  • shape - Dimensions of the tensor
§Returns

A new zero-filled Tensor

§Errors

Returns GnnError::InvalidShape if shape is empty or contains zero

Source

pub fn from_vec(data: Vec<f32>) -> Self

Create a 1D tensor from a vector

§Arguments
  • data - Vector data
§Returns

A new 1D Tensor

Source

pub fn dot(&self, other: &Tensor) -> Result<f32>

Compute dot product with another tensor (both must be 1D)

§Arguments
  • other - Another tensor to compute dot product with
§Returns

The dot product as a scalar

§Errors

Returns GnnError::DimensionMismatch if tensors are not 1D or have different lengths

Source

pub fn matmul(&self, other: &Tensor) -> Result<Tensor>

Matrix multiplication

§Arguments
  • other - Another tensor to multiply with
§Returns

The result of matrix multiplication

§Errors

Returns GnnError::DimensionMismatch if dimensions are incompatible

Source

pub fn add(&self, other: &Tensor) -> Result<Tensor>

Element-wise addition

§Arguments
  • other - Another tensor to add
§Returns

The sum of the two tensors

§Errors

Returns GnnError::DimensionMismatch if shapes don’t match

Source

pub fn scale(&self, scalar: f32) -> Tensor

Scalar multiplication

§Arguments
  • scalar - Scalar value to multiply by
§Returns

A new tensor with all elements scaled

Source

pub fn relu(&self) -> Tensor

ReLU activation function (max(0, x))

§Returns

A new tensor with ReLU applied element-wise

Source

pub fn sigmoid(&self) -> Tensor

Sigmoid activation function (1 / (1 + e^(-x))) with numerical stability

§Returns

A new tensor with sigmoid applied element-wise

Source

pub fn tanh(&self) -> Tensor

Tanh activation function

§Returns

A new tensor with tanh applied element-wise

Source

pub fn l2_norm(&self) -> f32

Compute L2 norm (Euclidean norm) with improved precision

§Returns

The L2 norm of the tensor

Source

pub fn normalize(&self) -> Result<Tensor>

Normalize the tensor to unit L2 norm

§Returns

A normalized tensor

§Errors

Returns GnnError::InvalidInput if norm is zero

Source

pub fn as_slice(&self) -> &[f32]

Get a slice view of the tensor data

§Returns

A slice reference to the underlying data

Source

pub fn into_vec(self) -> Vec<f32>

Consume the tensor and return the underlying vector

§Returns

The vector containing the tensor data

Source

pub fn len(&self) -> usize

Get the number of elements in the tensor

Source

pub fn is_empty(&self) -> bool

Check if the tensor is empty

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 PartialEq for Tensor

Source§

fn eq(&self, other: &Tensor) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Tensor

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more