-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVM.py
57 lines (43 loc) · 1.31 KB
/
SVM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import torch
import torch.optim as optim
import torch.nn as nn
import matplotlib.pyplot as plt
torch.manual_seed(1)
X = torch.Tensor([[1, 0, 0, -1],[0, 1, 0, -1]])
y = torch.Tensor([1, 1, -1, -1])
alpha = 0.001
C = 1
class ShallowNet(nn.Module):
def __init__(self):
super(ShallowNet, self).__init__()
self.fc1 = nn.Linear(2,1, bias=True)
def forward(self, X):
return self.fc1(X)
net = ShallowNet()
print(net)
print(net(torch.transpose(X,0,1)).squeeze())
optimizer = optim.SGD(net.parameters(), lr=alpha, weight_decay=0)
optimizer.zero_grad()
params = list(net.parameters())
params[0].data = torch.Tensor([[2, 2]])
params[1].data = torch.Tensor([-1])
farr = []
for iter in range(10000):
if iter==0:
print(1 - y*net(torch.transpose(X,0,1)).squeeze())
loss = C/2*torch.pow(torch.norm(params[0]),2) + torch.sum(torch.max(torch.zeros_like(y), 1 - y*net(torch.transpose(X,0,1)).squeeze()))
loss.backward()
gn = 0
for f in net.parameters():
if iter==0:
print("Test")
print(f.grad)
gn = gn + torch.norm(f.grad)
print("Iter: %d; Loss: %f; ||g||: %f" % (iter, loss, gn))
optimizer.step()
optimizer.zero_grad()
farr.append(loss.item())
for f in net.parameters():
print(f)
plt.plot(farr)
plt.show()