Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tensorflow/tfjs-backend-cpu

Package Overview
Dependencies
Maintainers
10
Versions
77
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tensorflow/tfjs-backend-cpu

Vanilla JavaScript backend for TensorFlow.js

  • 4.22.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
214K
decreased by-4.24%
Maintainers
10
Weekly downloads
 
Created

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

FAQs

Package last updated on 21 Oct 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc