使用tensorflow的viterbi_解码时出错
我在用这个 我刚刚使用了另一个相同格式的列车数据。 代码没有问题,因为当我使用原始的train_数据运行时,代码没有问题。这是什么原因造成的使用tensorflow的viterbi_解码时出错,tensorflow,ner,Tensorflow,Ner,我在用这个 我刚刚使用了另一个相同格式的列车数据。 代码没有问题,因为当我使用原始的train_数据运行时,代码没有问题。这是什么原因造成的 Traceback (most recent call last): File "main.py", line 83, in <module> model.train(train=train_data, dev=dev_data) File "/home/mengyuguang/NER/model.py", line 161,
Traceback (most recent call last):
File "main.py", line 83, in <module>
model.train(train=train_data, dev=dev_data)
File "/home/mengyuguang/NER/model.py", line 161, in train
self.run_one_epoch(sess, train, dev, self.tag2label, epoch, saver)
File "/home/mengyuguang/NER/model.py", line 221, in run_one_epoch
label_list_dev, seq_len_list_dev = self.dev_one_epoch(sess, dev)
File "/home/mengyuguang/NER/model.py", line 256, in dev_one_epoch
label_list_, seq_len_list_ = self.predict_one_batch(sess, seqs)
File "/home/mengyuguang/NER/model.py", line 277, in predict_one_batch
viterbi_seq, _ = viterbi_decode(logit[:seq_len], transition_params)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/crf/python/ops/crf.py", line 333, in viterbi_decode
trellis[0] = score[0]
IndexError: index 0 is out of bounds for axis 0 with size 0
回溯(最近一次呼叫最后一次):
文件“main.py”,第83行,在
model.train(train=train\u数据,dev=dev\u数据)
文件“/home/mengyuguang/NER/model.py”,第161行,列车中
self.run_one_epoch(sess、train、dev、self.tag2标签、epoch、saver)
文件“/home/mengyuguang/NER/model.py”,第221行,运行一个新纪元
label_list_dev,seq_len_list_dev=self.dev_one_epoch(sess,dev)
文件“/home/mengyuguang/NER/model.py”,第256行,在dev_one_epoch中
标签列表,序号列表=self.predict\u一批(sess,seqs)
文件“/home/mengyuguang/NER/model.py”,第277行,一批预测
维特比顺序,=维特比解码(logit[:seq\u len],转换参数)
文件“/usr/local/lib/python2.7/dist packages/tensorflow/contrib/crf/python/ops/crf.py”,第333行,viterbi_解码
网格[0]=分数[0]
索引器错误:索引0超出大小为0的轴0的界限
应将代码更改为以下内容:
def read_corpus(corpus_path):
"""
read corpus and return the list of samples
:param corpus_path:
:return: data
"""
data = []
with open(corpus_path, encoding='utf-8') as fr:
lines = fr.readlines()
sent_, tag_ = [], []
for line in lines:
if line != '\n' and line != '\t\n': #
[char, label] = line.strip().split()
sent_.append(char)
tag_.append(label)
#else:
elif sent_ !=[] and tag_ !=[]: #
data.append((sent_, tag_))
sent_, tag_ = [], []
return data
Tks兄弟。@Yuguang问的是一个具体的项目。导致他出现问题的缺陷已在评论中指出,并附有说明。
def read_corpus(corpus_path):
"""
read corpus and return the list of samples
:param corpus_path:
:return: data
"""
data = []
with open(corpus_path, encoding='utf-8') as fr:
lines = fr.readlines()
sent_, tag_ = [], []
for line in lines:
if line != '\n' and line != '\t\n': #
[char, label] = line.strip().split()
sent_.append(char)
tag_.append(label)
#else:
elif sent_ !=[] and tag_ !=[]: #
data.append((sent_, tag_))
sent_, tag_ = [], []
return data