NLP2
NLP2
Thank you!!
56
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
X
At its heart, a neural unit is taking z= z=a weighted
b+ w·x+wi xib sum of its inputs
neural network is a singleNeural
computational unit.
Network A unit
Unit takes Neural unit
mbers as input, performs some bias term
computation on tional
them,
This is not in your brain and term in the sum called a bias ter m. i Given a set of inputs x1
As defined in [Link]
a set of corresponding 7.2, zis just1a...w
realnvalued
and anumber.
unit is taking a weighted sum of its inputs, with one addi- Often it’smoreconvenient tow expressthisweighted bias sum b,using
so the weighte
vector notation
alled a bias term. Given a set of inputs x1...xn, a unit represented
has
y vector
Finally, instead of using z, a linear function of x, as the output,
as:linear algebra that a vector is, at heart, just a list or array of number
from
eights w1...wn and aOutput
bias b, sovalue
the weighted sum z can be • Take weighted sum Xwillplus
of inputs, a biasto the output of this
a apply
we’ll a non-linear
talk about z infunction
terms of af to z.
weight We
vector w, refer
a scalar bias b, and an inpu
X z = b+ w i xi
the activation value for the unit, a. Since we are just modeling a sin
z= b+ wi xi
Non-linear transform σ
activation
(7.1) x, and we’ll replace the sum with the convenient dot product:
i
z
i
activation for thenodeisin fact thefinal output of thenetwork, which we
todefined
nt to express this weighted sum using vector notation; recall
∑ it’smore
Weighted sum
a vector is, at heart, just a list or array of [Link]
Thus
call y. So the value y is
convenient expressas:z=
thisw·x+ b
weighted sum using vector
ms of a weight vector w, a scalar bias b, and an input vector • Instead of just using z, we'll apply a nonlinear activation
Weightsvector w1 w2 w3 from b
biasalgebra
linear that a vector
function f: is, at heart, just a list or array
of
sum with the convenient dot product:
As defined in Eq. 7.2, zis just a realyvalued
= a =number.
f (z)
Input layer x1 we’
x2 ll talk
x3 about
+1 z in terms of a weight vector w, a scalar bias b, and
z= w·x+ b (7.2) Finally, instead of using z, a linear function of x, as the output, neur
x, and we’apply
ll replace the sum
a non-linear with
function the
f to z. convenient dot
We will refer to theproduct:
output of this fun
s just a real valued number. We’ll discuss three popular non-linear functions f () below (the sigmo
activation
sing z, a linear function of x, as the output, neural units 57
the activation
and the rectified
BITS Pilani, Deemed to be University under value
Section
BITS for the
linear unit, but
ReLU)
3 ofPilani
Pilani, UGC Act, a. Since
1956
Campus we are just modeling
it’s pedagogically a single
convenient to su BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
y = s (w·x+ b) = 11 = The 11
resulting output y=would 11
y
be: = s =(w·x+
.70 b) = 11 = 11 =
y = s (w·x+ b) = 1+ e−−(w·x+ b) = 1+ e−−(.5
(w·x+b) (.5⇤⇤.2+.6
.2+ ⇤
.6⇤ .3+.1
.3+ ⇤
.1⇤ .5) = 1+ ey
.9+.5)
.9+ −
=
− 0.87 s =(w·x+
0.87 .70 b) = 1+ e−−(w·x+ (w·x+b)b) = 1+ e−−(.5(.5⇤⇤.2+.6
.2+ ⇤
.6⇤ .3+.1
.3+ ⇤
.1⇤ .5) =
.9+.5)
.9+
1+ e 1+ e 1+ e 1+ e 1+ e
1 1 1
Inpractice,
practice, the
thesigmoid
sigmoid is isnot
not commonly
commonly used y = sas
used (w·x+ b) =
an activation
activation [Link]=practice,
practice,
A1+ function the sigmoid is = commonly
not = used
.70 as an activation f
In as an e− (w·x+In
1+ function. b) A e− (.5the
function ⇤.2+sigmoid
.6⇤.3+ .1⇤.9+is.5)not 1+ e− 0.87 used
commonly as an activation fu
anh
anh
that is very similar but almost always better is the
that is very similar but almost always betterInispractice, tanh
the tanh function
function shown
tanh
shown
tanh
that
thatin Fig.
is
in is
Fig. very
7.3a;
very
7.3a;similar
similar but
but almost always
almostfunction.
always A better
better is the tanh function
is the tanh function
tanh is
isaavariant
variant of
of the
thesigmoid
sigmoid that
that ranges
rangesfrom
from -1 -1 to the
to +1:
+1: sigmoid is not commonly
tanh is used
isaavariant
variant ofas an activation
of the
thesigmoid
sigmoid that function
that ranges
rangesfrom
from -1 -1 to
to +1:
+1:
tanh tanhBITS Pilani, Deemed very tanh
thatto beisUniversity under
BITSsimilar
Section 3 ofPilani
Pilani, UGC Act, but
1956
Campus almost always better is the tanh function shown in Fig. 7.3a;
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
h an example just
Let’s
Let’s walkto get
walk an intuition.
through
through Let’sjust
an example
an example suppose
just getwe
to get
to have a Let’s
an intuition.
an intuition. Let’s suppose
suppose we
we have
have a y=
weight
g weightvector
vector
unit with
and
and
with the
bias:
bias:
thefollowing
following weight
weight vector
vector and
and bias: 1 1 1 ez + e− z
unit An example ybias:
= s (w·x+ b) = = Non-Linear = Functions = .70
1+ e− (w·x+ b) 1+ e− (.5⇤.2+ .6Activation
⇤.3+ .1⇤.9+ .5) 1+ e− 0.87 besides
ww == [0.2,
These activation 0.3,
functions
[0.2, 0.9]
0.3,have0.9]different properties that make them useful for The simplest activation function, and perhaps the most commo
sigmoid
ww == [0.2,
[0.2,
For In 0.3,
0.3, 0.9]
0.9]
practice, the sigmoid
func- is not commonly used as an activation function. A function
different language applications or network architectures.
ion has thebnice b ==properties
0.5
0.5 of being smoothly bbdifferentiable
example, thetanh
ReLU tified linear unit, also called the ReLU, shown in Fig. 7.3b. It
=tanh0.5that and is very
mapping similar but
outlier almost always better is the tanh function shown in Fig. 7.3a;
= 0.5 when z is from
positive,
values toward the mean. The rectifier function, on the other hand tanh has is anice
variantproperties
of the sigmoid that ranges -1 to and
+1: 0 otherwise: Most Common:
do • Suppose
ohatwith
result
with thefrom
the following
What would a
it unit
being
following
would thishas:input
very close
input
thisunit vector:
unit doto linear.
do with
with the In the sigmoid
thefollowing or
following input tanh functions,
input vector:
vector: very
What
high •valueswof=z result in values of y that are satur ated, i.e., extremely close to 1, ez − e− z
[0.2,0.3,0.9] y= z −z y = max(z,0)
(7.5)
and have derivatives
xx == very
[0.5,
[0.5, close
0.6,
0.6, to
0.1]0. Zero derivatives cause problems for learning, e + e
• b = 0.5 x == [0.5,
[0.5,0.6,
0.6,0.1] 0.1]
because as we’ ll see in Section 7.4, we’ ll trainxnetworks by propagating an error
signal• backwards,
What happens with input
multiplying x: (partial derivatives) from each layer of the function, and perhaps the most commonly used, is the rec-
gradients The simplest activation
would
would be:
be:
The resulting output y would be: U totified also called the ReL U, shown in Fig. 7.3b. It’s just the same as x
• The
network; x resulting
gradients that are
= [0.5,0.6,0.1]output
almosty0 would
cause be:
the error ReL
signal linearand
get smaller unit,
smaller
until it is too small to be used for training, a problem called the when x is positive,
vanishing gr adientand 0 otherwise:
11 1 11 11 1
1=ReLU 11
[Link]fiers
= ==
s (w·x+ don’ tb)
have= this problem, since
==
= the derivative of = .70
.70 for high = = .70
.70
−−(w·x+ y z=b)iss1 (w·x+
(w·x+ b)
e−−(.5
(.5⇤=.2+
⇤ ⇤.3+
.2+close
.6⇤.3+−.1
.1 ⇤
⇤.9+
(w·x+ .5) = 1+
.5)
b) 1+ e−−−(.5⇤
0.87
0.87.2+.6 ⇤
.6⇤ .3+.1.1⇤⇤ .5) = 1+ e−− 0.87
.9+.5) y ==max(x,
0.87 0) (7.6)
ee
values of b) 1+ ethan
1+
rather very 1+
.6
1+ e e−
to 0. .9+b)
(w·x+ 1+
1+ e ee
− (.5 ⇤ .2+ .3+ .9+ 1+ e ReLU
disisnotIn
not practice, the
commonly the sigmoid
used as an
an isisnot
not commonly
commonly
activation [Link] as
AAas an activation
functionactivation function.
function. A tanh
A function
function Rectified Linear Unit
Incommonly
practice, used as
sigmoid activation function.
used function
an
XOR
almostthat
anh
almost
anh problem
that isvery
always
always
is very similar
better
better isthe
is
similar buttanh
the
but almost
tanh
almost always
function
function
always betterin
shown
shown
better isFig.
in
is the tanh
7.3a;
[Link]
the function shown
7.3a;function shown in
in Fig.
Fig. 7.3a;
7.3a;
sigmoid
sigmoid tanh
that
that
tanh isranges
is aa variant
ranges
variant of the
from
from
of the sigmoid
-1 sigmoid
-1 to
to that ranges
+1: that
+1: rangesfrom
from -1-1 to
to +1:
+1: 66
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
Early in the history of eneural −networks it was realized thatezzthe − − z of neural net-
power
z − e− z
z z e−
y = e− e y = e −from e zcombining (7.5)
works, as with the
y =
real
z
neurons
The− z
that
XOR inspired
problem them,
y comes
= e z + e− z (7.5) (7.5)
these
(7.5) Perceptrons
units into larger [Link] ++ ee− z ez + e− z
One The
of thesimplest
most cleveractivation
demonstrations of the need
function, andfor multi-layer
perhaps the networks
most was
commonly used, is the rec-
nhefunction,
proofThe and
by simplest
Minsky perhaps the
andactivation
Papert most
(1969) commonly
function,
that and
aMinsky
single used,
perhaps
neural is
unit the
the rec-
most
cannot commonly
compute used, is the rec-
function,
tified and perhaps
linear the
unit,of also most
called commonly
the ReLU, used,
shown is the rec-
in Fig. 7.3b. It’s just the same as x
eLU
called the
tified ReL U, shown in Fig. 7.3b. It’sand Papert (1969)
just the same as x
x areIt’s just the same as x
some very simple functions
linear shown
unit, alsoits input. Consider
called7.3b. the
the ReLU, task of computing elementary
LU
alled functions
ogical
the
when ReLU,
x is
of positive,
two inputs,
in
and
like
Fig.
0
AND,otherwise:
OR,
It’sXOR.
and
justshown As
in Fig.
thea reminder,
same 7.3b.
ashere
d 0 otherwise:
when x• is Can neural units
positive, andcompute
0 otherwise:simple functions of input? • A very simple neural unit
0heotherwise:
truth tables for those functions:
y = max(x, 0) (7.6) •(7.6)
Binary output (0 or 1)
y = max(x,
AND 0) y = max(x,0)
y = max(x, 0) OR XOR
(7.6) •(7.6)
No non-linear activation function
x1 x2 y x1 x2 y x1 x2 y
0 0 0 0 0 0 0 0 0
0 1 0 0 1 1 0 1 1
1 0 0 1 0 1 1 0 1
1 1 1 1 1 1 1 1 0
This example was first shown for the per ceptr on, which is a very simple neural
unit that has a binary output and does not have a non-linear activation function. The
0 x1 0
h1
0 1 0 1 2
77 78
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
• Example: W b
Input layer
x xn +1
(vector)
1
W b vector W b
Input layer Input layer
x i xn +1 x xn +1
(vector) (vector)
1 1
W b W[1 b[1]
]
Input layer
x xn +1
(vector) x i xn +1
1
1
85
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
U U
h1 h2 h3 … hn
1
h1 h2 h3 … hn
1
W b W
x1 x2 … xn
0 +1 x0=1 x1 x2 … xn
0
89
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
σ
σ U
2-layer
Logistic
Regression W feedforward
network
W
x1 x
n
f1 f2 fn x1 x
n
f1 f2 fn
• Just adding a hidden layer to logistic regression
92
91 92
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
Even better: representation learning Neural Net Classification with embeddings as input
features!
σ
• The real power of deep learning comes from U
the ability to learn features from the data
• Instead of using hand-built human- W
engineered features for classification
• Use learned representations like x1 x
n
embeddings! e1 e2 en
93 94
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
95 96
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
Neural Language Models (LMs) Simple feedforward Neural Language Models
• Language Modeling: Calculating the probability of the next • Task: predict next word wt
word in a sequence given some history. • given prior words wt-1, wt-2, wt-3, …
• We've seen N-gram based LMs • Problem: Now we’re dealing with sequences of arbitrary
• But neural network LMs far outperform n-gram language length.
models • Solution: Sliding windows (of fixed length)
•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!
97 98
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
U Backward pass
Forward pass
W
Training instance x1 xn
99 100
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
Intuition: Training a 2-layer network Reminder: Loss Function for binary logistic
regression
101 102
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
Reminder: gradient descent for weight updates Where did that derivative come from?
ecause we don’t just want to move left or right, we want to know where
mensional space (of the N parameters that make up q) we should move. 104
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
Summary
How can I find that gradient for
every weight in the network?
• These derivatives on the prior slide only give the updates for • For training, we need the derivative of the loss with respect
one weight layer: the last one! to weights in early layers of the network
• What about deeper networks? • But loss is computed only at the very end of the network!
• Lots of layers, different activation functions? • Solution: backward differentiation
• Solution in the next lecture: • Given a computation graph and the derivatives of all the
• Even more use of the chain rule!! functions in it we can automatically compute the derivative
of the loss with respect to these early weights.
• Computation graphs and backward differentiation!
105 106
BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus BITS Pilani, Deemed to be University under Section
BITS 3 ofPilani
Pilani, UGC Act, 1956
Campus
T
• (n-1)th order Markov assumptionP(w1 , w2 ,..., wt 1 , wT ) P(wt | w1 , w2 ,..., wt 1 )
t 1
• Each p(wi |wi−4 , w i−3 , w i−2 , w i−1 ) may not have enough statistics to estimate
• we back off to p(wi |wi−3 , w i−2 , w i−1 ), p(wi |w i−2 , w i−1 ), etc., all the way to p(wi)
107
• Language Modeling: Calculating the probability of the next • Task: predict next word wt
word in a sequence given some history. given prior words wt-1, wt-2, wt-3, …
• We've seen N-gram based LMs • Output : probability distribution over possible next words.
• But neural network LMs far outperform n-gram language
models
• State-of-the-art neural LMs are based on more powerful
neural network technology like Transformers
• Problem: Now we’re dealing with sequences of arbitrary
• But simple feedforward LMs can do almost as well! length.
10 11
9 • Solution: Sliding windows (of fixed length) 0
• Word Embeddings
•• Why Neural
Neural LMsmodels
language work better thanwords
represent N-gram LMsprior context by
in this
• one-hot vector representation , e.g., dog = (0,0,0,0,1,0,0,0,0,....), cat = (0,0,0,0,0,0,0,1,0,....) their embeddings, rather than just by their word identity as used
• Represent each of the N previous words as a one-hot vector of length |V| one-hot vector , i.e., with one in n-gram language models.
dimension for each word in the vocabulary •• embeddings
Using embeddings allows neural language models to generalize
• word “toothpaste”, supposing it is V5, i.e., index 5 in the vocabulary, x5 = 1, and xi = 0 i 5,
better to unseen data.
Training data:
• We've seen: I have to make sure that the cat gets fed.
Embedding matrix One-hot vector
Equations:
• Two ways:
• Freeze the embedding layer E with initial word2vec values.
– Freezing means we use word2vec or some other pretraining algorithm to
compute the initial embedding matrix E, and then hold it constant while we
only Modify W, U, and b, i.e., we don’t update E during language model
training
concatenate 3 embeddings for the 3 context words
to produce the embedding layer e
• Learn the embeddings simultaneously with training the network.
11
– Useful when the task the network is designed for (like sentiment
3 classification, translation, or parsing) places strong constraints on what makes
a good representation for words.
• N-gram vs NLM
FAQ Assistant
• Assistant knows you much more in • Group of AI assistants that know every
customer personally
detail
• Eventually run large parts of company
• Quickly checks a few final things operations—from lead generation over
before giving you a quote tailored to marketing, sales, HR, or finance
your actual situation.
• Image Recognition
• Speech Recognition
[Link]