confidence weighted 置信权重在线算法(转)

来源:互联网 发布:bae java 编辑:程序博客网 时间:2024/06/01 21:59


转载链接:http://blog.csdn.net/nlp_zcchen/article/details/17381823


这个算法原作者意思是在NLP中面对高维向量和数据稀疏时效果会不错,算法保持了当前预测向量w的均值和方差,并做优化。代码实现如下:

[python] view plain copy
  1. #cw learning algorithm  
  2. def get_phi():  
  3.     '''''confidence parameter phi'''  
  4.     import numpy as np  
  5.     from scipy import stats  
  6.     eta = float(raw_input("please input confidence parameter between (0 1)\n"))  
  7.     X = stats.norm(0.0,1.0)  
  8.     phi = X.ppf(eta)  
  9.     return phi  
  10.   
  11. def get_data_size(datafile):  
  12.     f = open(datafile)  
  13.     size = f.readline().strip().split()  
  14.     f.close()  
  15.     return len(size)  
  16.   
  17. def dot(X,Y):  
  18.     sum = 0  
  19.     for (x,y) in zip(X,Y):  
  20.         sum += x*y  
  21.     return sum  
  22. def mul(X,Y):  
  23.     Z = []  
  24.     for (x,y) in zip(X,Y):  
  25.         Z.append(x*y)  
  26.     return Z  
  27. def plus(X,Y):  
  28.     Z = []  
  29.     for (x,y) in zip(X,Y):  
  30.         Z.append(x+y)  
  31.     return Z  
  32.   
  33. def opp(X):  
  34.     Y = []  
  35.     for x in X:  
  36.         Y.append(1/x)  
  37.     return Y  
  38. def init_u_E(datafile):  
  39.     #initialize  
  40.     #input  
  41.      #confidence parameter phi  
  42.     phi = get_phi()  
  43.     #init variance of predictor w  
  44.     a = float(raw_input("initial variance parameter a \n"))  
  45.     size = get_data_size(datafile)  
  46.     u = []  
  47.     E = []  
  48.     for i in range(size-1):  
  49.         u.append(0)  
  50.         E.append(a)  
  51.     return u,E,phi  
  52. def CWL(datafile,u,E,phi = 0.8):  
  53.     '''''wl learning algorithm'''  
  54.     import math  
  55.     for line in open(datafile):  
  56.         Y_X_str = line.strip().split()  
  57.         Y_X = [float(t) for t in Y_X_str]  
  58.         X = Y_X[1:]  
  59.         Y = Y_X[0]  
  60.         Mi = Y*dot(X,u) #Mi>0 then the prediction is right,otherwise the prediction is wrong  
  61.         Vi = dot(X,mul(X,E))  
  62.         Ri = (-1-2*phi*Mi+float(math.sqrt((1+2*phi*Mi)*(1+2*phi*Mi)-8*phi*(Mi-phi*Vi))))/(4*phi*Vi)  
  63.         alpha = max(Ri,0)  
  64.         dlt_u = [alpha*Y*x for x in mul(E,X)]  
  65.         u = plus(u,dlt_u)  
  66.         dlt_E = [ 2*alpha*phi*x for x in mul(X,X)]  
  67.         E = opp(plus(opp(E),dlt_E))  
  68.     return u,E  
  69.   
  70. #test  
  71. u,E,phi = init_u_E("data")  
  72. for i in range(10000):  
  73.     u,E = CWL("data",u,E,phi)  
  74. print u  
  75. print E  
  76. print dot(u,[0,1,0])  

数据类型为

1 0 0 1

-1 0 1 0

第一列为准确的类型,后面是feature set。

两个参数,phi是对于当前预测的置信度,即当预测的概率大于phi时才做预测(代码中预设为0.8),alpha为初始预测向量w的方差(预设为1)

测试效果不定,手写了7组数据,5个是可以判断正确的

还有一部分是对于有多个label时设计的算法,multi-class CW。这里是假设存在一个feature function将当前x和一个lable映射为一个值,即f(x,y),那么在算法实际过程中,利用这个值来对所有的label进行一个排序,那么将产生K个(K为label总数)限制条件(在binary classify中只有一个限制条件),算法再根据这K个条件做取舍决定最后的参数更新值(原文中有几种不同的更新方法)。

参考文献 confidence weighted linear classification by Mark Dredze,Koby Crammer

Multii-class confidence weighted algoritms


阅读全文
0 0