mirror of https://github.com/da0c/DL_Course_SamU
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3.2 KiB
3.2 KiB
In [27]:
import numpy as np
In [28]:
def softmax(s, y): l_i = -np.log(np.exp(s[y])/sum(np.exp(s))) return l_i def svm(s, y): m = np.maximum(np.zeros_like(s), s - s[y] + 1) m[y] = 0 l_i = np.sum(m) return l_i
In [34]:
s = np.array([[10, -2, 3], [10, 9, 9], [10, -100, -100]]) y = 0
In [35]:
print("softmax:\n") for i in range(s.shape[0]): print(softmax(s[i,:], y)) print("\nSVM:\n") for i in range(s.shape[0]): print(svm(s[i,:], y))
softmax: 0.0009176050495943237 0.5514447139320511 -0.0 SVM: 0 0 0
In [37]:
s[0,0] += 10 s
Out[37]:
array([[ 20, -2, 3], [ 10, 9, 9], [ 10, -100, -100]])
In [38]:
print("softmax:\n") for i in range(s.shape[0]): print(softmax(s[i,:], y)) print("\nSVM:\n") for i in range(s.shape[0]): print(svm(s[i,:], y))
softmax: 4.167832299541146e-08 0.5514447139320511 -0.0 SVM: 0 0 0
In [ ]: