Skip to content

Instantly share code, notes, and snippets.

@pratos
Created April 12, 2017 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pratos/f59cddc275711176e176527ade0417f1 to your computer and use it in GitHub Desktop.
Save pratos/f59cddc275711176e176527ade0417f1 to your computer and use it in GitHub Desktop.
CS231n Hinge Loss SVM Snippet - Vectorized
import numpy as np
W = np.array([(0.01,-0.05,0.1,0.05),(0.7,0.2,0.05,0.16),(0.0,-0.45,-0.2, 0.03)]) #Weights
xi = np.array([-15,22,-44,56]) #Input
b = np.array([0.0,0.2,-0.3]) #Bias
delta = 1
y = np.sum((np.dot(W,xi),b), axis=0)
#comp = (sj - syi)
# sj = y[:y.size-1]
# syi = y[y.size-1]
comp = np.sum((y[:y.size-1],-y[y.size-1]), axis=0)
# li = summation(max(0, sj-syi+delta))
li = np.sum(np.max((np.zeros((y.size-1,)), np.sum((comp,delta), axis=0)), axis=0))
# li = 1.5800000000000018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment