def rhinge_loss(self, X, targets): """ returns reverse hinge loss of points in X and their targets """ preds = np.matmul(X, self.weights.T) + self.bias res = [] for i in xrange(len(X)): target = targets[i] if np.argmax(preds) != target: max_ix, max_val = getMax(preds[i], target) loss = max_val - preds[i][target] else: loss = 0 res.append(loss) return res
def untargeted_loss(self, X, Y): """ computes the untargeted hinge loss of (X, Y) """ preds = np.matmul(X, self.weights.T) + self.bias n = len(X) loss = [] for i in xrange(n): y = Y[i] others = range(10) del others[y] if np.argmax(preds[i]) != y: res = 0 else: max_val = getMax(preds[i], y)[1] y_val = preds[i][y] res = y_val - max_val loss.append(res) return np.array(loss)
def gradient_untargeted(self, X, Y): """ computes gradients the untargeted hinge loss of (X, Y) """ preds = np.matmul(X, self.weights.T) + self.bias n = len(X) gradient = [] for i in xrange(n): y = Y[i] others = range(10) del others[y] if np.argmax(preds[i]) != y: res = np.zeros(self.dim) else: max_ix = getMax(preds[i], y)[0] w_max = self.weights[max_ix] w_y = self.weights[y] res = w_y - w_max gradient.append(res) return np.array(gradient)
def gradient(self, X, targets): """ returns gradient of the reverse (targeted) hinge loss """ preds = np.matmul(X, self.weights.T) + self.bias n = X.shape[0] gradient = [] for i in xrange(n): target = targets[i] others = range(10) del others[target] if np.argmax(preds[i]) == target: res = np.zeros(self.dim) else: max_ix = getMax(preds[i], target)[0] w_max = self.weights[max_ix] w_target = self.weights[target] res = w_max - w_target gradient.append(res) return np.array(gradient)