-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytorch.py
49 lines (35 loc) · 1.04 KB
/
pytorch.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
import torch
import torch.nn as nn
import torch.nn.functional as F
class NN(nn.Module):
def __init__(self, input_size, output_size):
super().__init__()
self.arch = nn.Sequential(nn.Linear(input_size, 2), nn.Sigmoid(), nn.Linear(2, output_size), nn.Sigmoid())
def forward(self, x):
return self.arch(x)
def main():
torch.random.manual_seed(0)
data = torch.tensor(
[
[0, 0, 0],
[0, 1, 1],
[1, 0, 1],
[1, 1, 0],
],
dtype=torch.float32,
)
model = NN(input_size=2, output_size=1)
optimizer = torch.optim.SGD(model.parameters(), lr=1)
in_data = data[:, :-1]
label = data[:, -1]
for i in range(2000):
pred = model(in_data).squeeze()
loss = F.mse_loss(pred, label)
loss.backward()
optimizer.step()
if (i + 1) % 100 == 0:
print(f"epoch: {i+1} loss: {loss}")
for i in data:
print(f"{i[0]} ^ {i[1]} = {model(i[:2]).item():.3f}")
if __name__ == "__main__":
main()