TensorBuffer

公共抽象类 TensorBuffer
已知的直接子类

表示模型的输入或输出的数据缓冲区。

公共方法

静态 TensorBuffer
createDynamic(DataType 数据类型)
创建具有指定 DataType 的空动态 TensorBuffer
静态 TensorBuffer
createFixedSize(int[] 形状, DataType数据类型)
使用指定的 shapeDataType 创建 TensorBuffer
静态 TensorBuffer
createFrom(TensorBuffer 缓冲区,DataType 数据类型)TensorBufferDataType
使用指定的 DataType 创建来自其他数据的 TensorBuffer 深层复制。
ByteBuffer
getBuffer()
返回数据缓冲区。
抽象 DataType
getDataType()
返回此缓冲区的数据类型。
整数
getFlatSize()
获取缓冲区的 FlatSize。
抽象 float[]
getFloatArray()
返回存储在此缓冲区中的值的浮点数组。
抽象 浮点数
getFloatValue(int absIndex)
返回指定索引处的浮点值。
抽象 整数 []
getIntArray()
返回存储在此缓冲区中的值的整型数组。
抽象 整数
getIntValue(int absIndex)
返回指定索引处的整数值。
整数 []
getShape()
获取当前形状。
抽象 整数
getTypeSize()
返回数组中单个元素的字节数。
布尔值
isDynamic()
如果 TensorBuffer 是动态大小(可以随意调整大小),则返回 。
抽象 无效
loadArray(int[] src, int[] 形状)
将一个整数数组加载到具有特定形状的该缓冲区。
抽象 无效
loadArray(float[] src, int[] 形状)
将一个具有特定形状的浮点数数组加载到此缓冲区。
无效
loadArray(float[] src)
将浮点数组加载到此缓冲区中。
无效
loadArray(int[] src)loadArray
将一个整型数组加载到此缓冲区中。
无效
loadBuffer(ByteBuffer 缓冲区)
将一个字节缓冲区加载到此 TensorBuffer 中。
无效
loadBuffer(ByteBufferbuffer, int[] Shape)
将一个字节缓冲区加载到此 TensorBuffer 中,且具有特定形状。

继承的方法

公共方法

<ph type="x-smartling-placeholder"></ph> 公开 静态 TensorBuffer createDynamic (DataType dataType)

创建具有指定 DataType 的空动态 TensorBuffer。该元素的形状 创建于 TensorBuffer 为 {0}。

动态 TensorBuffer 会在加载数组或数据缓冲区时重新分配内存, 缓冲区空间以下是一些示例:

 // Creating a float dynamic TensorBuffer:
 TensorBuffer tensorBuffer = TensorBuffer.createDynamic(DataType.FLOAT32);
 // Loading a float array:
 float[] arr1 = new float[] {1, 2, 3};
 tensorBuffer.loadArray(arr, new int[] {arr1.length});
 // loading another float array:
 float[] arr2 = new float[] {1, 2, 3, 4, 5};
 tensorBuffer.loadArray(arr, new int[] {arr2.length});
 // loading a third float array with the same size as arr2, assuming shape doesn't change:
 float[] arr3 = new float[] {5, 4, 3, 2, 1};
 tensorBuffer.loadArray(arr);
 // loading a forth float array with different size as arr3 and omitting the shape will result
 // in error:
 float[] arr4 = new float[] {3, 2, 1};
 tensorBuffer.loadArray(arr); // Error: The size of byte buffer and the shape do not match.
 

参数
dataType 要创建的 TensorBuffer 的数据类型。

<ph type="x-smartling-placeholder"></ph> 公开 静态 TensorBuffer createFixedSize (int[] 形状, DataType 数据类型)

使用指定的 shapeDataType 创建 TensorBuffer。以下是一些 示例:

 // Creating a float TensorBuffer with shape {2, 3}:
 int[] shape = new int[] {2, 3};
 TensorBuffer tensorBuffer = TensorBuffer.createFixedSize(shape, DataType.FLOAT32);
 
 // Creating an uint8 TensorBuffer of a scalar:
 int[] shape = new int[] {};
 TensorBuffer tensorBuffer = TensorBuffer.createFixedSize(shape, DataType.UINT8);
 
 // Creating an empty uint8 TensorBuffer:
 int[] shape = new int[] {0};
 TensorBuffer tensorBuffer = TensorBuffer.createFixedSize(shape, DataType.UINT8);
 

固定大小的 TensorBuffer 一旦创建,其大小便无法更改。

