表示用于模型推理输入或输出的多维数组。

interface Tensor {
    data: string[] | Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
    dims: readonly number[];
    gpuBuffer: GpuBufferTypeFallback;
    location: DataLocation;
    mlTensor: MLTensorTypeFallback;
    size: number;
    texture: WebGLTexture;
    type: keyof DataTypeMap;
    dispose(): void;
    getData(releaseData?): Promise<string[] | Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array>;
    reshape(dims): TypedTensor<keyof DataTypeMap>;
    toDataURL(options?): string;
    toImageData(options?): ImageData;
}

继承结构

  • TypedTensorBase<Type>
  • TypedTensorUtils<Type>
    • Tensor

属性

data: string[] | Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array

获取 tensor 的缓冲区数据。

如果数据不在 CPU 上(例如,它是 WebGL 纹理或 WebGPU 缓冲区的形式),则抛出错误。

dims: readonly number[]

获取 tensor 的维度。

获取持有 tensor 数据的 WebGPU 缓冲区。

如果数据不在 GPU 上作为 WebGPU 缓冲区,则抛出错误。

location: DataLocation

获取数据的位置。

获取持有 tensor 数据的 WebNN MLTensor。

如果数据不在 WebNN MLTensor 中,则抛出错误。

size: number

获取 tensor 中的元素数量。

texture: WebGLTexture

获取持有 tensor 数据的 WebGL 纹理。

如果数据不在 GPU 上作为 WebGL 纹理,则抛出错误。

type: keyof DataTypeMap

获取 tensor 的数据类型。

方法

  • 释放 tensor 数据。

    如果数据在 CPU 上,则移除其对底层数据的内部引用。如果数据在 GPU 上,则释放 GPU 上的数据。

    调用此函数后,tensor 被视为不再有效。其位置将设置为 'none'。

    返回值 void

  • 获取 tensor 的缓冲区数据。

    如果数据在 CPU 上,立即返回数据。如果数据在 GPU 上,则下载数据并返回 Promise。

    参数

    • 可选 releaseData: boolean

      是否释放 GPU 上的数据。如果数据已在 CPU 上,则忽略此项。

    返回值 Promise<string[] | Int8Array | Uint8Array | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array>

  • 从 tensor 创建一个 DataURL 实例

    参数

    • 可选 options: TensorToDataUrlOptions

      一个可选对象,表示从 tensor 创建 DataURL 实例的选项。

      将应用以下默认设置

      • format: 'RGB'
      • tensorLayout: 'NCHW'

    返回值 string

    表示从 tensor 数据转换而来的图像的 DataURL 字符串

  • 从 tensor 创建一个 ImageData 实例

    参数

    • 可选 options: TensorToImageDataOptions

      一个可选对象,表示从 tensor 创建 ImageData 实例的选项。

      将应用以下默认设置

      • format: 'RGB'
      • tensorLayout: 'NCHW'

    返回值 ImageData

    表示从 tensor 数据转换而来的图像的 ImageData 实例

使用 TypeDoc 生成