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


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

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

.broadcastTo() 函數用於將數組循環到 NumPy-style 的一致模型。

注意:

  • 這裏,張量的形狀等同於廣播從最後到開始的形狀。其中,1 是張量形狀的前綴,隻要它具有與廣播形狀相同的長度即可。
  • 如果 input.shape[i]==shape[i],那麽第 (i+1) 軸以前與廣播一致,如果 input.shape[i]==1 加上 shape[i] ==N,那麽指定的輸入張量被該軸覆蓋 N 次。

用法:

tf.broadcastTo(x, shape)


Parameters: 

  • x:它是指定的張量輸入,可以是 tf.Tensor、TypedArray 或 Array 類型。
  • shape:它是輸入要廣播到的指定形狀。

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

範例1:

Javascript


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

輸出:

Tensor
     [[1, 2, 3, 4],]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling broadcastTo() method and
// Printing output
tf.broadcastTo(tf.tensor1d([3.6, 5.8, 3.7, 1.4, 9.3, 10.5]), 
                           [1, 6]).print();

輸出:

Tensor
     [[3.5999999, 5.8000002, 3.7, 1.4, 9.3000002, 10.5],]

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

相關用法


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