Пример #1
0
def svm_primal_sgd(X, Y, lam, T):  
  w = CuckooVector({})
  x = CuckooVector({})
  m = len(Y)
  
  for t in range(T):
    step = 1 / (lam * (t+1))
    i = np.random.randint(m)
    y = Y[i]
    x.reset(X[i])
    w.scale(1 - step * lam)
    if y * w.dot(x) < 1:
      w.add_scale(x, step * y)

  return w
  
Пример #2
0
def pegasos(lam):
    w = CuckooVector({})
    wx = 0
    t = 0

    #  while True:
    #    example = yield (w,wx)
    #    if example == None: break
    #    x,y = example
    for xy in (yield (w, wx)):
        x, y = xy
        print(x)
        print(y)
        step = 1 / (lam * (t + 1))
        w.scale(1 - step * lam)
        wx = w.dot(x)
        if y * wx < 1:
            w.add_scale(x, step * y)
        t = t + 1
Пример #3
0
def weightedError(w, Xt, Yt, thresh = 0):
   xv = CuckooVector({})
   return (1 + np.mean([(xv.reset(x) == None and np.sign(xv.dot(w) - thresh)*y) for (x,y) in zip(Xt,Yt)]))/2