Note
This is one of my personal programming assignments after studying the course nlp sequence models at the 2nd week and the copyright belongs to deeplearning.ai.
Emojify!
Welcome to the second assignment of Week 2. You are going to use word vector representations to build an Emojifier.
Have you ever wanted to make your text messages more expressive? Your emojifier app will help you do that. So rather than writing “Congratulations on the promotion! Lets get coffee and talk. Love you!” the emojifier can automatically turn this into “Congratulations on the promotion! 👍 Lets get coffee and talk. ☕️ Love you! ❤️”
You will implement a model which inputs a sentence (such as “Let’s go see the baseball game tonight!”) and finds the most appropriate emoji to be used with this sentence (⚾️). In many emoji interfaces, you need to remember that ❤️ is the “heart” symbol rather than the “love” symbol. But using word vectors, you’ll see that even if your training set explicitly relates only a few words to a particular emoji, your algorithm will be able to generalize and associate words in the test set to the same emoji even if those words don’t even appear in the training set. This allows you to build an accurate classifier mapping from sentences to emojis, even using a small training set.
In this exercise, you’ll start with a baseline model (Emojifier-V1) using word embeddings, then build a more sophisticated model (Emojifier-V2) that further incorporates an LSTM.
Lets get started! Run the following cell to load the package you are going to use.
1 | import numpy as np |
1 - Baseline model: Emojifier-V1
1.1 - Dataset EMOJISET
Let’s start by building a simple baseline classifier.
You have a tiny dataset (X, Y) where:
- X contains 127 sentences (strings)
- Y contains a integer label between 0 and 4 corresponding to an emoji for each sentence
Let’s load the dataset using the code below. We split the dataset between training (127 examples) and testing (56 examples).
1 | X_train, Y_train = read_csv('data/train_emoji.csv') |
1 | maxLen = len(max(X_train, key=len).split()) |
Run the following cell to print sentences from X_train and corresponding labels from Y_train. Change index
to see different examples. Because of the font the iPython notebook uses, the heart emoji may be colored black rather than red.
1 | index = 1 |
I am proud of your achievements 😄
1.2 - Overview of the Emojifier-V1
In this part, you are going to implement a baseline model called “Emojifier-v1”.
The input of the model is a string corresponding to a sentence (e.g. “I love you). In the code, the output will be a probability vector of shape (1,5), that you then pass in an argmax layer to extract the index of the most likely emoji output.
To get our labels into a format suitable for training a softmax classifier, lets convert $Y$ from its current shape current shape $(m, 1)$ into a “one-hot representation” $(m, 5)$, where each row is a one-hot vector giving the label of one example, You can do so using this next code snipper. Here, Y_oh
stands for “Y-one-hot” in the variable names Y_oh_train
and Y_oh_test
:
1 | Y_oh_train = convert_to_one_hot(Y_train, C = 5) |
Let’s see what convert_to_one_hot()
did. Feel free to change index
to print out different values.
1 | index = 50 |
0 is converted into one hot [ 1. 0. 0. 0. 0.]
All the data is now ready to be fed into the Emojify-V1 model. Let’s implement the model!
1.3 - Implementing Emojifier-V1
As shown in Figure (2), the first step is to convert an input sentence into the word vector representation, which then get averaged together. Similar to the previous exercise, we will use pretrained 50-dimensional GloVe embeddings. Run the following cell to load the word_to_vec_map
, which contains all the vector representations.
1 | word_to_index, index_to_word, word_to_vec_map = read_glove_vecs('data/glove.6B.50d.txt') |
You’ve loaded:
word_to_index
: dictionary mapping from words to their indices in the vocabulary (400,001 words, with the valid indices ranging from 0 to 400,000)index_to_word
: dictionary mapping from indices to their corresponding words in the vocabularyword_to_vec_map
: dictionary mapping words to their GloVe vector representation.
Run the following cell to check if it works.
1 | word = "cucumber" |
the index of cucumber in the vocabulary is 113317
the 289846th word in the vocabulary is potatos
Exercise: Implement sentence_to_avg()
. You will need to carry out two steps:
- Convert every sentence to lower-case, then split the sentence into a list of words.
X.lower()
andX.split()
might be useful. - For each word in the sentence, access its GloVe representation. Then, average all these values.
1 | # GRADED FUNCTION: sentence_to_avg |
1 | avg = sentence_to_avg("Morrocan couscous is my favorite dish", word_to_vec_map) |
avg = [-0.008005 0.56370833 -0.50427333 0.258865 0.55131103 0.03104983
-0.21013718 0.16893933 -0.09590267 0.141784 -0.15708967 0.18525867
0.6495785 0.38371117 0.21102167 0.11301667 0.02613967 0.26037767
0.05820667 -0.01578167 -0.12078833 -0.02471267 0.4128455 0.5152061
0.38756167 -0.898661 -0.535145 0.33501167 0.68806933 -0.2156265
1.797155 0.10476933 -0.36775333 0.750785 0.10282583 0.348925
-0.27262833 0.66768 -0.10706167 -0.283635 0.59580117 0.28747333
-0.3366635 0.23393817 0.34349183 0.178405 0.1166155 -0.076433
0.1445417 0.09808667]
Expected Output:
**avg= ** | [-0.008005 0.56370833 -0.50427333 0.258865 0.55131103 0.03104983 -0.21013718 0.16893933 -0.09590267 0.141784 -0.15708967 0.18525867 0.6495785 0.38371117 0.21102167 0.11301667 0.02613967 0.26037767 0.05820667 -0.01578167 -0.12078833 -0.02471267 0.4128455 0.5152061 0.38756167 -0.898661 -0.535145 0.33501167 0.68806933 -0.2156265 1.797155 0.10476933 -0.36775333 0.750785 0.10282583 0.348925 -0.27262833 0.66768 -0.10706167 -0.283635 0.59580117 0.28747333 -0.3366635 0.23393817 0.34349183 0.178405 0.1166155 -0.076433 0.1445417 0.09808667] |
Model
You now have all the pieces to finish implementing the model()
function. After using sentence_to_avg()
you need to pass the average through forward propagation, compute the cost, and then backpropagate to update the softmax’s parameters.
Exercise: Implement the model()
function described in Figure (2). Assuming here that $Yoh$ (“Y one hot”) is the one-hot encoding of the output labels, the equations you need to implement in the forward pass and to compute the cross-entropy cost are:
$$ z^{(i)} = W . avg^{(i)} + b$$
$$ a^{(i)} = softmax(z^{(i)})$$
$$ \mathcal{L}^{(i)} = - \sum_{k = 0}^{n_y - 1} Yoh^{(i)}_k * log(a^{(i)}_k)$$
It is possible to come up with a more efficient vectorized implementation. But since we are using a for-loop to convert the sentences one at a time into the avg^{(i)} representation anyway, let’s not bother this time.
We provided you a function softmax()
.
1 | # GRADED FUNCTION: model |
1 | print(X_train.shape) |
(132,)
(132,)
(132, 5)
never talk to me again
<class 'numpy.ndarray'>
(20,)
(20,)
(132, 5)
<class 'numpy.ndarray'>
Run the next cell to train your model and learn the softmax parameters (W,b).
1 | pred, W, b = model(X_train, Y_train, word_to_vec_map) |
Epoch: 0 --- cost = 1.95204988128
Accuracy: 0.348484848485
Epoch: 100 --- cost = 0.0797181872601
Accuracy: 0.931818181818
Epoch: 200 --- cost = 0.0445636924368
Accuracy: 0.954545454545
Epoch: 300 --- cost = 0.0343226737879
Accuracy: 0.969696969697
[[ 3.]
[ 2.]
[ 3.]
[ 0.]
[ 4.]
[ 0.]
[ 3.]
[ 2.]
[ 3.]
[ 1.]
[ 3.]
[ 3.]
[ 1.]
[ 3.]
[ 2.]
[ 3.]
[ 2.]
[ 3.]
[ 1.]
[ 2.]
[ 3.]
[ 0.]
[ 2.]
[ 2.]
[ 2.]
[ 1.]
[ 4.]
[ 3.]
[ 3.]
[ 4.]
[ 0.]
[ 3.]
[ 4.]
[ 2.]
[ 0.]
[ 3.]
[ 2.]
[ 2.]
[ 3.]
[ 4.]
[ 2.]
[ 2.]
[ 0.]
[ 2.]
[ 3.]
[ 0.]
[ 3.]
[ 2.]
[ 4.]
[ 3.]
[ 0.]
[ 3.]
[ 3.]
[ 3.]
[ 4.]
[ 2.]
[ 1.]
[ 1.]
[ 1.]
[ 2.]
[ 3.]
[ 1.]
[ 0.]
[ 0.]
[ 0.]
[ 3.]
[ 4.]
[ 4.]
[ 2.]
[ 2.]
[ 1.]
[ 2.]
[ 0.]
[ 3.]
[ 2.]
[ 2.]
[ 0.]
[ 3.]
[ 3.]
[ 1.]
[ 2.]
[ 1.]
[ 2.]
[ 2.]
[ 4.]
[ 3.]
[ 3.]
[ 2.]
[ 4.]
[ 0.]
[ 0.]
[ 3.]
[ 3.]
[ 3.]
[ 3.]
[ 2.]
[ 0.]
[ 1.]
[ 2.]
[ 3.]
[ 0.]
[ 2.]
[ 2.]
[ 2.]
[ 3.]
[ 2.]
[ 2.]
[ 2.]
[ 4.]
[ 1.]
[ 1.]
[ 3.]
[ 3.]
[ 4.]
[ 1.]
[ 2.]
[ 1.]
[ 1.]
[ 3.]
[ 1.]
[ 0.]
[ 4.]
[ 0.]
[ 3.]
[ 3.]
[ 4.]
[ 4.]
[ 1.]
[ 4.]
[ 3.]
[ 0.]
[ 2.]]
Expected Output (on a subset of iterations):
**Epoch: 0** | cost = 1.95204988128 | Accuracy: 0.348484848485 |
**Epoch: 100** | cost = 0.0797181872601 | Accuracy: 0.931818181818 |
**Epoch: 200** | cost = 0.0445636924368 | Accuracy: 0.954545454545 |
**Epoch: 300** | cost = 0.0343226737879 | Accuracy: 0.969696969697 |
Great! Your model has pretty high accuracy on the training set. Lets now see how it does on the test set.
1.4 - Examining test set performance
1 | print("Training set:") |
Training set:
Accuracy: 0.977272727273
Test set:
Accuracy: 0.857142857143
Expected Output:
**Train set accuracy** | 97.7 |
**Test set accuracy** | 85.7 |
Random guessing would have had 20% accuracy given that there are 5 classes. This is pretty good performance after training on only 127 examples.
In the training set, the algorithm saw the sentence “I love you“ with the label ❤️. You can check however that the word “adore” does not appear in the training set. Nonetheless, lets see what happens if you write “I adore you.”
1 | X_my_sentences = np.array(["i adore you", "i love you", "funny lol", "lets play with a ball", "food is ready", "not feeling happy"]) |
Accuracy: 0.833333333333
i adore you ❤️
i love you ❤️
funny lol 😄
lets play with a ball ⚾
food is ready 🍴
not feeling happy 😄
Amazing! Because adore has a similar embedding as love, the algorithm has generalized correctly even to a word it has never seen before. Words such as heart, dear, beloved or adore have embedding vectors similar to love, and so might work too—feel free to modify the inputs above and try out a variety of input sentences. How well does it work?
Note though that it doesn’t get “not feeling happy” correct. This algorithm ignores word ordering, so is not good at understanding phrases like “not happy.”
Printing the confusion matrix can also help understand which classes are more difficult for your model. A confusion matrix shows how often an example whose label is one class (“actual” class) is mislabeled by the algorithm with a different class (“predicted” class).
1 | print(Y_test.shape) |
(56,)
❤️ ⚾ 😄 😞 🍴
Predicted 0.0 1.0 2.0 3.0 4.0 All
Actual
0 6 0 0 1 0 7
1 0 8 0 0 0 8
2 2 0 16 0 0 18
3 1 1 2 12 0 16
4 0 0 1 0 6 7
All 9 9 19 13 6 56
**What you should remember from this part**:
- Even with a 127 training examples, you can get a reasonably good model for Emojifying. This is due to the generalization power word vectors gives you.
- Emojify-V1 will perform poorly on sentences such as *"This movie is not good and not enjoyable"* because it doesn't understand combinations of words--it just averages all the words' embedding vectors together, without paying attention to the ordering of words. You will build a better algorithm in the next part.
2 - Emojifier-V2: Using LSTMs in Keras:
Let’s build an LSTM model that takes as input word sequences. This model will be able to take word ordering into account. Emojifier-V2 will continue to use pre-trained word embeddings to represent words, but will feed them into an LSTM, whose job it is to predict the most appropriate emoji.
Run the following cell to load the Keras packages.
1 | import numpy as np |
Using TensorFlow backend.
2.1 - Overview of the model
Here is the Emojifier-v2 you will implement:
2.2 Keras and mini-batching
In this exercise, we want to train Keras using mini-batches. However, most deep learning frameworks require that all sequences in the same mini-batch have the same length. This is what allows vectorization to work: If you had a 3-word sentence and a 4-word sentence, then the computations needed for them are different (one takes 3 steps of an LSTM, one takes 4 steps) so it’s just not possible to do them both at the same time.
The common solution to this is to use padding. Specifically, set a maximum sequence length, and pad all sequences to the same length. For example, of the maximum sequence length is 20, we could pad every sentence with “0”s so that each input sentence is of length 20. Thus, a sentence “i love you” would be represented as $(e_{i}, e_{love}, e_{you}, \vec{0}, \vec{0}, \ldots, \vec{0})$. In this example, any sentences longer than 20 words would have to be truncated. One simple way to choose the maximum sequence length is to just pick the length of the longest sentence in the training set.
2.3 - The Embedding layer
In Keras, the embedding matrix is represented as a “layer”, and maps positive integers (indices corresponding to words) into dense vectors of fixed size (the embedding vectors). It can be trained or initialized with a pretrained embedding. In this part, you will learn how to create an Embedding() layer in Keras, initialize it with the GloVe 50-dimensional vectors loaded earlier in the notebook. Because our training set is quite small, we will not update the word embeddings but will instead leave their values fixed. But in the code below, we’ll show you how Keras allows you to either train or leave fixed this layer.
The Embedding()
layer takes an integer matrix of size (batch size, max input length) as input. This corresponds to sentences converted into lists of indices (integers), as shown in the figure below.
The largest integer (i.e. word index) in the input should be no larger than the vocabulary size. The layer outputs an array of shape (batch size, max input length, dimension of word vectors).
The first step is to convert all your training sentences into lists of indices, and then zero-pad all these lists so that their length is the length of the longest sentence.
Exercise: Implement the function below to convert X (array of sentences as strings) into an array of indices corresponding to words in the sentences. The output shape should be such that it can be given to Embedding()
(described in Figure 4).
1 | # GRADED FUNCTION: sentences_to_indices |
Run the following cell to check what sentences_to_indices()
does, and check your results.
1 | X1 = np.array(["funny lol", "lets play baseball", "food is ready for you"]) |
X1 = ['funny lol' 'lets play baseball' 'food is ready for you']
X1_indices = [[ 155345. 225122. 0. 0. 0.]
[ 220930. 286375. 69714. 0. 0.]
[ 151204. 192973. 302254. 151349. 394475.]]
Expected Output:
**X1 =** | ['funny lol' 'lets play football' 'food is ready for you'] |
**X1_indices =** |
[[ 155345. 225122. 0. 0. 0.] [ 220930. 286375. 151266. 0. 0.] [ 151204. 192973. 302254. 151349. 394475.]] |
Let’s build the Embedding()
layer in Keras, using pre-trained word vectors. After this layer is built, you will pass the output of sentences_to_indices()
to it as an input, and the Embedding()
layer will return the word embeddings for a sentence.
Exercise: Implement pretrained_embedding_layer()
. You will need to carry out the following steps:
- Initialize the embedding matrix as a numpy array of zeroes with the correct shape.
- Fill in the embedding matrix with all the word embeddings extracted from
word_to_vec_map
. - Define Keras embedding layer. Use Embedding(). Be sure to make this layer non-trainable, by setting
trainable = False
when callingEmbedding()
. If you were to settrainable = True
, then it will allow the optimization algorithm to modify the values of the word embeddings. - Set the embedding weights to be equal to the embedding matrix
1 | # GRADED FUNCTION: pretrained_embedding_layer |
1 | embedding_layer = pretrained_embedding_layer(word_to_vec_map, word_to_index) |
weights[0][1][3] = -0.3403
Expected Output:
**weights[0][1][3] =** | -0.3403 |
2.3 Building the Emojifier-V2
Lets now build the Emojifier-V2 model. You will do so using the embedding layer you have built, and feed its output to an LSTM network.
Exercise: Implement Emojify_V2()
, which builds a Keras graph of the architecture shown in Figure 3. The model takes as input an array of sentences of shape (m
, max_len
, ) defined by input_shape
. It should output a softmax probability vector of shape (m
, C = 5
). You may need Input(shape = ..., dtype = '...')
, LSTM(), Dropout(), Dense(), and Activation().
1 | # GRADED FUNCTION: Emojify_V2 |
Run the following cell to create your model and check its summary. Because all sentences in the dataset are less than 10 words, we chose max_len = 10
. You should see your architecture, it uses “20,223,927” parameters, of which 20,000,050 (the word embeddings) are non-trainable, and the remaining 223,877 are. Because our vocabulary size has 400,001 words (with valid indices from 0 to 400,000) there are 400,001*50 = 20,000,050 non-trainable parameters.
1 | model = Emojify_V2((maxLen,), word_to_vec_map, word_to_index) |
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 10) 0
_________________________________________________________________
embedding_2 (Embedding) (None, 10, 50) 20000050
_________________________________________________________________
lstm_1 (LSTM) (None, 10, 128) 91648
_________________________________________________________________
dropout_1 (Dropout) (None, 10, 128) 0
_________________________________________________________________
lstm_2 (LSTM) (None, 128) 131584
_________________________________________________________________
dropout_2 (Dropout) (None, 128) 0
_________________________________________________________________
dense_1 (Dense) (None, 5) 645
_________________________________________________________________
activation_1 (Activation) (None, 5) 0
=================================================================
Total params: 20,223,927
Trainable params: 223,877
Non-trainable params: 20,000,050
_________________________________________________________________
As usual, after creating your model in Keras, you need to compile it and define what loss, optimizer and metrics your are want to use. Compile your model using categorical_crossentropy
loss, adam
optimizer and ['accuracy']
metrics:
1 | model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) |
It’s time to train your model. Your Emojifier-V2 model
takes as input an array of shape (m
, max_len
) and outputs probability vectors of shape (m
, number of classes
). We thus have to convert X_train (array of sentences as strings) to X_train_indices (array of sentences as list of word indices), and Y_train (labels as indices) to Y_train_oh (labels as one-hot vectors).
1 | X_train_indices = sentences_to_indices(X_train, word_to_index, maxLen) |
Fit the Keras model on X_train_indices
and Y_train_oh
. We will use epochs = 50
and batch_size = 32
.
1 | model.fit(X_train_indices, Y_train_oh, epochs = 50, batch_size = 32, shuffle=True) |
Epoch 1/50
132/132 [==============================] - 0s - loss: 1.6086 - acc: 0.1818
Epoch 2/50
132/132 [==============================] - 0s - loss: 1.5867 - acc: 0.3409
Epoch 3/50
132/132 [==============================] - 0s - loss: 1.5721 - acc: 0.2652
Epoch 4/50
132/132 [==============================] - 0s - loss: 1.5540 - acc: 0.3485
Epoch 5/50
132/132 [==============================] - 0s - loss: 1.5413 - acc: 0.3030
Epoch 6/50
132/132 [==============================] - 0s - loss: 1.5195 - acc: 0.3712
Epoch 7/50
132/132 [==============================] - 0s - loss: 1.5275 - acc: 0.3258
Epoch 8/50
132/132 [==============================] - 0s - loss: 1.4633 - acc: 0.4545
Epoch 9/50
132/132 [==============================] - 0s - loss: 1.4320 - acc: 0.4924
Epoch 10/50
132/132 [==============================] - 0s - loss: 1.3712 - acc: 0.6136
Epoch 11/50
132/132 [==============================] - 0s - loss: 1.3441 - acc: 0.6136
Epoch 12/50
132/132 [==============================] - 0s - loss: 1.2784 - acc: 0.6894
Epoch 13/50
132/132 [==============================] - 0s - loss: 1.2723 - acc: 0.6364
Epoch 14/50
132/132 [==============================] - 0s - loss: 1.2651 - acc: 0.6667
Epoch 15/50
132/132 [==============================] - 0s - loss: 1.2106 - acc: 0.6970
Epoch 16/50
132/132 [==============================] - 0s - loss: 1.2334 - acc: 0.7197
Epoch 17/50
132/132 [==============================] - 0s - loss: 1.2150 - acc: 0.7045
Epoch 18/50
132/132 [==============================] - 0s - loss: 1.1613 - acc: 0.7803
Epoch 19/50
132/132 [==============================] - 0s - loss: 1.1587 - acc: 0.7576
Epoch 20/50
132/132 [==============================] - 0s - loss: 1.1129 - acc: 0.8182
Epoch 21/50
132/132 [==============================] - 0s - loss: 1.1016 - acc: 0.8030
Epoch 22/50
132/132 [==============================] - 0s - loss: 1.1939 - acc: 0.6970
Epoch 23/50
132/132 [==============================] - 0s - loss: 1.2618 - acc: 0.6288
Epoch 24/50
132/132 [==============================] - 0s - loss: 1.2123 - acc: 0.6818
Epoch 25/50
132/132 [==============================] - 0s - loss: 1.1606 - acc: 0.7652
Epoch 26/50
132/132 [==============================] - 0s - loss: 1.1066 - acc: 0.8030
Epoch 27/50
132/132 [==============================] - 0s - loss: 1.1312 - acc: 0.7727
Epoch 28/50
132/132 [==============================] - 0s - loss: 1.1400 - acc: 0.7652
Epoch 29/50
132/132 [==============================] - 0s - loss: 1.1107 - acc: 0.8030
Epoch 30/50
132/132 [==============================] - 0s - loss: 1.0676 - acc: 0.8485
Epoch 31/50
132/132 [==============================] - 0s - loss: 1.0660 - acc: 0.8258
Epoch 32/50
132/132 [==============================] - 0s - loss: 1.0450 - acc: 0.8712
Epoch 33/50
132/132 [==============================] - 0s - loss: 1.0246 - acc: 0.8939
Epoch 34/50
132/132 [==============================] - 0s - loss: 1.0163 - acc: 0.8939
Epoch 35/50
132/132 [==============================] - 0s - loss: 1.0080 - acc: 0.9015
Epoch 36/50
132/132 [==============================] - 0s - loss: 1.0144 - acc: 0.9015
Epoch 37/50
132/132 [==============================] - 0s - loss: 1.0861 - acc: 0.8106
Epoch 38/50
132/132 [==============================] - 0s - loss: 1.0484 - acc: 0.8561
Epoch 39/50
132/132 [==============================] - 0s - loss: 1.1126 - acc: 0.7955
Epoch 40/50
132/132 [==============================] - 0s - loss: 1.0712 - acc: 0.8561
Epoch 41/50
132/132 [==============================] - 0s - loss: 1.0277 - acc: 0.8864
Epoch 42/50
132/132 [==============================] - 0s - loss: 1.0459 - acc: 0.8561
Epoch 43/50
132/132 [==============================] - 0s - loss: 1.0214 - acc: 0.8864
Epoch 44/50
132/132 [==============================] - 0s - loss: 1.0012 - acc: 0.9091
Epoch 45/50
132/132 [==============================] - 0s - loss: 0.9877 - acc: 0.9242
Epoch 46/50
132/132 [==============================] - 0s - loss: 0.9827 - acc: 0.9167
Epoch 47/50
132/132 [==============================] - 0s - loss: 0.9835 - acc: 0.9167
Epoch 48/50
132/132 [==============================] - 0s - loss: 0.9817 - acc: 0.9242
Epoch 49/50
132/132 [==============================] - 0s - loss: 0.9894 - acc: 0.9167
Epoch 50/50
132/132 [==============================] - 0s - loss: 0.9780 - acc: 0.9318
<keras.callbacks.History at 0x7f49ffd55e48>
Your model should perform close to 100% accuracy on the training set. The exact accuracy you get may be a little different. Run the following cell to evaluate your model on the test set.
1 | X_test_indices = sentences_to_indices(X_test, word_to_index, max_len = maxLen) |
32/56 [================>.............] - ETA: 0s
Test accuracy = 0.839285714286
You should get a test accuracy between 80% and 95%. Run the cell below to see the mislabelled examples.
1 | # This code allows you to see the mislabelled examples |
Expected emoji:😄 prediction: she got me a nice present ❤️
Expected emoji:😞 prediction: work is hard 😄
Expected emoji:😞 prediction: This girl is messing with me ❤️
Expected emoji:😞 prediction: work is horrible 😄
Expected emoji:😄 prediction: you brighten my day ❤️
Expected emoji:😞 prediction: she is a bully 😄
Expected emoji:😞 prediction: My life is so boring ❤️
Expected emoji:😄 prediction: will you be my valentine 😞
Expected emoji:😄 prediction: What you did was awesome 😞
Now you can try it on your own example. Write your own sentence below.
1 | # Change the sentence below to see your prediction. Make sure all the words are in the Glove embeddings. |
not feeling happy 😄
Previously, Emojify-V1 model did not correctly label “not feeling happy,” but our implementation of Emojiy-V2 got it right. (Keras’ outputs are slightly random each time, so you may not have obtained the same result.) The current model still isn’t very robust at understanding negation (like “not happy”) because the training set is small and so doesn’t have a lot of examples of negation. But if the training set were larger, the LSTM model would be much better than the Emojify-V1 model at understanding such complex sentences.
Congratulations!
You have completed this notebook! ❤️❤️❤️
**What you should remember**: - If you have an NLP task where the training set is small, using word embeddings can help your algorithm significantly. Word embeddings allow your model to work on words in the test set that may not even have appeared in your training set. - Training sequence models in Keras (and in most other deep learning frameworks) requires a few important details: - To use mini-batches, the sequences need to be padded so that all the examples in a mini-batch have the same length. - An `Embedding()` layer can be initialized with pretrained values. These values can be either fixed or trained further on your dataset. If however your labeled dataset is small, it's usually not worth trying to train a large pre-trained set of embeddings. - `LSTM()` has a flag called `return_sequences` to decide if you would like to return every hidden states or only the last one. - You can use `Dropout()` right after `LSTM()` to regularize your network.Congratulations on finishing this assignment and building an Emojifier. We hope you’re happy with what you’ve accomplished in this notebook!
😀😀😀😀😀😀
Acknowledgments
Thanks to Alison Darcy and the Woebot team for their advice on the creation of this assignment. Woebot is a chatbot friend that is ready to speak with you 24/7. As part of Woebot’s technology, it uses word embeddings to understand the emotions of what you say. You can play with it by going to http://woebot.io