Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。
tf.TensorBuffer 類 .set() 函數用於在緩衝區中的指定位置設置給定值。
用法:
set (value, ...locations)
參數:此函數接受兩個參數,如下所示:
- value:要設置的指定值。
- locations:指定的位置索引。
返回值:它不返回任何值。
範例1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a buffer of 2*2 dimensions
const buffer = tf.buffer([2, 2]);
// Setting values at particular indices.
buffer.set(5, 0, 0);
buffer.set(10, 1, 0);
// Converting the above buffer
// back to a tensor value to print
buffer.toTensor().print();
輸出:
Tensor [[5 , 0], [10, 0]]
範例2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a buffer of 3*3 dimensions
const buffer = tf.buffer([3, 3]);
// Setting values at particular indices.
buffer.set(5, 0, 0);
buffer.set(10, 0, 1);
buffer.set(15, 1, 0);
buffer.set(20, 1, 1);
buffer.set(25, 2, 0);
buffer.set(30, 2, 1);
buffer.set(35, 2, 2);
// Converting the above buffer
// back to a tensor value to print
buffer.toTensor().print()
輸出:
Tensor [[5 , 10, 0 ], [15, 20, 0 ], [25, 30, 35]]
參考:https://js.tensorflow.org/api/latest/#tf.TensorBuffer.set
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Tensorflow.js tf.TensorBuffer Class .set() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。