05.序列模型 W2.自然语言处理与词嵌入(作业:词向量+Emoji表情生成)
文章目录
- 作业1:
- 1. 余弦相似度
- 2. 单词类比
- 3. 词向量纠偏
- 3.1 消除对非性别词语的偏见
- 3.2 性别词的均衡算法
- 作业2:Emojify表情生成
- 1. Baseline model: Emojifier-V1
- 1.1 数据集
- 1.2 模型预览
- 1.3 实现 Emojifier-V1
- 1.4 在训练集上测试
- 2. Emojifier-V2: Using LSTMs in Keras
- 2.1 模型预览
- 2.2 Keras and mini-batching
- 2.3 Embedding 层
- 2.3 建立 Emojifier-V2
测试题:参考博文
笔记:W2.自然语言处理与词嵌入
作业1:
- 加载预训练的 单词向量,用 cos(θ)cos(\theta)cos(θ) 余弦夹角 测量相似度
- 使用词嵌入解决类比问题
- 修改词嵌入降低性比歧视
import numpy as np
from w2v_utils import *
这个作业使用 50-维的 GloVe vectors 表示单词
words, word_to_vec_map = read_glove_vecs('data/glove.6B.50d.txt')
1. 余弦相似度
CosineSimilarity(u, v)=u.v∣∣u∣∣2∣∣v∣∣2=cos(θ)\text{CosineSimilarity(u, v)} = \frac {u . v} {||u||_2 ||v||_2} = cos(\theta)CosineSimilarity(u, v)=∣∣u∣∣2∣∣v∣∣2u.v=cos(θ)
其中 ∣∣u∣∣2=∑i=1nui2||u||_2 = \sqrt{\sum_{i=1}^{n} u_i^2}∣∣u∣∣2=∑i=1nui2
# GRADED FUNCTION: cosine_similaritydef cosine_similarity(u, v):"""Cosine similarity reflects the degree of similariy between u and vArguments:u -- a word vector of shape (n,) v -- a word vector of shape (n,)Returns:cosine_similarity -- the cosine similarity between u and v defined by the formula above."""distance = 0.0### START CODE HERE #### Compute the dot product between u and v (≈1 line)dot = np.dot(u, v)# Compute the L2 norm of u (≈1 line)norm_u = np.linalg.norm(u)# Compute the L2 norm of v (≈1 line)norm_v = np.linalg.norm(v)# Compute the cosine similarity defined by formula (1) (≈1 line)cosine_similarity = dot/(norm_u*norm_v)### END CODE HERE ###return cosine_similarity
2. 单词类比
例如:男人:女人 --> 国王:王后
# GRADED FUNCTION: complete_analogydef complete_analogy(word_a, word_b, word_c, word_to_vec_map):"""Performs the word analogy task as explained above: a is to b as c is to ____. Arguments:word_a -- a word, stringword_b -- a word, stringword_c -- a word, stringword_to_vec_map -- dictionary that maps words to their corresponding vectors. Returns:best_word -- the word such that v_b - v_a is close to v_best_word - v_c, as measured by cosine similarity"""# convert words to lower caseword_a, word_b, word_c = word_a.lower(), word_b.lower(), word_c.lower()### START CODE HERE #### Get the word embeddings v_a, v_b and v_c (≈1-3 lines)e_a, e_b, e_c = word_to_vec_map[word_a],word_to_vec_map[word_b],word_to_vec_map[word_c]### END CODE HERE ###words = word_to_vec_map.keys()max_cosine_sim = -100 # Initialize max_cosine_sim to a large negative numberbest_word = None # Initialize best_word with None, it will help keep track of the word to output# loop over the whole word vector setfor w in words: # to avoid best_word being one of the input words, pass on them.if w in [word_a, word_b, word_c] :continue### START CODE HERE #### Compute cosine similarity between the vector (e_b - e_a) and the vector ((w's vector representation) - e_c) (≈1 line)cosine_sim = cosine_similarity(e_b-e_a, word_to_vec_map[w]-e_c)# If the cosine_sim is more than the max_cosine_sim seen so far,# then: set the new max_cosine_sim to the current cosine_sim and the best_word to the current word (≈3 lines)if cosine_sim > max_cosine_sim:max_cosine_sim = cosine_simbest_word = w### END CODE HERE ###return best_word
测试:
triads_to_try = [('italy', 'italian', 'spain'), ('india', 'delhi', 'japan'), ('man', 'woman', 'boy'), ('small', 'smaller', 'large')]
for triad in triads_to_try:print ('{} -> {} :: {} -> {}'.format( *triad, complete_analogy(*triad,word_to_vec_map)))
输出:
italy -> italian :: spain -> spanish
india -> delhi :: japan -> tokyo
man -> woman :: boy -> girl
small -> smaller :: large -> larger
额外测试:
good -> ok :: bad -> oops(糟糕)
father -> dad :: mother -> mom
3. 词向量纠偏
研究反映在单词嵌入中的性别偏见,并探索减少这种偏见的算法
g = word_to_vec_map['woman'] - word_to_vec_map['man']
print(g)
输出:向量(50维)
[-0.087144 0.2182 -0.40986 -0.03922 -0.1032 0.94165-0.06042 0.32988 0.46144 -0.35962 0.31102 -0.868240.96006 0.01073 0.24337 0.08193 -1.02722 -0.211220.695044 -0.00222 0.29106 0.5053 -0.099454 0.404450.30181 0.1355 -0.0606 -0.07131 -0.19245 -0.06115-0.3204 0.07165 -0.13337 -0.25068714 -0.14293 -0.224957-0.149 0.048882 0.12191 -0.27362 -0.165476 -0.204260.54376 -0.271425 -0.10245 -0.32108 0.2516 -0.33455-0.04371 0.01258 ]
print ('List of names and their similarities with constructed vector:')# girls and boys name
name_list = ['john', 'marie', 'sophie', 'ronaldo', 'priya', 'rahul', 'danielle', 'reza', 'katy', 'yasmin']for w in name_list:print (w, cosine_similarity(word_to_vec_map[w], g))
输出:
List of names and their similarities with constructed vector:
john -0.23163356145973724
marie 0.315597935396073
sophie 0.31868789859418784
ronaldo -0.31244796850329437
priya 0.17632041839009402
rahul -0.16915471039231716
danielle 0.24393299216283895
reza -0.07930429672199553
katy 0.2831068659572615
yasmin 0.2331385776792876
可以看出,
- 女性的名字往往与向量
05.序列模型 W2.自然语言处理与词嵌入(作业:词向量+Emoji表情生成)相关推荐
- 05.序列模型 W2.自然语言处理与词嵌入
文章目录 1. 词汇表征 2. 使用词嵌入 3. 词嵌入的特性 4. 嵌入矩阵 5. 学习词嵌入 6. Word2Vec 7. 负采样 8. GloVe 词向量 9. 情感分类 10. 词嵌入除偏 作 ...
- 2021-1-9 吴恩达-C5 序列模型-w2 自然语言处理与词嵌(课后编程1-Operations on word vectors 词向量运算-含UnicodeDecodeError解决)
原文链接 如果打不开,也可以复制链接到https://nbviewer.jupyter.org中打开. 自然语言处理与词嵌 Operations on word vectors 词向量运算 1-余弦相 ...
- 2020-11-11 吴恩达DL学习-C5 序列模型-W2 自然语言处理与词嵌入(2.9 情绪分类-使用RNN模型,考虑词序)
1.视频网站:mooc慕课https://mooc.study.163.com/university/deeplearning_ai#/c 2.详细笔记网站(中文):http://www.ai-sta ...
- 05.序列模型 W1.循环序列模型
文章目录 1. 为什么选择序列模型 2. 数学符号 3. 循环Recurrent 神经网络模型 4. 通过时间的反向传播 5. 不同类型的循环神经网络 6. 语言模型和序列生成 7. 对新序列采样 8 ...
- 05.序列模型 W1.循环序列模型(作业:手写RNN+恐龙名字生成)
文章目录 作业1:建立你的循环神经网络 1. RNN 前向传播 1.1 RNN 单元 1.2 RNN 前向传播 2. LSTM 网络 2.1 LSTM 单元 2.2 LSTM 前向传播 3. RNN ...
- NLP:Word Embedding词嵌入/word2vec词向量思想方法(一种主流的分布式表示)的简介、使用方法、案例应用之详细攻略
NLP:Word Embedding词嵌入/word2vec词向量思想方法(一种主流的分布式表示)的简介.使用方法.案例应用之详细攻略 目录 词嵌入(一种主流的分布式表示)的简介 词嵌入的案例理解
- 05.序列模型 W3.序列模型和注意力机制(作业:机器翻译+触发词检测)
文章目录 作业1:机器翻译 1. 日期转换 1.1 数据集 2. 用注意力模型进行机器翻译 2.1 注意力机制 3. 可视化注意力 作业2:触发词检测 1. 数据合成:创建语音数据集 1.1 听一下数 ...
- 05.序列模型 W3.序列模型和注意力机制
文章目录 1. 基础模型 2. 选择最可能的句子 3. 集束搜索(Beam Search) 4. 改进集束搜索 5. 集束搜索的误差分析 6. Bleu 得分 7. 注意力模型直观理解 8. 注意力模 ...
- 词嵌入、句向量等方法汇总
在cips2016出来之前,笔者也总结多类似词向量的内容,自然语言处理︱简述四大类文本分析中的"词向量"(文本词特征提取)事实证明,笔者当时所写的基本跟CIPS2016一章中总结的 ...
最新文章
- Spark和Hadoop,孰优孰劣?
- virtualbox 安装ubuntu 时,看不到继续、退出按钮?共享文件无权限?
- 人类第一次将42写成3个整数的立方和,最后一个100以内的自然数告破
- matlab 最小二乘法拟合_机器学习十大经典算法之最小二乘法
- java 自动封装_自动补全的java封装
- Jquery的事件操作和文档操作
- Telnet连接详细步骤
- 【图像处理面试题】——1
- MyEclipse中SVN的使用方法
- 云计算时代企业内部IT人员的新定位
- Halcon算子学习:get_sheet_of_light_result
- pythonocc 扩展数据_如何在pythonOCC中使用样条函数?
- 小熊的人生回忆(二)
- RabbitMQ基本管理(下)
- Nginx正确记录post日志的方法
- python取列表前几个元素_从Python列表中获取前n个唯一元素
- linux全局启动tomcat,linux下启动tomcat服务
- python json转字符串_python如何将json转成字符串
- 国外常用免费博客平台
- PVE系列教程(十五)、安装Windows10系统(专业版、企业版、家庭版通用)
热门文章
- tornado学习笔记day06-应用安全
- Django中object.all
- 关于在新建的package中用SetContentView()函数时无法找到已创建的R.layout的布局文件的的问题的解决办法
- python深入_Python深入(上)
- JPG PNG GIF BMP图片格式的区别
- 关于scanf和的问题
- 计算机常用存储芯片分为内存和什么,计算机类论文范例,与安徽省计算机一级考试宝典相关研究生毕业论文开题报告范文...
- 【floyd】【bitset】洛谷 P1841 [JSOI2007]重要的城市 题解
- Introducing the ClearGLASS App on ClearOS
- HTML5权威指南 11.通信API
- 05.序列模型 W2.自然语言处理与词嵌入