CS307
Machine Learning
Lecture 24
Neural Networks
Krishnendu Ghosh
Department of Computer Science & Engineering
Indian Institute of Information Technology Dharwad
Neural Networks
Neural networks are machine learning models that mimic the complex functions of the human brain. These models consist of interconnected nodes or neurons that process data, learn patterns, and enable tasks such as pattern recognition and decision-making.
2
Neural Networks
Neural networks are capable of learning and identifying patterns directly from data without predefined rules. These networks are built from several key components:
3
Neural Networks
Learning in neural networks follows a structured, three-stage process:
4
Neural Networks
5
Neural Networks
Importance of Neural Networks
Neural networks are pivotal in identifying complex patterns, solving intricate challenges, and adapting to dynamic environments. Their ability to learn from vast amounts of data is transformative, impacting technologies like natural language processing, self-driving vehicles, and automated decision-making.
Neural networks streamline processes, increase efficiency, and support decision-making across various industries. As a backbone of artificial intelligence, they continue to drive innovation, shaping the future of technology.
6
Neural Networks
Layers in Neural Network Architecture
7
Neural Networks
Forward Propagation
When data is input into the network, it passes through the network in the forward direction, from the input layer through the hidden layers to the output layer. This process is known as forward propagation. Here’s what happens during this phase:
8
Neural Networks
Backpropagation
After forward propagation, the network evaluates its performance using a loss function, which measures the difference between the actual output and the predicted output. The goal of training is to minimize this loss. This is where backpropagation comes into play:
9
Neural Networks
Types of Neural Networks
10
Neural Networks
Advantages of Neural Networks
11
Neural Networks
Disadvantages of Neural Networks
12
Units in Neural Networks
This is in your brain
14
By BruceBlaus - Own work, CC BY 3.0, https://commons.wikimedia.org/w/index.php?curid=28761830
Neural Network Unit: This is not in your brain
15
Weights
Input layer
Weighted sum
Non-linear transform
Output value
bias
Neural unit
Take weighted sum of inputs, plus a bias
Instead of just using z, we'll apply a nonlinear activation function f:
Non-Linear Activation Functions
17
Sigmoid
We're already seen the sigmoid for logistic regression:
Final function the unit is computing
Final unit again
19
Weights
Input layer
Weighted sum
Non-linear activation function
Output value
bias
An example
Suppose a unit has:
w = [0.2,0.3,0.9]
b = 0.5
What happens with input x:
x = [0.5,0.6,0.1]
An example
Suppose a unit has:
w = [0.2,0.3,0.9]
b = 0.5
What happens with the following input x?
x = [0.5,0.6,0.1]
An example
Suppose a unit has:
w = [0.2,0.3,0.9]
b = 0.5
What happens with input x:
x = [0.5,0.6,0.1]
An example
Suppose a unit has:
w = [0.2,0.3,0.9]
b = 0.5
What happens with input x:
x = [0.5,0.6,0.1]
Non-Linear Activation Functions besides sigmoid
24
tanh
ReLU
Rectified Linear Unit
Most Common:
The XOR problem
The XOR problem
Can neural units compute simple functions of input?
Minsky and Papert (1969)
Perceptrons
A very simple neural unit
Easy to build AND or OR with perceptrons
AND
OR
Easy to build AND or OR with perceptrons
AND
OR
Easy to build AND or OR with perceptrons
AND
OR
Not possible to capture XOR with perceptrons
Pause the lecture and try for yourself!
Why? Perceptrons are linear classifiers
Perceptron equation given x1 and x2, is the equation of a line
w1x1 + w2x2 + b = 0
(in standard linear format: x2 = (−w1/w2)x1 + (−b/w2) )
This line acts as a decision boundary
Decision boundaries
XOR is not a linearly separable function!
Solution to the XOR problem
XOR can't be calculated by a single perceptron
XOR can be calculated by a layered network of units.
ReLU
ReLU
Solution to the XOR problem
XOR can't be calculated by a single perceptron
XOR can be calculated by a layered network of units.
The hidden representation h
(With learning: hidden layers will learn to form useful representations)
Feedforward Neural Networks
Feedforward Neural Networks
Can also be called multi-layer perceptrons (or MLPs) for historical reasons
Binary Logistic Regression as a 1-layer Network
39
w
xn
x1
+1
w1
wn
b
(y is a scalar)
σ
Output layer
(σ node)
Input layer
vector x
(we don't count the input layer in counting layers!)
(vector)
(scalar)
Multinomial Logistic Regression as a 1-layer Network
40
W
xn
x1
Fully connected single layer network
W is a matrix
+1
y is a vector
y1
yn
b is a vector
b
s
s
s
Output layer
(softmax nodes)
Input layer
scalars
Reminder: softmax: a generalization of sigmoid
For a vector z of dimensionality k, the softmax is:
Example:
Two-Layer Network with scalar output
U
W
xn
x1
+1
y is a scalar
b
hidden units
(σ node)
Input layer
(vector)
Output layer
(σ node)
Could be ReLU
Or tanh
Two-Layer Network with scalar output
U
W
xn
x1
+1
b
hidden units
(σ node)
Input layer
(vector)
Output layer
(σ node)
i
j
Wji
vector
y is a scalar
Two-Layer Network with scalar output
U
W
xn
x1
+1
b
hidden units
(σ node)
Input layer
(vector)
Output layer
(σ node)
Could be ReLU
Or tanh
y is a scalar
Two-Layer Network with softmax output
U
W
xn
x1
+1
b
hidden units
(σ node)
Input layer
(vector)
Output layer
(σ node)
Could be ReLU
Or tanh
y is a vector
Multi-layer Notation
W[1]
xn
x1
+1
b[1]
i
j
W[2]
b[2]
sigmoid or softmax
ReLU
Multi Layer Notation
47
Replacing the bias unit
Let's switch to a notation without the bias unit
Just a notational change
Replacing the bias unit
Instead of: We'll do this:
x= x1, x2, …, xn0
x= x0, x1, x2, …, xn0
Replacing the bias unit
Instead of: We'll do this:
Applying feedforward networks to NLP tasks
Use cases for feedforward networks
Let's consider 2 (simplified) sample tasks:
State of the art systems use more powerful neural architectures, but simple models are useful to consider!
Classification: Sentiment Analysis
We could do exactly what we did with logistic regression
Input layer are binary features as before
Output layer is 0 or 1
U
W
xn
x1
σ
Sentiment Features
55
Classifying sentiment for input x
56
Suppose w =
b = 0.1
Classifying sentiment for input x
57
Feedforward nets for simple classification
Just adding a hidden layer to logistic regression
58
U
W
xn
x1
f1
f2
fn
58
W
xn
x1
f1
f2
fn
Logistic
Regression
2-layer
feedforward
network
σ
σ
Even better: representation learning
The real power of deep learning comes from the ability to learn features from the data
Instead of using hand-built human-engineered features for classification
Use learned representations like embeddings!
U
W
xn
x1
e1
e2
en
σ
Neural Net Classification with embeddings as input features!
Issue: texts come in different sizes
This assumes a fixed size length (3)!
Kind of unrealistic.
Some simple solutions (more sophisticated solutions later)
Reminder: Multiclass Outputs
What if you have more than two output classes?
U
W
xn
x1
Neural Language Models (LMs)
Language Modeling:
Calculating the probability of the next word in a sequence given some history.
State-of-the-art neural LMs are based on more powerful neural network technology like Transformers
But simple feedforward LMs can do almost as well!
Simple feedforward Neural Language Models
Task: predict next word wt
given prior words wt-1, wt-2, wt-3, …
Problem: Now we’re dealing with sequences of arbitrary length.
Solution: Sliding windows (of fixed length)
Neural Language Model
Why Neural LMs work better than N-gram LMs
Training data:
We've seen: I have to make sure that the cat gets fed.
Never seen: dog gets fed
Test data:
I forgot to make sure that the dog gets ___
N-gram LM can't predict "fed"!
Neural LM can use similarity of "cat" and "dog" embeddings to generalize and predict “fed” after dog
Training Neural Nets: Overview
Intuition: training a 2-layer Network
U
W
xn
x1
Training instance
Forward pass
Backward pass
Intuition: Training a 2-layer network
Reminder: Loss Function for binary logistic regression
A measure for how far off the current answer is to the right answer
Cross entropy loss for logistic regression:
Reminder: gradient descent for weight updates
Let's see if this works for our sentiment example
True value is y=1. How well is our model doing?
Pretty well! What's the loss?
Let's see if this works for our sentiment example
Suppose true value instead was y=0.
What's the loss?
Let's see if this works for our sentiment example
The loss when model was right (if true y=1)
Is lower than the loss when model was wrong (if true y=0):
Sure enough, loss was bigger when model was wrong!
Where did that derivative come from?
Using the chain rule! f (x) = u(v(x))
Intuition (see the text for details)
Derivative of the Loss
Derivative of the Activation
Derivative of the weighted sum
How can I find that gradient for every weight in the network?
These derivatives on the prior slide only give the updates for one weight layer: the last one!
What about deeper networks?
Solution in the next lecture:
Stochastic Gradient Descent
Our goal: minimize the loss
Intuition of gradient descent
How do I get to the bottom of this river canyon?
x
Look around me 360∘
Find the direction of steepest slope down
Go that way
Our goal: minimize the loss
For logistic regression, loss function is convex
Let's first visualize for a single scalar w
Q: Given current w, should we make it bigger or smaller?
A: Move w in the reverse direction from the slope of the function
Let's first visualize for a single scalar w
Q: Given current w, should we make it bigger or smaller?
A: Move w in the reverse direction from the slope of the function
So we'll move positive
Let's first visualize for a single scalar w
Q: Given current w, should we make it bigger or smaller?
A: Move w in the reverse direction from the slope of the function
So we'll move positive
Gradients
The gradient of a function of many variables is a vector pointing in the direction of the greatest increase in a function.
Gradient Descent: Find the gradient of the loss function at the current point and move in the opposite direction.
How much do we move in that direction ?
Now let's consider N dimensions
We want to know where in the N-dimensional space (of the N parameters that make up θ ) we should move.
The gradient is just such a vector; it expresses the directional components of the sharpest slope along each of the N dimensions.
Imagine 2 dimensions, w and b
Visualizing the gradient vector at the red point
It has two dimensions shown in the x-y plane
Real gradients
Are much longer; lots and lots of weights
For each dimension wi the gradient component i tells us the slope with respect to that variable.
The gradient is then defined as a vector of these partials.
The gradient
The final equation for updating θ based on the gradient is thus
What are these partial derivatives for logistic regression?
The loss function
The elegant derivative of this function (see textbook 5.8 for derivation)
Hyperparameters
The learning rate η is a hyperparameter
Hyperparameters:
Stochastic Gradient Descent: An example and more details
Working through an example
One step of gradient descent
A mini-sentiment example, where the true y=1 (positive)
Two features:
x1 = 3 (count of positive lexicon words)
x2 = 2 (count of negative lexicon words)
Assume 3 parameters (2 weights and 1 bias) in Θ0 are zero:
w1 = w2 = b = 0
η = 0.1
Example of gradient descent
Update step for update θ is:
where
Gradient vector has 3 dimensions:
w1 = w2 = b = 0;
x1 = 3; x2 = 2
Example of gradient descent
Update step for update θ is:
where
Gradient vector has 3 dimensions:
w1 = w2 = b = 0;
x1 = 3; x2 = 2
Example of gradient descent
Update step for update θ is:
where
Gradient vector has 3 dimensions:
w1 = w2 = b = 0;
x1 = 3; x2 = 2
Example of gradient descent
Update step for update θ is:
where
Gradient vector has 3 dimensions:
w1 = w2 = b = 0;
x1 = 3; x2 = 2
Example of gradient descent
Update step for update θ is:
where
Gradient vector has 3 dimensions:
w1 = w2 = b = 0;
x1 = 3; x2 = 2
Example of gradient descent
η = 0.1;
Now that we have a gradient, we compute the new parameter vector θ1 by moving θ0 in the opposite direction from the gradient:
Example of gradient descent
η = 0.1;
Now that we have a gradient, we compute the new parameter vector θ1 by moving θ0 in the opposite direction from the gradient:
Example of gradient descent
η = 0.1;
Now that we have a gradient, we compute the new parameter vector θ1 by moving θ0 in the opposite direction from the gradient:
Example of gradient descent
η = 0.1;
Now that we have a gradient, we compute the new parameter vector θ1 by moving θ0 in the opposite direction from the gradient:
Note that enough negative examples would eventually make w2 negative
Mini-batch training
Stochastic gradient descent chooses a single random example at a time.
That can result in choppy movements
More common to compute gradient over batches of training instances.
Batch training: entire dataset
Mini-batch training: m examples (512, or 1024)
Computation Graphs and Backward Differentiation
Why Computation Graphs
For training, we need the derivative of the loss with respect to each weight in every layer of the network
Solution: error backpropagation (Rumelhart, Hinton, Williams, 1986)
Computation Graphs
A computation graph represents the process of computing a mathematical expression
Example:
Computations:
Example:
109
Computations:
Backwards differentiation in computation graphs
The importance of the computation graph comes from the backward pass
This is used to compute the derivatives that we’ll need for the weight update.
Example
111
We want:
The chain rule
Computing the derivative of a composite function:
f (x) = u(v(x))
f (x) = u(v(w(x)))
Example
113
Example
114
Example
115
Example
116
Backward differentiation on a two layer network
117
σ
W[2]
W[1]
y
x2
x1
Sigmoid activation
ReLU activation
1
1
b[1]
b[2]
Backward differentiation on a two layer network
118
Backward differentiation on a 2-layer network
Summary
For training, we need the derivative of the loss with respect to weights in early layers of the network
Solution: backward differentiation
Given a computation graph and the derivatives of all the functions in it we can automatically compute the derivative of the loss with respect to these early weights.
121
Extra: Pooling
There are various ways to represent an input for classification. One simple baseline is to apply some sort of pooling function to the embeddings of all the words in the input. For example, for a text with n input words w1, ..., wn, we can turn the n embeddings e(w1), ..., e(wn), each of dimension d, into a single embedding by just summing the embeddings, or by taking their mean:
122
Extra: Pooling
There are other options, like taking element-wise max.
x = mean(e(w1), e(w2), . . . , e(wn))
h = σ (Wx + b)
z = Uh
ŷ = softmax(z)
123
Extra: Pretraining
Here are the final equations for computing the output class distribution for an entire test set:
H = σ (XWT | + b)
Z = HUT
Ŷ = softmax(Z)
The idea of using word2vec or GloVe embeddings as our input representation and more generally the idea of relying on another algorithm to have already learned an embedding representation for our input words is called pretraining.
124
Extra: One-hot Vector
A vector with one value equal to 1 and the rest 0, is called a one-hot vector.
125
Extra: Dropout
Various forms of regularization are used to prevent overfitting.
One of the most important is dropout: randomly dropping some units and their connections from the network during training.
At each iteration of training, we repeatedly choose a probability p and for each unit we replace its output with zero with probability p.
126
Thank You
127