metrics 计算报错
miaozjs opened this issue · 1 comments
miaozjs commented
metrics.py
文件中计算报错,断点调试,计算的 stdout 为 '',转换float报错,文件中完整代码如下,这里应该如何处理,是否subprocess.Popen中指令需要变动,期待大佬的解答!
@register('map')
@register('mrr')
def ranking(outputs):
args = outputs['args']
dataset = outputs['dataset']
prediction = [o[1] for o in outputs['prob']]
ref_file = os.path.join(dataset, '{}.ref'.format(args.eval_file))
rank_file = os.path.join(dataset, '{}.rank'.format(args.eval_file))
tmp_file = os.path.join(args.summary_dir, 'tmp-pred.txt')
with open(rank_file) as f:
prefix = []
for line in f:
prefix.append(line.strip().split())
assert len(prefix) == len(prediction), \
'prefix {}, while prediction {}'.format(len(prefix), len(prediction))
with open(tmp_file, 'w') as f:
for prefix, pred in zip(prefix, prediction):
prefix[-2] = str(pred)
f.write(' '.join(prefix) + '\n')
sp = subprocess.Popen('./resources/trec_eval {} {} | egrep "map|recip_rank"'.format(ref_file, tmp_file),
shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = sp.communicate()
stdout, stderr = stdout.decode(), stderr.decode()
os.remove(tmp_file)
if '':
print(00)
else:
print('11')
map_, mrr = [float(s[-6:]) for s in stdout.strip().split('\n')]
return {
'map': map_,
'mrr': mrr,
}
miaozjs commented
报错位置为return 的上一行
map_, mrr = [float(s[-6:]) for s in stdout.strip().split('\n')]