1.导入引用的python库
# encoding=utf8
import io
import os
import sys
import requests
from collections import OrderedDict
import math
import random
import numpy as np
import paddle
from paddle.nn import Embedding
import paddle.nn.functional as F
import paddle.nn as nn
2.数据处理
# 下载语料用来训练word2vec
def download():
# 可以从百度云服务器下载一些开源数据集(dataset.bj.bcebos.com)
corpus_url = "https://dataset.bj.bcebos.com/word2vec/text8.txt"
# 使用python的requests包下载数据集到本地
web_request = requests.get(corpus_url)
corpus = web_request.content
# 把下载后的文件存储在当前目录的text8.txt文件内
with open("./text8.txt", "wb") as f:
f.write(corpus)
f.close()
download()
2.1 读取数据
# 读取text8数据
def load_text8():
with open("./text8.txt", "r") as f:
corpus = f.read().strip("n")
f.close()
return corpus
corpus = load_text8()
# 打印前500个字符,简要看一下这个语料的样子
print(corpus[:500])
anarchism originated as a term of abuse first used against early working class radicals including the diggers of the english revolution and the sans culottes of the french revolution whilst the term is still used in a pejorative way to describe any act that used violent means to destroy the organization of society it has also been taken up as a positive label by self defined anarchists the word anarchism is derived from the greek without archons ruler chief king anarchism as a political philoso
2.2 语料切词
一般来说,在自然语言处理中,需要先对语料进行切词。对于英文来说,可以比较简单地直接使用空格进行切词:
# 对语料进行预处理(分词)
def data_preprocess(corpus):
# 由于英文单词出现在句首的时候经常要大写,所以我们把所有英文字符都转换为小写,
# 以便对语料进行归一化处理(Apple vs apple等)
corpus = corpus.strip().lower()
corpus = corpus.split(" ")
return corpus
corpus = data_preprocess(corpus)
print(corpus[:50])
['anarchism', 'originated', 'as', 'a', 'term', 'of', 'abuse', 'first', 'used', 'against', 'early', 'working', 'class', 'radicals', 'including', 'the', 'diggers', 'of', 'the', 'english', 'revolution', 'and', 'the', 'sans', 'culottes', 'of', 'the', 'french', 'revolution', 'whilst', 'the', 'term', 'is', 'still', 'used', 'in', 'a', 'pejorative', 'way', 'to', 'describe', 'any', 'act', 'that', 'used', 'violent', 'means', 'to', 'destroy', 'the']
2.3 排序
重点是学习sorted方法,还是直接文心一言学习,比搜索快,且准确:
Python中的sorted()方法是一个内置函数,用于对可迭代对象(如列表、元组、字符串等)进行排序。它的基本语法如下:
sorted(iterable, key=None, reverse=False, key default=None)
其中,iterable 是要排序的可迭代对象,key 参数是可选的,用于指定排序规则,reverse 参数设置为 True 表示降序排序,key 参数默认为 None,表示使用默认的排序规则。key 参数的默认值为 None,表示使用默认的排序规则。
下面是一个使用sorted()方法对列表进行排序的示例:
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
# 使用 sorted() 方法进行排序
sorted_list = sorted(my_list)
# 输出排序后的结果
print(sorted_list)
输出结果为:
[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
在上面的示例中,我们首先定义了一个包含多个元素的列表 my_list,然后使用 sorted() 方法对列表进行排序,并将结果存储在 sorted_list 变量中。最后,我们输出排序后的结果。
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
# 使用 sorted() 方法进行排序
sorted_list = sorted(my_list)
# 输出排序后的结果
print(sorted_list)
[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]
- 对语料进行统计,构造ID
- 频次越高,ID越小,以方便对词典进行管理
my_dict = {'apple': 1, 'banana': 2, 'orange': 3}
print(my_dict)
# 按照键值对的值进行排序,并翻转结果
sorted_items = sorted(my_dict.items(), key=lambda x: x[1], reverse=True)
# 输出结果
print(sorted_items) # [('apple', 1), ('banana', 2), ('orange', 3)]
{'apple': 1, 'banana': 2, 'orange': 3}
[('orange', 3), ('banana', 2), ('apple', 1)]
-
可以使用 key 参数来指定排序规则。在上面的例子中,key 参数使用 lambda x: x[1] 来指定排序规则,其中 x[1] 表示 x 的第二个元素。
-
reverse 参数设置为 True 表示降序排序,默认情况下是升序排序。如果不设置 reverse 参数,默认情况下会根据键的字母顺序进行排序。
-
最后得到的结果是按照第二个元素(即值)进行排序,得到的是一个降序排列的字典。
def build_dict(corpus):
# 定义词典,存储单词出现的频率
word_freq_dict = dict()
for word in corpus:
if word not in word_freq_dict:
word_freq_dict[word] = 0
word_freq_dict[word] += 1
# 对单词出现的频率进行排序
word_freq_dict = sorted(word_freq_dict.items(), key=lambda x: x[1], reverse=True)
# 构造3个不同的词典,分别存储,
# 每个词到id的映射关系:word2id_dict
# 每个id出现的频率:word2id_freq
# 每个id到词的映射关系:id2word_dict
word2id_dict = dict()
word2id_freq = dict()
id2word_dict = dict()
# 按照频率,从高到低,开始遍历每个单词,并为这个单词构造一个独一无二的id
# key value, 直接对应过去
for word, freq in word_freq_dict:
# 牛逼,id直接按长度增加,不过求len和 i++不知道哪个开销少
curr_id = len(word2id_dict)
word2id_dict[word] = curr_id
word2id_freq[word2id_dict[word]] = freq
id2word_dict[curr_id] = word
return word2id_freq, word2id_dict, id2word_dict
word2id_freq, word2id_dict, id2word_dict = build_dict(corpus)
vocab_size = len(word2id_freq)
print("there are totoally %d different words in the corpus" % vocab_size)
for _, (word, word_id) in zip(range(50), word2id_dict.items()):
print("word %s, its id %d, its word freq %d" % (word, word_id, word2id_freq[word_id]))
there are totoally 253854 different words in the corpus
word the, its id 0, its word freq 1061396
word of, its id 1, its word freq 593677
word and, its id 2, its word freq 416629
word one, its id 3, its word freq 411764
word in, its id 4, its word freq 372201
word a, its id 5, its word freq 325873
word to, its id 6, its word freq 316376
word zero, its id 7, its word freq 264975
word nine, its id 8, its word freq 250430
word two, its id 9, its word freq 192644
word is, its id 10, its word freq 183153
word as, its id 11, its word freq 131815
word eight, its id 12, its word freq 125285
word for, its id 13, its word freq 118445
word s, its id 14, its word freq 116710
word five, its id 15, its word freq 115789
word three, its id 16, its word freq 114775
word was, its id 17, its word freq 112807
word by, its id 18, its word freq 111831
word that, its id 19, its word freq 109510
word four, its id 20, its word freq 108182
word six, its id 21, its word freq 102145
word seven, its id 22, its word freq 99683
word with, its id 23, its word freq 95603
word on, its id 24, its word freq 91250
word are, its id 25, its word freq 76527
word it, its id 26, its word freq 73334
word from, its id 27, its word freq 72871
word or, its id 28, its word freq 68945
word his, its id 29, its word freq 62603
word an, its id 30, its word freq 61925
word be, its id 31, its word freq 61281
word this, its id 32, its word freq 58832
word which, its id 33, its word freq 54788
word at, its id 34, its word freq 54576
word he, its id 35, its word freq 53573
word also, its id 36, its word freq 44358
word not, its id 37, its word freq 44033
word have, its id 38, its word freq 39712
word were, its id 39, its word freq 39086
word has, its id 40, its word freq 37866
word but, its id 41, its word freq 35358
word other, its id 42, its word freq 32433
word their, its id 43, its word freq 31523
word its, its id 44, its word freq 29567
word first, its id 45, its word freq 28810
word they, its id 46, its word freq 28553
word some, its id 47, its word freq 28161
word had, its id 48, its word freq 28100
word all, its id 49, its word freq 26229
2.4 转换数据形式
# 把语料转换为id序列
def convert_corpus_to_id(corpus, word2id_dict):
# 使用一个循环,将语料中的每个词替换成对应的id,以便于神经网络进行处理
corpus = [word2id_dict[word] for word in corpus]
return corpus
corpus = convert_corpus_to_id(corpus, word2id_dict)
print("%d tokens in the corpus" % len(corpus))
print(corpus[:50])
17005207 tokens in the corpus
[5233, 3080, 11, 5, 194, 1, 3133, 45, 58, 155, 127, 741, 476, 10571, 133, 0, 27349, 1, 0, 102, 854, 2, 0, 15067, 58112, 1, 0, 150, 854, 3580, 0, 194, 10, 190, 58, 4, 5, 10712, 214, 6, 1324, 104, 454, 19, 58, 2731, 362, 6, 3672, 0]
2.5 构建训练数据
使用二次采样法处理原始文本
- 二次采样法的主要思想是降低高频词在语料中出现的频次。
- 方法是随机将高频的词抛弃,频率越高,被抛弃的概率就越大;
- 频率越低,被抛弃的概率就越小。
- 标点符号或冠词这样的高频词就会被抛弃,从而优化整个词表的词向量训练效果
这个鬼公式我得查查。。。。。。
# 使用二次采样算法(subsampling)处理语料,强化训练效果
def subsampling(corpus, word2id_freq):
# 这个discard函数决定了一个词会不会被替换,这个函数是具有随机性的,每次调用结果不同,这是个什么鬼公式转的???
# 如果一个词的频率很大,那么它被遗弃的概率就很大
def discard(word_id):
return random.uniform(0, 1) < 1 - math.sqrt(
1e-4 / word2id_freq[word_id] * len(corpus))
# 随即丢,丢完了再重新组
corpus = [word for word in corpus if not discard(word)]
return corpus
corpus = subsampling(corpus, word2id_freq)
print("%d tokens in the corpus" % len(corpus))
print(corpus[:50])
8742730 tokens in the corpus
[5233, 3080, 3133, 155, 741, 476, 10571, 133, 27349, 854, 15067, 58112, 854, 3580, 10, 10712, 214, 1324, 454, 19, 2731, 362, 3672, 0, 708, 40, 539, 97, 1423, 2757, 567, 686, 7088, 247, 5233, 1052, 320, 248, 44611, 2877, 186, 5233, 602, 1134, 2621, 8983, 279, 4147, 141, 6437]
在完成语料数据预处理之后,需要构造训练数据。根据上面的描述,我们需要使用一个滑动窗口对语料从左到右扫描,在每个窗口内,中心词需要预测它的上下文,并形成训练数据。
在实际操作中,由于词表往往很大(50000,100000等),对大词表的一些矩阵运算(如softmax)需要消耗巨大的资源,因此可以通过负采样的方式模拟softmax的结果。
- 给定一个中心词和一个需要预测的上下文词,把这个上下文词作为正样本。
- 通过词表随机采样的方式,选择若干个负样本。
- 把一个大规模分类问题转化为一个2分类问题,通过这种方式优化计算速度。
# 构造数据,准备模型训练
# max_window_size代表了最大的window_size的大小,程序会根据max_window_size从左到右扫描整个语料
# negative_sample_num代表了对于每个正样本,我们需要随机采样多少负样本用于训练,
# 一般来说,negative_sample_num的值越大,训练效果越稳定,但是训练速度越慢。
def build_data(corpus, word2id_dict, word2id_freq, max_window_size = 3, negative_sample_num = 4):
# 使用一个list存储处理好的数据
dataset = []
# 从左到右,开始枚举每个中心点的位置
for center_word_idx in range(len(corpus)):
# 以max_window_size为上限,随机采样一个window_size,这样会使得训练更加稳定
window_size = random.randint(1, max_window_size)
# 当前的中心词就是center_word_idx所指向的词
center_word = corpus[center_word_idx]
# 以当前中心词为中心,左右两侧在window_size内的词都可以看成是正样本
positive_word_range = (max(0, center_word_idx - window_size), min(len(corpus) - 1, center_word_idx + window_size))
positive_word_candidates = [corpus[idx] for idx in range(positive_word_range[0], positive_word_range[1]+1) if idx != center_word_idx]
# 对于每个正样本来说,随机采样negative_sample_num个负样本,用于训练
for positive_word in positive_word_candidates:
# 首先把(中心词,正样本,label=1)的三元组数据放入dataset中,
# 这里label=1表示这个样本是个正样本
dataset.append((center_word, positive_word, 1))
# 开始负采样
i = 0
while i < negative_sample_num:
negative_word_candidate = random.randint(0, vocab_size-1)
if negative_word_candidate not in positive_word_candidates:
# 把(中心词,正样本,label=0)的三元组数据放入dataset中,
# 这里label=0表示这个样本是个负样本
dataset.append((center_word, negative_word_candidate, 0))
i += 1
return dataset
corpus_light = corpus[:int(len(corpus)*0.2)]
dataset = build_data(corpus_light, word2id_dict, word2id_freq)
for _, (center_word, target_word, label) in zip(range(50), dataset):
print("center_word %s, target %s, label %d" % (id2word_dict[center_word],
id2word_dict[target_word], label))
center_word anarchism, target originated, label 1
center_word anarchism, target hisakazu, label 0
center_word anarchism, target liverwurst, label 0
center_word anarchism, target bilisi, label 0
center_word anarchism, target tolerability, label 0
center_word anarchism, target abuse, label 1
center_word anarchism, target ztat, label 0
center_word anarchism, target saskatchewann, label 0
center_word anarchism, target kinara, label 0
center_word anarchism, target ndbm, label 0
center_word originated, target anarchism, label 1
center_word originated, target qinling, label 0
center_word originated, target gerbert, label 0
center_word originated, target petroglyphs, label 0
center_word originated, target hanegbi, label 0
center_word originated, target abuse, label 1
center_word originated, target ipac, label 0
center_word originated, target bartley, label 0
center_word originated, target ushpizin, label 0
center_word originated, target ghattas, label 0
center_word originated, target against, label 1
center_word originated, target predidate, label 0
center_word originated, target kavvana, label 0
center_word originated, target zeuxippus, label 0
center_word originated, target ribbon, label 0
center_word originated, target working, label 1
center_word originated, target tbk, label 0
center_word originated, target huella, label 0
center_word originated, target amarantos, label 0
center_word originated, target mustique, label 0
center_word abuse, target anarchism, label 1
center_word abuse, target masking, label 0
center_word abuse, target moviemistakes, label 0
center_word abuse, target reichstein, label 0
center_word abuse, target misunderstood, label 0
center_word abuse, target originated, label 1
center_word abuse, target cicada, label 0
center_word abuse, target pamyat, label 0
center_word abuse, target cohanite, label 0
center_word abuse, target hovertank, label 0
center_word abuse, target against, label 1
center_word abuse, target gonzal, label 0
center_word abuse, target sclc, label 0
center_word abuse, target preproduction, label 0
center_word abuse, target arsonval, label 0
center_word abuse, target working, label 1
center_word abuse, target cilau, label 0
center_word abuse, target anchoretism, label 0
center_word abuse, target plentitude, label 0
center_word abuse, target stingray, label 0
2.6 组装mini-batch数据
训练数据准备好后,把训练数据都组装成mini-batch,并准备输入到网络中进行训练,代码如下:
# 构造mini-batch,准备对模型进行训练
# 我们将不同类型的数据放到不同的tensor里,便于神经网络进行处理
# 并通过numpy的array函数,构造出不同的tensor来,并把这些tensor送入神经网络中进行训练
def build_batch(dataset, batch_size, epoch_num):
# center_word_batch缓存batch_size个中心词
center_word_batch = []
# target_word_batch缓存batch_size个目标词(可以是正样本或者负样本)
target_word_batch = []
# label_batch缓存了batch_size个0或1的标签,用于模型训练
label_batch = []
for epoch in range(epoch_num):
# 每次开启一个新epoch之前,都对数据进行一次随机打乱,提高训练效果
random.shuffle(dataset)
for center_word, target_word, label in dataset:
# 遍历dataset中的每个样本,并将这些数据送到不同的tensor里
center_word_batch.append([center_word])
target_word_batch.append([target_word])
label_batch.append(label)
# 当样本积攒到一个batch_size后,我们把数据都返回回来
# 在这里我们使用numpy的array函数把list封装成tensor
# 并使用python的迭代器机制,将数据yield出来
# 使用迭代器的好处是可以节省内存
if len(center_word_batch) == batch_size:
yield np.array(center_word_batch).astype("int64"),
np.array(target_word_batch).astype("int64"),
np.array(label_batch).astype("float32")
center_word_batch = []
target_word_batch = []
label_batch = []
if len(center_word_batch) > 0:
yield np.array(center_word_batch).astype("int64"),
np.array(target_word_batch).astype("int64"),
np.array(label_batch).astype("float32")
for _, batch in zip(range(10), build_batch(dataset, 128, 3)):
print(batch)
break
(array([[ 90],
[ 588],
[ 982],
[ 334],
[ 3770],
[ 344],
[ 1201],
[ 34373],
[ 3522],
[ 1996],
[ 384],
[ 1089],
[ 1098],
[ 965],
[ 3847],
[ 8111],
[ 4971],
[ 8975],
[ 1238],
[ 1430],
[ 27871],
[ 20604],
[ 14278],
[ 281],
[ 2996],
[ 2295],
[ 42539],
[ 1587],
[ 22266],
[ 33013],
[ 1150],
[ 1067],
[ 767],
[ 4663],
[ 240],
[ 592],
[ 6285],
[ 854],
[ 172],
[ 3193],
[ 392],
[ 3178],
[ 5803],
[ 413],
[ 0],
[ 13138],
[ 20100],
[ 276],
[ 1577],
[ 28],
[ 17444],
[ 905],
[ 281],
[ 5896],
[ 9164],
[ 421],
[ 40051],
[ 2255],
[ 16329],
[ 3420],
[ 5711],
[ 2163],
[ 98],
[ 3715],
[ 1761],
[ 3581],
[ 2420],
[ 504],
[ 58477],
[ 160],
[ 3794],
[ 978],
[ 59],
[ 1965],
[ 19106],
[ 6462],
[ 550],
[ 138],
[ 14207],
[ 4669],
[108313],
[ 1663],
[ 785],
[ 1775],
[ 2299],
[ 11068],
[ 681],
[ 4947],
[ 9482],
[ 54617],
[ 4755],
[ 7502],
[ 1113],
[ 688],
[ 98],
[ 22978],
[ 885],
[ 711],
[ 45986],
[ 368],
[ 10402],
[ 3574],
[ 6930],
[ 695],
[ 3264],
[ 841],
[ 18505],
[ 267],
[ 466],
[ 64],
[ 28452],
[ 728],
[ 2490],
[ 2425],
[ 1507],
[ 760],
[ 4600],
[ 1879],
[ 1271],
[ 34440],
[ 2270],
[ 11137],
[ 1852],
[ 9232],
[ 257],
[ 4270],
[ 1512],
[ 31547]], dtype=int64), array([[ 2827],
[ 13796],
[103439],
[ 16480],
[ 29571],
[144095],
[ 75916],
[ 4541],
[ 930],
[ 2708],
[103147],
[ 20194],
[ 10286],
[ 4975],
[162742],
[109192],
[ 968],
[ 8801],
[ 650],
[ 4299],
[ 56979],
[ 66745],
[ 90132],
[ 89302],
[183085],
[211328],
[215867],
[226421],
[230690],
[ 2027],
[181287],
[ 68227],
[ 1886],
[177493],
[ 14429],
[ 19151],
[158341],
[238722],
[ 56248],
[ 19939],
[ 721],
[170988],
[ 60351],
[201496],
[135780],
[228575],
[ 36277],
[ 89419],
[197125],
[222194],
[ 33692],
[ 50198],
[160937],
[224291],
[187053],
[ 89955],
[ 14144],
[188683],
[172533],
[201584],
[193150],
[234380],
[110731],
[ 2842],
[ 97179],
[143441],
[ 72314],
[ 191],
[227196],
[214612],
[ 725],
[144038],
[124484],
[ 3315],
[123621],
[173700],
[ 18402],
[ 98103],
[ 238],
[145282],
[ 592],
[188998],
[ 81425],
[ 40827],
[ 23973],
[194500],
[237439],
[144121],
[211876],
[234473],
[ 37290],
[ 63905],
[ 2655],
[232259],
[ 929],
[179475],
[197707],
[ 17272],
[188889],
[ 8810],
[164693],
[ 54438],
[ 5120],
[174645],
[ 27173],
[ 87828],
[ 9932],
[241565],
[ 173],
[ 3957],
[ 63051],
[ 141],
[ 41401],
[183427],
[ 46558],
[104789],
[216617],
[ 448],
[109615],
[ 16352],
[ 7107],
[ 82236],
[177176],
[ 2441],
[ 1023],
[193967],
[ 56406],
[154326]], dtype=int64), array([1., 1., 0., 1., 1., 0., 0., 0., 1., 1., 0., 0., 1., 1., 0., 0., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 1., 0., 1., 1., 0., 1., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1.], dtype=float32))
3. 网络定义
定义skip-gram的网络结构,用于模型训练。在飞桨动态图中,对于任意网络,都需要定义一个继承自paddle.nn.layer
的类来搭建网络结构、参数等数据的声明。同时需要在forward
函数中定义网络的计算逻辑。值得注意的是,我们仅需要定义网络的前向计算逻辑,飞桨会自动完成神经网络的后向计算。
在skip-gram的网络结构中,使用的最关键的API是paddle.nn.Embedding函数,可以用其实现Embedding的网络层。通过查询飞桨的API文档,可以得到如下更详细的说明:
paddle.nn.Embedding(numembeddings, embeddingdim, paddingidx=None, sparse=False, weightattr=None, name=None)
该接口用于构建 Embedding 的一个可调用对象,其根据input中的id信息从embedding矩阵中查询对应embedding信息,并会根据输入的size (num_embeddings, embedding_dim)自动构造一个二维embedding矩阵。 输出Tensor的shape是在输入Tensor shape的最后一维后面添加了emb_size的维度。注:input中的id必须满足 0 =< id < size[0],否则程序会抛异常退出。
#定义skip-gram训练网络结构
#使用paddlepaddle的2.0.0版本
#一般来说,在使用paddle训练的时候,我们需要通过一个类来定义网络结构,这个类继承了paddle.nn.layer
class SkipGram(nn.Layer):
def __init__(self, vocab_size, embedding_size, init_scale=0.1):
# vocab_size定义了这个skipgram这个模型的词表大小
# embedding_size定义了词向量的维度是多少
# init_scale定义了词向量初始化的范围,一般来说,比较小的初始化范围有助于模型训练
super(SkipGram, self).__init__()
self.vocab_size = vocab_size
self.embedding_size = embedding_size
# 使用Embedding函数构造一个词向量参数
# 这个参数的大小为:[self.vocab_size, self.embedding_size]
# 数据类型为:float32
# 这个参数的初始化方式为在[-init_scale, init_scale]区间进行均匀采样
self.embedding = Embedding(
num_embeddings = self.vocab_size,
embedding_dim = self.embedding_size,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Uniform(
low=-init_scale, high=init_scale)))
# 使用Embedding函数构造另外一个词向量参数
# 这个参数的大小为:[self.vocab_size, self.embedding_size]
# 这个参数的初始化方式为在[-init_scale, init_scale]区间进行均匀采样
self.embedding_out = Embedding(
num_embeddings = self.vocab_size,
embedding_dim = self.embedding_size,
weight_attr=paddle.ParamAttr(
initializer=paddle.nn.initializer.Uniform(
low=-init_scale, high=init_scale)))
# 定义网络的前向计算逻辑
# center_words是一个tensor(mini-batch),表示中心词
# target_words是一个tensor(mini-batch),表示目标词
# label是一个tensor(mini-batch),表示这个词是正样本还是负样本(用0或1表示)
# 用于在训练中计算这个tensor中对应词的同义词,用于观察模型的训练效果
def forward(self, center_words, target_words, label):
# 首先,通过self.embedding参数,将mini-batch中的词转换为词向量
# 这里center_words和eval_words_emb查询的是一个相同的参数
# 而target_words_emb查询的是另一个参数
center_words_emb = self.embedding(center_words)
target_words_emb = self.embedding_out(target_words)
# 我们通过点乘的方式计算中心词到目标词的输出概率,并通过sigmoid函数估计这个词是正样本还是负样本的概率。
word_sim = paddle.multiply(center_words_emb, target_words_emb)
word_sim = paddle.sum(word_sim, axis=-1)
word_sim = paddle.reshape(word_sim, shape=[-1])
pred = F.sigmoid(word_sim)
# 通过估计的输出概率定义损失函数,注意我们使用的是binary_cross_entropy_with_logits函数
# 将sigmoid计算和cross entropy合并成一步计算可以更好的优化,所以输入的是word_sim,而不是pred
loss = F.binary_cross_entropy_with_logits(word_sim, label)
loss = paddle.mean(loss)
# 返回前向计算的结果,飞桨会通过backward函数自动计算出反向结果。
return pred, loss
4. 网络训练
完成网络定义后,就可以启动模型训练。我们定义每隔100步打印一次Loss,以确保当前的网络是正常收敛的。同时,我们每隔10000步观察一下skip-gram计算出来的同义词(使用 embedding的乘积),可视化网络训练效果,代码如下:
# 开始训练,定义一些训练过程中需要使用的超参数
batch_size = 512
epoch_num = 3
embedding_size = 200
step = 0
learning_rate = 0.001
#定义一个使用word-embedding查询同义词的函数
#这个函数query_token是要查询的词,k表示要返回多少个最相似的词,embed是我们学习到的word-embedding参数
#我们通过计算不同词之间的cosine距离,来衡量词和词的相似度
#具体实现如下,x代表要查询词的Embedding,Embedding参数矩阵W代表所有词的Embedding
#两者计算Cos得出所有词对查询词的相似度得分向量,排序取top_k放入indices列表
def get_similar_tokens(query_token, k, embed):
W = embed.numpy()
x = W[word2id_dict[query_token]]
cos = np.dot(W, x) / np.sqrt(np.sum(W * W, axis=1) * np.sum(x * x) + 1e-9)
flat = cos.flatten()
indices = np.argpartition(flat, -k)[-k:]
indices = indices[np.argsort(-flat[indices])]
for i in indices:
print('for word %s, the similar word is %s' % (query_token, str(id2word_dict[i])))
# 通过我们定义的SkipGram类,来构造一个Skip-gram模型网络
skip_gram_model = SkipGram(vocab_size, embedding_size)
# 构造训练这个网络的优化器
adam = paddle.optimizer.Adam(learning_rate=learning_rate, parameters = skip_gram_model.parameters())
# 使用build_batch函数,以mini-batch为单位,遍历训练数据,并训练网络
for center_words, target_words, label in build_batch(
dataset, batch_size, epoch_num):
# 使用paddle.to_tensor,将一个numpy的tensor,转换为飞桨可计算的tensor
center_words_var = paddle.to_tensor(center_words)
target_words_var = paddle.to_tensor(target_words)
label_var = paddle.to_tensor(label)
# 将转换后的tensor送入飞桨中,进行一次前向计算,并得到计算结果
pred, loss = skip_gram_model(
center_words_var, target_words_var, label_var)
# 程序自动完成反向计算
loss.backward()
# 程序根据loss,完成一步对参数的优化更新
adam.step()
# 清空模型中的梯度,以便于下一个mini-batch进行更新
adam.clear_grad()
# 每经过100个mini-batch,打印一次当前的loss,看看loss是否在稳定下降
step += 1
if step % 1000 == 0:
print("step %d, loss %.3f" % (step, loss.numpy()[0]))
# 每隔10000步,打印一次模型对以下查询词的相似词,这里我们使用词和词之间的向量点积作为衡量相似度的方法,只打印了5个最相似的词
if step % 10000 ==0:
get_similar_tokens('movie', 5, skip_gram_model.embedding.weight)
get_similar_tokens('one', 5, skip_gram_model.embedding.weight)
get_similar_tokens('chip', 5, skip_gram_model.embedding.weight)
step 1000, loss 0.691
step 2000, loss 0.684
step 3000, loss 0.619
step 4000, loss 0.505
step 5000, loss 0.452
step 6000, loss 0.269
下一步计划使用PaddleNLP加载词向量
欢迎大家一起讨论交流
本文正在参加「金石计划」