Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def loss(X, Y, W):
delta = 1
scores = np.dot(W, X.T)
rows = np.arange(Y.shape[0])
margins = np.maximum(0, scores - scores[Y, rows] + delta)
# Find indices that need to be set to zero.
# Case when j == y in loss_i_v.
subtract_Y_indices = np.zeros(shape=margins.shape, dtype=bool)
subtract_Y_indices[Y, rows] = True
# Set proper indices to 0.
margins[subtract_Y_indices] = 0
return np.sum(margins)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment