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

  • 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: 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()).

dtype: keyof DataTypeMap

The data type for the array.

id: number

Unique id of this tensor.

isDisposed: boolean
kept: boolean

Whether this tensor has been globally kept.

rank: number
rankType: R

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

scopeId: number

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

shape: ShapeMap[R]

The shape of the tensor.

size: number

Number of elements in the tensor.

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(): 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(): 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<D>(): Promise<TensorBuffer<R, D>>
  • Returns a promise of tf.TensorBuffer that holds the underlying data.

    doc

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

    Type Parameters

    • D extends keyof DataTypeMap = "float32"

    Returns Promise<TensorBuffer<R, D>>

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

    doc

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

    Type Parameters

    • D extends keyof DataTypeMap = "float32"

    Returns TensorBuffer<R, D>

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

    Returns Promise<Uint8Array | Uint8Array[]>

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

    doc

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

    Type Parameters

    Parameters

    • this: T

    Returns T

  • 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 extends keyof DataTypeMap = NumericDataType

    Returns Promise<DataTypeMap[D]>

  • 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 extends keyof DataTypeMap = NumericDataType

    Returns DataTypeMap[D]

  • dataToGPU(options?: DataToGPUWebGLOption): GPUData
  • Copy the tensor's data to a new GPU resource. Comparing to the dataSync() and data(), this method prevents data from being downloaded to CPU.

    For WebGL backend, the data will be stored on a densely packed texture. This means that the texture will use the RGBA channels to store value.

    For WebGPU backend, the data will be stored on a buffer. There is no parameter, so can not use an user defined size to create the buffer.

    doc

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

    Parameters

    • Optional options: DataToGPUWebGLOption

    Returns GPUData

    For WebGL backend, a GPUData contains the new texture and its information. { tensorRef: The tensor that is associated with this texture, texture: WebGLTexture, texShape: [number, number] // [height, width] }

    For WebGPU backend, a GPUData contains the new buffer and
    its information.
    {
    tensorRef: The tensor that is associated with this buffer,
    buffer: GPUBuffer,
    bufSize: number
    }

    Remember to dispose the GPUData after it is used by
    `res.tensorRef.dispose()`.
  • dispose(): void
  • Disposes tf.Tensor from memory.

    doc

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

    Returns void

  • 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(): void
  • Returns void

  • 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(trainable?: boolean, name?: string, dtype?: keyof DataTypeMap): Variable<R>
  • Parameters

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

    Returns Variable<R>