Open In App

Tensorflow.js tf.loadLayersModel() Function

Last Updated : 26 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.

The function tf.loadLayersModel() is used to load a model composed of Layer objects, including its topology and optionally weights.

Syntax:

tf.loadLayersModel(pathOrIOHandler, options?)

Parameters:

  • pathOrIOHandler: It’s possible to use one of the two forms:
    • A string path to the ModelAndWeightsConfig JSON file that describes the model in TensorFlow.js format. For the file:// (tfjs-node-only), http://, and https:// schemas, the path can be absolute or relative.
    • A tf.io.IOHandler object that uses the load method to load model artifacts.
  • options (Object): Optional model loading configuration parameters, including:
    • requestInit: RequestInit (options) for HTTP requests.
    • onProgress: A progress callback.
    • fetchFunc: An override function for the window.
    • strict (boolean): Strict loading model: whether missing or extraneous weights should cause an Error. The default value is true.
    • weightPathPrefix (string): Weight file path prefix, which is derived by default from the path of the model JSON file.
    • fromTFHub (boolean): If the module or model should be loaded from the TF Hub. The default value is false.
    • weightUrlConverter: An async method to translate the name of a weight file to its URL. The names of the weight files are recorded in the weightsManifest.paths field of model.json. Weight files are assumed to be co-located with the model.json file by default.

Return Value: Promise<tf.LayersModel>

Example 1:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const model = await tf.loadLayersModel(
model.summary();


Output:

 

Example 2:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const input = tf.input({ shape: [4, 4, 4] });
const conv2DLayer = tf.layers.conv2d({ filters: 2, kernelSize: 2});
const output = conv2DLayer.apply(input);
  
const model = tf.model({ inputs: input, outputs: output });
console.log("Original Model's prediction:");
model.predict(tf.ones([1, 4, 4, 4])).print();
  
const path='file://./model'
  
const saveResults = await model.save(path);
  
const loadedModel = await tf.loadLayersModel(path);
console.log("Loaded Model's Prediction:");
model.predict(tf.ones([1, 4, 4, 4])).print();


Output:

Original Model's prediction:
Tensor
    [[[[-0.592868, 0.4749331],
       [-0.592868, 0.4749331],
       [-0.592868, 0.4749331]],

      [[-0.592868, 0.4749331],
       [-0.592868, 0.4749331],
       [-0.592868, 0.4749331]],

      [[-0.592868, 0.4749331],
       [-0.592868, 0.4749331],
       [-0.592868, 0.4749331]]]]
Loaded Model's Prediction:
Tensor
    [[[[-0.592868, 0.4749331],
       [-0.592868, 0.4749331],
       [-0.592868, 0.4749331]],

      [[-0.592868, 0.4749331],
       [-0.592868, 0.4749331],
       [-0.592868, 0.4749331]],

      [[-0.592868, 0.4749331],
       [-0.592868, 0.4749331],
       [-0.592868, 0.4749331]]]]

Reference: https://js.tensorflow.org/api/latest/#loadLayersModel



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads