Tensorflow.js是由Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。
.tidy() 函數用於執行給定的函數,即 fn,一旦它終止,它就會清除由所述函數 fn 分配的所有等距張量,但不包括 fn 返回的那些。在這裏, fn 不應產生承諾。但是,返回的輸出可能是一個複雜的對象。
注意:
- 這種方法有利於防止內存泄漏。通常,在 tf.tidy() 函數中包裝對進程的調用以自動清理內存。
- 但是,在 tidy() 函數中不會清除這些變量。如果要處理變量,則可以使用 tf.disposeVariables() 或立即對變量調用 dispose() 方法。
用法:
tf.tidy(nameOrFn, fn?)
Parameters:
- nameOrFn:停止器的指定名稱,或者要執行的函數。如果給出了一個名稱,那麽第二個參數必須是一個函數。如果調試模式打開,那麽調度以及所述函數的內存利用率將被跟蹤並使用給定的名稱顯示在控製台上。它可以是字符串或函數類型。
- fn:要執行的規定函數。它是可選的,屬於函數類型。
返回值:它返回 void、number、string、TypedArray、tf.Tensor、tf.Tensor[] 或 {[key:string]:tf.Tensor、number 或 string}。
範例1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling tidy() method
const res = tf.tidy(() => {
// Calling scalar() method
const x = tf.scalar(3);
// Calling sqrt() function
const y = tf.sqrt(5);
// Calling square() method
const z = y.square();
// Calling sub() method
return z.sub(x);
});
// Printing output
res.print();
輸出:
Tensor 2
範例2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling tidy() method
const res = tf.tidy(() => {
// Calling sin() method
const op = tf.sin(45);
// Printing number of tensors inside tidy
// Using memory() method
console.log('number of tensors inside tidy:'
+ tf.memory().numTensors);
// Calling sqrt() method
return op.sqrt();
});
// Printing number of tensors outside tidy
// Using memory() method
console.log('number of tensors outside tidy:'
+ tf.memory().numTensors);
// Printing output
res.print();
輸出:
number of tensors inside tidy:1 number of tensors outside tidy:1 Tensor 0.9224448204040527
參考: https://js.tensorflow.org/api/latest/#tidy
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- PHP Imagick floodFillPaintImage()用法及代碼示例
- PHP geoip_continent_code_by_name()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP GmagickPixel setcolor()用法及代碼示例
- Tensorflow.js tf.layers.embedding()用法及代碼示例
- PHP opendir()用法及代碼示例
- PHP cal_to_jd()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Tensorflow.js tf.tidy() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。