Lesson 2 of 8 · The Idea
From Machine Learning to Deep Learning
How deep learning differs from classical machine learning: hand-made features versus learned ones, end-to-end models, data needs, and how to choose.
- 7 min read
- Beginner
- Reviewed
- What feature engineering is and why it limits classical models
- How end-to-end learning lets a network find its own features
- Why a straight line is not enough, and how layers bend the boundary
- How to choose between classical machine learning and deep learning
The classical recipe: features first
Suppose you want a classical model, such as logistic regression, to spot cars in photos. It cannot use the raw photo well, so a person first turns each photo into a short list of useful measurements:
- Write code that finds straight edges, round shapes (wheels) and bright spots (headlights).
- Turn those findings into numbers, such as "number of round shapes" or "width divided by height".
- Train the model on that table of numbers.
photo -> features designed by a person -> classical model -> "car"The model can only be as good as those features. If nobody thought of a feature that separates cars from vans, the model cannot learn the difference.
The deep learning recipe: learn the features too
Deep learning removes the hand-made step. The raw data goes straight into the network, and its layers learn the features while learning the task:
photo -> neural network (learns features and the answer together) -> "car"This is called end-to-end learning: one model is trained from the raw input all the way to the final answer. Early layers end up detecting edges, middle layers combine them into parts, and the last layers decide.
One straight line is not enough
A single neuron draws one straight line (or a flat plane) through the data. Many real patterns are not separated by a straight line. Below, the points inside a diamond are class 1 and the points outside are class 0. A one-line model gets some of them wrong. A small network with one hidden layer of four neurons gets all of them right, because the hidden neurons combine four straight edges into the diamond's shape.
points = [(0.0, 0.0), (0.5, 0.3), (-0.4, 0.4), (1.2, 0.0), (0.0, -1.5), (-0.9, 0.8)]
def relu(z):
return max(0.0, z)
def one_line(x1, x2):
# one neuron: everything on one side of the line x1 + x2 = 1
return 1 if x1 + x2 < 1 else 0
def network(x1, x2):
# hidden layer: four neurons, each looking in one direction
h = [relu(x1), relu(-x1), relu(x2), relu(-x2)]
# output neuron: inside when the four add up to less than 1
return 1 if 1 - sum(h) > 0 else 0
line_right = net_right = 0
print("point true one line network")
for x1, x2 in points:
true = 1 if abs(x1) + abs(x2) < 1 else 0
a, b = one_line(x1, x2), network(x1, x2)
line_right += a == true
net_right += b == true
print(f"({x1:4.1f}, {x2:4.1f}) {true} {a} {b}")
print(f"one line: {line_right}/6 correct, network: {net_right}/6 correct")Here the weights were chosen by hand to show the idea. In real deep learning, training finds them.
More data helps deep learning more
Andrew Ng often showed a simple picture in his talks to explain why deep learning took over:
accuracy
^ large network
| ......
| .......
| .... ______ classical model
| .. _______/
| ._____/
+---------------------------------> amount of data- Classical models improve quickly at first, then level off. Extra data adds little.
- Large neural networks keep improving for much longer as data grows, because they have far more parameters that can capture more detail.
Choosing between them
| Question | Classical machine learning | Deep learning |
|---|---|---|
| Type of data | Tables: rows and columns | Images, audio, text, video |
| Amount of data | Hundreds to millions of rows | Usually many thousands of examples or more |
| Features | Designed by people | Learned by the network |
| Hardware | An ordinary laptop | Often one or more GPUs |
| Training time | Seconds to minutes | Minutes to weeks |
| Explaining decisions | Easier | Harder |
One exception to the data rule: with transfer learning, you start from a network already trained on a huge dataset and adjust it with a few hundred of your own examples. You will meet it in later courses.
Common mistakes
Work out how many raw numbers a network receives for each input. Imagine designing features by hand from 150,528 numbers: that is why learned features matter.
# A network reads raw data as one long list of numbers. # Print how many numbers each input contains: # a 28 x 28 grayscale image (1 number per pixel) # a 224 x 224 colour image (3 numbers per pixel: red, green and blue) # one second of audio, recorded 16,000 times per second # Output must be exactly: # 784 # 150528 # 16000
What does end-to-end learning mean?
Summary
- Classical models learn from features designed by people; their quality limits the model.
- Deep learning is end to end: the network learns the features and the answer together.
- One neuron draws a straight line. Hidden layers combine lines into curved or closed boundaries.
- Deep learning gains more from more data. For small tables, classical models usually win.
Next: the kinds of problems neural networks can solve.
Finished reading? Tick it off to track your progress.