What is @tensorflow/tfjs-backend-cpu?
@tensorflow/tfjs-backend-cpu is a backend for TensorFlow.js that allows you to run machine learning models on the CPU. It is particularly useful for environments where GPU acceleration is not available or necessary. This package provides the necessary infrastructure to execute TensorFlow.js operations using the CPU, making it versatile for various applications including training and inference of machine learning models.
What are @tensorflow/tfjs-backend-cpu's main functionalities?
Tensor Operations
This feature allows you to perform tensor operations using the CPU backend. The code sample demonstrates how to set the backend to CPU, create a tensor, and perform a square operation on it.
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-backend-cpu');
// Set the backend to CPU
await tf.setBackend('cpu');
// Create a tensor
const tensor = tf.tensor([1, 2, 3, 4]);
// Perform an operation
const squared = tensor.square();
squared.print();
Model Training
This feature allows you to train machine learning models using the CPU backend. The code sample demonstrates how to define a simple model, compile it, generate synthetic data, train the model, and make a prediction.
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-backend-cpu');
// Set the backend to CPU
await tf.setBackend('cpu');
// Define a simple model
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
// Generate some synthetic data for training
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);
// Train the model
await model.fit(xs, ys, {epochs: 10});
// Make a prediction
model.predict(tf.tensor2d([5], [1, 1])).print();
Model Inference
This feature allows you to perform model inference using the CPU backend. The code sample demonstrates how to load a pre-trained model, prepare an input tensor, and perform inference to get predictions.
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-backend-cpu');
// Set the backend to CPU
await tf.setBackend('cpu');
// Load a pre-trained model
const model = await tf.loadLayersModel('https://storage.googleapis.com/tfjs-models/tfjs/mobilenet_v1_0.25_224/model.json');
// Prepare an input tensor
const input = tf.zeros([1, 224, 224, 3]);
// Perform inference
const prediction = model.predict(input);
prediction.print();
Other packages similar to @tensorflow/tfjs-backend-cpu
@tensorflow/tfjs-backend-webgl
@tensorflow/tfjs-backend-webgl is a backend for TensorFlow.js that allows you to run machine learning models using WebGL for GPU acceleration. It is suitable for environments where GPU resources are available and can significantly speed up tensor operations and model training compared to the CPU backend.
@tensorflow/tfjs-node
@tensorflow/tfjs-node is a TensorFlow.js backend for Node.js that provides native TensorFlow execution. It leverages the TensorFlow C library for high-performance operations and is suitable for server-side applications where maximum performance is required.
onnxruntime
onnxruntime is a high-performance runtime for ONNX (Open Neural Network Exchange) models. It supports multiple execution providers, including CPU and GPU, and is designed to be cross-platform. It can be used as an alternative to TensorFlow.js for running machine learning models in various environments.
Usage
This package implements a JavaScript based CPU backend to TensorFlow.js.
Importing the backend
Note: this backend is included by default in @tensorflow/tfjs
.
Via NPM
import * as tf from '@tensorflow/tfjs-core';
import '@tensorflow/tfjs-backend-cpu';
Via a script tag
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu"></script>
You can also get ES2017 code using the following links
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core@2.0.0-rc.4/dist/tf-core.es2017.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu@2.0.0-rc.4/dist/tf-backend-cpu.es2017.js"></script>