多读书多实践,勤思考善领悟

从零开始学习知识图谱 之 九.百科知识图谱构建 3.基于TensorFlow神经网络关系抽取的数据集构建(使用OpenNRE)

本文于1582天之前发表,文中内容可能已经过时。

一. 简介

在数据爬取过程中,想尝试复现一个经典的神经网络关系抽取模型。经过看论文筛选最终确定清华的Neural Relation Extraction with Selective Attention over Instances。但在看了开源的代码后感觉自己现在造一遍轮子也不如人家的…因此就按照NYT的论文描述做了一个远程监督数据集,并在OpenNRE 上跑了一遍。

本教程的项目代码放在github上,下载地址为《从零开始学习知识图谱》项目源代码

二. 环境准备

1. 操作系统

支持操作系统:windows、macOS、Linux。为了方便大家搭建开发环境,笔者尽可能在windows下构建,系列篇未特意说明时操作系统都是windows。Linux安装可以参考VirtualBox虚拟机安装UbuntuVirtualBox虚拟机安装CentOS8进行安装。

2. jdk

安装参见windows系统安装JDK

3. Python3

安装参见从零开始学习知识图谱 之 一

4. Stanford NLP

Stanford NLP 团队发布了包含 53 种语言预训练模型的自然语言处理工具包 StanfordNLP,该工具包支持 Python 3.6 及之后版本,并基于 PyTorch,支持多种语言的完整文本分析管道,包括分词、词性标注、词形归并和依存关系解析,此外它还提供了与 CoreNLP 的 Python 接口。

StanfordCoreNLP提供了一系列用于自然语言的技术工具。它可以给出不管是公司名还是人名亦或标准化日期、时间和数量等单词的基本形式,词性等。如下图所示它还可以根据短语和句法依存关系标记句子结构,指明哪些名词短语表示相同的实体,指明情感,提取实体及之间的特定或开放类关系,获取名人名言等等。

安装

1)首先从stanford NLP网页下载两个包,分别是stanford-corenlp-full-2018-10-05.zip和中文处理包stanford-chinese-corenlp-2018-10-05-models.jar,下载后解压stanford-corenlp-full-2018-10-05.zip压缩包,然后将stanford-chinese-corenlp-2018-10-05-models.jar放入解压文件夹中。

2) 安装Python的stanfordnlp库,在命令提示符中切换到安装Python的路径的Scripts文件夹下执行命令pip install stanfordnlppip install stanforcorednlp, 安装完成后就可以开始使用了。

1
2
C:\my\Python\Scripts>pip install stanfordnlp
C:\my\Python\Scripts>pip install stanforcorednlp

pip install stanforcorednlp 命令一直安装不了,最后使用下面这条镜像命令就可以安装了,被墙了。 选择USTC镜像安装(安装速度很快,毕竟国内镜像):pip install stanfordcorenlp -i http://pypi.mirrors.ustc.edu.cn/simple/ --trusted-host pypi.mirrors.ustc.edu.cn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
C:\my\Python\Scripts>pip install stanfordcorenlp -i http://pypi.mirrors.ustc.edu.cn/simple/ --trusted-host pypi.mirrors.ustc.edu.cn

Looking in indexes: http://pypi.mirrors.ustc.edu.cn/simple/
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='mirrors.ustc.edu.cn', port=443): Read timed out. (read timeout=15)")': /pypi/web/simple/stanfordcorenlp/
Collecting stanfordcorenlp
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/35/cb/0a271890bbe3a77fc1aca2bc3a58b14e11799ea77cb5f7d6fb0a8b4c46fa/stanfordcorenlp-3.9.1.1-py2.py3-none-any.whl
Requirement already satisfied: requests in c:\my\python\lib\site-packages (from stanfordcorenlp) (2.22.0)
Collecting psutil
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/7c/58/f5d68ddca37480d8557b8566a20bf6108d7e1c6c9b9208ee0786e0cd012b/psutil-5.6.3-cp37-cp37m-win_amd64.whl (234kB)
|████████████████████████████████| 235kB 467kB/s
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\my\python\lib\site-packages (from requests->stanfordcorenlp) (1.25.6)
Requirement already satisfied: idna<2.9,>=2.5 in c:\my\python\lib\site-packages (from requests->stanfordcorenlp) (2.8)
Requirement already satisfied: certifi>=2017.4.17 in c:\my\python\lib\site-packages (from requests->stanfordcorenlp) (2019.9.11)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\my\python\lib\site-packages (from requests->stanfordcorenlp) (3.0.4)
Installing collected packages: psutil, stanfordcorenlp
Successfully installed psutil-5.6.3 stanfordcorenlp-3.9.1.1

