已知的直接子類別 |
代表模型輸入或輸出內容的資料緩衝區。
公用方法
靜態 TensorBuffer | |
靜態 TensorBuffer | |
靜態 TensorBuffer | |
ByteBuffer |
getBuffer()
傳回資料緩衝區。
|
抽象 DataType |
getDataType()
傳回這個緩衝區的資料類型。
|
攔截 |
getFlatSize()
取得緩衝區的 smallSize。
|
抽象 float[] |
getFloatArray()
傳回儲存在這個緩衝區中的值的浮點陣列。
|
抽象 浮點值 |
getFloatValue(int absIndex)
傳回指定索引的浮點值。
|
抽象 整數 [] |
getIntArray()
傳回儲存在這個緩衝區中的值的 int 陣列。
|
抽象 攔截 |
getIntValue(int absIndex)
傳回指定索引的 int 值。
|
整數 [] |
getShape()
取得目前的形狀。
|
抽象 攔截 |
getTypeSize()
傳回陣列中單一元素的位元組數。
|
布林值 |
isDynamic()
如果
TensorBuffer 是動態大小,則傳回 (可任意調整大小)。 |
抽象 void |
loadArray(int[] src, int[] Shape)
將特定形狀的 int 陣列載入這個緩衝區。
|
抽象 void |
loadArray(float[] src, int[] Shape)
將浮點陣列載入特定形狀的緩衝區。
|
void |
loadArray(float[] src)
將浮點陣列載入這個緩衝區。
|
void |
loadArray(int[] src)
將 int 陣列載入這個緩衝區。
|
void | |
void |
繼承的方法
公用方法
公開 靜態 TensorBuffer createDynamic (DataType資料類型)
建立具有指定 DataType
的空白動態 TensorBuffer
。形狀的形狀
已建立「TensorBuffer
」{0}。
載入陣列或資料緩衝區時,動態 TensorBuffers 會重新分配記憶體 緩衝區大小不同以下列舉幾個例子:
// 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 的資料類型。
|
---|
公開 靜態 TensorBuffer createFixedSize (int[] 形狀,DataType資料類型)
使用指定的 shape
和 DataType
建立 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 為空值。 |
---|---|
IllegalArgumentException | 如果 shape 含有非正數元素。
|
公開 靜態 TensorBuffer createFrom (TensorBuffer 緩衝區、DataType資料類型)
使用指定的 DataType
從其他來源建立 TensorBuffer
深複製資料。
參數
緩衝區 | 要複製的來源 TensorBuffer 。 |
---|---|
dataType | 新建立的「TensorBuffer 」預期有 DataType |
擲回
NullPointerException | 如果 buffer 為空值。
|
---|
公開 抽象 float[] getFloatArray ()
傳回儲存在這個緩衝區中的值的浮點陣列。如果緩衝區屬於不同型別
值就會轉換為浮點值例如,TensorBufferUint8
中的值會從 uint8 轉換為 float。
公開 抽象 浮點值 getFloatValue (int absIndex)
傳回指定索引的浮點值。如果緩衝區的型別與浮點值不同,
值會轉換為浮點值。例如,讀取 TensorBufferUint8
中的值時,該值會先顯示為 uint8,然後再從
使用 uint8 來浮動值。
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 | 待讀取值的絕對索引。 |
---|
公開 抽象 整數 [] getIntArray ()
傳回儲存在這個緩衝區中的值的 int 陣列。如果緩衝區類型不同
系統會將值轉換為 int,而精確度可能會降低。例如:
從 TensorBufferFloat
取得值為 {400.32f, 23.04f} 的 int 陣列,輸出結果
為 {400, 23}。
公開 抽象 攔截 getIntValue (int absIndex)
傳回指定索引的 int 值。如果緩衝區的型別與 int 不同,則值
會轉換為 int例如,在讀取 TensorBufferFloat
中的值時
這些值會先以浮點值的形式顯示,接著才會從浮點值轉換為 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 | 待讀取值的絕對索引。 |
---|
公開 抽象 攔截 getTypeSize ()
傳回陣列中單一元素的位元組數。舉例來說,浮動緩衝區會 會傳回 4,而位元組緩衝區會傳回 1。
公開 抽象 void loadArray (int[] src, int[] 形狀)
將特定形狀的 int 陣列載入這個緩衝區。如果緩衝區屬於不同型別
大於 int,值會先轉換為緩衝區的型別,才載入到
但精確度可能會降低舉例來說,載入值為 {400、
-23} 轉換為 TensorBufferUint8
,其值會限制為 [0, 255],然後
由 {255, 0} 投放到 uint8。
參數
src | 要載入的來源陣列。 |
---|---|
形狀 | src 代表的張量形狀。 |
擲回
NullPointerException | 如果 src 為空值。 |
---|---|
NullPointerException | 如果 shape 為空值。 |
IllegalArgumentException | 如果要載入的陣列大小與 指定的形狀。 |
公開 抽象 void loadArray (float[] src, int[] 形狀)
將浮點陣列載入特定形狀的緩衝區。如果緩衝區屬於不同型別
的值會先轉換為緩衝區的型別,才載入到
但精確度可能會降低舉例來說,將浮點陣列載入 TensorBufferUint8
,並將值設為 {400.32f, -23.04f},並將值限制為 [0, 255],
然後由 {255, 0} 投放到 uint8。
參數
src | 要載入的來源陣列。 |
---|---|
形狀 | src 代表的張量形狀。 |
擲回
NullPointerException | 如果 src 為空值。 |
---|---|
NullPointerException | 如果 shape 為空值。 |
IllegalArgumentException | 如果要載入的陣列大小與 指定的形狀。 |
公開 void loadArray (浮點值 [] src)
將浮點陣列載入這個緩衝區。如果緩衝區的型別與浮點值不同,
值會在載入緩衝區前轉換為緩衝區類型,並遺失
但可能有精確的精確度例如,將浮點陣列載入 TensorBufferUint8
值為 {400.32f, -23.04f},系統會將這些值調整為 [0, 255],並轉換為
uint8 by {255, 0}。
使用這個方法時,系統會假設 src
的形狀與這個形狀相同
TensorBuffer
。因此,buffer
(src.length
) 的大小應一律相符
此 TensorBuffer
的固定大小,適用於固定大小和動態 TensorBuffer
。如果 src
不同形狀,請使用 loadArray(float[], int[])
。
參數
src | 要載入的來源陣列。 |
---|
公開 void loadArray (int[] src)
將 int 陣列載入這個緩衝區。如果緩衝區的型別與 int 不同,則值會顯示
會轉換為緩衝區的型別,才載入緩衝區中,
但精確度可能會更高。舉例來說,將值為 {400, -23} 的 int 陣列載入 TensorBufferUint8
中,系統會將這些值調整為 [0, 255],並轉換為 uint8,方法是:
{255, 0}。
使用這個方法時,系統會假設 src
的形狀與這個形狀相同
TensorBuffer
。因此,buffer
(src.length
) 的大小應一律相符
此 TensorBuffer
的固定大小,適用於固定大小和動態 TensorBuffer
。如果 src
不同形狀,請使用 loadArray(int[], int[])
。
參數
src | 要載入的來源陣列。 |
---|
公開 void loadBuffer (ByteBuffer 緩衝區)
將位元組緩衝區載入這個 TensorBuffer
。緩衝區空間必須與
此TensorBuffer
。
使用這個方法時,系統會假設 buffer
的形狀與這個形狀相同
TensorBuffer
。因此,buffer
(buffer.limit()
) 的大小應一律為
符合這個 TensorBuffer
的固定大小,適用於固定大小和動態 TensorBuffer
。如果 buffer
不同,請使用 loadBuffer(ByteBuffer, int[])
形狀。
重要事項:載入的緩衝區為參照。請勿修改。我們不會在這裡建立 但如果需要修改,請建立副本
為獲得最佳效能,請一律載入直接的 ByteBuffer
或 ByteBuffer
陣列。
如果 buffer
為唯讀性質,我們會採用複製/寫入策略來提高效能。
參數
緩衝區 | 要載入的位元組緩衝區。 |
---|
公開 void loadBuffer (ByteBuffer 緩衝區, int[] 形狀)
將位元組緩衝區載入特定形狀的 TensorBuffer
。
重要事項:載入的緩衝區為參照。請勿修改。我們不會在這裡建立 但如果需要修改,請建立副本
為獲得最佳效能,請一律載入直接的 ByteBuffer
或 ByteBuffer
陣列。
參數
緩衝區 | 要載入的位元組緩衝區。 |
---|---|
形狀 |
擲回
NullPointerException | 如果 buffer 為空值。 |
---|---|
IllegalArgumentException | 表示 buffer 和 typeSize 的大小
或 buffer 和 flatSize 的大小不相符。
|