Tensorflow.js是由Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型以及深度學習神經網絡。
.valueAndGrads() 函數等效於 tf.grads() 方法,但它也返回 f() 的度量。當 f() 返回一個您需要證明的近似值時有效。
注意:這裏的輸出是一個富裕的對象以及以下函數:
- grads:它是 f() 參考每個輸入的梯度,即 grads() 方法的輸出。
- value:它是通過 f(x) 還原的值。
用法:
tf.valueAndGrads(f)
Parameters:
- f:它是要計算梯度的指定函數 f(x)。它的類型為 (...args:tf.Tensor[]) => tf.Tensor。
返回值:它返回梯度和值,即 (args:tf.Tensor[], dy?:tf.Tensor) => { grads:tf.Tensor[];值:tf.張量; }.
範例1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Defining funtion
const fn = (x, y) => x.add(y);
// Calling valueAndGrads() method
const gr = tf.valueAndGrads(fn);
// Defining tf.tensor1d inputs
const x = tf.tensor1d([66, 51]);
const y = tf.tensor1d([-21, -13]);
// Defining value and grads
const {value, grads} = gr([x, y]);
const [dx, dy] = grads;
// Printing value
console.log('val');
value.print();
// Printing gradients
console.log('dx');
dx.print();
console.log('dy');
dy.print();
輸出:
val Tensor [45, 38] dx Tensor [1, 1] dy Tensor [1, 1]
範例2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling valueAndGrads() method
// with its parameter
const gr = tf.valueAndGrads((x, y) => x.div(y));
// Defining tf.tensor1d inputs of
// floating point numbers
const x = tf.tensor1d([4.7, 5.8, 99.7]);
const y = tf.tensor1d([9.5, -20.5, null]);
// Defining value and grads
const {value, grads} = gr([x, y]);
const [dx, dy] = grads;
// Printing value
console.log('val');
value.print();
// Printing gradients
console.log('dx');
dx.print();
console.log('dy');
dy.print();
輸出:
val Tensor [0.4947368, -0.2829268, Infinity] dx Tensor [0.1052632, -0.0487805, Infinity] dy Tensor [-0.0520776, -0.0138013, -Infinity]
參考:https://js.tensorflow.org/api/latest/#valueAndGrads
相關用法
- 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()用法及代碼示例
- d3.js d3.bisectLeft()用法及代碼示例
注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Tensorflow.js tf.valueAndGrads() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。