表示多维数组,用于向模型推理提供数据或从中获取数据。

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>
    • 张量

属性

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

获取张量的缓冲区数据。

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

dims: readonly number[]

获取张量的维度。

获取保存张量数据的 WebGPU 缓冲区。

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

location: DataLocation

获取数据的位置。

获取保存张量数据的 WebNN MLTensor。

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

size: number

获取张量中的元素数量。

texture: WebGLTexture

获取保存张量数据的 WebGL 纹理。

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

type: keyof DataTypeMap

获取张量的数据类型。

方法

  • 释放张量数据。

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

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

    返回 void

  • 获取张量的缓冲区数据。

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

    参数

    • Optional releaseData: boolean

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

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

  • 从张量创建 DataURL 实例

    参数

    • Optional options: TensorToDataUrlOptions

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

      将应用以下默认设置

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

    返回 string

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

  • 从张量创建 ImageData 实例

    参数

    • Optional options: TensorToImageDataOptions

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

      将应用以下默认设置

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

    返回 ImageData

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

使用 TypeDoc 生成