Options
All
  • Public
  • Public/Protected
  • All
Menu

A tf.Tensor object represents an immutable, multidimensional array of numbers that has a shape and a data type.

For performance reasons, functions that create tensors do not necessarily perform a copy of the data passed to them (e.g. if the data is passed as a Float32Array), and changes to the data will change the tensor. This is not a feature and is not supported. To avoid this behavior, use the tensor before changing the input data or create a copy with copy = tf.add(yourTensor, 0).

See tf.tensor for details on how to create a tf.Tensor.

doc

{heading: 'Tensors', subheading: 'Classes'}

Type parameters

Hierarchy

  • Tensor

Index

Constructors

constructor

  • new Tensor<R>(shape: ShapeMap[R], dtype: keyof DataTypeMap, dataId: object, id: number): Tensor<R>
  • Type parameters

    Parameters

    • shape: ShapeMap[R]
    • dtype: keyof DataTypeMap
    • dataId: object
    • id: number

    Returns Tensor<R>

Properties

dataId

dataId: object

Id of the bucket holding the data for this tensor. Multiple arrays can point to the same bucket (e.g. when calling array.reshape()).

Readonly dtype

dtype: keyof DataTypeMap

The data type for the array.

Readonly id

id: number

Unique id of this tensor.

Readonly isDisposed

isDisposed: boolean

kept

kept: boolean

Whether this tensor has been globally kept.

Readonly rank

rank: number

Readonly rankType

rankType: R

The rank type for the array (see Rank enum).

scopeId

scopeId: number

The id of the scope this tensor is being tracked in.

Readonly shape

shape: ShapeMap[R]

The shape of the tensor.

Readonly size

size: number

Number of elements in the tensor.

Readonly strides

strides: number[]

Number of elements to skip in each dimension when indexing. See https://docs.scipy.org/doc/numpy/reference/generated/\ numpy.ndarray.strides.html

Methods

array

  • array(): Promise<ArrayMap[R]>
  • Returns the tensor data as a nested array. The transfer of data is done asynchronously.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Returns Promise<ArrayMap[R]>

arraySync

  • arraySync(): ArrayMap[R]
  • Returns the tensor data as a nested array. The transfer of data is done synchronously.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Returns ArrayMap[R]

buffer

  • buffer<D>(): Promise<TensorBuffer<R, D>>
  • Returns a promise of tf.TensorBuffer that holds the underlying data.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Type parameters

    • D: keyof DataTypeMap = "float32"

    Returns Promise<TensorBuffer<R, D>>

bufferSync

  • bufferSync<D>(): TensorBuffer<R, D>
  • Returns a tf.TensorBuffer that holds the underlying data.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Type parameters

    • D: keyof DataTypeMap = "float32"

    Returns TensorBuffer<R, D>

bytes

  • bytes(): Promise<Uint8Array | Uint8Array[]>
  • Returns the underlying bytes of the tensor's data.

    Returns Promise<Uint8Array | Uint8Array[]>

clone

  • clone<T>(): T
  • Returns a copy of the tensor. See tf.clone for details.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Type parameters

    Returns T

data

  • data<D>(): Promise<DataTypeMap[D]>
  • Asynchronously downloads the values from the tf.Tensor. Returns a promise of TypedArray that resolves when the computation has finished.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Type parameters

    • D: keyof DataTypeMap = NumericDataType

    Returns Promise<DataTypeMap[D]>

dataSync

  • dataSync<D>(): DataTypeMap[D]
  • Synchronously downloads the values from the tf.Tensor. This blocks the UI thread until the values are ready, which can cause performance issues.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Type parameters

    • D: keyof DataTypeMap = NumericDataType

    Returns DataTypeMap[D]

dispose

  • dispose(): void
  • Disposes tf.Tensor from memory.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Returns void

print

  • print(verbose?: boolean): void
  • Prints the tf.Tensor. See tf.print for details.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Parameters

    • Optional verbose: boolean

      Whether to print verbose information about the tensor, including dtype and size.

    Returns void

throwIfDisposed

  • throwIfDisposed(): void
  • Returns void

toString

  • toString(verbose?: boolean): string
  • Returns a human-readable description of the tensor. Useful for logging.

    doc

    {heading: 'Tensors', subheading: 'Classes'}

    Parameters

    • Optional verbose: boolean

    Returns string

variable

  • variable(trainable?: boolean, name?: string, dtype?: keyof DataTypeMap): Variable<R>
  • Parameters

    • Optional trainable: boolean
    • Optional name: string
    • Optional dtype: keyof DataTypeMap

    Returns Variable<R>