3) 在Python中引用模型,执行下面语句:

1
2
from stanfordcorenlp import StanfordCoreNLP
nlp=StanfordCoreNLP(r'C:\my\stanford-corenlp',lang='zh')

4) 启动服务

Stanford CoreNLP附带一个内置服务器,该服务器仅需要CoreNLP依赖项。要运行此服务器,只需运行:

1
2
# Run the server using all jars in the current directory (e.g., the CoreNLP home directory)
java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 15000

请注意,超时时间以毫秒为单位。

如果要处理非英语语言,请将此命令与适当的语言属性一起使用,下面为中文语言:

1
2
# Run a server using Chinese properties
java -Xmx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -serverProperties StanfordCoreNLP-chinese.properties -port 9000 -timeout 15000

如果未port提供的值,则默认使用端口9000。然后,您可以通过访问来测试服务器。

1
http://localhost:9000/

5. word2vec

word2vec是Google在2013年提出的一款开源工具,其是一个Deep Learning(深度学习)模型(实际上该模型层次较浅,严格上还不能算是深层模型,如果word2vec上层再套一层与具体应用相关的输出层,如Softmax,便更像是一个深层模型),它将词表征成实数值向量,采用CBOW(Continuous Bag-Of-Words Model,连续词袋模型)和Skip-Gram(Continuous Skip-GramModel)两种模型。具体原理,网上有很多。Google word2vec需要linux环境,下载地址为:http://word2vec.googlecode.com/svn/trunk/。

有小伙伴们移植了word2vec的Windows版本,可以像在Linux上一样直接在命令行里运行,下载地址https://github.com/anoidgit/word2vec-win/releases 。您可以尝试使用dev c ++构建的文件,它们更加通用,可以轻松地在许多具有不同版本的Windows上运行。

笔者使用 word2vec-win_devc_x64.zip ,下载解压即可调用,我放在C:\my\word2vec。

6. OpenNRE

OpenNRE 是神经关系提取的开源框架。它是基于TensorFlow的框架,用于轻松建立关系提取(RE)模型。我们将关系提取流水线分为四个部分,即嵌入,编码器,选择器(用于远程监视)和分类器。

1). 下载OpenNRE存储库:

官方下载地址:https://codeload.github.com/thunlp/OpenNRE/zip/modelrefine

百度云下载地址(笔者用的OpenNRE,已改代码):https://pan.baidu.com/s/1JQYe57Ade8E0pv8_JZ9VYA 提取码:xqv2

2).下载后解压,我把放在C:\my\OpenNRE

3). 然后安装所有依赖

1
2
3
4
C:\my\OpenNRE>pip install --index-url https://pypi.douban.com/simple Numpy
C:\my\OpenNRE>pip install --index-url https://pypi.douban.com/simple tensorflow==1.15.0
C:\my\OpenNRE>pip install --index-url https://pypi.douban.com/simple Matplotlib
C:\my\OpenNRE>pip install --index-url https://pypi.douban.com/simple scikit-learn

4). 按以下结构制作数据文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
OpenNRE
|-- ...
|-- data
|
|-- {DATASET_NAME_1}
| |
| |-- train.json
| |-- test.json
| |-- word_vec.json
| |-- rel2id.json
|
|-- {DATASET_NAME_2}
| |
| |-- ...
|
|-- ...

三. 远程监督数据集的获取

