# Brain.js — GPU-Accelerated Neural Networks in JavaScript > A JavaScript library for building and training neural networks in the browser and Node.js with optional GPU acceleration via headless-gl. ## Install Save in your project root: # Brain.js ## Quick Use ```bash npm install brain.js ``` ```js import { NeuralNetwork } from 'brain.js'; const net = new NeuralNetwork(); net.train([ { input: [0, 0], output: [0] }, { input: [1, 0], output: [1] }, { input: [0, 1], output: [1] }, { input: [1, 1], output: [0] }, ]); console.log(net.run([1, 0])); // ~[1] ``` ## Introduction Brain.js is a lightweight JavaScript library for neural networks that runs in both the browser and Node.js. It focuses on simplicity, letting developers train feedforward, recurrent, and LSTM networks with just a few lines of code and no ML background required. ## What Brain.js Does - Trains feedforward, recurrent, LSTM, and GRU neural networks in JavaScript - Accelerates training with GPU.js for WebGL-based parallel computation - Serializes trained models to JSON for storage and later reuse - Supports time-series prediction, pattern recognition, and text classification - Runs entirely client-side with no server dependency for inference ## Architecture Overview Brain.js provides a set of network classes: NeuralNetwork for feedforward, RNNTimeStep and LSTMTimeStep for sequential data, and recurrent variants for text. Training runs a configurable backpropagation loop with learning rate, momentum, and error threshold parameters. GPU acceleration is opt-in via the NeuralNetworkGPU class which offloads matrix operations to WebGL shaders through GPU.js. ## Self-Hosting & Configuration - Install via npm for Node.js or include via CDN for browser usage - Choose the network type matching your data: feedforward for classification, LSTM for sequences - Configure training options like iterations, error threshold, learning rate, and log interval - Export trained models with toJSON and reload them with fromJSON - For GPU acceleration in Node.js install gpu.js as a peer dependency ## Key Features - Simple API that requires no knowledge of linear algebra or ML theory - GPU acceleration via WebGL for faster training on supported environments - JSON model serialization for saving, sharing, and deploying trained networks - Multiple network architectures including LSTM and GRU for sequence tasks - Runs in browsers and Node.js with the same API ## Comparison with Similar Tools - **TensorFlow.js** — full ML framework with broader model support; Brain.js is simpler for basic networks - **Synaptic** — similar concept but unmaintained; Brain.js is actively developed - **ConvNetJS** — focused on CNNs and archived; Brain.js covers RNNs and LSTMs - **ml5.js** — higher-level wrapper over TensorFlow.js; Brain.js is a standalone lightweight library - **ONNX Runtime Web** — inference-only; Brain.js supports both training and inference ## FAQ **Q: What tasks is Brain.js good for?** A: Pattern recognition, simple classification, time-series prediction, and text categorization with small to medium datasets. **Q: Can I use it for image recognition?** A: It lacks convolutional layers, so for images consider TensorFlow.js or ONNX Runtime instead. **Q: Does GPU acceleration work in all browsers?** A: It requires WebGL support. Most modern desktop and mobile browsers support it. **Q: How large can my training data be?** A: Brain.js works best with small to medium datasets. For large-scale training, consider server-side frameworks. ## Sources - https://github.com/BrainJS/brain.js - https://brain.js.org --- Source: https://tokrepo.com/en/workflows/asset-83222af5 Author: AI Open Source