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


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

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

.booleanMaskAsync() 函數用於實現指定輸入張量的布爾掩碼。

用法:

tf.booleanMaskAsync(tensor, mask, axis?)

Parameters: 

  • tensor:它是規定的 N-D 張量,它可以是 tf.Tensor、TypedArray 或 Array 類型。
  • mask:它是規定的 K-D 布爾張量。其中,K <= N 加 K 應該是非主動識別的。它可以是 tf.Tensor、TypedArray 或 Array 類型。
  • axis:它是類型為 number 的可選參數。它是一個 0-D 整數類型的張量,表示要屏蔽的所述張量中的軸。在這裏,默認值是零,它屏蔽了第一個尺寸,否則 (K + 軸 <= N)。

返回值:它返回 Promise(tf.Tensor)。



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input and mask
const tn = tf.tensor2d(
    [7, 8, 9, 10, 11, 12, 5, 62], [4, 2]);
const msk = tf.tensor1d([2, 1, 2, 3], 'bool');
  
// Calling tf.booleanMaskAsync() method and
// Printing output
const res = await tf.booleanMaskAsync(tn, msk);
res.print();

輸出:

Tensor
    [[7 , 8 ],
     [9 , 10],
     [11, 12],
     [5 , 62]]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling tf.booleanMaskAsync() method and
// Printing output
const res = await tf.booleanMaskAsync(
    tf.tensor2d([34, 17, 7, 8], [2, 2]), 
    tf.tensor1d([1, 1], 'bool'), 1);
      
res.print();

輸出:

Tensor
    [[34, 17],
     [7 , 8 ]]

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

相關用法


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