TensorLabel

公開類別 TensorLabel

TensorLabel 是 TensorBuffers 適用的公用程式,在軸上具有有意義的標籤。

舉例來說,圖片分類模型的輸出張量可能為 {1, 10},其中 1 是批量,10 是類別數量。事實上,在第二軸上,我們可以為每個子張量加上標籤,分別加上每個對應類別的名稱或說明。TensorLabel 可協助將 TensorBuffer 中的純 Tensor 轉換為從預先定義標籤到子張量的對應關係。在本例中,如果第 2 軸提供 10 個標籤,TensorLabel 可以將原本的 {1, 10} Tensor 轉換為 10 元素對應,亦即每個值都是 Tensor 中的 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<Integer, List<String>> axisLabels、TensorBuffer tensorBuffer)
建立可於多維度張量軸上加上標籤的 TensorLabel 物件。
TensorLabel(List<String> axisLabels、TensorBuffer tensorBuffer)
建立可加上標籤的多維度張量標籤的 TensorLabel 物件。

公用方法

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

繼承的方法

公用建構函式

public TensorLabel (Map<IntegerList<String>> axisLabels, TensorBuffer tensorBuffer)

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

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

public TensorLabel (List<String> axisLabels、TensorBuffer tensorBuffer)

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

注意:標籤會套用至大小大於 1 的第一軸。舉例來說,如果張量的形狀為 [1, 10, 3],則標籤將套用至 1 軸 (ID 從 0 開始),而 axisLabels 的大小也應為 10。

參數
axisLabels 標籤清單,其大小應與待加標籤軸上的張量大小相同。
tensorBuffer 要加上標籤的 TensorBuffer。

公用方法

public List<Category> getCategoryList ()

TensorLabel 物件取得 Category 清單。

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

getMapWithFloatValue() 是替代方法,但傳回 Map 做為結果。

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

public Map<StringFloat> getMapWithFloatValue ()

取得將標籤對應到浮點的地圖。只允許第一軸的對應值大於 1,而且最後軸應有效 (也就是說,此軸指定的每個子張量應有 1 的固定尺寸)。

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

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

public Map<StringTensorBuffer> getMapWithTensorBuffer ()

取得含有一對標籤和對應 TensorBuffer 的地圖。目前只能對應第一軸大小大於 1 的位置。