NYT 数据集依靠 Freebase知识库,采用非百科类文本 - New York Times Corpus 来生成远程监督数据集。包含53 个可能的关系(包含NA),训练数据集包含句子 522 611, 实体对 281 270, 关系事实 18252,测试集包含句子 172 448, 实体对 96678, 关系事实1950. 原始论文链接为Modeling Relations and Their Mentions without Labeled Text

由于是学习性质,就采用百科内的文本作为数据集,没有额外去爬取非百科类的数据来训练。

下面我们介绍数据集的生成步骤。

1. 加载字典

为了提高最终数据集的有效性,我们需要尽量保证实体对中的两个实体是实体。。。这是因为在产生NA关系的实体对时,若对两个实体的要求不严格,将会产生大量的 垃圾 关系对。因此我们采用 jieba 分词 和 stanfordCorenlp 的NER模块来保证实体的有效性。

对于 jieba 分词,我们可以采用百度词条的全部 title 生成的外挂字典来提升最终分词结果的准确性(也可以使用其他通用领域的外挂字典,如腾讯那个)。不过我试验了一下,百度词条的外挂字典的提升效果有限,我才有可能是因为百度词条的标题有时候不能严格算一个单词的缘故,所以这个外挂字典不是很准确。而且还会拖慢生成速度。

因为jieba 没有ner标记功能,因此采用 stanfordCorenlp来做NER,它的使用也很简单。

2. 从数据库导出数据并清洗

程序 gen_re_from_baidu.py 是生成语料的核心程序。它的输入包含:

  • 百度 410 万 词条的 标题 文件, 410_title.csv。它可以直接从数据库导出获得。
  • 百度 410 万 词条的 消岐名称-标题 文件, 410_title_disambi.csv。从数据库直接导出获得。
  • 百度 6 万 词条的 词条 标题 title, 消岐名称 disambi, 词条文本 all_text 文件。从数据库直接导出获得。
  • 百度 410 万词条的 消岐名称-属性-属性值文件,410_disambi_attr_title.csv。它是程序gen_disambi_infobox.py的输出(410_disambi_infobox_out.csv)。

以 6w_disambi_text.csv 文件为例,从数据库导出文件语句如下所示:

1
SELECT title, disambi, all_text from lemmas where title_id < 60000 into outfile 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/6w_disambi_text.csv' fields terminated by ',' optionally enclosed by '"' lines terminated by '\r\n';

在得到数据后,我们调用clean_sql_output() 函数对 6w_disambi_text.csv 进行初步的清洗,主要目的是去除文本中的特殊符号和换行符。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def clean_sql_output(in_file, out_file):
with open(in_file, "r", encoding='utf-8') as inf, open(out_file, "w", encoding='utf-8') as ouf:
total_lines = linecount(in_file)
for line_num in tqdm(range(total_lines)):
line = inf.readline()
line = re.sub(u"[\.\!#&%@\^\*\(\)\+“”:』『《》$¥\<\>\\\:\{\}]", "", line)
line = re.sub(u"\[.*?\]", "", line)
if not line.endswith("\r\n"):
ouf.write(line.strip())
else:
ouf.write(line.strip() + "\n")

def linecount(file_path):
count = -1
for count, line in enumerate(open(file_path, 'r', encoding='utf-8')): pass
return count + 1

把6w_disambi_text.csv移动到re_cnn_att\data文件夹中。

3. 生成数据

把输入的文410_title.csv、410_title_disambi.csv、…等文件移动到re_cnn_att\data文件夹中。

清洗完数据后,就该用它来生成数据啦,这一部分工作由 build_entity_relation() 函数完成。这个函数首先生成三个字典和两个集合:

  • disambi_dict 包含每一个词条的消岐名称 和对应的属性-属性值。形式为”{“上海”: “(所属地, 中国)..”}”
  • title_id 保存了所有的词条的标题作为key值,value 是按顺序得到的序号。
  • disambi_id 包含所有 消岐名称作为 key 值,value 是按顺序得到的序号。
  • tt_pair_set 保存了所有 标题-属性对,其中属性被约束为标题(即非标题的属性不被认为是实体,被丢弃)。
  • all_title 包含所有标题的集合。

而后函数分两部分得到关系事实和NA关系实体对。

