def surrogateF1_loss(target, output): # WiP #https://arxiv.org/pdf/1608.04802.pdf y_true = K.flatten(target * 2 - 1) y_pred = K.flatten(output * 2 - 1) Yp = K.flatten(target) Yn = 1. - Yp tpl = K.sum(1. - categorical_hinge(y_true * Yp, y_pred * Yp)) fpu = K.sum(categorical_hinge(y_true * Yn, y_pred * Yn)) # 2 tpl / |Y+| + tpl + fpu return 2 * tpl / (K.sum(Yp) + tpl + fpu)
def test_categorical_hinge(): y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([[0, 1, 0], [1, 0, 0]])) expected_loss = ((0.3 - 0.2 + 1) + (0.7 - 0.1 + 1)) / 2.0 loss = K.eval(losses.categorical_hinge(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
def test_categorical_hinge(): y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([[0, 1, 0], [1, 0, 0]])) expected_loss = ((0.3 - 0.2 + 1) + (0.7 - 0.1 + 1)) / 2.0 loss = K.eval(losses.categorical_hinge(y_true, y_pred)) assert np.isclose(expected_loss, np.mean(loss))
def call(self, inputs): y_pred, y_true = inputs y_true = K.reshape(y_true, (-1, 8, 80)) y_true = K.max(y_true, axis=1) return categorical_hinge(y_pred, y_true)
print("PREPAIRING") ytrain_inds = y_train.argmax(axis=1) X_batch = X_train[indices] true_class = ytrain_inds[indices] grads, scores, weights = weights_grad(X_batch, model) gstack = np.zeros((num_weights, num_classes * batch_size)) for nn in range(batch_size): for class_ind in range(num_classes): gstack[:, [nn * num_classes + class_ind]] = np.vstack([ param.reshape(-1, 1) for param in (grads[3 * nn + class_ind]) ]) #print(gstack.shape) y = scores keras_l = np.sum(k.eval(categorical_hinge(y_train[indices, :], y))) #print(y) #print(y.reshape((-1,1))) wk = np.vstack([param.reshape(-1, 1) for param in weights]) print("SETTING UP CONVEX PROBLEM") w = cp.Variable((num_weights, 1)) #yhat = cp.Variable((batch_size,num_classes)) #const = [yhat == y + cp.reshape((gstack.T@(w - wk)),(batch_size,num_classes))] const = [] yhat = y + cp.reshape((gstack.T @ (w - wk)), (batch_size, num_classes)) #f = cp.sum(-yhat[np.arange(batch_size),true_class] + cp.log_sum_exp(yhat, axis=1)) + cp.norm(w,2) f = cp.sum( cp.pos(yhat - yhat[np.arange(batch_size), [true_class]].T @ np.ones( (1, 3)) + 1)) + 10 * cp.norm(w - wk, 2) objective = cp.Minimize(f)