Lecture 11: Introduction to Neural Networks
Stanford CS229 lecture 11 notes: Introduction to Neural Networks.
- cs229
- machine-learning
- course-notes
On this page
Starting from logistic regression#
Logistic regression is just one neuron:
A neural network stacks many of those. Each neuron takes some inputs, does a linear function, and then applies an activation function.
The house price example#
Say we want to predict the price of a house from 4 features: size, number of bedrooms, zip code, and wealth of the neighborhood.
We could build neurons whose job is to figure out intermediate things:
- "Family size" (depends on size and bedrooms)
- "Walkability" (depends on zip code)
- "School quality" (depends on zip code and wealth)
Then the final neuron takes these 3 and outputs the price.
graph LR
x1["size"] --> a1["family size"]
x2["bedrooms"] --> a1
x3["zip code"] --> a2["walkability"]
x3 --> a3["school quality"]
x4["wealth"] --> a3
a1 --> y["price"]
a2 --> y
a3 --> yBut here's the beauty of it. We don't tell the network what those hidden neurons should compute. We just connect every input to every hidden neuron and let the training figure out what's useful. The hidden units learn their own features.
Notation#
For a network with layers :
- has shape (neurons in layer ) × (neurons in layer )
- has shape (neurons in layer ) × 1
Example: input of 3 features, 4 hidden units, 1 output. is , is , is , is . Total parameters: .
Vectorization#
Don't loop over neurons or examples. Stack all examples as columns in a matrix of shape . Then:
and so on. This is one big matrix multiplication per layer, which GPUs are extremely good at. A vectorized implementation can be orders of magnitude faster than for-loops.
Activation functions#
- Sigmoid: . Gradients vanish at the extremes.
- Tanh: zero-centered, but still saturates.
- ReLU: . Cheap, doesn't saturate for positive , and the default for hidden layers.
Why do we need non-linear activations? Without them, stacking layers collapses into one linear function: . The non-linearity is what makes the network powerful.
Output layers and cost#
- Binary classification: sigmoid output, cross-entropy loss:
- Multi-class: softmax output, cross-entropy loss
- Regression: no activation (linear) on the output, squared error loss
The cost is the average of the loss over all examples:
We minimize this with gradient descent, and that needs gradients, which is the next lecture.