Friday, July 24, 2026
HomeLanguagesJavascriptTensorflow.js tf.train.sgd() Function

Tensorflow.js tf.train.sgd() Function

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .train.sgd() function is used to build a tf.SGDOptimizer which utilizes stochastic gradient descent.

Syntax:

tf.train.sgd(learningRate)

Parameters:

  • learningRate: The stated learning rate which is used in favor of the SGD algorithm. It is of type number.

Return Value: It returns tf.SGDOptimizer.

 

Example 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);


Output:

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

Example 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))));


Output:

{"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}}

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS