Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 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 library
const tf = require("@tensorflow/tfjs");
  
// define the model
const model = tf.sequential({
    layers: [tf.layers.dense({ units: 1, inputShape: [10] })],
});
  
// compile the model
// using "adam" optimizer and "meanSquaredError" loss
model.compile({ optimizer: "adam", loss: "meanSquaredError" });
  
// evaluate the model which was compiled above
// computation is done in batches of size 4
const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {
    batchSize: 4,
});
  
// print the result
result.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 library
const tf = require("@tensorflow/tfjs");
  
// define the model
const model = tf.sequential({
    layers: [tf.layers.dense({ units: 1, inputShape: [10] })],
});
  
// compile the model
// using "adam" optimizer, "meanSquaredError" loss and "accuracy" metrics
model.compile(
    { optimizer: "adam", loss: "meanSquaredError" },
    (metrics = ["accuracy"])
);
  
// evaluate the model which was compiled above
// computation is done in batches of size 4
const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {
    batchSize: 4,
});
  
// print the result
result.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 library
const tf = require("@tensorflow/tfjs");
  
// define the model
const model = tf.sequential({
    layers: [tf.layers.dense({ units: 1, inputShape: [10] })],
});
  
// compile the model
// using "adam" optimizer, "meanSquaredError" loss and "accuracy" metrics
model.compile(
    { optimizer: "sgd", loss: "meanAbsoluteError" },
    (metrics = ["precision"])
);
  
// evaluate the model which was compiled above
// computation is done in batches of size 4
const result = model.evaluate(tf.ones([8, 10]), tf.ones([8, 1]), {
    batchSize: 4,
});
  
// print the result
result.print();

Output:

Tensor
   1.507279634475708

Reference: https://js.tensorflow.org/api/latest/#tf.LayersModel.compile




My Personal Notes arrow_drop_up
Recommended Articles
Page :