Homework3 Submit

This commit is contained in:
unlockable
2024-05-18 16:23:40 +08:00
parent 820f679067
commit c850f38778
24 changed files with 206 additions and 12 deletions

View File

@@ -85,11 +85,13 @@ class Hinge(torch.autograd.Function):
# TODO: compute the hinge loss (together with L2 norm for SVM): loss = 0.5*||w||^2 + C*\sum_i{max(0, 1 - y_i*output_i)}
# you may need F.relu() to implement the max() function.
# print("output size", output.size())
# print("label size", label.size())
# print("product", label * output.reshape_as(label))
# print("minus", 1 - label * output.reshape_as(label))
# print("relu", F.relu(1 - label * output.reshape_as(label)))
# print("sum", (F.relu(1 - label * output.reshape_as(label))).sum())
loss = 1/2 * (W @ W.T) + C * (F.relu(1 - label * output.reshape_as(label))).sum()
loss = 1/2 * (W @ W.T) + C * (F.relu(1 - (output.T * label).T)).sum()
ctx.save_for_backward(output, W, label, C)
return loss
@@ -109,7 +111,7 @@ class Hinge(torch.autograd.Function):
# print("output", output, "label", label, "product", (1 - label.reshape_as(output) * output))
# print("grad_loss size", grad_loss.size())
# print("sizeof l / output", (C * torch.heaviside(1 - label.reshape_as(output) * output, torch.tensor(0).type_as(output)) * (-label.reshape_as(output))).size())
grad_output = grad_loss * C * (torch.heaviside(1 - label.reshape_as(output) * output, torch.tensor(1).type_as(output)) * (-label.reshape_as(output)))
grad_output = grad_loss * C * ((torch.heaviside(1 - (output.T * label).T, torch.tensor(1).type_as(output)).T * (-label))).T
grad_W = grad_loss * W
return grad_output, grad_W, None, None