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.

Type Parameters

Hierarchy (view full)

Implements

  • TensorInfo

Constructors

  • 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.

kept: boolean

Whether this tensor has been globally kept.

kerasMask?: Tensor<Rank>

The keras mask that some keras layers attach to the tensor

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

Accessors

  • get isDisposed(): boolean
  • Returns boolean

  • get rank(): number
  • Returns number

Methods

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns T

  • Type Parameters

    Parameters

    • Optionalaxis: number

    Returns T

  • Type Parameters

    Parameters

    • Optionalaxis: number

    Returns T

  • Returns the tensor data as a nested array. The transfer of data is done asynchronously.

    Returns Promise<ArrayMap[R]>

  • Returns the tensor data as a nested array. The transfer of data is done synchronously.

    Returns ArrayMap[R]

  • Type Parameters

    Returns Tensor1D

  • Type Parameters

    Parameters

    • rows: number
    • columns: number

    Returns Tensor2D

  • Type Parameters

    Parameters

    • rows: number
    • columns: number
    • depth: number

    Returns Tensor3D

  • Type Parameters

    Parameters

    • rows: number
    • columns: number
    • depth: number
    • depth2: number

    Returns Tensor4D

  • Type Parameters

    Parameters

    • rows: number
    • columns: number
    • depth: number
    • depth2: number
    • depth3: number

    Returns Tensor5D

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Returns Scalar

  • Type Parameters

    Parameters

    • this: T
    • dtype: keyof DataTypeMap

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • filterSize: number | [number, number]
    • strides: number | [number, number]
    • pad:
          | number
          | "valid"
          | "same"
          | ExplicitPadding
    • OptionaldimRoundingMode: "floor" | "round" | "ceil"

    Returns T

  • Type Parameters

    Parameters

    • blockShape: number[]
    • crops: number[][]

    Returns Tensor<R>

  • Type Parameters

    Parameters

    • shape: ShapeMap[R]

    Returns Tensor<R>

  • Returns a promise of tf.TensorBuffer that holds the underlying data.

    Type Parameters

    • D extends keyof DataTypeMap = "float32"

    Returns Promise<TensorBuffer<R, D>>

  • Returns a tf.TensorBuffer that holds the underlying data.

    Type Parameters

    • D extends keyof DataTypeMap = "float32"

    Returns TensorBuffer<R, D>

  • Returns the underlying bytes of the tensor's data.

    Returns Promise<Uint8Array | Uint8Array[]>

  • Type Parameters

    Parameters

    • dtype: keyof DataTypeMap

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • min: number
    • max: number

    Returns Tensor<Rank>

  • Returns a copy of the tensor. See tf.clone for details.

    Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • filter: Tensor3D | TensorLike3D
    • stride: number
    • pad:
          | number
          | "valid"
          | "same"
          | ExplicitPadding
    • OptionaldataFormat: "NWC" | "NCW"
    • Optionaldilation: number
    • OptionaldimRoundingMode: "floor" | "round" | "ceil"

    Returns T

  • Type Parameters

    Parameters

    • filter: Tensor4D | TensorLike4D
    • strides: number | [number, number]
    • pad: number | "valid" | "same"
    • OptionaldataFormat: "NHWC" | "NCHW"
    • Optionaldilations: number | [number, number]
    • OptionaldimRoundingMode: "floor" | "round" | "ceil"

    Returns T

  • Type Parameters

    Parameters

    • filter: Tensor4D | TensorLike4D
    • outputShape: [number, number, number, number] | [number, number, number]
    • strides: number | [number, number]
    • pad: number | "valid" | "same"
    • OptionaldimRoundingMode: "floor" | "round" | "ceil"

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • Optionalaxis: number
    • Optionalexclusive: boolean
    • Optionalreverse: boolean

    Returns Tensor<R>

  • Type Parameters

    Parameters

    • Optionalaxis: number
    • Optionalexclusive: boolean
    • Optionalreverse: boolean

    Returns Tensor<R>

  • Asynchronously downloads the values from the tf.Tensor. Returns a promise of TypedArray that resolves when the computation has finished.

    Type Parameters

    • D extends keyof DataTypeMap = NumericDataType

    Returns Promise<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.

    Type Parameters

    • D extends keyof DataTypeMap = NumericDataType

    Returns DataTypeMap[D]

  • 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 a user-defined size to create the buffer.

    Parameters

    • Optionaloptions: 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.
    {
       tensorRef: The tensor that is associated with this buffer,
       buffer: GPUBuffer,
    }
    
    Remember to dispose the GPUData after it is used by
    `res.tensorRef.dispose()`.
    
  • Type Parameters

    Parameters

    • blockSize: number
    • dataFormat: "NHWC" | "NCHW"

    Returns T

  • Type Parameters

    Parameters

    • filter: Tensor4D | TensorLike4D
    • strides: number | [number, number]
    • pad: number | "valid" | "same"
    • OptionaldataFormat: "NHWC" | "NCHW"
    • Optionaldilations: number | [number, number]
    • OptionaldimRoundingMode: "floor" | "round" | "ceil"

    Returns T

  • Type Parameters

    Parameters

    • filter: Tensor3D | TensorLike3D
    • strides: number | [number, number]
    • pad: "valid" | "same"
    • Optionaldilations: number | [number, number]
    • OptionaldataFormat: "NHWC"

    Returns T

  • Disposes tf.Tensor from memory.

    Returns void

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • Optionalaxis: number

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    Returns Tensor<Rank>

  • Type Parameters

    Returns Tensor1D

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns Tensor<Rank>

  • Type Parameters

    Parameters

    Returns Tensor<Rank>

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • alpha: number

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • OptionaldepthRadius: number
    • Optionalbias: number
    • Optionalalpha: number
    • Optionalbeta: number

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionalaxis: number

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns T

  • Type Parameters

    Parameters

    Returns Tensor<Rank>

  • Type Parameters

    Parameters

    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • filterSize: number | [number, number]
    • strides: number | [number, number]
    • pad:
          | number
          | "valid"
          | "same"
          | ExplicitPadding
    • OptionaldimRoundingMode: "floor" | "round" | "ceil"

    Returns T

  • Type Parameters

    Parameters

    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns T

  • Type Parameters

    Parameters

    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • paddings: [number, number][]
    • mode: "reflect" | "symmetric"

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • Optionalord: number | "euclidean" | "fro"
    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns Tensor<Rank>

  • Type Parameters

    Parameters

    Returns T

  • Parameters

    • depth: number
    • onValue: number
    • offValue: number

    Returns Tensor<Rank>

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • paddings: [number, number][]
    • OptionalconstantValue: number

    Returns T

  • Type Parameters

    Parameters

    • windowShape: number | [number, number]
    • poolingType: "avg" | "max"
    • padding:
          | number
          | "valid"
          | "same"
          | ExplicitPadding
    • OptionaldiationRate: number | [number, number]
    • Optionalstrides: number | [number, number]
    • OptionaldimRoundingMode: "floor" | "round" | "ceil"

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Prints the tf.Tensor. See tf.print for details.

    Parameters

    • Optionalverbose: boolean

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

    Returns void

  • Type Parameters

    Parameters

    • this: T
    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Returns T

  • Type Parameters

    Returns T

  • Type Parameters

    Parameters

    • shape: number[]

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • newShape2D: [number, number]
    • OptionalalignCorners: boolean
    • OptionalhalfPixelCenters: boolean

    Returns T

  • Type Parameters

    Parameters

    • newShape2D: [number, number]
    • OptionalalignCorners: boolean
    • OptionalhalfFloatCenters: boolean

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionalaxis: number | number[]

    Returns T

  • Type Parameters

    Parameters

    Returns Tensor<Rank>

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Returns T

  • Type Parameters

    Parameters

    • depthwiseFilter: Tensor4D | TensorLike4D
    • pointwiseFilter: Tensor4D | TensorLike
    • strides: number | [number, number]
    • pad: "valid" | "same"
    • Optionaldilation: number | [number, number]
    • OptionaldataFormat: "NHWC" | "NCHW"

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • begin: number | number[]
    • Optionalsize: number | number[]

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionaldim: number

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • blockShape: number[]
    • paddings: number[][]

    Returns Tensor<R>

  • Type Parameters

    Parameters

    • numOrSizeSplits: number | number[]
    • Optionalaxis: number

    Returns T[]

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • Optionalaxis: number[]

    Returns T

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionalalpha: number

    Returns T

  • Type Parameters

    Parameters

    • this: Tensor<Rank>
    • begin: number[]
    • end: number[]
    • strides: number[]
    • OptionalbeginMask: number
    • OptionalendMask: number
    • OptionalellipsisMask: number
    • OptionalnewAxisMask: number
    • OptionalshrinkAxisMask: number

    Returns Tensor<Rank>

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • Optionalaxis: number | number[]
    • OptionalkeepDims: boolean

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Returns void

  • Type Parameters

    Parameters

    • b: number[]

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionalk: number
    • Optionalsorted: boolean

    Returns {
        indices: T;
        values: T;
    }

    • indices: T
    • values: T
  • Returns a human-readable description of the tensor. Useful for logging.

    Parameters

    • Optionalverbose: boolean

    Returns string

  • Type Parameters

    Parameters

    • Optionalperm: number[]

    Returns T

  • Type Parameters

    Parameters

    • this: T
    • Optionalaxis: number

    Returns {
        indices: T;
        values: T;
    }

    • indices: T
    • values: T
  • Type Parameters

    Parameters

    • this: T
    • segmentIds: Tensor1D | TensorLike1D
    • numSegments: number

    Returns T

  • Type Parameters

    Parameters

    • Optionalaxis: number

    Returns T[]

  • Parameters

    • Optionaltrainable: boolean
    • Optionalname: string
    • Optionaldtype: keyof DataTypeMap

    Returns Variable<R>

  • Type Parameters

    Parameters

    Returns T

  • Type Parameters

    Parameters

    • this: T

    Returns T