对于关系事实,我们对于对于每一个词条,读取disambi_dict 的属性和属性值,并组装出 title-attr_title 三元组,对于每个三元组,我们去每句话中进行查找。

对于关系事实,其生成步骤如下所示:

  • 我们对于对于每一个词条,读取disambi_dict 的属性和属性值,并组装出 title-attr_title 三元组
  • 调用 stanfordCorenlp 对三元组进行命名实体识别,排除非命名实体以及命名实体中的 ‘MONEY’、’PERCENT’、’DATE’、’NUMBER’、’ORDINAL’几类,这是因为它们包含的范围太广泛了,会对得到大量的数字,对数据集质量造成影响。
  • 对于每个三元组,我们将在词条的每句话中进行寻找。
  • 为了保证准确性,在查找之前先调用 jieba对每句话进行分词
  • 在分词结果中进行查找,若三元组中的两个实体同时出现在一句话中,且两个实体不相同,那么就认为这句话表达了该三元组的关系。

重复上述过程就可以得到关系事实了。

对于NA关系,为了保证数据集的质量,规定了如下要求:

  • 两个实体对必须不在现有知识库的关系中,这个没啥好说的。。。
  • 每个词条中获得的NA关系不得大于15,这个是观察NYT数据集的NA和关系事实的比例得到的,防止出现太多的NA。也可以使用max_NA_in_lemmas 进行更改。
  • 每句话中最多被使用1次,防止一句话被多个NA关系利用。
  • NA关系的实体必须通过 stanfordCorenlp 的NER 标记。

对于满足以上要求的实体对,我们就将其作为数据集的一部分了。生成数据集后,将前80%作为训练集,后20%作为测试集。

运行程序 gen_re_from_baidu.py

用户根据实际的Stanford CoreNLP安装路径,修改gen_re_from_baidu.py中stanford_path的值。

1
2
3
4
5
6
7
C:\my\Python\python.exe C:/d/mmm/pycharm/re_cnn_att/gen_re_from_baidu.py
error_counts: 32
0%| | 0/1 [00:00<?, ?it/s]count_re: 50 count_na: 50 count_total: 120
total_sentence_used: 60
100%|██████████| 1/1 [00:00<00:00, 55.71it/s]

Process finished with exit code 0

4. 数据集的格式

为了使用OpenNRE,对于训练集和数据集的格式和 OpenNRE 的要求一致,格式为:

1
2
3
4
5
6
7
8
9
[
{
'sentence': 'Bill Gates is the founder of Microsoft .',
'head': {'word': 'Bill Gates', 'id': 'm.03_3d', 'type': 'None'},
'tail': {'word': 'Microsoft', 'id': 'm.07dfk', 'type': 'None'},
'relation': 'founder'
},
...
]

名称为train.json 和 test.json。对于 Relation-ID 的映射文件,里面保存着用到的关系,其中需要注意的是NA的 value必须为0。得到的文件名为rel2id.json,其格式为:

1
2
3
4
5
6
{
'NA': 0,
'relation_1': 1,
'relation_2': 2,
...
}

5. 词向量的生成

这里我用 word2vec来生成词向量,文本是用6w词条的,分词用jieba做。输出向量维度是50,包含1 552 081个词。为了在 OpenNRE 中使用,我们需要将其转化为如下格式:

1
2
3
4
5
[
{'word': 'the', 'vec': [0.418, 0.24968, ...]},
{'word': ',', 'vec': [0.013441, 0.23682, ...]},
...
]

关于词向量的生成由 word2vec.py 完成。这里需要注意的是,在生成词向量时, 我将Word2vec 包放在C:\my\word2vec文件夹下 ,由下述命令完成:

1
os.system("C:\my\word2vec\word2vec -train seg_6w_disambi_text.txt -output word_vec.txt -size 50 -window 5 -sample 1e-4 -negative 5 -hs 0 -binary 0 -cbow 0 -iter 3 -min-count 1 -hs 1")transfer_json("word_vec.txt", "word_vec.json")

运行word2vec.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
C:\my\Python\python.exe C:/d/mmm/pycharm/re_cnn_att/word2vec.py

