Struct Tensor
Source pub struct Tensor {
pub data: Vec<f32>,
pub shape: Vec<usize>,
}
Expand description
Basic tensor operations for GNN computations
Shape of the tensor (dimensions)
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
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
Create a 1D tensor from a vector
§Arguments
§Returns
A new 1D Tensor
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
Matrix multiplication
§Arguments
other - Another tensor to multiply with
§Returns
The result of matrix multiplication
§Errors
Returns GnnError::DimensionMismatch if dimensions are incompatible
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
Scalar multiplication
§Arguments
scalar - Scalar value to multiply by
§Returns
A new tensor with all elements scaled
ReLU activation function (max(0, x))
§Returns
A new tensor with ReLU applied element-wise
Sigmoid activation function (1 / (1 + e^(-x))) with numerical stability
§Returns
A new tensor with sigmoid applied element-wise
Tanh activation function
§Returns
A new tensor with tanh applied element-wise
Compute L2 norm (Euclidean norm) with improved precision
§Returns
The L2 norm of the tensor
Normalize the tensor to unit L2 norm
§Returns
A normalized tensor
§Errors
Returns GnnError::InvalidInput if norm is zero
Get a slice view of the tensor data
§Returns
A slice reference to the underlying data
Consume the tensor and return the underlying vector
§Returns
The vector containing the tensor data
Get the number of elements in the tensor
Check if the tensor is empty
Performs copy-assignment from
source.
Read more
Formats the value using the given formatter.
Read more
Tests for self and other values to be equal, and is used by ==.
Tests for !=. The default implementation is almost always sufficient,
and should not be overridden without very good reason.
Immutably borrows from an owned value.
Read more
Mutably borrows from an owned value.
Read more
🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from
self to
dest.
Read more
Returns the argument unchanged.
Instruments this type with the provided
Span, returning an
Instrumented wrapper.
Read more
Calls U::from(self).
That is, this conversion is whatever the implementation of
From<T> for U chooses to do.
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning.
Read more
Uses borrowed data to replace owned data, usually by cloning.
Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.