当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。