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


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

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

.squeeze() 函數用於從指定的 tf.Tensor 的形狀中丟棄長度為 1 的維度。

用法:

tf.squeeze(x, axis?)

Parameters: 

  • x:它是要壓縮的指定張量輸入,它可以是 tf.Tensor、TypedArray 或 Array 類型。
  • axis:它是一個包含數字列表的可選參數。在指定的情況下,它僅擠壓規定的尺寸。而且,這裏的維度索引從零開始,壓縮長度不是一的維度是一個缺陷。

返回值:它返回tf.Tensor對象。



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input elements
const y = tf.tensor([11, 76, -4, 6], [2, 2, 1]);
  
// Calling squeeze() method and
// Printing output
y.squeeze().print();

輸出:

Tensor
    [[11, 76],
     [-4, 6 ]]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling squeeze() method with
// all its parameter
var res = tf.squeeze(tf.tensor(
    [2.1, 5.6, 8.6, 7.6], 
    [4, 1]), [1]
);
  
// Printing output
res.print();

輸出:

Tensor
    [2.0999999, 5.5999999, 8.6000004, 7.5999999]

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

相關用法


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