sliceLinear method

Future<Tensor<T>> sliceLinear({
  1. required int start,
  2. required int end,
})

Creates a new tensor by slicing the flattened tensor data. start is the starting flat index and end is the ending flat index (exclusive).

Implementation

Future<Tensor<T>> sliceLinear({required int start, required int end}) async {
  if (start < 0 || end > size || start >= end) {
    throw Exception(
      "Invalid slice indices: start=$start, end=$end, size=$size.",
    );
  }
  int newSize = end - start;
  // Read the tensor data then slice it using getTypedDataSublist.
  T fullData = await getData();
  final T slicedData = getTypedDataSublist<T>(fullData, start, end);
  // Create a new tensor with 1D shape.
  return await Tensor.create<T>(
    [newSize],
    data: slicedData,
    dataType: dataType,
  );
}