Skip to content

Commit 598f835

Browse files
authored
Create feedforward_net.py
1 parent 91fed3e commit 598f835

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

feedforward_net.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import torch
2+
import torch.nn as nn
3+
import torch.optim as optim
4+
from torch.utils.tensorboard import SummaryWriter
5+
writer = SummaryWriter('runs/dummy_model')
6+
x = torch.tensor([[0,0,1],[0,1,1],[1,0,1],[1,1,1]]).float()
7+
y = torch.tensor([[0], [1], [1], [0]]).float()
8+
class Net(nn.Module):
9+
def __init__(self, inp, out):
10+
super(Net, self).__init__()
11+
self.input = nn.Linear(inp, 4)
12+
self.sigmoid = nn.Sigmoid()
13+
self.h1 = nn.Linear(4, 8)
14+
self.h2 = nn.Linear(8, 16)
15+
self.output = nn.Linear(16, out)
16+
17+
def forward(self, x):
18+
x = self.input(x)
19+
x = self.sigmoid(self.h1(x))
20+
x = self.h2(x)
21+
x = self.output(x)
22+
return x
23+
24+
25+
model = Net(inp=3, out=1)
26+
for name, param in model.named_parameters():
27+
if param.requires_grad:
28+
print(name)
29+
#print(model.forward(x))
30+
#print(model.forward(torch.tensor([[0, 0, 1]]).float()))
31+
model.zero_grad()
32+
criterion = nn.MSELoss()
33+
optimr = optim.SGD(model.parameters(), lr=0.001)
34+
for i in range(60000):
35+
output = model(x)
36+
target_ = y
37+
loss = criterion(output, target_)
38+
print(loss)
39+
loss.backward()
40+
optimr.step()
41+
42+
print(model(x))

0 commit comments

Comments
 (0)