Homework 3 Language Modeling


Rate this product

CPSC 477


Problem Grade
QUESTION 1 (Language Modeling) /10
QUESTION 2 (Recurrent Neural Networks) /10
QUESTION 3 (Morphology) /10
QUESTION 4 (Text Similarity) /10
QUESTION 5 (Dimensionality Reduction /10
QUESTION 6 (NaΓ―ve Bayes Classifier) /10
QUESTION 7 (Training Neural Networks) /10
QUESTION 8 (Word Embeddings) /10
Submission Instructions
1. Submit each solution on a separate sheet.
2. Include your name, netID and problem number on each page.
QUESTION 1 (Language Modeling)
You are in a noisy coffee shop, diligently studying for your midterm, and your friend is
trying to get your attention, using only a two words vocabulary. She has said a sentence
but you couldn’t hear one of the words:
(𝑀1 = β€²β„Žπ‘–β€²
; 𝑀2 =
; 𝑀3 =? ? ? ; 𝑀4 =
Assume that your friend was generating words from this first-order Markov model:
𝑝(β€²β„Žπ‘–β€²|β€²β„Žπ‘–β€²) = 0.7 𝑝(β€²π‘¦π‘œβ€²|β€²β„Žπ‘–β€²) = 0.3
𝑝(β€²β„Žπ‘–β€²|β€²π‘¦π‘œβ€²) = 0.5 𝑝(β€²π‘¦π‘œβ€²|β€²π‘¦π‘œβ€²) = 0.5
Given these parameters, what is the posterior probability of whether the missing word is
β€˜hi’ or β€˜yo’? Show your work.
QUESTION 2 (Recurrent Neural Networks)
Your goal will be to set up weights for a simple recurrent network (SRN and also known
as an Elman Netowork) to solve two simple sequence tasks. The network reads in a
sequence π‘₯1, . . , π‘₯𝑑and produces outputs 𝑦1
, . . , 𝑦𝑑
. We consider the final output 𝑦𝑑
to be the
answer given by the network.
We will assume a threshold activation function:
𝑓(π‘₯) = 1 if π‘₯ 0; otherwise 𝑓(π‘₯) = 0.
The network has one simple recurrent layer followed by a linear layer (shown in below
diagram). The dimension of both the input to the network and its output should be 1.
1. Construct an SRN with 1 hidden unit that reads a sequence of 1s and 0s and
outputs 1 if any of π‘₯1, .., π‘₯𝑑 had the value 1. Justify the correctness of your network.
2. Construct an SRN with 2 hidden units to solve the parity task (read in a sequence
of 1s and 0s and return the sum mod 2, shown in the below table). Justify the
correctness of your network.
π‘₯ 𝑦
0 0
1 1
010 1
0110 0
10101 1
QUESTION 3 (Morphology)
Refer to the slides for lecture #143 for background on this question.
1. Consider the English verb drink. Give two inflected forms of drink, and two
derivational forms of drink.
2. Identify all the morphemes in infallibilities. For each morpheme, say whether it is a
prefix, suffix, or root.
3. Consider the word unnegotiable. Draw and label two morphological trees for this
word, one which corresponds to the meaning β€˜not able to be negotiated’, and
another which corresponds to β€˜able to be unnegotiated’.
QUESTION 4 (Text Similarity)
We first need to introduce a function that compares the similarity of two sentences.
Recall the definition for the cosine distance between two vectors π‘₯ and 𝑦:
π‘π‘œπ‘ π‘–π‘›π‘’(π‘₯, 𝑦) =
βˆ‘ π‘₯𝑖𝑦𝑖
βˆšβˆ‘ π‘₯
𝑛 2
𝑖=1 βˆšβˆ‘ 𝑦
𝑛 2
Part A: How would you define a mapping from sentences s to vectors 𝑓(𝑠) such that the
cosine distance π‘π‘œπ‘ π‘–π‘›π‘’(𝑓(𝑠1), 𝑓(𝑠2)) is a measure of the similarity between two
sentences 𝑠1 and 𝑠2? We will give full marks for any reasonable mapping.
Part B: Now, we will define a dynamic programming algorithm that computes sentence
alignment of two translated documents. The alignment score is the sum of the similarity
scores of the aligned sentences. Our goal is to find an alignment with the highest score.
We will consider alignments of the following form:
● a sentence can be aligned to an empty sentence. This happens when one of the
translators omits a sentence.
● a sentence can be aligned to exactly one sentence.
● a sentence can be aligned to two sentences. This happens when one of the
translators either breaks or joins sentences.
Our sentence alignment algorithm recursively computes the alignment matrix 𝐹 indexed
by 𝑖 and 𝑗, one index for each sentence. The value stored in 𝐹(𝑖,𝑗) is the score of the
best alignment between the first 𝑖 sentences of π‘₯ and the first 𝑗 sentences of 𝑦. 𝑠(π‘₯𝑖
, 𝑦𝑗)
is the similarity between sentence π‘₯𝑖 and sentence 𝑦𝑗
Based on the above information, please
Define 𝐹(0,0), 𝐹(𝑖, 0) and 𝐹(0,𝑗).
Define 𝐹(𝑖,𝑗) for 𝑖 0 and 𝑗 0.
Part C: Next, we will modify the alignment score to be the same as before, but to include
a fixed penalty 𝑝 each time a sentence is aligned to an empty sentence. 𝑝 is a parameter,
which is = 0, chosen by the user of the algorithm. Describe a modified dynamic
programming method that takes this new penalty into account.
QUESTION 5 (Dimensionality reduction and SVD)
(a) Prove that the left singular vectors of a matrix 𝐴 are the right singular vectors of 𝐴
(b) Calculate the covariance matrix for the matrix 𝑀 shown below.
𝑀 = [
1 1 0 1
0 0 0 1
1 1 0 0
(c) Find the singular values of the matrix 𝑀
QUESTION 6 (NaΓ―ve Bayes Classifier)
Suppose you are given the following set of data with three Boolean input variables π‘Ž, 𝑏,
and 𝑐, and a single Boolean output variable 𝐾.
π‘Ž 𝑏 𝑐 𝑲
1 0 1 0
1 1 1 1
0 1 1 0
1 1 0 0
1 0 1 0
0 0 0 1
0 0 0 1
0 0 1 0
(For parts (a) and (b), assume we are using a NaΓ―ve Bayes classifier to predict the
value of K from the values of the other variables.)
(a) According to the naive Bayes Classifier, what is 𝑝(𝐾 = 1|π‘Ž = 1, 𝑏 = 1, 𝑐 = 0) ?
(b) According to the naive Bayes Classifier, what is 𝑝(𝐾 = 0|π‘Ž = 1, 𝑏 = 1) ?
(c) Given 𝑃(𝑍|𝑋) = 0.7 and 𝑃(𝑍|π‘Œ) = 0.4.
Do you have enough information to compute 𝑃(𝑍|𝑋, π‘Œ)? If not, write β€œnot enough info”.
If so, compute the value of 𝑃(𝑍|𝑋, π‘Œ) from the above information
(e) Given 𝑃(𝑍, 𝑋) = 0.2, 𝑃(𝑋) = 0.3, 𝑃(π‘Œ) = 1
Do you now have enough information to compute 𝑃(𝑍|𝑋, π‘Œ)? If not, write β€œnot enough
info”. If so, compute the value of 𝑃(𝑍|𝑋, π‘Œ)from the above information.
QUESTION 7 (Training Neural Networks)
This question is to train a three-layer neural network for classification task. Let 𝐻1 denote
the number of hidden units, let 𝐷 be the dimension of the input 𝑋, and let 𝐾 be the
number of classes. The three-layer network has the following form:
β„Ž1 = π‘…π‘’πΏπ‘ˆ(π‘Š1𝑋+𝑏1
β„Ž2 = π‘…π‘’πΏπ‘ˆ(π‘Š2β„Ž1 +𝑏2
𝑝 = π‘†π‘œπ‘“π‘‘π‘šπ‘Žπ‘₯(π‘Š3β„Ž2 + 𝑏3)
where the parameters of the network are π‘Š1 ∈ 𝑅
, 𝑏1∈ 𝑅
, π‘Š2 ∈ 𝑅
𝑏2∈ 𝑅
, π‘Š3 ∈ 𝑅
, 𝑏3∈ 𝑅
π‘…π‘’πΏπ‘ˆ is the β€œrectified linear unit” π‘…π‘’πΏπ‘ˆ(π‘₯) = π‘šπ‘Žπ‘₯{0, π‘₯} applied componentwise, and
SoftMax maps a d-vector to the probability simplex according to
π‘†π‘œπ‘“π‘‘π‘šπ‘Žπ‘₯(𝑣)π‘˜ =
βˆ‘ 𝑒π‘₯𝑝(𝑣𝑗)
For a given input 𝑋, this specifies how to calculate the probabilities 𝑃(π‘Œ = π‘˜|𝑋) for π‘˜ =
1,2, . . . , π‘˜.
For a given training set {(𝑋𝑖
, π‘Œπ‘–
)}, consider the loss function:
𝐿 =
βˆ‘βˆ’ π‘™π‘œπ‘” 𝑝(π‘Œ = π‘Œπ‘–
) +
2 + β€–π‘Š2β€–
2 + β€–π‘Š3β€–
where ‖𝐴‖
2 means the sum of the squares of the entries of the matrix 𝐴.
Give formulas for the derivatives for each layer of the network𝑗 = 1,2,3.
i. You should ignore the fact that π‘…π‘’πΏπ‘ˆ(π‘₯) is not differentiable at π‘₯𝑖 = 0.
ii. You might try to first do the calculations with π‘…π‘’πΏπ‘ˆ() replaced by the identity function.
QUESTION 8 (Word Embeddings1
Word2Vec represents a family of word-embedding algorithms that are commonly used in
a variety of contexts.
(a) Let’s consider a recommender system for an online music-streaming service. It
includes information about a set of songs on how frequently users play them together
(e.g., song X is commonly played together with song Y). Explain how you would use
ideas similar to Word2Vec to recommend the next song to a user who has played
some songs from this list.
(b) Although pre-trained word vectors work quite well in many applications, it is
sometimes better to β€˜β€™re-train” (i.e. continue to learn) the word vectors as parameters
of our neural network. Explain why retraining the word vectors may hurt the model
performance if the dataset for the specific task is too small.
(c) Give two examples of how we can evaluate word-embedding vectors.
(d) A simple way to produce word embeddings for a vocabulary of words is to create onehot vectors for each word – a vector with 0s everywhere and only a single 1 in the
position corresponding to the word’s index in the vocabulary. Give at least two reasons
why word2vec-produced word embeddings are better than these one-hot vectors.

Open chat
Need help?
Can we help you?