0it [00:00, ?it/s]Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\mmm\AppData\Local\Temp\jieba.cache
Loading model cost 0.977 seconds.
Prefix dict has been built succesfully.
1it [00:06, 6.57s/it]
Starting training using file seg_6w_disambi_text.txt
Vocab size: 70593
Words in train file: 814504
Alpha: 0.000098 Progress: 100.92% Words/thread/sec: 1125.03k Total word_num: 70593
0%| | 0/70593 [00:00<?, ?it/s]Word dim: 50
100%|██████████| 70593/70593 [00:06<00:00, 10144.12it/s]

Process finished with exit code 0

生成的word_vec.json结果格式:

1
2
3
[{"word": "</s>", "vec": [0.008005, 0.008839, -0.007661, -0.006556, 0.002733, 0.006042, 0.001882, 0.000423, -0.007207, 0.004437, -0.008713, 0.002499, -0.001503, -0.001914, -0.006631, -0.003764, 0.005159, 0.006051, 0.005938, 0.003195, 0.00309, -0.007605, -0.008192, 0.009939, 0.007603, 0.00618, -0.001208, 0.008031, -0.00099, 0.001469, -0.000298, -0.005966, 0.002625, -0.002675, -0.007651, 0.009508, 0.008759, -0.00219, -0.000452, 0.001018, -0.007275, -0.008014, 0.009109, 0.000126, -0.005165, -0.006084, -0.006153, 0.003394, 0.000403, 0.002662]}, {"word": "\uff0c", "vec": [-0.255787, -0.137732, 0.144594, 0.138146, -0.224961, -0.198183, 0.168778, -0.021675, -0.293887, -0.247066, 0.185316, 0.137508, -0.401537, -0.242721, 0.069197, -0.161912, -0.168342, -0.15231, 0.313346, 0.136589, -0.300042, -0.082335, 0.705634, -0.395934, -0.18271, -0.206994, -0.316542, -0.464895, -0.208067, 0.031974, -0.005736, 

......

四. 运行 OpenNRE

我的train.json、test.json、rel2id.json、word2vec.json 四个文件放在百度云上,供大家使用,百度云下载链接(提取码:0k5h)。

这部就比较简单了,前面我们已经得到运行该程序所需的文件,现在只需将上面得到的 train.json、test.json、rel2id.json、word2vec.json 四个文件打包放到data目录下,我的在C:\my\OpenNRE\data\baidu\。而后运行python train_demo.py baidu cnn att即可训练数据。

1. 运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
C:\my\OpenNRE>python train_demo.py baidu cnn att

2019-11-01 10:39:22.737101: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
2019-11-01 10:39:22.743047: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
WARNING:tensorflow:From C:\my\OpenNRE\nrekit\framework.py:126: The name tf.train.GradientDescentOptimizer is deprecated. Please use tf.compat.v1.train.GradientDescentOptimizer instead.

Pre-processed files exist. Loading them...
Finish loading
Total relation fact: 6298
Pre-processed files exist. Loading them...
Finish loading
Total relation fact: 1761
Pre-processed files exist. Loading them...
Finish loading
Total relation fact: 1761
Start training...

......

2019-11-01 10:39:54.093624: W tensorflow/core/common_runtime/colocation_graph.cc:983] Failed to place the graph without changing the devices of some resources. Some of the operations (that had to be colocated with resource generating operations) are not supported on the resources' devices. Current candidate devices are [
/job:localhost/replica:0/task:0/device:CPU:0].
See below for details of this colocation group:
Colocation Debug Info:
Colocation group had the following types and supported devices:
Root Member(assigned_device_name_index_=-1 requested_device_name_='/device:GPU:0' assigned_device_name_='' resource_device_name_='/device:GPU:0' supported_device_types_=[CPU] possible_devices_=[]
VariableV2: CPU
Const: CPU
Assign: CPU
Identity: CPU
GatherV2: CPU

Colocation members, user-requested devices, and framework assigned devices, if any:
weights_table/weights_table (VariableV2) /device:GPU:0
weights_table/weights_table/Assign (Assign) /device:GPU:0
weights_table/weights_table/read (Identity) /device:GPU:0
gpu_0/loss/embedding_lookup/axis (Const) /device:GPU:0
gpu_0/loss/embedding_lookup (GatherV2) /device:GPU:0
save/Assign_6 (Assign) /device:GPU:0

###### Epoch 1 ######
epoch 1 step 684 time 0.43 | loss: 0.320573, not NA accuracy: 0.122737, accuracy: 0.943412
Average iteration time: 0.545785
Testing...
[TEST] step 184 | not NA accuracy: 0.119584, accuracy: 0.946689
[TEST] auc: 0.3006554130143355
Finish testing
Best model, storing...
Finish storing
###### Epoch 2 ######
epoch 2 step 684 time 0.55 | loss: 0.015132, not NA accuracy: 0.213719, accuracy: 0.949443
Average iteration time: 0.642220
Testing...
[TEST] step 184 | not NA accuracy: 0.338533, accuracy: 0.957770
[TEST] auc: 0.5440238744502314
Finish testing
Best model, storing...
Finish storing
###### Epoch 3 ######
epoch 3 step 684 time 0.47 | loss: 0.025789, not NA accuracy: 0.336138, accuracy: 0.956259
Average iteration time: 0.615477
Testing...
[TEST] step 184 | not NA accuracy: 0.392259, accuracy: 0.961486
[TEST] auc: 0.6110335239333476
Finish testing
Best model, storing...
Finish storing
###### Epoch 4 ######
epoch 4 step 684 time 0.47 | loss: 0.131589, not NA accuracy: 0.399333, accuracy: 0.960073
Average iteration time: 0.545155
Testing...
[TEST] step 184 | not NA accuracy: 0.517620, accuracy: 0.963953
[TEST] auc: 0.6347928854797641
Finish testing
Best model, storing...
Finish storing
##### Epoch 5 ######
epoch 5 step 684 time 0.68 | loss: 0.039580, not NA accuracy: 0.425214, accuracy: 0.961551
Average iteration time: 0.641899
Testing...
[TEST] step 184 | not NA accuracy: 0.489890, accuracy: 0.965709
[TEST] auc: 0.6604051878963836
Finish testing
Best model, storing...
Finish storing
###### Epoch 6 ######
epoch 6 step 684 time 0.58 | loss: 0.096361, not NA accuracy: 0.448396, accuracy: 0.962464
Average iteration time: 0.696763
Testing...
[TEST] step 184 | not NA accuracy: 0.533218, accuracy: 0.967399
[TEST] auc: 0.6855477905925986
Finish testing
Best model, storing...
Finish storing
###### Epoch 7 ######
epoch 7 step 684 time 0.63 | loss: 0.076175, not NA accuracy: 0.469514, accuracy: 0.963622
Average iteration time: 0.691868
Testing...
[TEST] step 184 | not NA accuracy: 0.514154, accuracy: 0.967230
[TEST] auc: 0.6920503761834449
Finish testing
Best model, storing...
Finish storing
###### Epoch 8 ######
epoch 8 step 684 time 0.60 | loss: 0.033911, not NA accuracy: 0.479994, accuracy: 0.964151
Average iteration time: 0.694419
Testing...
[TEST] step 184 | not NA accuracy: 0.534373, accuracy: 0.968007
[TEST] auc: 0.7014862217419188
Finish testing
Best model, storing...
Finish storing

最终,当训练8个epoch时,在测试机上得到的准确率为70.15%,其中非NA准确率为53.44%。

2. 运行可能出现的问题及解决

问题1:错误信息FileNotFoundError: [Errno 2] No such file or directory: ‘_processed_data/baidu\data\baidu\train_word.npy’。

解决:手工创建data\baidu\这两级目录。

问题2:错误信息AttributeError: module ‘nrekit’ has no attribute ‘embedding’。

解决:修改train_demo.py,把nrekit.embedding改成nrekit.network.embedding

问题3:错误信息NameError: name ‘encoder’ is not defined。

解决:修改train_demo.py,把encoder + '_' + selector改成self.encoder + '_' + self.selector