cbow word2vec 损失_Word2Vec之Skip-Gram与CBOW模型原理-阿里云开发者社区

word2vec是一个开源的nlp工具,它可以将所有的词向量化。至于什么是词向量,最开始是我们熟悉的one-hot编码,但是这种表示方法孤立了每个词,不能表示词之间的关系,当然也有维度过大的原因。后面发展出一种方法,术语是词嵌入。

[1.词嵌入]

词嵌入(Word Embedding)是NLP中语言模型与表征学习技术的统称,它就是将one-hot表示的词“嵌入”到一个低维空间中,简单点就是嵌入矩阵E与词的one-hot的乘积,数学关系为:

举个例子,如果有2000个词的字典,那么每个词的one-hot形式就是2000*1,如果学习到嵌入矩阵为300 * 2000的化,那么将生成300 * 1的词向量,就那么简单。还有,嵌入矩阵一般这样表示,频数是统计来的:

[2.Word2Vec]

Word2Vec是很流行的词嵌入算法,它通常包含两个模型,一个是Skip-Gram模型,这个模型的思想是,选定中间的词(Context),然后在这个词的正负n个词内选择目标词(Target)与之对应,构造一个用Context预测输出为Target的监督学习问题,训练一个如下图结构的网络(吴恩达的课件):

Skip-Gram

其实还有其他的一些细节,但是说的太细了,写不完,具体我一会慢慢完善。

还有一个是CBOW(Continuous Bag-of-Words Model),它的工作原理与Skip-Gram原理相反,他是用上下文预测中间的词。

接下来,我将用tensorflow来实现一个Skip-Gram,代码来自于tensorflow的word2vec的改编,是基于这位博主,如果想看可以去github看tensorflow的例子。不罗嗦,直接上代码。

3. 高亮一段代码[^code]

import time

import numpy as np

import tensorflow as tf

import random

from collections import Counter

with open('data/text8') as f:

text = f.read()

#定义函数来完成数据的预处理

def preprocess(text, freq=5):

'''

对文本进行预处理

:param text: 文本数据

:param freq: 词频阈值

'''

#对文本中的符号进行替换,下面都是用英文翻译来替代符号

text = text.lower()

text = text.replace('.', ' ')

text = text.replace(',', ' ')

text = text.replace('"', ' ')

text = text.replace(';', ' ')

text = text.replace('!', ' ')

text = text.replace('?', ' ')

text = text.replace('(', ' ')

text = text.replace(')', ' ')

text = text.replace('--', ' ')

text = text.replace(':', ' ')

words = text.split()

#删除低频词,减少噪音影响

word_counts = Counter(words)

trimmed_words = [word for word in words if word_counts[word] > freq] #这句话很好,可以模仿

return trimmed_words

#清洗文本并分词

words = preprocess(text)

# print(words[: 200])

#构建映射表

vocab = set(words)

vocab_to_int = {w: c for c, w in enumerate(vocab)}

int_to_vocab = {c: w for c, w in enumerate(vocab)}

print('total words: {}'.format(len(words))) #还有这种输出方法,学习一下

print('unique words: {}'.format(len(set(words))))

#对原文本进行vocab到int的转换

int_words = [vocab_to_int[w] for w in words]

#--------------------------------------------------------------------采样

t = 1e-5 #t值

threshold = 0.8 #删除概率阈值

#统计单词出现频数

int_word_counts = Counter(int_words)

total_count = len(int_words)

#计算单词频率

word_freqs = {w: c / total_count for w, c in int_word_counts.items()}

#计算单词被删除的概率

prob_drop = {w: 1 - np.sqrt(t / word_freqs[w]) for w in int_word_counts}

#对单词进行采样

train_words = [w for w in int_words if prob_drop[w] < threshold]

print(len(train_words))

#-----------------------------------------------------------------------采样

#获得input word的上下文单词列表

def get_targets(words, idx, window_size = 5):

'''

获得input word的上下文单词列表

:param words: 单词列表

:param idx: input word 的索引号

:param window_size: 窗口大小

'''

target_window = np.random.randint(1, window_size + 1) #从1到 window_size+1 之间的数,包括1,不包括最后一个

#这里要考虑input word前面单词不够的情况,但是为什么没有写后面单词不够的情况呢,

#因为python里面的list取分片越界时会自动只取到结尾的

start_point = idx - target_window if (idx - target_window) > 0 else 0 #虽说不能写三元表达式,但这个挺不错的

end_point = idx + target_window

#output words(即窗口中的上下文单词,不包含目标词,只是它的上下文的词)

targets = set(words[start_point: idx] + words[idx + 1: end_point + 1])

return list(targets)

#构造一个获取batch的生成器

def get_batches(words, batch_size, window_size = 5):

'''

构造一个获取batch的生成器

'''

n_batches = len(words) // batch_size

#仅取full batches

words = words[: n_batches * batch_size]

for idx in range(0, len(words), batch_size): #range(start, stop[, step])

x, y = [], []

batch = words[idx: idx + batch_size]

for i in range(len(batch)):

batch_x = batch[i]

batch_y = get_targets(batch, i, window_size) #从一个batch的第0位开始,一直往下滑,直到最后一个

