SVM and PCA not working

This commit is contained in:
unlockable
2024-05-18 00:12:06 +08:00
parent 81de7b1d58
commit 820f679067
8 changed files with 85 additions and 64 deletions

View File

@@ -2,8 +2,8 @@
# Media and Cognition
# Homework 3 Support Vector Machine
# svm_hw.py - The implementation of SVM using hinge loss
# Student ID:
# Name:
# Student ID: 2022010639
# Name: Yixuan Gao
# Tsinghua University
# (C) Copyright 2024
# ========================================================
@@ -34,7 +34,7 @@ class LinearFunction(torch.autograd.Function):
'''
# TODO
y = ???
y = torch.matmul(x, W.T) + b
ctx.save_for_backward(x, W)
return y
@@ -59,9 +59,9 @@ class LinearFunction(torch.autograd.Function):
# you can use torch.matmul(A, B) to compute matrix product of A and B
# TODO
grad_input = ???
grad_W = ???
grad_b = ???
grad_input = torch.matmul(grad_output, W)
grad_W = torch.matmul(grad_output.T, x)
grad_b = grad_output.sum(0)
return grad_input, grad_W, grad_b
@@ -85,7 +85,11 @@ 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.
loss = ???
# 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()
ctx.save_for_backward(output, W, label, C)
return loss
@@ -102,8 +106,11 @@ class Hinge(torch.autograd.Function):
"""
output, W, label, C = ctx.saved_tensors
# TODO: compute the grad with respect to the output of the linear function and W: dL/doutput, dL/dW
grad_output = ???
grad_W = ???
# 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_W = grad_loss * W
return grad_output, grad_W, None, None
@@ -124,8 +131,8 @@ class SVM_HINGE(nn.Module):
please use torch.randn() to initialize W and b
"""
self.W = ???
self.b = ???
self.W = nn.Parameter(torch.rand(1, in_channels), requires_grad=True)
self.b = nn.Parameter(torch.rand(1, ), requires_grad=True)
self.C = torch.tensor([[C]], requires_grad=False)
def forward(self, x, label=None):