1、单词的表达方式:
①One hot representation
②Distributed representation
2、One hot representation(每个单词都是一个维度,彼此独立)
当有n个词时,就会创造n维向量。假设某个词的索引为i,则对应的n维向量在第i个位置为1,其他均为0.
【缺点】①无法计算单词之间的相似度;②每个单词在空间中都是正交的向量,彼此之间没有联系.
3、Distributed representation
【举例】给定四个单词,寻找四个单词的关系。
0 | 1 | |
gender | female | male |
age | child | adult |
对应上述表格,girl对应编码为[0,1],man对应编码为[1,1](第一维度为gender,第二维度为age)
用两个节点去表示四个单词,可降低维度、减少训练所需要的参数数量。同时,与girl共享相同的连接的其他输入例子也可以被训练到。word embedding就是要从数据中自动学习输入空间到distributed representation空间的映射 f 。
4、一般使用的低维embedding
将n维向量降成v维向量,矩阵W为Rn-->Rv的映射关系,W为随机初始化并且是可训练的。
低维向量嵌入在tensorflow中的实现:tf.nn.embedding_lookup()
embedding_map = tf.get_variable( # 默认参数trainable = True,可训练
name="embedding_map",
shape=[self.config.vocab_size, self.config.embedding_size],
initializer=self.initializer)
seq_embeddings = tf.nn.embedding_lookup(embedding_map, self.input_seqs)
【原理】tf.nn.embedding_lookup(params, ids, partition_strategy='mod', max_norm=None)
按照ids从params这个矩阵中取向量(行),ids就是这个矩阵的索引(行号)
5、使用word2vec进行word embedding
①跳字模型(skip-gram):通过中心词预测周围词
②连续词袋模型(continuous bag of words, CBOW):通过中心词周围的词来预测中心词