词向量的基本操作:
导入包:
加载单词向量:
1 余弦相似度:
预期输出:
cosine_similarity(father, mother) = 0.8909038442893615
cosine_similarity(ball, crocodile) = 0.2743924626137942
cosine_similarity(france - paris, rome - italy) = -0.6751479308174202
2 单词类比任务:
预期输出:
italy -> italian :: spain -> spanish
india -> delhi :: japan -> tokyo
man -> woman :: boy -> girl
small -> smaller :: large -> larger
Emoji表情情感分类器:
导入:
1 基准模型:Emojifier-V1
预期输出:
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]
预期输出 (on a subset of iterations):
Epoch: 0 --- cost = 1.9520498812810076
Accuracy: 0.3484848484848485
Epoch: 100 --- cost = 0.07971818726014794
Accuracy: 0.9318181818181818
Epoch: 200 --- cost = 0.04456369243681402
Accuracy: 0.9545454545454546
Epoch: 300 --- cost = 0.03432267378786059
Accuracy: 0.9696969696969697
预期输出:
Training set:
Accuracy: 0.9772727272727273
Test set:
Accuracy: 0.8571428571428571
2 Emojifier-V2:在Keras中使用LSTM:![](https://i-blog.csdnimg.cn/blog_migrate/1c529c2e1c442f899913f74cbc9f6026.png)
练习:实现以下函数,将X(字符串形式的句子数组)转换为与句子中单词相对应的索引数组。输出维度应使其可以赋予Embedding()
预期输出:
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.]]
预期输出:
weights[0][1][3] = -0.3403
练习:实现Emojify_V2()
,它构建图3所示结构的Keras图。该模型将由input_shape
定义的维度为(m
, max_len
, )的句子数组作为输入。 它应该输出形状为softmax的概率向量(m
, C = 5
)。你可能需要Input(shape = ..., dtype = '...')
,LSTM(), Dropout(), Dense(), 和 Activation()。
<keras.callbacks.History at 0x7f0c6f629860>
Test accuracy = 0.8359756123546232
not feeling happy 😞