Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.51 KB | None | 0 0
  1. def svm_subgradient_solver(x, y, C, tol=1e-6, max_iter=100, verbose=False):
  2.     Dict={}
  3.     w=np.zeros(x.shape[1])
  4.     g=np.zeros(x.shape[1])
  5.     a = 1
  6.     b = 10000
  7.     for iteration in range(max_iter):
  8.         g=np.copy(w)
  9.         for n in range(y.shape[0]):
  10.             if 1>y[n]*np.sum(w*x[n]):
  11.                 g-=C*y[n]*x[n]
  12.         w=w-g * a / b
  13.         Sum=0
  14.         for i in range(y.shape[0]):
  15.             Sum+=max(0,1-y[i]*np.sum(w*x[i]))
  16.         print( 1/2*(np.sum(w*w)+C*Sum))
  17.     return Dict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement