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


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

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

.scatterND()函數用於根據聲明的索引,通過對聲明的形狀張量的零張量內的單個切片或值進行分散更新,來形成不同的張量。此外,此函數是對tf.gatherND()函數的取反,該函數從指定的張量中獲取切片或值。

用法:

tf.scatterND(indices, updates, shape)

Parameters: 

  • indices:指定的張量保存指向輸出張量的索引,並且可以是tf.Tensor,TypedArray或Array類型。
  • updates:它是聲明的張量,用於保存索引的值,並且可以是tf.Tensor,TypedArray或Array類型。
  • shape:它是輸出張量的規定形狀,類型為number []。

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



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining indices, updates and shape
const ind = tf.tensor2d([6, 5, 2], [3, 1], 'int32');
const updat = tf.tensor1d([1, 2, 3]);
const shp = [6];
  
// Calling tf.scatterND() method and
// Printing output
tf.scatterND(ind, updat, shp).print();

輸出:

Tensor
    [0, 0, 3, 0, 0, 2]

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling tf.scatterND() method and
// Printing output
tf.scatterND(tf.tensor2d([5.4, 2.4], [2, 1], 'int32'), 
            tf.tensor1d([1.8, 4.2]), 
            [4]).print();

輸出:

Tensor
    [0, 0, 4.1999998, 0]

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

相關用法


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