参数
形状 要创建的 TensorBuffer 的形状。
dataType 要创建的 TensorBuffer 的数据类型。
抛出
NullPointerException 如果 shape 为 null,则会发生该错误。
IllegalArgumentException 如果 shape 包含非正元素,则返回此值。

<ph type="x-smartling-placeholder"></ph> 公开 静态 TensorBuffer createFrom TensorBuffer 缓冲区,DataType 数据类型)

使用指定的 DataType 创建来自其他数据的 TensorBuffer 深层复制。

参数
缓存空间 要从中复制数据的源 TensorBuffer
dataType 新创建的 TensorBuffer 的预期 DataType
抛出
NullPointerException 如果 buffer 为 null,则会发生该错误。

<ph type="x-smartling-placeholder"></ph> 公开 ByteBuffer getBuffer ()

返回数据缓冲区。

<ph type="x-smartling-placeholder"></ph> 公开 抽象 DataType getDataType ()

返回此缓冲区的数据类型。

<ph type="x-smartling-placeholder"></ph> 公开 整数 getFlatSize ()

获取缓冲区的 FlatSize。

抛出
IllegalStateException 如果底层数据已损坏

<ph type="x-smartling-placeholder"></ph> 公开 抽象 float[] getFloatArray ()

返回存储在此缓冲区中的值的浮点数组。如果缓冲区的类型不同 而不是浮点数,这些值将转换为浮点数。例如,TensorBufferUint8 中的值将从 uint8 转换为 float。

<ph type="x-smartling-placeholder"></ph> 公开 抽象 浮点数 getFloatValue (int absIndex)

返回指定索引处的浮点值。如果缓冲区的类型与浮点数不同,则 值将转换为浮点数。例如,从 TensorBufferUint8 中读取值时,系统首先将该值读取为 uint8,然后从 uint8 转换为 float。

 For example, a TensorBuffer with shape {2, 3} that represents the following array,
 [[0.0f, 1.0f, 2.0f], [3.0f, 4.0f, 5.0f]].

 The fourth element (whose value is 3.0f) in the TensorBuffer can be retrieved by:
 float v = tensorBuffer.getFloatValue(3);
 

参数
absIndex 要读取的值的绝对索引。

<ph type="x-smartling-placeholder"></ph> 公开 抽象 整数 [] getIntArray ()

返回存储在此缓冲区中的值的整型数组。如果缓冲区的类型 而不是 int,这些值将转换为 int,并且可能会损失精度。例如: 从 TensorBufferFloat 获取一个值为 {400.32f, 23.04f} 的 int 数组,输出 为 {400, 23}。

<ph type="x-smartling-placeholder"></ph> 公开 抽象 整数 getIntValue (int absIndex)

返回指定索引处的整数值。如果缓冲区的类型与 int 类型不同, 将转换为整数。例如,从 TensorBufferFloat 读取值时, 系统会先将值读取为 float,然后再从 float 转换为 int。损失 可能适用。

 For example, a TensorBuffer with shape {2, 3} that represents the following array,
 [[0.0f, 1.0f, 2.0f], [3.0f, 4.0f, 5.0f]].

 The fourth element (whose value is 3.0f) in the TensorBuffer can be retrieved by:
 int v = tensorBuffer.getIntValue(3);
 Note that v is converted from 3.0f to 3 as a result of type conversion.
 

参数
absIndex 要读取的值的绝对索引。

<ph type="x-smartling-placeholder"></ph> 公开 整数 [] getShape ()

获取当前形状。(在此处返回副本以避免意外修改。)

抛出
IllegalStateException 如果底层数据已损坏

<ph type="x-smartling-placeholder"></ph> 公开 抽象 整数 getTypeSize ()

返回数组中单个元素的字节数。例如,浮动缓冲区将 返回 4,而字节缓冲区会返回 1。

<ph type="x-smartling-placeholder"></ph> 公开 布尔值 isDynamic ()

如果 TensorBuffer 是动态大小(可以随意调整大小),则返回 。

<ph type="x-smartling-placeholder"></ph> 公开 抽象 无效 loadArray (int[] src, int[] 形状)

将一个整数数组加载到具有特定形状的该缓冲区。如果缓冲区的类型不同 比 int 多,这些值将转换为缓冲区的类型,然后再加载到 缓冲区,还可能会损失精度。例如,加载值为 {400, -23} 转换为 TensorBufferUint8,则这些值将被限制为 [0, 255],然后被限制为 已由 {255, 0} 转换为 uint8。

