Tensor class
An N-dimensional tensor backed by a flat Float32List.
Supports common tensor operations needed for neural network inference:
- Element-wise: add, subtract, multiply, divide
- Matrix multiplication (2D and batched)
- Reshape, transpose, permute, squeeze, unsqueeze
- Reduction: sum, mean, max, softmax
- Slicing and indexing
Constructors
-
Tensor(Float32List data, List<
int> shape) - Creates a tensor from existing data and shape.
- Tensor.arange(int start, int end)
-
Creates a 1D tensor with values from
starttoend(exclusive).factory - Tensor.fromList(List list)
-
Creates a tensor from a nested list of doubles.
factory
-
Tensor.full(List<
int> shape, double value) -
Creates a tensor filled with a constant value.
factory
-
Tensor.ones(List<
int> shape) -
Creates a tensor filled with ones.
factory
-
Tensor.randn(List<
int> shape, {double mean = 0.0, double std = 1.0, int? seed}) -
Creates a tensor with random values from a normal distribution.
factory
- Tensor.scalar(double value)
-
Creates a scalar tensor (shape =
1).factory -
Tensor.zeros(List<
int> shape) -
Creates a tensor filled with zeros.
factory
Properties
- data → Float32List
-
The underlying flat data storage.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- ndim → int
-
Number of dimensions.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
shape
→ List<
int> -
The shape of the tensor (list of dimension sizes).
final
- size → int
-
Total number of elements.
no setter
-
strides
↔ List<
int> -
The strides for each dimension (number of elements to skip).
latefinal
Methods
-
abs(
) → Tensor - Element-wise absolute value.
-
addScalar(
double s) → Tensor - Add a scalar to all elements.
-
argmax(
) → int - Argmax over all elements.
-
at(
List< int> indices) → double - Get a single element by multi-dimensional index.
-
clamp(
double minVal, double maxVal) → Tensor -
Clamp values to
minVal, maxVal. -
clone(
) → Tensor - Create a deep copy.
-
divScalar(
double s) → Tensor - Divide all elements by a scalar.
-
expand(
List< int> newShape) → Tensor - Expand a size-1 dimension to the given size (broadcast).
-
flatten(
) → Tensor - Flatten to a 1D tensor.
-
gelu(
) → Tensor - Element-wise GELU activation.
-
logSoftmax(
int dim) → Tensor - Log-softmax over a given dimension.
-
map(
double fn(double)) → Tensor - Apply a function to each element.
-
matmul(
Tensor other) → Tensor - Matrix multiplication for 2D tensors.
-
maxAll(
) → double - Max of all elements.
-
maxDim(
int dim, {bool keepDim = false}) → Tensor - Max over a given dimension.
-
mean(
int dim, {bool keepDim = false}) → Tensor - Mean over a given dimension.
-
meanAll(
) → double - Mean of all elements.
-
mulScalar(
double s) → Tensor - Multiply all elements by a scalar.
-
neg(
) → Tensor - Element-wise negation.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
permute(
List< int> order) → Tensor - Permute dimensions. E.g., (0, 2, 1) transposes last two dims.
-
pow(
double exponent) → Tensor - Element-wise power.
-
relu(
) → Tensor - Element-wise ReLU activation.
-
repeat(
List< int> times) → Tensor - Repeat along each dimension.
-
reshape(
List< int> newShape) → Tensor - Reshape the tensor to a new shape. -1 infers that dimension.
-
setAt(
List< int> indices, double value) → void - Set a single element by multi-dimensional index.
-
sigmoid(
) → Tensor - Element-wise sigmoid.
-
slice(
int start, int end, [int dim = 0]) → Tensor -
Slice: returns a sub-tensor from
starttoendalong dimension 0. -
softmax(
int dim) → Tensor - Softmax over a given dimension.
-
sqrt(
) → Tensor - Element-wise square root.
-
squeeze(
[int? dim]) → Tensor - Remove a dimension of size 1 at the given position.
-
sum(
int dim, {bool keepDim = false}) → Tensor - Sum over a given dimension, keeping the dimension (size 1).
-
sumAll(
) → double - Sum all elements.
-
tanh(
) → Tensor - Element-wise tanh.
-
toList(
) → dynamic - Convert to a nested Dart list.
-
toString(
) → String -
A string representation of this object.
override
-
transpose(
int dim0, int dim1) → Tensor - Transpose: swap two dimensions.
-
unsqueeze(
int dim) → Tensor - Add a dimension of size 1 at the given position.
Operators
-
operator *(
Tensor other) → Tensor -
operator +(
Tensor other) → Tensor -
operator -(
Tensor other) → Tensor -
operator /(
Tensor other) → Tensor -
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator [](
int index) → Tensor - Get a sub-tensor along the first dimension.