# TensorFlow.js — Machine Learning in JavaScript > A JavaScript library for training and deploying machine learning models in the browser and on Node.js with GPU acceleration via WebGL and WebGPU. ## Install Save in your project root: # TensorFlow.js ## Quick Use ```bash npm install @tensorflow/tfjs ``` ```js import * as tf from '@tensorflow/tfjs'; const model = tf.sequential(); model.add(tf.layers.dense({ units: 1, inputShape: [1] })); model.compile({ loss: 'meanSquaredError', optimizer: 'sgd' }); ``` ## Introduction TensorFlow.js brings the TensorFlow ecosystem to JavaScript, letting you train models directly in the browser or on Node.js. It provides GPU-accelerated tensor operations through WebGL and WebGPU, making client-side ML inference fast enough for real-time applications. ## What TensorFlow.js Does - Trains and runs ML models entirely in the browser with no server required - Converts existing TensorFlow and Keras models for JavaScript deployment - Accelerates tensor math via WebGL, WebGPU, and WASM backends - Provides pre-built models for image classification, object detection, pose estimation, and NLP - Supports transfer learning to fine-tune models on client-side data ## Architecture Overview The library has three layers: a low-level Ops API for tensor operations, a Layers API matching the Keras interface, and a high-level Model API for loading pre-trained models. Backends (WebGL, WebGPU, WASM, CPU) are pluggable and selected automatically based on browser capabilities. A converter tool transforms SavedModel and Keras H5 files into the TFJS format. ## Self-Hosting & Configuration - Install the core package or use a CDN script tag for quick prototyping - Choose a backend explicitly or let the library auto-select the fastest available - Use the Node.js binding with tfjs-node for server-side training with native C++ acceleration - Convert Python models using the tensorflowjs_converter CLI tool - Host converted model files on any static file server or CDN ## Key Features - Full training and inference loop runs in the browser with no backend calls - WebGPU backend delivers near-native GPU performance on supported browsers - Extensive pre-trained model zoo for vision, language, and audio tasks - Seamless model conversion from Python TensorFlow and Keras - Privacy-preserving ML since data never leaves the user device ## Comparison with Similar Tools - **ONNX Runtime Web** — inference-only for ONNX models; TensorFlow.js supports both training and inference - **Brain.js** — simpler neural network API; TensorFlow.js offers the full TensorFlow ecosystem - **PyTorch** — Python-first; TensorFlow.js targets JavaScript runtimes natively - **ml5.js** — higher-level wrapper built on TensorFlow.js for beginners - **Transformers.js** — focuses on Hugging Face model inference; TensorFlow.js covers broader ML tasks ## FAQ **Q: Can I train large models in the browser?** A: Small to medium models train well. For large models, train in Python and convert for browser inference. **Q: Which backend should I use?** A: WebGPU gives the best performance on supported browsers. WebGL is the most widely compatible fallback. **Q: Can I use pre-trained Hugging Face models?** A: TensorFlow.js supports converted TF/Keras models. For Hugging Face transformers, consider Transformers.js. **Q: Does it work in Node.js?** A: Yes. The tfjs-node package provides native C++ bindings for server-side performance. ## Sources - https://github.com/tensorflow/tfjs - https://www.tensorflow.org/js --- Source: https://tokrepo.com/en/workflows/asset-584537ac Author: AI Open Source