参数
src 要加载的源数组。
形状 src 表示的张量的形状。
抛出
NullPointerException 如果 src 为 null,则会发生该错误。
NullPointerException 如果 shape 为 null,则会发生该错误。
IllegalArgumentException 要加载的数组的大小与 指定形状。

<ph type="x-smartling-placeholder"></ph> 公开 抽象 无效 loadArray (float[] src, int[] 形状)

将一个具有特定形状的浮点数数组加载到此缓冲区。如果缓冲区的类型不同 而不是浮点数,这些值会在加载到 缓冲区,还可能会损失精度。例如,将浮点数组加载到值为 {400.32f, -23.04f} 的 TensorBufferUint8 中,这些值将被限制为 [0, 255], 然后由 {255, 0} 转换为 uint8。

参数
src 要加载的源数组。
形状 src 表示的张量的形状。
抛出
NullPointerException 如果 src 为 null,则会发生该错误。
NullPointerException 如果 shape 为 null,则会发生该错误。
IllegalArgumentException 要加载的数组的大小与 指定形状。

<ph type="x-smartling-placeholder"></ph> 公开 无效 loadArray (float[] src)

将浮点数组加载到此缓冲区中。如果缓冲区的类型与浮点数不同,则 值在加载到缓冲区之前转换为缓冲区的类型,而损失 可能适用。例如,将浮点数数组加载到 TensorBufferUint8 中 如果值为 {400.32f, -23.04f},则这些值会限定为 [0, 255],然后强制转换为 uint8 by {255, 0}。

使用此方法假设 src 的形状与它的形状相同 TensorBuffer。因此,buffer (src.length) 的大小应始终匹配 此 TensorBuffer 的平面尺寸(适用于固定尺寸和动态 TensorBuffer)。如果 src 具有不同的形状,请使用 loadArray(float[], int[])

参数
src 要加载的源数组。

<ph type="x-smartling-placeholder"></ph> 公开 无效 loadArray (int[] src)

将一个整型数组加载到此缓冲区中。如果缓冲区的类型与 int 类型不同, 在加载到缓冲区之前,将转换为该缓冲区的类型,而损失 可能适用。例如,将值为 {400, -23} 的 int 数组加载到 TensorBufferUint8 中,这些值将被限制为 [0, 255],然后通过 {255, 0}。

使用此方法假设 src 的形状与它的形状相同 TensorBuffer。因此,buffer (src.length) 的大小应始终匹配 此 TensorBuffer 的平面尺寸(适用于固定尺寸和动态 TensorBuffer)。如果 src 具有不同的形状,请使用 loadArray(int[], int[])

参数
src 要加载的源数组。

<ph type="x-smartling-placeholder"></ph> 公开 无效 loadBuffer ByteBuffer 缓冲区)

将一个字节缓冲区加载到此 TensorBuffer 中。缓冲区空间必须与 本TensorBuffer

使用此方法假设 buffer 的形状与它的形状相同 TensorBuffer。因此,buffer (buffer.limit()) 的大小应始终 对于固定尺寸和动态 TensorBuffer,与此 TensorBuffer 的平面尺寸匹配。如果 buffer 有不同的值,请使用 loadBuffer(ByteBuffer, int[]) 形状。

重要提示 :已加载的缓冲区是一个引用。请勿修改。我们不会在此处创建以下内容的副本: 性能方面的顾虑,但如果需要进行修改,请制作副本。

为了获得最佳性能,请务必加载直接 ByteBufferByteBuffer 由数组提供支持。

如果 buffer 是只读的,我们会采用写入时复制策略来提高性能。

参数
缓存空间 要加载的字节缓冲区。

<ph type="x-smartling-placeholder"></ph> 公开 无效 loadBuffer (ByteBuffer 缓冲区,int[] 形状)

将一个字节缓冲区加载到此 TensorBuffer 中,且具有特定形状。

重要提示 :已加载的缓冲区是一个引用。请勿修改。我们不会在此处创建以下内容的副本: 性能方面的顾虑,但如果需要进行修改,请制作副本。

为了获得最佳性能,请务必加载直接 ByteBufferByteBuffer 由数组提供支持。

参数
缓存空间 要加载的字节缓冲区。
形状
抛出
NullPointerException 如果 buffer 为 null,则会发生该错误。
IllegalArgumentException 如果 buffertypeSize 的大小 或者 bufferflatSize 的大小不匹配。