Tensorflow.js tf.LayersModel class .compile() Method - GeeksforGeeks

10 May, 2021
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.
The .compile() function configures and makes the model for training and evaluation process. By calling .compile() function we prepare the model with an optimizer, loss, and metrics. The .compile() function takes an argument object as a parameter.
Note: If you call .fit() or .evaluate() function on an uncompiled model, then program will throw an error.
Syntax:
tf.model.compile({optimizer, loss}, metrics=[])
Parameters:
optimizer: It is a compulsory parameter. It accepts either an object of tf.train.Optimizer or a string name for an Optimizer. Following are string names for the optimizers — “sgd”, “adam”, “adamax”, “adadelta”, “adagrad”, “rmsprop”, “momentum”.
loss: It is a compulsory parameter. It accepts a string value or string array for the type of loss. If our model has multiple outputs, we can use a different loss on each output by passing an array of losses. The loss value that will be minimized by the model will then be the sum of all individual losses. Following are the string name for loss — “meanSquaredError”, “meanAbsoluteError”, etc.
metrics: It is an optional parameter. It accepts a list of metrics to be evaluated by the model during the training and testing phase. Usually, we use metrics=[‘accuracy’]. To specify different metrics for different outputs of a multi-output model, we can also pass a dictionary.
Return Value: Since it prepares the model for training, it does not return anything. (i.e. return type is void)
Example 1: In this example, we will create a simple model, and we will pass values for optimizer and loss parameters. Here we are using optimizer as “adam” and loss as “meanSquaredError”.
Javascript
// Importing the tensorflow.js libraryconst tf = require("@tensorflow/tfjs");  // define the modelconst model = tf.sequential({    layers: [tf.layers.dense({ units: 1, inputShape: [10] })],});  // compile the model// using "adam" optimizer and "meanSquaredError" lossmodel.compile({ optimizer: "adam", loss: "meanSquaredError" });  // evaluate the model which was compiled above// computation is done in batches of size 4const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {    batchSize: 4,});  // print the resultresult.print();
Output:
Tensor
    2.6806342601776123 
Example 2: In this example, we will create a simple model, and we will pass values for optimizer, loss, and metrics parameters. Here we are using optimizer as “sgd” and loss as “meanAbsoluteError” and “accuracy” as metrics.
Javascript
// Importing the tensorflow.js libraryconst tf = require("@tensorflow/tfjs");  // define the modelconst model = tf.sequential({    layers: [tf.layers.dense({ units: 1, inputShape: [10] })],});  // compile the model// using "adam" optimizer, "meanSquaredError" loss and "accuracy" metricsmodel.compile(    { optimizer: "adam", loss: "meanSquaredError" },    (metrics = ["accuracy"]));  // evaluate the model which was compiled above// computation is done in batches of size 4const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {    batchSize: 4,});  // print the resultresult.print();
Output:
Tensor
    1.4847172498703003
Example 3: In this example, we will create a simple model and we will pass values for optimizer, loss and metrics parameters. Here we are using optimizer as “sgd” and loss as “meanAbsoluteError” and “precision” as metrics.
Javascript
// Importing the tensorflow.js libraryconst tf = require("@tensorflow/tfjs");  // define the modelconst model = tf.sequential({    layers: [tf.layers.dense({ units: 1, inputShape: [10] })],});  // compile the model// using "adam" optimizer, "meanSquaredError" loss and "accuracy" metricsmodel.compile(    { optimizer: "sgd", loss: "meanAbsoluteError" },    (metrics = ["precision"]));  // evaluate the model which was compiled above// computation is done in batches of size 4const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {    batchSize: 4,});  // print the resultresult.print();
Output:
Tensor
   1.507279634475708
Reference: https://js.tensorflow.org/api/latest/#tf.LayersModel.compile
Picked
Tensorflow.js
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Difference between var, let and const keywords in JavaScript
Difference Between PUT and PATCH Request
JavaScript | Promises
How to get character array from string in JavaScript?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript