What Computer Do You Need for Machine Learning?
If you are starting machine learning, the computer you already have is almost certainly enough. A laptop, a home desktop or a school computer can train the small models that beginners build, often in seconds. Special hardware matters only when models and datasets get much bigger. This guide explains why, what a GPU is for, and how to estimate the memory a training run needs.
Start with what you have
The examples on this site are small on purpose. The nearest-neighbor fruit classifier has 16 examples. The flower-sized network with layers of 4, 8 and 3 neurons has 67 parameters (numbers to learn). Even the handwritten-digit network with layers of 784, 128, 64 and 10 neurons has 109,386 parameters, which the Neural Network Calculator will count for you. Models like these train comfortably on an ordinary processor (CPU).
The ideas you learn on small models (splitting data, training, testing honestly, spotting overfitting) are exactly the same ideas used on huge ones. You lose nothing by starting small, and you can see results faster.
What a GPU is for
Training a neural network is mostly multiplication: every input times every weight, added up, for every example in a batch, again and again. A CPU has a few powerful cores that work through tasks one after another very quickly. A GPU (graphics processing unit) was first designed to work out the colors of millions of screen pixels at once, so it has a very large number of simpler cores that do many multiplications at the same time.
That makes a GPU much faster for large neural networks, especially on images, sound and long text. For small tables of numbers, simple models such as decision trees and linear regression, or the examples on this site, a GPU makes little difference.
A GPU has its own memory, separate from the computer's main memory. During training, everything the model needs has to fit in it, which is why people ask "how much GPU memory do I need?"
What training memory holds
While a model trains, memory holds several kinds of numbers:
- Parameters: the weights and biases themselves.
- Gradients: for every parameter, the direction and size of the next change. One gradient per parameter, so the same amount again.
- Optimizer state: extra numbers the update rule keeps. Plain SGD (stochastic gradient descent) keeps none; SGD with momentum keeps one extra number per parameter; Adam keeps two.
- Master weights: when training uses 16-bit numbers to save memory (called mixed precision), most setups also keep a 32-bit copy of the weights so small updates are not lost.
- Activations: the outputs of every layer for the current batch, saved so backpropagation can use them. These grow with the batch size and with the size of each input (a bigger picture or a longer piece of text means more activations).
Worked examples
The GPU Memory Calculator adds these up. You give it the number of parameters (in millions), the batch size, the bytes per parameter (4 for 32-bit numbers, 2 for 16-bit) and the optimizer. Here are its results for a 100-million-parameter model:
| Setup | Parameters | Gradients | Optimizer state | Master weights | Activations | Total |
|---|---|---|---|---|---|---|
| 32-bit, Adam, batch 32 | 400 MB | 400 MB | 800 MB | 0 | 1.28 GB | 2.88 GB |
| 16-bit, Adam, batch 32 | 200 MB | 200 MB | 800 MB | 400 MB | 640 MB | 2.24 GB |
| 32-bit, SGD, batch 0 | 400 MB | 400 MB | 0 | 0 | 0 | 800 MB |
| 32-bit, Adam, batch 256 | 400 MB | 400 MB | 800 MB | 0 | 10.24 GB | 11.84 GB |
How the numbers come about:
- Parameters: 100 million x 4 bytes = 400 MB. The calculator uses decimal units, so 1 MB is 1,000,000 bytes and 1 GB is 1,000,000,000 bytes.
- Gradients: the same size as the parameters.
- Adam's state: two numbers per parameter, kept at 4 bytes each whatever the weights use: 100 million x 2 x 4 = 800 MB. That is why it stays at 800 MB in the 16-bit row.
- Master weights: 0 in 32-bit training; in the 16-bit row, a 32-bit copy, 100 million x 4 = 400 MB.
- Activations: the calculator uses a rough rule, batch size x parameter bytes x 0.1. At batch 32 and 32-bit that is 32 x 400 MB x 0.1 = 1.28 GB.
Three lessons stand out. First, 16-bit training saves less than you might guess: 2.24 GB against 2.88 GB, because the optimizer state and the master copy stay 32-bit. Before activations the 16-bit row needs 2 + 2 + 8 + 4 = 16 bytes per parameter, against 4 + 4 + 8 = 16 bytes in the 32-bit row; in the calculator, the whole saving comes from its smaller activation figure. Second, batch size can dominate: at batch 256 the activation figure is most of the total. Third, the "batch 0" row is not a real training run; it just shows the fixed cost of parameters and gradients with nothing else.
A classroom-sized model
Now the 109,386-parameter digit network, with 32-bit numbers, Adam and batch 32. Its weights take 437,544 bytes (about 437.54 KB), and the calculator's total is 3.15 MB. An ordinary laptop has thousands of times that much memory. Small models really do fit anywhere.
What the calculator simplifies
Treat its total as a rough planning estimate, not a guarantee.
- Activations are a placeholder. Real activation memory depends on the width of each layer, the size of each input (image size or text length), the number of layers and some framework settings. It does not really depend on the parameter count the way the rule assumes. Two models with the same number of parameters can need very different activation memory.
- Software takes room too. Training frameworks reserve working memory, keep caches and load their own code onto the GPU, so real use is usually higher than the sum of the parts.
- It counts training, not just using a model. Running a finished model to make predictions needs no gradients or optimizer state, so it needs much less.
If a run does not fit, the usual first steps are a smaller batch, a smaller model, or smaller inputs. Measure what actually happens on your own setup and adjust from there. To estimate how long training takes, see epochs, batches and training time; for where the parameter count comes from, see neural networks explained.
When you outgrow your computer
If you reach projects that need a GPU, you still may not need to buy anything. Some schools and universities provide shared computers for students, and there are hosted notebook services that run code in a web browser on someone else's hardware, some with free access for learning. Before using one:
- Ask a parent or teacher first. Check the service's own terms for its minimum age (many services set one, and some require a parent's permission), and never give a false age.
- Read its privacy information, and never upload personal information about yourself or anyone else, such as names, photos, voice recordings or school details. Your practice datasets should be made up or openly published for learning.
- Use the school's approved tools for schoolwork.