Skip to content
Related Articles
Get the best out of our app
Open App

Related Articles

Tensorflow.js tf.LayersModel Class

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Tensorflow.js is an open-source library developed by Google for running machine learning models and deep learning neural networks in the browser or node environment. Tensorflow. js tf.LayerModel class is used to training, interface and evaluation of model. It have many method for training, evaluation, prediction and saving.




  • args: Different method except different parameters.

Returns: Different methods returned different values tf.tensor object, etc.

Below we will see the implementation of methods of tf.LayerModel class.

Example 1: In this example,  will see trainOnBatch() method which is used to apply optimizer update on a single batch of data. It takes two tensor first as input value tensor and second as target tensor. It returns a promise of number.


import * as tf from "@tensorflow/tfjs"
async function run() {
// Training Model
  const gfg = tf.sequential();
// Adding layer to model 
  const layer = tf.layers.dense({units:3,
               inputShape : [5]});
// Compiling our model
  const config = {optimizer:'sgd',
// Test tensor and target tensor
  const layerOne = tf.ones([3,5]);
  const layerTwo = tf.ones([3,3]);
// Apply trainOnBatch to out test data
  const result =
    await gfg.trainOnBatch(layerOne, layerTwo);
// Printing out result
// Function call
await run();



Example 2: In this example, we will see getLayer() method which is used to fetch layers with the help of its name of the index. It takes the name of the layer of the index of the layer as parameter. It returns tf.layers.Layer.


import * as tf from "@tensorflow/tfjs"
// Defining model
 const gfg_Model = tf.sequential();
// Adding layers
 const config = {units: 4, inputShape: [1] };
 const layer = tf.layers.dense( config);;
 gfg_Model.add( layer);
 const config2 = {units: 2, inputShape: [3] , activation: 'sigmoid'};
 const layer2 = tf.layers.dense( config2 );;
// Calling getLayer() method 
 const layer_1 = gfg_Model.getLayer('denselayer', 1);
// Printing layer config


  "units": 2,
  "activation": "sigmoid",
  "useBias": true,
  "kernelInitializer": {
    "className": "VarianceScaling",
    "config": {
      "scale": 1,
      "mode": "fanAvg",
      "distribution": "normal",
      "seed": null
  "biasInitializer": {
    "className": "Zeros",
    "config": {}
  "kernelRegularizer": null,
  "biasRegularizer": null,
  "activityRegularizer": null,
  "kernelConstraint": null,
  "biasConstraint": null,
  "name": "dense_Dense53",
  "trainable": true,
  "batchInputShape": [
  "dtype": "float32"


My Personal Notes arrow_drop_up
Last Updated : 12 Dec, 2022
Like Article
Save Article
Similar Reads
Related Tutorials