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


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

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

.train.sgd() 函數用於構建利用隨機梯度下降的 tf.SGDOptimizer。

用法:

tf.train.sgd(learningRate)

參數:

  • learningRate:用於支持 SGD 算法的規定學習率。它是類型號。

返回值:它返回 tf.SGDOptimizer。



範例1:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Declaring learningRate
const learning_rate = 0.02;
  
// Calling train.sgd() method
const res = tf.train.sgd(learning_rate);
  
// Printing output
console.log(res);

輸出:

{
  "learningRate":0.02,
  "c":{
    "kept":true,
    "isDisposedInternal":false,
    "shape":[],
    "dtype":"float32",
    "size":1,
    "strides":[],
    "dataId":{
      "id":1298
    },
    "id":1192,
    "rankType":"0",
    "scopeId":1251
  }
}

範例2:

Javascript


// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling train.sgd() and sin() method
console.log(JSON.stringify(tf.train.sgd(tf.sin(45))));

輸出:

{"learningRate":{"kept":false,"isDisposedInternal":false,"shape":[],
"dtype":"float32","size":1,"strides":[],"dataId":{"id":1316},"id":1207,
"rankType":"0","scopeId":1269},"c":{"kept":true,"isDisposedInternal":false,
"shape":[],"dtype":"float32","size":1,"strides":[],"dataId":{"id":1318},
"id":1208,"rankType":"0","scopeId":1269}}

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

相關用法


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