#由于一个input word会对应多个output word,因此需要长度统一

x.extend([batch_x] * len(batch_y))

y.extend(batch_y)

yield x, y

#构建网络,该部分主要包括:输入层,Embedding,Negative Sampling

train_graph = tf.Graph()

with train_graph.as_default():

inputs = tf.placeholder(tf.int32, shape=[None], name='inputs')

labels = tf.placeholder(tf.int32, shape=[None, None], name='labels') #一般是[batch_size, num_true],num_true一般为1

vocab_size = len(int_to_vocab)

embedding_size = 200 #嵌入维度

with train_graph.as_default():

#嵌入层权重矩阵

embedding = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1, 1)) #tf.random_uniform((4, 4), minval=low,maxval=high,dtype=tf.float32)))返回4*4的矩阵

# ,产生于low和high之间,产生的值是均匀分布的。

embed = tf.nn.embedding_lookup(embedding, inputs) #[None, embedding_size],一般是[batch_size, embedding_size]

#-----------------------------------------------------------负采样(Negative Sampling)

n_sampled = 100

with train_graph.as_default():

sotfmax_w = tf.Variable(tf.truncated_normal([vocab_size, embedding_size], stddev=0.1)) #[vocab_size, dim], dim就是embedding_size

sotfmax_b = tf.Variable(tf.zeros(vocab_size)) #[vocab_size]

#计算negative sampling下的损失

#tf.nn.sampled_softmax_loss()进行了negative sampling,它主要用在分类的类别较大的情况

loss = tf.nn.sampled_softmax_loss(sotfmax_w, sotfmax_b, labels, embed, n_sampled, vocab_size)

cost = tf.reduce_mean(loss)

optimizer = tf.train.AdamOptimizer().minimize(cost)

#-----------------------------------------------------------负采样

#为了直观看到训练结果,我们将查看训练出的相近语义的词

with train_graph.as_default():

#随机挑选一些单词

valid_size = 16

valid_window = 100

#从不同位置各选8个词

valid_examples = np.array(random.sample(range(valid_window), valid_size // 2)) #random.sample(seq, n) 从序列seq中选择n个随机且独立的元素

#np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]]),结果是array([1, 2, 3, 4, 5, 6, 7, 8, 9])

valid_examples = np.append(valid_examples, random.sample(range(1000, 1000 + valid_window), valid_size // 2))

valid_size = len(valid_examples)

#验证单词集

valid_dataset = tf.constant(valid_examples, dtype=tf.int32)

#计算每个词向量的模并进行单位化

norm = tf.sqrt(tf.reduce_sum(tf.squeeze(embedding), 1, keep_dims=True))

normalized_embedding = embedding / norm

#查找验证单词的词向量

valid_embedding = tf.nn.embedding_lookup(normalized_embedding, valid_dataset)

#计算余弦相似度

similarity = tf.matmul(valid_embedding, tf.transpose(normalized_embedding))

#进行训练

epochs = 10 #迭代次数

batch_size = 1000 #batch大小

window_size = 10 #窗口大小

with train_graph.as_default():

saver = tf.train.Saver() #文件存储

with tf.Session(graph=train_graph) as sess:

iteration = 1

loss = 0

sess.run(tf.global_variables_initializer())

for e in range(1, epochs + 1):

batches = get_batches(train_words, batch_size, window_size)

start = time.time()

for x, y in batches:

feed = {

inputs : x,

labels : np.array(y)[:, None]

}

train_loss, _ = sess.run([cost, optimizer], feed_dict=feed)

loss += train_loss

if iteration % 100 == 0:

end = time.time()

print('Epoch {}/{}'.format(e, epochs),

'Iteration: {}'.format(iteration),

'Avg. Training loss: {:.4f}'.format(loss / 100), #这是一种数字格式化的方法,{:.4f}表示保留小数点后四位

'{:.4f} sec / batch'.format((end-start) / 100))

loss = 0

start = time.time()

#计算相似的词

if iteration % 1000 == 0:

#计算similarity

sim = similarity.eval()

for i in range(valid_size):

valid_word = int_to_vocab[valid_examples[i]]

top_k = 8 #取最相似单词的前8个

nearest = (-sim[i, :]).argsort()[1: top_k + 1]

log = 'Nearest to [%s]:' % valid_word

for k in range(top_k):

close_word = int_to_vocab[nearest[k]]

log = '%s%s,' % (log, close_word)

print(log)

iteration += 1

save_path = saver.save(sess, 'model/text8.ckpt')

embed_mat = sess.run(normalized_embedding)

#下面这部分代码出错了,暂时没明白咋回事

import matplotlib.pyplot as plt

from sklearn.manifold import TSNE

viz_words = 500

tsne = TSNE()

embed_tsne = tsne.fit_transform(embed_mat[:viz_words, :])

fig, ax = plt.subplots(figsize=(14, 14))

for idx in range(viz_words):

plt.scatter(*embed_tsne[idx, :], color='steelblue')

plt.annotate(int_to_vocab[idx], (embed_tsne[idx, 0], embed_tsne[idx, 1]), alpha=0.7)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值