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


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

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

tf.onesLike()函數用於創建一個tf.Tensor,其中所有元素都設置為1,並且形狀與給定張量相同。

用法:

tf.onesLike (x)

參數:

  • x:一個tf.Tensor,其所有元素都將設置為1。

返回值:它返回一個tf.Tensor。



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
const x = tf.tensor([1, 2]);
  
// Printing the tensor after setting the value of tensor to 1
tf.onesLike(x).print();

輸出:

Tensor
   [1, 1]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
const x = tf.tensor2d([8, 2, 5, 6], [2, 2]);
  
// Printing the tensor after setting the value of tensor to 1
tf.onesLike(x).print();

輸出:

Tensor
   [[1, 1],
    [1, 1]]

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

相關用法


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