You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#Write in python the loss function for support vector machines from equation (7.48) of Daumé. You can use the following hinge loss surrogate:defhinge_loss_surrogate(y_gold, y_pred):
returnnp.max([0, 1-y_gold*y_pred])
defsvm_loss(wb:np.ndarray,
C:float,
D:np.ndarray):
w=wb[:-1]
b=wb[-1]
# replace with your implementation, must call hinge_loss_surrogatecumul_loss=0foriteminD:
cumul_loss+=hinge_loss_surrogate(item[1], np.dot(w, item[0]) +b)
loss=np.dot(w, w) /2+C*cumul_lossreturnloss
#Use scipy.optimize.minimize with jac=False to implement support vector machines.defsvm(D:np.ndarray,
C:float=1):
# compute w and b with scipy.optimize.minimize and return themwb=np.zeros(D[0][0].shape[0] +1)
minfunc=partial(svm_loss, C=C, D=D)
res=scipy.optimize.minimize(minfunc, x0=wb, jac=False).xreturnres[:-1], res[-1]
#Implement the gradient of svm_loss, and add an optional flag to svm to use it:defgradient_hinge_loss_surrogate(y_gold, y_pred):
ifhinge_loss_surrogate(y_gold, y_pred) ==0:
return0else:
return-y_golddefgradient_svm_loss(wb:np.ndarray,
D:np.ndarray,
C:float=1):
w, b=wb[:-1], wb[-1]
der_by_w=0der_by_b=0foriteminD:
der_by_w+=item[0] *gradient_hinge_loss_surrogate(
item[1],
np.dot(w, item[0]) +b
)
der_by_b+=gradient_hinge_loss_surrogate(
item[1],
np.dot(w, item[0]) +b
)
der_w=w+C*der_by_wder_b=C*der_by_breturnnp.hstack([der_w, der_b])
defsvm(D:np.ndarray,
C:float=1,
use_gradient=False):
wb=np.zeros(D[0][0].shape[0] +1)
minfunc=partial(svm_loss, C=C, D=D)
minder=partial(gradient_svm_loss, C=C, D=D)
ifuse_gradient:
res=scipy.optimize.minimize(minfunc, x0=wb, jac=minder).xnew_w, new_b=res[:-1], res[-1]
else:
res=scipy.optimize.minimize(minfunc, x0=wb, jac=False).xnew_w, new_b=res[:-1], res[-1]
returnnew_w, new_b
#Use numpy.random.normal to generate two isolated clusters of points in 2 dimensions, one x_plus and one x_minus, and #graph the three hyperplanes found by training:#an averaged perceptron#support vector machine without gradient#support vector machine with gradient.#How do they compare?x_plus=np.random.normal(loc=[1,1], scale=0.5, size=(20,2))
x_minus=np.random.normal(loc=[-1,-1], scale=0.5, size=(20,2))