ByteJoseph commited on
Commit
dbadc63
·
verified ·
1 Parent(s): 6b1faed

Upload folder using huggingface_hub

Browse files
README.md CHANGED
@@ -1,3 +1,41 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # SolveQ
2
+
3
+ Simple neural network that learns `y = 2x + 3`. Built with PyTorch.
4
+
5
+ ## What it does
6
+
7
+ Trains a small network to approximate a linear function. The model has one hidden layer and learns from synthetic data.
8
+
9
+ ## Setup
10
+
11
+ ```bash
12
+ pip install -r requirements.txt
13
+ ```
14
+
15
+ ## Running
16
+
17
+ Train the model:
18
+
19
+ ```bash
20
+ python src/train.py
21
+ ```
22
+
23
+ This generates 200 samples, trains for 1000 epochs, shows the loss plot, and saves to `models/model.pt`.
24
+
25
+ ![Training Loss](models/loss.png)
26
+
27
+ Make predictions:
28
+
29
+ ```bash
30
+ python src/predict.py
31
+ ```
32
+
33
+ Predicts `x = 4.0` by default. To test other values, edit the `predict()` call in `predict.py`.
34
+
35
+ ## Model
36
+
37
+ - Input: 1 neuron
38
+ - Hidden: 16 neurons (ReLU)
39
+ - Output: 1 neuron
40
+
41
+ Uses Adam optimizer (lr=0.01) and MSE loss.
models/loss.png ADDED
models/model.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:41e829441287457f6ea5379c37a4c8447a2ae205487869ad032dc3862ae70118
3
+ size 2505
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch>=2.0.0
2
+ matplotlib>=3.5.0
src/__pycache__/dataset.cpython-311.pyc ADDED
Binary file (722 Bytes). View file
 
src/__pycache__/model.cpython-311.pyc ADDED
Binary file (1.22 kB). View file
 
src/dataset.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ def get_data(n=200):
4
+ x = torch.linspace(-10,10,n).unsqueeze(1)
5
+ y = 2 * x + 3
6
+ return x, y
7
+
8
+ if __name__ == '__main__':
9
+ x,y = get_data()
10
+ print(x)
11
+ print(y)
src/model.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch.nn as nn
2
+
3
+ class LinearModel(nn.Module):
4
+ def __init__(self):
5
+ super().__init__()
6
+ self.net = nn.Sequential(
7
+ nn.Linear(1,16),
8
+ nn.ReLU(),
9
+ nn.Linear(16,1)
10
+ )
11
+ def forward(self, x):
12
+ return self.net(x)
src/predict.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from model import LinearModel
3
+
4
+ def predict(x_value):
5
+ model = LinearModel()
6
+ model.load_state_dict(torch.load("models/model.pt"))
7
+ model.eval()
8
+
9
+ x = torch.tensor([[x_value]], dtype=torch.float32)
10
+ y = model(x)
11
+ print(f"Input: {x_value} | Prediction: {y.item()}")
12
+ def actual(x):
13
+ return 2 * x + 3
14
+ if __name__ == "__main__":
15
+ predict(4.0)
16
+ print(f"Actual: {actual(4.0)}")
src/train.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ from dataset import get_data
4
+ from model import LinearModel
5
+ from matplotlib import pyplot as plt
6
+ def train():
7
+ x_train, y_train = get_data()
8
+ model = LinearModel()
9
+ loss_fn = nn.MSELoss()
10
+ optimizer = torch.optim.Adam(model.parameters(),lr=0.01)
11
+ epochs = 1000
12
+ y_axis = []
13
+ for epoch in range(epochs):
14
+ pred = model(x_train)
15
+ loss = loss_fn(pred,y_train)
16
+ optimizer.zero_grad()
17
+ loss.backward()
18
+ optimizer.step()
19
+ y_axis.append(loss.item())
20
+ if epoch % 100 == 0:
21
+ print(f'Epoch {epoch}, Loss: {loss.item()}')
22
+ x_axis = range(epochs)
23
+ plt.plot(x_axis, y_axis)
24
+ plt.xlabel('Epochs')
25
+ plt.ylabel('Loss')
26
+ plt.title('Training Loss over Epochs')
27
+
28
+
29
+ torch.save(model.state_dict(),'models/model.pt')
30
+ print('Model saved!')
31
+ plt.show()
32
+
33
+ if __name__ == '__main__':
34
+ train()