Open In App

Tensorflow.js tf.loadLayersModel() Function

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:



Return Value: Promise<tf.LayersModel>

Example 1:




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

Output:

 

Example 2:




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


Article Tags :