Expert: deep learning and AI · Neural networks and deep learning · topic 47 of 62 · 25 min
Deep learning tools and hardware
Explanation
Frameworks
| Framework | Known for |
|---|---|
| PyTorch | Python-first, easy to debug; the most common choice in research and very common in industry |
| TensorFlow with Keras | Keras offers a simple high-level API; TensorFlow has mature deployment tools, including for mobile |
| JAX | Fast numerical computing with automatic differentiation, popular in some research groups |
| Hugging Face Transformers | A library of thousands of pre-trained models for text, images and audio, on top of PyTorch and others |
All of them provide tensors (multi-dimensional arrays like NumPy's, but able to live on a GPU), automatic gradients, ready-made layers, losses and optimisers. Learning one well is enough; the ideas carry over.
import torch
x = torch.tensor([[1.0, 2.0], [3.0, 4.0]])
print(x.shape) # torch.Size([2, 2])
device = "cuda" if torch.cuda.is_available() else "cpu"
x = x.to(device) # move the data to the GPU if there is one
print((x @ x).sum().item()) # 54.0 (matrix multiply, then sum)Why GPUs?
A CPU has a few powerful cores that handle varied tasks one after another. A GPU has thousands of simpler cores that do the same maths on many numbers at once. Training a network is mostly multiplying large matrices, which splits perfectly across those cores, so GPUs (and Google's TPUs) can be many times faster than CPUs for this work. For small classic ML models (module 7), a CPU is fine.
How much memory does a model need?
A quick estimate: number of parameters × bytes per parameter. In float32 each parameter takes 4 bytes; in float16 or bfloat16, 2 bytes; in 8-bit quantised form, 1 byte. A 7-billion-parameter model in float16 needs about 7 × 10⁹ × 2 = 14 × 10⁹ bytes, roughly 14 GB, just to hold the weights. Training needs several times more for gradients and optimiser state. Quantisation shrinks models so they fit on smaller GPUs or even laptops, with some loss of quality.
Where to run things
- Your laptop: fine for learning, classic ML and small networks on CPU.
- Free notebooks: Google Colab and Kaggle Notebooks offer limited free GPU time. Limits and hardware change often, so check the current terms.
- Cloud GPUs: AWS, Google Cloud, Azure and Indian providers rent GPUs by the hour for bigger jobs. Watch costs and shut machines down.
Saving, sharing and serving models
Save trained weights to a file (in PyTorch, torch.save(model.state_dict(), "model.pt")) so you can load them later without retraining. The Hugging Face Hub hosts open models and datasets you can download with a few lines. For production, models are often exported to formats like ONNX and served behind an API (module 11).
Common mistakes
- Model on the GPU but data on the CPU (or the reverse), giving a "tensors on different devices" error. Fix: move both with .to(device).
- Running out of GPU memory. Fix: smaller batches, mixed precision (float16) or a smaller model.
- Leaving cloud GPUs running overnight and getting a big bill.
- Using deep learning where gradient boosting on a table would be simpler and better.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Deep learning tools and hardware · 1/6
PyTorch is the most widely used deep learning framework. TensorFlow with Keras and JAX are other options.
Prefer a video by a teacher? Find free videos on “pytorch tutorial for beginners tensors gpu google colab” on YouTube ↗
In real life: A student project on a free GPU
A final-year student builds a classifier for rice-leaf diseases. She uses a free notebook GPU, starts from a pre-trained image model from the Hugging Face Hub, fine-tunes it on a public dataset of a few thousand labelled photos, saves the weights, and serves it from a small web app. The whole project costs nothing but time, and it becomes the centrepiece of her portfolio.
Practice (0/2 done)
Write model_memory_gb(params, bytes_per_param) that returns the memory needed to hold the weights in gigabytes, using 1 GB = 1,000,000,000 bytes, rounded to 2 decimal places.
Plan a small deep learning project: classifying photos of 5 types of Indian street food. Write: the data you would collect and how many images per class, the model you would start from, where you would train it, how you would evaluate it, and how you would avoid overfitting.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Layers, activations and the forward passNeural networks and deep learning · same module
- MLOps: from notebook to productionBuilding with generative AI, and MLOps
- Fine-tuning, and when not toBuilding with generative AI, and MLOps
- What machine learning isThe ML workflow, regression and classification
- The shared responsibility modelCyber Security: from zero to SOC analyst · Cloud security basics
- What Python is used for: web, data and AIPython Programming · Python in the real world
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Why are GPUs fast for training neural networks?
Answer: They run thousands of simple maths operations in parallel, ideal for matrix multiplication
Training is dominated by parallel matrix maths.
Roughly how much memory do the weights of a 13-billion-parameter model need in float16?
Answer: 26 GB
13 × 10⁹ × 2 bytes ≈ 26 GB.
What is quantisation?
Answer: Storing weights with fewer bits (for example 8-bit) to save memory, with some quality loss
Fewer bytes per parameter make models smaller and often faster.
You get "expected all tensors to be on the same device". What is the likely fix?
Answer: Move both the model and the data to the same device with .to(device)
Model and data must live on the same device.
For a table of 50,000 loan records with 30 columns, which is usually the better first choice?
Answer: Gradient boosting on a CPU
Tabular data is usually best served by tree ensembles.
Training loss keeps falling but validation loss starts rising. What is happening?Related
Answer: Overfitting
The network is memorising the training data; consider early stopping.
What is training-serving skew?Related
Answer: Data processed differently in training and in production
Shipping one saved pipeline prevents it.
At the current weight the gradient of the loss is positive. What should gradient descent do?Related
Answer: Decrease the weight
Positive slope means the loss rises as w rises, so step the other way.
A dense layer goes from 100 inputs to 50 neurons. How many parameters?Related
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- This is the last topic of “Neural networks and deep learning”. The module exam comes next.
- Module 9 exam: Neural networks and deep learning · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, NLP, embeddings and large language models. 62 topics left in the course before the final exam.