
Saving and Restoring Models in TensorFlow.js
Learn how to save and restore TensorFlow.js models with this easy-to-follow tutorial. Perfect for beginners who want to understand how to manage machine learning models.
· tutorials · 2 minutes
How to Save and Restore Models in TensorFlow.js
In machine learning, saving and restoring models is super important. You don’t want to train your model from scratch every time you run your program. Instead, you can save the model after training and restore it later when you want to use it again. TensorFlow.js makes this process simple.
In this tutorial, you’ll learn how to:
- Save a model after training.
- Restore (load) a model so you can use it later without retraining.
Let’s get started!
Saving a Model in TensorFlow.js
After you’ve trained your model, you can easily save it to your local machine or a remote server.
Here’s how to save your model:
import * as tf from '@tensorflow/tfjs';
// Create a simple modelconst model = tf.sequential();model.add(tf.layers.dense({units: 10, inputShape: [5]}));model.add(tf.layers.dense({units: 1}));
// Compile the modelmodel.compile({ optimizer: 'sgd', loss: 'meanSquaredError',});
// Save the model to the browser's local storageasync function saveModel() { await model.save('localstorage://my-model'); console.log('Model saved to local storage!');}
// Call the function to save the modelsaveModel();
What Does This Code Do?
- Define the Model: We create a simple neural network model with two layers.
- Compile the Model: We define how the model will train (using an optimizer and a loss function).
- Save the Model: We use the model.save() method to save the model. In this case, it’s saved to the browser’s local storage, but you can also save it to a file or a remote server.
Restoring (Loading) a Saved Model
Once you’ve saved a model, you can load it back and use it for making predictions, retraining, or further fine-tuning. Here’s how to restore a model:
import * as tf from '@tensorflow/tfjs';
// Load the model from local storageasync function loadModel() { const restoredModel = await tf.loadLayersModel('localstorage://my-model'); console.log('Model restored from local storage!');
// Use the model to make predictions or continue training restoredModel.summary();}
// Call the function to load the modelloadModel();
Other Save Options
You can save models to different locations:
- Local storage: localstorage://my-model
- IndexedDB: indexeddb://my-model
- Download as file: downloads://my-model
- Remote server: Provide a server URL to save the model remotely.
Example of saving the model as a downloadable file:
async function saveModelAsFile() { await model.save('downloads://my-model'); console.log('Model saved as a file!');}saveModelAsFile();
More posts
-
Implementing a Convolutional Neural Network (CNN) in TensorFlow.js Using Tabular Data
Learn how to implement a convolutional neural network (CNN) in TensorFlow.js using a tabular dataset. This step-by-step guide covers data preprocessing, model architecture, training, and evaluation for binary classification tasks.
-
Evaluating the Performance of a TensorFlow.js Model Using Metrics
Understand how to evaluate the performance of a TensorFlow.js model using metrics like accuracy, precision, recall, and loss. Learn practical examples for different tasks.
-
How to Create a Tensor in TensorFlow.js
Learn how to create a tensor in TensorFlow.js with easy-to-follow examples and explanations, perfect for beginners starting their machine learning journey.