参考资料:
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到相同的长度。
这个模型可以应对反义词汇。


