# Tensor

```
interface Tensor
```

*** ** * ** ***

A typed multi-dimensional array used in Tensorflow Lite.

The native handle of a `Tensor` is managed by `NativeInterpreterWrapper`, and does not needed to be closed by the client. However, once the `NativeInterpreterWrapper` has been closed, the tensor handle will be invalidated.

## Summary

| ### Nested types |
|---|
| `class https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor.QuantizationParams` Quantization parameters that corresponds to the table, `QuantizationParameters`, in the [TFLite Model schema file.](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/schema/schema.fbs) |

| ### Public functions |
|---|---|
| `https://developers.google.com/edge/api/litert/kotlin/java/nio/ByteBuffer!` | `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor#asReadOnlyBuffer()()` Returns a read-only `ByteBuffer` view of the tensor data. |
| `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/DataType!` | `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor#dataType()()` Returns the `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/DataType` of elements stored in the Tensor. |
| `https://developers.google.com/edge/api/litert/kotlin/kotlin/Int` | `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor#numBytes()()` Returns the size, in bytes, of the tensor data. |
| `https://developers.google.com/edge/api/litert/kotlin/kotlin/Int` | `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor#numDimensions()()` Returns the number of dimensions (sometimes referred to as [rank](https://www.tensorflow.org/resources/dims_types.html#rank)) of the Tensor. |
| `https://developers.google.com/edge/api/litert/kotlin/kotlin/Int` | `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor#numElements()()` Returns the number of elements in a flattened (1-D) view of the tensor. |
| `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor.QuantizationParams!` | `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor#quantizationParams()()` Returns the quantization parameters of the tensor within the owning interpreter. |
| `https://developers.google.com/edge/api/litert/kotlin/kotlin/IntArray<https://developers.google.com/edge/api/litert/kotlin/kotlin/Int>!` | `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor#shape()()` Returns the [shape](https://www.tensorflow.org/resources/dims_types.html#shape) of the Tensor, i.e., the sizes of each dimension. |
| `https://developers.google.com/edge/api/litert/kotlin/kotlin/IntArray<https://developers.google.com/edge/api/litert/kotlin/kotlin/Int>!` | `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/Tensor#shapeSignature()()` Returns the original [shape](https://www.tensorflow.org/resources/dims_types.html#shape) of the Tensor, i.e., the sizes of each dimension - before any resizing was performed. |

## Public functions

### asReadOnlyBuffer

```
fun asReadOnlyBuffer(): ByteBuffer!
```

Returns a read-only `ByteBuffer` view of the tensor data.

In general, this method is most useful for obtaining a read-only view of output tensor data, \*after\* inference has been executed (e.g., via `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/InterpreterApi#run(java.lang.Object,java.lang.Object)`). In particular, some graphs have dynamically shaped outputs, which can make feeding a predefined output buffer to the interpreter awkward. Example usage:

```kotlin
interpreter.run(input, null);
ByteBuffer outputBuffer = interpreter.getOutputTensor(0).asReadOnlyBuffer();
// Copy or read from outputBuffer.
```

WARNING: If the tensor has not yet been allocated, e.g., before inference has been executed, the result is undefined. Note that the underlying tensor pointer may also change when the tensor is invalidated in any way (e.g., if inference is executed, or the graph is resized), so it is \*not\* safe to hold a reference to the returned buffer beyond immediate use directly following inference. Example \*bad\* usage:

```kotlin
ByteBuffer outputBuffer = interpreter.getOutputTensor(0).asReadOnlyBuffer();
interpreter.run(input, null);
// Copy or read from outputBuffer (which may now be invalid).
```

| Throws |
|---|---|
| `https://developers.google.com/edge/api/litert/kotlin/java/lang/IllegalArgumentException` | if the tensor data has not been allocated. |

### dataType

```
fun dataType(): DataType!
```

Returns the `https://developers.google.com/edge/api/litert/kotlin/org/tensorflow/lite/DataType` of elements stored in the Tensor.

### numBytes

```
fun numBytes(): Int
```

Returns the size, in bytes, of the tensor data.

### numDimensions

```
fun numDimensions(): Int
```

Returns the number of dimensions (sometimes referred to as [rank](https://www.tensorflow.org/resources/dims_types.html#rank)) of the Tensor.

Will be 0 for a scalar, 1 for a vector, 2 for a matrix, 3 for a 3-dimensional tensor etc.

### numElements

```
fun numElements(): Int
```

Returns the number of elements in a flattened (1-D) view of the tensor.

### quantizationParams

```
fun quantizationParams(): Tensor.QuantizationParams!
```

Returns the quantization parameters of the tensor within the owning interpreter.

Only quantized tensors have valid `QuantizationParameters`. For tensor that are not quantized, the values of scale and zero_point are both 0.

### shape

```
fun shape(): IntArray<Int>!
```

Returns the [shape](https://www.tensorflow.org/resources/dims_types.html#shape) of the Tensor, i.e., the sizes of each dimension.

| Returns |
|---|---|
| `https://developers.google.com/edge/api/litert/kotlin/kotlin/IntArray<https://developers.google.com/edge/api/litert/kotlin/kotlin/Int>!` | an array where the i-th element is the size of the i-th dimension of the tensor. |

### shapeSignature

```
fun shapeSignature(): IntArray<Int>!
```

Returns the original [shape](https://www.tensorflow.org/resources/dims_types.html#shape) of the Tensor, i.e., the sizes of each dimension - before any resizing was performed. Unknown dimensions are designated with a value of -1.

| Returns |
|---|---|
| `https://developers.google.com/edge/api/litert/kotlin/kotlin/IntArray<https://developers.google.com/edge/api/litert/kotlin/kotlin/Int>!` | an array where the i-th element is the size of the i-th dimension of the tensor. |