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


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

Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。 tf.zeroslIke()用於通過傳遞參數值來創建所有元素都設置為‘0’且形狀與給定張量相同的tf.tensor。

用法:

tf.zerosLike(value)

參數:它接受如上所述和以下描述的單個參數:

  • value:它是張量的值,它可以是數字的簡單或嵌套Array或TypedArray。我們在此處通過所需形狀的張量。

返回值:它返回所需形狀的張量。

注意:上述函數不會更改原始張量。



範例1:在此示例中,我們使用tf.tensor的tf.zeroslike()方法。

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor([1, 2, 3, 4, 5, 6, 7]);
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)
Tensor
    [0, 0, 0, 0, 0, 0, 0]
Original tensor:
Tensor
    [1, 2, 3, 4, 5, 6, 7]

範例2:在此示例中,我們使用tf.tensor1d()方法創建張量並應用tf.zerosLike方法。

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor1d([1, 2, 3]);
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)
Tensor
    [0, 0, 0]
Original tensor:
Tensor
    [1, 2, 3]

範例3:在此示例中,我們使用tf.tensfor2d()方法創建張量並應用tf.zerosLike方法。

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor2d([[1, 2], [3, 4]]);
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)
​Tensor
    [[0, 0],
     [0, 0]]
Original tensor:
Tensor
    [[1, 2],
     [3, 4]]

範例4:在此示例中,我們將使用tensor3d()方法創建張量並應用tf.zerosLike()方法。

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor3d([[[1], [2]], [[3], [4]]]);
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)
Tensor
    [[[0],
      [0]],

     [[0],
      [0]]]
Original tensor:
Tensor
    [[[1],
      [2]],

     [[3],
      [4]]]

範例5:在此示例中,我們使用tensor4d()方法創建張量並應用tf.zerosLike()方法。

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating the tensor
var val = tf.tensor4d([[[[1], [2]], [[3], [4]]]])
  
//using tf.zeroslike() and printing the tensor
tf.zerosLike(val).print()
  
// Printing the tensor
tf.print("Original tensor:\n"+val)
Tensor
    [[[[0],
       [0]],

      [[0],
       [0]]]]
Original tensor:
Tensor
    [[[[1],
       [2]],

      [[3],
       [4]]]]

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

相關用法


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