Tensorflow.js tf.LayersModel Class
Last Updated :
12 Dec, 2022
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.
Syntax:
tf.LayerModel.method(args);
Parameters:
- 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.
Javascript
import * as tf from "@tensorflow/tfjs"
async function run() {
const gfg = tf.sequential();
const layer = tf.layers.dense({units:3,
inputShape : [5]});
gfg.add(layer);
const config = {optimizer: 'sgd' ,
loss: 'meanSquaredError' };
gfg.compile(config);
const layerOne = tf.ones([3,5]);
const layerTwo = tf.ones([3,3]);
const result =
await gfg.trainOnBatch(layerOne, layerTwo);
console.log(result);
}
await run();
|
Output:
3.683875560760498
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.
Javascript
import * as tf from "@tensorflow/tfjs"
const gfg_Model = tf.sequential();
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 );;
gfg_Model.add(layer2);
const layer_1 = gfg_Model.getLayer( 'denselayer' , 1);
console.log(layer_1.getConfig());
|
Output:
{
"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": [
null,
3
],
"dtype": "float32"
}
Reference: https://js.tensorflow.org/api/latest/#class:LayersModel
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...