-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogisticRegression.py
44 lines (32 loc) · 954 Bytes
/
LogisticRegression.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
import torch
import torch.optim as optim
import torch.nn as nn
torch.manual_seed(1)
X = torch.Tensor([[-1, 1, 2],[1, 1, 1]])
y = torch.Tensor([0, 1, 1])
alpha = 1
class ShallowNet(nn.Module):
def __init__(self):
super(ShallowNet, self).__init__()
self.fc1 = nn.Linear(2,1, bias=False)
def forward(self, X):
return self.fc1(X)
net = ShallowNet()
print(net)
net.fc1.weight.data = torch.Tensor([[0.1, 0.1]])
print(net(torch.transpose(X,0,1)).squeeze())
optimizer = optim.SGD(net.parameters(), lr=alpha)
optimizer.zero_grad()
criterion = nn.BCEWithLogitsLoss()
for iter in range(100):
netOutput = net(torch.transpose(X,0,1)).squeeze()
loss = criterion(netOutput, y)
loss.backward()
gn = 0
for f in net.parameters():
gn = gn + torch.norm(f.grad)
print("Loss: %f; ||g||: %f" % (loss, gn))
optimizer.step()
optimizer.zero_grad()
for f in net.parameters():
print(f)