Melange
API ReferenceFlutter

Tensor

Complete API reference for the Tensor class in Flutter.

This page reflects zetic_mlange 1.8.1.

Tensor is the Dart data container used for model inputs and outputs. It stores bytes, a DataType, and a shape.

Import

import 'package:zetic_mlange/zetic_mlange.dart';

Constructors

Tensor

Tensor({
  required Uint8List data,
  DataType dataType = DataType.int8,
  List<int>? shape,
})

Creates a tensor with defensive-copy semantics. If shape is omitted, the tensor shape defaults to [data.lengthInBytes].

Tensor.view

Tensor.view({
  required Uint8List data,
  DataType dataType = DataType.int8,
  List<int>? shape,
})

Creates a tensor view over the provided byte list without copying. Mutating the source Uint8List also mutates the tensor bytes.

For every constructor and factory, shape must describe exactly data.lengthInBytes / dataType.bytesPerElement elements. A mismatch throws MlangeException.


Typed Factories

Tensor.float32List(Float32List data, {List<int>? shape})
Tensor.float32View(Float32List data, {List<int>? shape})
Tensor.float64List(Float64List data, {List<int>? shape})
Tensor.float64View(Float64List data, {List<int>? shape})
Tensor.int32List(Int32List data, {List<int>? shape})
Tensor.int32View(Int32List data, {List<int>? shape})
Tensor.int64List(Int64List data, {List<int>? shape})
Tensor.int64View(Int64List data, {List<int>? shape})
Tensor.bytes(List<int> data, {DataType dataType = DataType.int8, List<int>? shape})
Tensor.bytesView(Uint8List data, {DataType dataType = DataType.int8, List<int>? shape})
Tensor.random(DataType dataType, List<int> shape, {Random? random})

Use List factories when you want Dart to copy the source data. Use View factories when you already own a correctly laid-out typed list and want to avoid an extra Dart-side allocation.


Properties

PropertyTypeDescription
dataUint8ListRaw tensor bytes.
dataTypeDataTypeTensor element data type.
shapeList<int>Tensor shape.
byteLengthintNumber of bytes in data.
elementCountintNumber of elements implied by shape.

Methods

int count()
int size()
Uint8List asUint8List()
Float32List asFloat32List()
Float64List asFloat64List()
Int32List asInt32List()
Int64List asInt64List()

Typed accessors validate that dataType matches the requested view.

MethodDescription
count()Number of elements based on data.lengthInBytes ~/ dataType.bytesPerElement.
size()Number of bytes in data.
asUint8List()Raw byte view without dtype validation.
asFloat32List()Typed Float32List view. Requires DataType.float32.
asFloat64List()Typed Float64List view. Requires DataType.float64.
asInt32List()Typed Int32List view. Requires DataType.int32.
asInt64List()Typed Int64List view. Requires DataType.int64.

Example

final input = Tensor.float32View(
  Float32List.fromList([1.0, 2.0, 3.0, 4.0]),
  shape: const [1, 4],
);

print(input.count()); // 4
print(input.asFloat32List());

On this page