-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_onboarding_extra.py
More file actions
66 lines (54 loc) · 1.72 KB
/
auto_onboarding_extra.py
File metadata and controls
66 lines (54 loc) · 1.72 KB
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
58
59
60
61
62
63
64
65
66
import torch
from torch import nn
from math import pi
import matplotlib.pyplot as plt
# Set up data
r = torch.arange(0, 3, 0.2).unsqueeze(1) # radii from 0–3
y = pi * (r ** 2) # true area
# Split into train/test
trainsplit = int(0.7 * len(r))
rtrain, ytrain = r[:trainsplit], y[:trainsplit]
rtest, ytest = r[trainsplit:], y[trainsplit:]
# Define model
class CircleAreaPredictor(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(nn.Linear(1, 8), nn.ReLU(), nn.Linear(8, 8), nn.ReLU(), nn.Linear(8, 1))
def forward(self, x):
return self.net(x)
# Create instance
torch.manual_seed(0)
model = CircleAreaPredictor()
# Loss & optimizer
lossfn = nn.L1Loss()
optfn = torch.optim.SGD(model.parameters(), lr=0.01)
# Save predictions before training (for visualization)
with torch.inference_mode():
ypred_before = model(r)
# Training loop
iterations = 2000
for i in range(iterations):
model.train()
ypred = model(rtrain)
loss = lossfn(ypred, ytrain)
optfn.zero_grad()
loss.backward()
optfn.step()
# Predictions after training
with torch.inference_mode():
ypred_after = model(r)
# visialie
plt.figure(figsize=(8, 5))
plt.scatter(r, y, color='blue', label='True area (πr²)', s=60)
plt.plot(r, ypred_before, color='red', linestyle='--', label='Before training')
plt.plot(r, ypred_after, color='green', label='Predicted area (after training)')
plt.xlabel("Radius (r)")
plt.ylabel("Area")
plt.title("Neural Network Learning the Area of a Circle")
plt.legend()
plt.grid(True)
plt.show()
with torch.inference_mode():
sample_r = torch.tensor([[1.0]])
predicted_area = model(sample_r).item()
print(f"Predicted area for r=1: {predicted_area:.4f} (true = {pi:.4f})")