简体   繁体   English

Python Numpy浮点数组精度

[英]Python numpy floating point array precision

I am trying to solve for SVM optimisation problem using Pegasos mini-batch algorithm (as in Fig 2) from this link: http://www.cs.huji.ac.il/~shais/papers/ShalevSiSrCo10.pdf 我正在尝试通过此链接使用Pegasos迷你批处理算法(如图2所示)解决SVM优化问题: http : //www.cs.huji.ac.il/~shais/papers/ShalevSiSrCo10.pdf

#X: m*n matrix with m examples and n features per example (m=4000 and n=784 in my case), Y: m length vector containing 1 or -1 for each example, l: lambda as given in algorithm (l=1 in my code), itr: number of iterations, k: size of batch (100) in my case
def pegasos(X,Y,l,n,m,itr,k):
w = np.zeros((1,n),dtype=np.float32)
print m, n
diff = 0.0
for t in range(1,itr+1):
    A = random.sample(range(1,m),k)
    total = np.zeros((1,n),dtype=np.float32)
    eta = 1/(l*t)
    for i in A:
        x = X[i]
        y = Y[i]
        p = y*(np.dot(w,x.T))
        if p < 1:
            p1 = y*x
            total = np.add(total,p1)
    #update rule
    w = np.add((w*(1-(1/t))) , (eta*total*(1/k)))
return w

My dataset is in such a way that when my variable total is computed, I get mostly 0s but there are a few values in the order of 10^(-1) to 10^(-5). 我的数据集以这样一种方式来计算我的变量total时,我得到的大多数为0,但有一些值的顺序为10 ^(-1)到10 ^(-5)。 As soon as total is multiplied by (eta/k) at the update rule, all the values become 0. and hence at every iteration the w I obtain is 0. which should not be the case. 在更新规则中,将total乘以(eta / k)后,所有值均变为0。因此,在每次迭代中,我获得的w为0。情况并非如此。 I have tried ways to increase the precision of my floats but they don't seem to work at all. 我尝试了一些方法来提高浮子的精度,但它们似乎根本不起作用。 When I use basic Pegasos algorithm (as given in Fig 1 in the above link), I don't face any problem, thus my dataset is not utterly weird. 当我使用基本的Pegasos算法(如上面链接中的图1所示)时,我没有遇到任何问题,因此我的数据集并不完全奇怪。 Any help regarding this issue would be highly appreciated :) 任何有关此问题的帮助将不胜感激:)

If you need precision, you should use np.float64 (the normal floating point precision double ). 如果需要精度,则应使用np.float64 (普通浮点精度double )。

If you are using Python 2, you are using integer divition in (1/t) , (1/k) , and (1/l) . 如果您使用的是Python 2,则在(1/t)(1/k)(1/l)中使用整数除法。 Write it as 1.0/ , to force a floating point division. 将其写为1.0/ ,以强制进行浮点除法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM