Skip to content

Expert: deep learning and AI · Neural networks and deep learning · topic 47 of 62 · 25 min

Deep learning tools and hardware

Explanation

PyTorch is the most widely used deep learning framework today, with TensorFlow/Keras and JAX as other options. Training is fast on GPUs because neural networks are mostly matrix maths, which GPUs do in parallel. A model's memory need is roughly its number of parameters times the bytes per parameter. Free notebooks like Google Colab and Kaggle let you start without buying hardware.

Frameworks

FrameworkKnown for
PyTorchPython-first, easy to debug; the most common choice in research and very common in industry
TensorFlow with KerasKeras offers a simple high-level API; TensorFlow has mature deployment tools, including for mobile
JAXFast numerical computing with automatic differentiation, popular in some research groups
Hugging Face TransformersA 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.

python
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)
Tensors work like NumPy arrays and can move to a GPU. Shown only.

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 · TensorFlow/Keras · JAX

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)

Task 1 · Code · Python

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.

Task 2 · Practice

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

Question 1

1.Why are GPUs fast for training neural networks?

Question 2

2.Roughly how much memory do the weights of a 13-billion-parameter model need in float16?

Question 3

3.What is quantisation?

Question 4

4.You get "expected all tensors to be on the same device". What is the likely fix?

Question 5

5.For a table of 50,000 loan records with 30 columns, which is usually the better first choice?

Read more (free)

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.

From Training: loss, backpropagation and optimisers

What is training-serving skew?Related

Answer: Data processed differently in training and in production

Shipping one saved pipeline prevents it.

From MLOps: from notebook to production

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.

From Gradient descent: learning step by step

A dense layer goes from 100 inputs to 50 neurons. How many parameters?Related

Answer: 5,050

100 × 50 weights + 50 biases.

From Layers, activations and the forward pass

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

  1. This is the last topic of “Neural networks and deep learning”. The module exam comes next.
  2. Module 9 exam: Neural networks and deep learning · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, NLP, embeddings and large language models. 62 topics left in the course before the final exam.

← Previous: CNNs, RNNs and transformers at a glance