Join at slido.com�#2095864
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
2095864
Neural Networks (1)
Lecture 12
Non-linearity, Architecture, Activation Functions, Output Layers, and Loss
EECS 189/289, Fall 2025 @ UC Berkeley
Joseph E. Gonzalez and Narges Norouzi
EECS 189/289, Fall 2025 @ UC Berkeley
Joseph E. Gonzalez and Narges Norouzi
Recap: Linear Models
2095864
Logistic Regression on XOR
2095864
Roadmap
2095864
The Neuron
2095864
From Biological to Artificial Neurons
+
Linear Combination �of Inputs
Processing unit
2095864
Neuron – Activation Function
Bias Term
Weights
Output
Linear Combination �of Inputs
Activation
Function
2095864
Perceptron
2095864
Functional Completeness - AND
| | |
1 | 0 | 0 |
1 | 1 | 1 |
0 | 0 | 0 |
0 | 1 | 0 |
1
1
2095864
What are example parameters for the AND gate?�w_0=?, w_1=?, w_2=?
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
2095864
Functional Completeness - AND
| | |
1 | 0 | 0 |
1 | 1 | 1 |
0 | 0 | 0 |
0 | 1 | 0 |
1
1
2095864
Functional Completeness - XOR
| | |
1 | 0 | 1 |
1 | 1 | 0 |
0 | 0 | 0 |
0 | 1 | 1 |
1
1
2095864
Can we build the XOR gate with only one neuron?
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
2095864
Architecture
2095864
XOR with Two Layers
| | |
1 | 0 | 1 |
1 | 1 | 0 |
0 | 0 | 0 |
0 | 1 | 1 |
1
1
Non-linear Decision Boundary
(No linear separability)
| | |
1 | 0 | 1 |
1 | 1 | 0 |
0 | 0 | 0 |
0 | 1 | 0 |
| | |
1 | 0 | 0 |
1 | 1 | 0 |
0 | 0 | 0 |
0 | 1 | 1 |
1
1
1
1
OR
1
1
2095864
Hidden Units Are New Features
2095864
Neural Network
Weights
Bias Term
Pre-activation or logits
Activation Function
Output of basis functions
Hidden layer
Input layer
2095864
Now, let's add output layer.
Hidden layer
Input layer
Output layer
2095864
Deep Neural Network
Input layer
Output layer
Hidden layer 1
Hidden layer 2
Hidden layer 3
2095864
Count the Parameters
784
input
128
hidden
64
hidden
10
output
How many weights and biases?
2095864
Count the Parameters
784
input
128
hidden
64
hidden
10
output
100,480
8,256
650
2095864
Universality Theorem
Visual proof: http://neuralnetworksanddeeplearning.com/chap4.html
Can be realized by a network with one hidden layer
(given enough hidden neurons)
Why “Deep” neural network not “Fat” neural network?
Neural networks are said to be universal approximators.
2095864
Why Deep? Analogy
less gates needed
Logic circuits
Neural network
less parameters
less data?
2095864
Activation Functions
2095864
Depth Alone Does Not Help
2095864
Why Linear Layers Collapse
2095864
Depth Alone Doesn't Help
Demo in the code
2095864
Activation Functions
2095864
Other Activation Functions
We need an activation that has non-zero gradient at least when activations are large positive values.
2095864
ReLU and Its Variants
Neurons with negative activations will receive "no error signal".
A small slope for negative inputs keeps the gradient nonzero.
Also called soft ReLU.
A smooth ReLU, used in transformers. It dips slightly below 0 for negative inputs.
2095864
Training a ReLU Network on XOR
2095864
Where Gradients Vanish
2095864
In a ReLU network, one hidden unit's pre-activation a is negative for every training point. What happens to that unit's incoming weights during gradient descent?
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
2095864
A Dead ReLU Unit
Unit 0 has a negative pre-activation on all 400 training points.
2095864
Output Layers and Loss
2095864
The Output Layer Is a GLM
From linear regression lectures
Neural network
2095864
Match the Output Layer to the Target
Target | Output activation | Likelihood | Loss | PyTorch loss |
a real number | identity | Gaussian | squared error | nn.MSELoss |
one of 2 classes | sigmoid | Bernoulli | binary cross-entropy | nn.BCEWithLogitsLoss |
one of K classes | softmax | categorical | cross-entropy | nn.CrossEntropyLoss |
2095864
Multi-Label: One Sigmoid per Label
One of K classes
Any subset of K labels
a digit from 0 to 9
every animal in a photo
2095864
Building It in PyTorch
2095864
Big Concepts in PyTorch
2095864
torch.nn.Module
When building a new model you extend the nn.Module
class MLP(nn.Module):
def __init__(self, dims, activation="relu"):
super().__init__()
self.layers = nn.ModuleList(
[nn.Linear(dims[i], dims[i+1]) for i in range(len(dims)-1)])
if activation == "relu": self.act = nn.functional.relu
else: self.act = torch.tanh�
def forward(self, x):
for i, layer in enumerate(self.layers):
x = layer(x)
if i < len(self.layers)-1:
x = self.act(x)
return x
Extend nn.Module
Add modules to your module. These add parameters.
Compute the forward pass using the layer modules.
2095864
Implement Gradient Descent with Optimizer
In PyTorch you implement your own gradient descent loop but you can use built in update rules (e.g., Adam) called optimizers.
loader = DataLoader(tr_data, batch_size=32, shuffle=True)
optimizer = torch.optim.Adam(model.parameters(),eta)
for epoch in range(nepochs):
for (x, t) in loader:
optimizer.zero_grad()
loss = loss_fn(model(x), t)
loss.backward()
optimizer.step()
Minibatch SGD DataLoader
Adam optimizer
Epoch is one pass over data
Load one minibatch at a time
Compute the gradient of the loss
Use optimizer to update all the parameters (1-step)
2095864
Demo
Width, depth, and activation
2095864
Neural Networks (1)
Lecture 12
Credit: Joseph E. Gonzalez and Narges Norouzi
Reference: Bishop & Bishop, Deep Learning: Foundations and Concepts, Ch. 6, Sections 6.1, 6.2.1–6.2.3, 6.3.1, 6.4