怎么去使用finetune_ner_dygraph
Created by: jtyoui
请问一下:
我跑完这个代码: https://github.com/PaddlePaddle/ERNIE/blob/develop/demo/finetune_ner_dygraph.py
请问跑完之后怎么去使用呢??
我根据代码去使用得到的结果全是预测6,
['这', '也', '是', '美', '国', '、', '俄', '罗', '斯', '等', '石', '油', '、', '天', '然', '气', '都', '很', '丰', '富', '的', '国', '家', '搞', '代', '用', '燃', '料', '的', '原', '因'] [6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6]
很显然美国和俄罗斯都没有识别出现,
我不知道是不是我的测试代码有问题. 测试代码:
with graph.guard():
para_dict, _ = graph.load_dygraph(self.save_model)
model = ErnieModelForTokenClassification.from_pretrained(
self.pretreatment_model,
num_labels=len(self.labels_index) + 1)
model.set_dict(para_dict)
place = fluid.CUDAPlace(0) if self.gpu else None
with graph.base._switch_tracer_mode_guard_(is_train=False):
model.eval()
for step, (ids, sids, aligned_label, label, orig_pos) in enumerate(test_ds.start(place)):
chine, pre_labels = [], []
for id_ in ids.numpy():
chine.append(list(map(lambda x: self.index_vocab.get(x, 0), id_)))
loss, logic = model(ids, sids, labels=aligned_label)
for pos, lo, la in zip(orig_pos.numpy(), logic.numpy(), label.numpy()):
_dic = OrderedDict()
for p, l in zip(pos, lo):
_dic.setdefault(p, []).append(l)
del _dic[-100]
merged_lo = np.array([np.array(l).mean(0) for _, l in six.iteritems(_dic)])
merged_pred = np.argmax(merged_lo, -1)
la = la[np.where(la != -100)]
if len(la) > len(merged_pred):
print('accuracy loss due to truncation: label len:%d, truncate to %d' % (
len(la), len(merged_pred)))
merged_pred = np.pad(merged_pred, [0, len(la) - len(merged_pred)], mode='constant',
constant_values=-100)
pre_labels.append(merged_pred)
for c, l in zip(chine, pre_labels):
print(c)
print(l)
print()
return