Skip to main content
  1. Posts/

使用python计算误差代码

·1 min
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

import os

 1import math
 2ave_err=0.0
 3max_err=0.0
 4max_err_rate=0.0
 5length=0
 6with open("cpu_result.txt","r") as fp1, open("cuda_ppl_result.txt","r") as fp2:
 7    for l1 in fp1:
 8        l2 = fp2.readline()
 9        l1=l1[:-2]
10        l2=l2[:-2]
11        lst = l1.split(' ')
12        lst2 = l2.split(' ')
13        #print lst
14        lst = [float(x) for x in lst ]
15        length = length + len(lst)
16        lst2 = [float(x) for x in lst2]
17        #print (lst)
18        #print (lst2)
19
20        for index,x in enumerate(lst):
21            y = lst2[index]
22            ave_err = ave_err + abs(x-y)
23            max_err = max(max_err,abs(x-y))
24            max_err_rate = max(max_err_rate,abs(x-y)/x)
25
26    print("len=",length)
27    print("max_err=",max_err)
28    print("max_err_rate=",max_err_rate*100,"%")
29    print("ave_err=",ave_err/length)

需要提供两个文件,并且两个文件的数据格式相同。

Related

conda升级anaconda ValueError的解决办法

·1 min
conda update anaconda 后提示 1ValueError: unsupported format character ')' (0x29) at index 49 查到了这个:anaconda update issue I have narrowed this down to the following packages: package build psutil-1.2.1 py27_0 hard-link pycparser-2.10 py27_0 hard-link pykit-0.1.0 np18py27_2 hard-link pyparsing-2.0.1 py27_0 hard-link by calling "conda install anaconda" and then successfully installing everything else one at a time. These four packages consistently exhibit the described behaviour. (note: pykit depends on pycparser so may itself be ok - can't tell) 我先把psutil卸载掉,重新update了一下,成功。