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


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

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

tf.zeros()函數用於創建所有元素都設置為零的新張量。

用法:

tf.zeros(shape, dataType)

參數:

  • shape:它采用我們將要生成的張量的形狀。
  • dataType:它是結果元素中張量的類型。它可以是“ float32”,“ int32”或‘bool’。默認為“float32”值。它是一個可選參數。

返回值:它返回給定形狀的張量,所有元素都設置為零。



範例1:在此示例中,我們使用tf.zeros()創建形狀為1 * 1的張量。

Javascript


// Importing the tensorflow.Js lbrary
import * as tf from "@tensorflow/tfjs"
  
// Creating a shape variable
// which stores the shape
var shape = [1,1]
  
// Creating the tensor
var val = tf.zeros(shape)
  
// Printing the tensor
val.print()

輸出:

Tensor
     [[0],]

範例2:在此示例中,我們使用tf.zeros()和dataType參數創建形狀為2 * 2的張量。

Javascript


// Importing the tensorflow.Js lbrary
import * as tf from "@tensorflow/tfjs"
  
// Creating a shape variable
// which stores the shape
var shape = [2, 2]
  
// Creating a d_Type variable
// which strores the data-type
var d_Type = 'bool'
  
// Creating the tensor
var val = tf.zeros(shape, d_Type)
  
// Printing the tensor
val.print()

輸出:

Tensor
    [[false, false],
     [false, false]]

範例3:在此示例中,我們使用tf.zeros()創建形狀為1 * 1的張量。

Javascript


// Importing the tensorflow.Js lbrary
import * as tf from "@tensorflow/tfjs"
  
// Creating a shape variable
// which stores the shape
var shape = [3,3]
  
// Creating the tensor
var val = tf.zeros(shape)
  
// Printing the tensor
val.print()

輸出:

Tensor
    [[0, 0, 0],
     [0, 0, 0],
     [0, 0, 0]]

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

相關用法


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