What is @tensorflow/tfjs-backend-webgl?
@tensorflow/tfjs-backend-webgl is a WebGL-accelerated backend for TensorFlow.js, enabling high-performance machine learning computations in the browser. It leverages the power of the GPU to perform operations faster than the CPU backend.
What are @tensorflow/tfjs-backend-webgl's main functionalities?
Tensor Operations
This feature allows you to perform tensor operations such as addition, multiplication, etc., using the WebGL backend for accelerated performance.
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-backend-webgl');
async function run() {
await tf.setBackend('webgl');
const a = tf.tensor([1, 2, 3, 4]);
const b = tf.tensor([5, 6, 7, 8]);
const c = a.add(b);
c.print(); // Output: [6, 8, 10, 12]
}
run();
Model Training
This feature allows you to train machine learning models directly in the browser using the WebGL backend for faster computations.
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-backend-webgl');
async function run() {
await tf.setBackend('webgl');
const model = tf.sequential();
model.add(tf.layers.dense({units: 100, activation: 'relu', inputShape: [10]}));
model.add(tf.layers.dense({units: 1}));
model.compile({optimizer: 'sgd', loss: 'meanSquaredError'});
const xs = tf.randomNormal([100, 10]);
const ys = tf.randomNormal([100, 1]);
await model.fit(xs, ys, {epochs: 10});
console.log('Model training complete');
}
run();
Model Inference
This feature allows you to perform model inference, i.e., making predictions using a pre-trained model, with the WebGL backend for improved performance.
const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-backend-webgl');
async function run() {
await tf.setBackend('webgl');
const model = await tf.loadLayersModel('https://example.com/model.json');
const input = tf.tensor([1, 2, 3, 4], [1, 4]);
const output = model.predict(input);
output.print();
}
run();
Other packages similar to @tensorflow/tfjs-backend-webgl
@tensorflow/tfjs-backend-cpu
@tensorflow/tfjs-backend-cpu is a CPU-based backend for TensorFlow.js. It is generally slower than the WebGL backend but can be used in environments where WebGL is not available or desired.
@tensorflow/tfjs-node
@tensorflow/tfjs-node is a Node.js backend for TensorFlow.js, providing high-performance machine learning computations on the server-side. It leverages the TensorFlow C library for accelerated performance.
gpu.js
gpu.js is a JavaScript library for GPU-accelerated computations. While it is not specifically designed for machine learning, it can be used to perform general-purpose computations on the GPU, similar to the WebGL backend of TensorFlow.js.
Usage
This package implements a GPU accelerated WebGL backend for 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-webgl';
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-webgl"></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-webgl@2.0.0-rc.4/dist/tf-backend-webgl.es2017.js"></script>