當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Tensorflow.js tf.setdiff1dAsync()用法及代碼示例

Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。

tf.setdiff1dAsync()函數用於查找兩個指定數字列表之間的差異。

假設給定了兩個Tensor “a”和“b”,那麽此函數將返回一個Tensor out,它表示存在於“a”中的所有值,而不代表存在於“b”中的所有值。返回的張量輸出按照表示“a”的相同順序進行排序。此函數還返回存在於Tensor中的數字的索引的Tensor。

用法:

tf.setdiff1dAsync (a, b)

參數:此函數接受兩個參數,如下所示:



  • a:它是要保留的值的一維張量。
  • b:它是要排除在輸出中的值的一維張量。它應該具有與“a”相同的數據類型。

範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing two Tensors
const a = [10, 20, 30, 40, 50, 60];
const b = [20, 40, 50];
  
// Calling the .setdiff1dAsync() function over
// the above two specified Tesnors as parameters
// and returns the Tensor out and indices for
// the same
const [out, indices] = await tf.setdiff1dAsync(a, b);
  
// Getting the Tensor of values present in 
// Tensor "a" but not in "b"
out.print();
  
// Getting the Tensor of indices for the 
// returned Tensor's values
indices.print();

輸出:

Tensor
   [10, 30, 60]
Tensor
   [0, 2, 5]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Using two Tensors as the parameters for the 
// .setdiff1dAsync() function and
// returns the Tensor out and indices
const [out, indices] = await 
  tf.setdiff1dAsync([0.1, 0, 7.0, 7.1], [.1, 7]);
  
// Getting the Tensor of values present in 
// Tensor first Tensor parameter
// but not in second one
out.print();
  
// Getting the Tensor of indices for the 
// returned Tensor's values
indices.print();

輸出:

Tensor
   [0, 7.0999999]
Tensor
   [1, 3]

參考: https://js.tensorflow.org/api/latest/#setdiff1dAsync

相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Tensorflow.js tf.setdiff1dAsync() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。