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


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

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

tf.square()函數用於返回指定張量元素的平方。

用法:

tf.square(x)

參數:此函數接受如下所示的參數:

  • x:指定的輸入張量。

返回值:它返回指定張量元素的平方。



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Initializing a tensor of some elements
const x = tf.tensor1d([2, 3, 4, 5]);
  
// Calling the .square() function over
// the above tensor as its parameter
x.square().print();

輸出:

Tensor
   [4, 9, 16, 25]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Using a tensor of some elements
// as the parameter for the .square() function
tf.tensor1d([0, 1, -2, 1.7, -2.5]).square().print();

輸出:

Tensor
   [0, 1, 4, 2.8900001, 6.25]

相關用法


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