Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
@tensorflow/tfjs-backend-cpu
Advanced tools
@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.
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();
@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 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 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.
This package implements a JavaScript based CPU backend to TensorFlow.js.
Note: this backend is included by default in @tensorflow/tfjs
.
// Import @tensorflow/tfjs-core
import * as tf from '@tensorflow/tfjs-core';
// Adds the CPU backend to the global backend registry.
import '@tensorflow/tfjs-backend-cpu';
<!-- Import @tensorflow/tfjs-core -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core"></script>
<!-- Adds the CPU backend to the global backend registry -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu"></script>
You can also get ES2017 code using the following links
<!-- Import @tensorflow/tfjs-core -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-core@2.0.0-rc.4/dist/tf-core.es2017.js"></script>
<!-- Adds the CPU backend to the global backend registry -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-cpu@2.0.0-rc.4/dist/tf-backend-cpu.es2017.js"></script>
FAQs
Vanilla JavaScript backend for TensorFlow.js
The npm package @tensorflow/tfjs-backend-cpu receives a total of 201,281 weekly downloads. As such, @tensorflow/tfjs-backend-cpu popularity was classified as popular.
We found that @tensorflow/tfjs-backend-cpu demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 10 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.