TensorLabel

公開課程 TensorLabel

TensorLabel 是 TensorBuffers 的 util 包裝函式,在軸上顯示有意義的標籤。

舉例來說,圖片分類模型的輸出張量可能為 {1, 10}、 其中 1 是批量,10 代表類別數量。事實上,根據第 2 軸 以標示每個子張量,並標示對應類別的名稱或說明。TensorLabel 可協助將 TensorBuffer 中的純 Tensor 從 將預先定義的標籤提供給子張量在本例中,如果為第二軸提供 10 個標籤,TensorLabel 即可將原始 {1, 10} Tensor 轉換為 10 元素對應, 是 Tensor {} (純量)。使用範例:

   TensorBuffer outputTensor = ...;
   List<String> labels = FileUtil.loadLabels(context, labelFilePath);
   // labels the first axis with size greater than one
   TensorLabel labeled = new TensorLabel(labels, outputTensor);
   // If each sub-tensor has effectively size 1, we can directly get a float value
   Map<String, Float> probabilities = labeled.getMapWithFloatValue();
   // Or get sub-tensors, when each sub-tensor has elements more than 1
   Map<String, TensorBuffer> subTensors = labeled.getMapWithTensorBuffer();
 

注意:針對第一個大小較大的標籤,我們目前只支援張量對地圖的轉換。 大於 1。

公用建構函式

TensorLabel(Map<IntegerList<String>> axisLabels, TensorBuffer tensorBuffer)
建立 TensorLabel 物件,以在多維度張量軸上加上標籤。
TensorLabel(List<String> axisLabels, TensorBuffer tensorBuffer)
建立可在多維度張量的軸上加上標籤的 TensorLabel 物件。

公用方法

清單<類別>
getCategoryList()
TensorLabel 物件取得 Category 清單。
Map<StringFloat>
getMapWithFloatValue()
取得將標籤對應至浮動的地圖。
Map<StringTensorBuffer>
getMapWithTensorBuffer()
取得包含一對標籤和對應 TensorBuffer 的地圖。

繼承的方法

公用建構函式

公開 TensorLabel (地圖<整數, List<String>> axisLabels、TensorBuffer tensorBuffer)

建立 TensorLabel 物件,以在多維度張量軸上加上標籤。

參數
axisLabels 對應,鍵為軸 ID (從 0 開始),且值是對應 標籤注意:標籤大小應與該軸上的張量相同。
tensorBuffer 要加上標籤的 TensorBuffer。
擲回
NullPointerException 如果 axisLabelstensorBuffer 為空值,或者是 axisLabels 中的值為空值。
IllegalArgumentException 如果 axisLabels 中的任何索引鍵超出範圍 (相較於 tensorBuffer 的形狀,或任何值 (標籤) 在指定維度上使用 tensorBuffer 的大小不同。

公開 TensorLabel (List<String> axisLabels,TensorBuffer tensorBuffer)

建立可在多維度張量的軸上加上標籤的 TensorLabel 物件。

注意:標籤會套用至大小大於 1 的第一軸。舉例來說 張量的形狀是 [1, 10, 3],系統會在 1 軸上套用標籤 (ID 從 0),axisLabels 的大小也應為 10。

參數
axisLabels 標籤清單,其大小應與 Tensor 大小相同 對應至要加上標籤的軸
tensorBuffer 要加上標籤的 TensorBuffer。

公用方法

公開 清單<類別> getCategoryList ()

TensorLabel 物件取得 Category 清單。

標籤軸應有效指出最後一個軸 (也就是每個子張量) 這個軸指定的扁平大小應為 1,因此每個加上標籤的子張量 轉換成浮點值分數範例:TensorLabel 形狀為 {2, 5, 3} 且軸 2 有效。如果軸為 1 或 0,則無法轉換為 Category

getMapWithFloatValue() 是替代方案,但會以下列形式傳回 Map 結果。

擲回
IllegalStateException 表示每個標籤上的子張量大小不是 1。

公開 Map<StringFloat> getMapWithFloatValue ()

取得將標籤對應至浮動的地圖。只允許繪製大小較高的第一軸 大於 1,且軸應有效為最後一個軸 (意即每個子張量) 根據這個軸指定的固定大小應為 1)。

getCategoryList() 是用來取得結果的替代 API。

擲回
IllegalStateException 表示每個標籤上的子張量大小不是 1。

公開 Map<StringTensorBuffer> getMapWithTensorBuffer ()

取得包含一對標籤和對應 TensorBuffer 的地圖。只允許 大於目前寬度大於 1 的軸對應。