參考資料:
Coursera-Sequence Models 第二周作業2
作業任務
將句子映射到emoji表情
"Congratulations on the promotion! Lets get coffee and talk. Love you!"
"Congratulations on the promotion! ?? Lets get coffee and talk. ?? Love you! ??"-
數據集
-
第一個模型Emojifier-V1
很簡單的模型,對詞向量進行平均,然后經過一個隱藏層的神經網絡,softmax輸出
計算平均
# GRADED FUNCTION: sentence_to_avg
def sentence_to_avg(sentence, word_to_vec_map):
"""
Converts a sentence (string) into a list of words (strings). Extracts the GloVe representation of each word
and averages its value into a single vector encoding the meaning of the sentence.
Arguments:
sentence -- string, one training example from X
word_to_vec_map -- dictionary mapping every word in a vocabulary into its 50-dimensional vector representation
Returns:
avg -- average vector encoding information about the sentence, numpy-array of shape (50,)
"""
### START CODE HERE ###
# Step 1: Split sentence into list of lower case words (≈ 1 line)
words = sentence.lower().split()
# Initialize the average word vector, should have the same shape as your word vectors.
avg = np.zeros(shape=word_to_vec_map[words[0]].shape)
# Step 2: average the word vectors. You can loop over the words in the list "words".
for w in words:
avg += word_to_vec_map[w]
avg = avg/len(words)
### END CODE HERE ###
return avg
這個模型的準確率:
Train set accuracy 97.7
Test set accuracy 85.7
測試結果:
i adore you ??
i love you ??
funny lol ??
lets play with a ball ?
food is ready ??
not feeling happy ??
但是不能預測好not feeling happy
- confusion_matrix
print(Y_test.shape)
print(' '+ label_to_emoji(0)+ ' ' + label_to_emoji(1) + ' ' + label_to_emoji(2)+ ' ' + label_to_emoji(3)+' ' + label_to_emoji(4))
print(pd.crosstab(Y_test, pred_test.reshape(56,), rownames=['Actual'], colnames=['Predicted'], margins=True))
plot_confusion_matrix(Y_test, pred_test)
-
改進模型Emojifier-V2: Using LSTMs in Keras
因為句子的長度不同,所以需要padding和truncate到相同的長度。
這個模型可以應對反義詞匯。