This is a small educational PyTorch prototype created to learn the basic neural network training workflow.
The goal of the project is to train a simple model to learn the relationship between an input value x and an output value y using the formula:
y = 3x + 2
This project is not intended for real-world use. Its purpose is to demonstrate the minimum working machine learning pipeline:
- preparing data;
- creating a model;
- calculating loss;
- running backpropagation;
- updating model parameters;
- testing the trained model.
At the current stage, the project contains one main script:
train.py
This script includes:
- training data;
- an
nn.Linearmodel; - the
MSELossloss function; - the
SGDoptimizer; - the training loop;
- model prediction testing;
- output of the learned formula.
- Python
- PyTorch
- Git
This project helps understand the basic PyTorch components.
Data is stored as tensors:
x_train = torch.tensor([[1.0], [2.0], [3.0]])The model is created with:
model = nn.Linear(1, 1)It tries to learn a relationship in the form:
y = wx + b
Where:
wis the weight;bis the bias;- both parameters are adjusted automatically during training.
The error is calculated using:
loss_fn = nn.MSELoss()It measures how far the model predictions are from the correct answers.
The model parameters are updated using:
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)The core PyTorch training loop:
y_pred = model(x_train)
loss = loss_fn(y_pred, y_train)
optimizer.zero_grad()
loss.backward()
optimizer.step()- Make sure Python is installed.
- Make sure PyTorch is installed.
- Open the project folder in a terminal.
- Run:
python train.pyAfter running the script, the program should print the training progress, the model prediction, and the approximate formula learned by the model.
After training, the model should learn an approximation of:
y = 3x + 2
For example, for:
x = 10
The correct answer is:
y = 32
The model should predict a value close to 32.
pytorch-formula-prototype/
│
├── train.py
├── README.md
└── .gitignore
# Python
__pycache__/
*.pyc
*.pyo
*.pyd
# Virtual environment
.venv/
venv/
env/
# IDE
.vscode/
.idea/
# PyTorch / model files
*.pt
*.pth
*.onnx
# System files
.DS_Store
Thumbs.dbCurrently, the model is tested with one value. More test values can be added:
x = 10
x = 15
x = 25
x = -5
This will make it easier to check how well the model generalizes.
The script can print more useful training information:
- epoch number;
- current loss;
- model weight;
- model bias;
- final learned formula.
Example output:
Epoch 100 | Loss: 0.00231 | y = 2.98x + 2.11
Move training parameters into variables:
epochs = 1000
learning_rate = 0.01This will make experiments easier.
After the first working version, the project can be split into separate files:
pytorch-formula-prototype/
│
├── train.py
├── model.py
├── data.py
├── predict.py
├── config.py
├── README.md
└── .gitignore
Contains the model definition.
Contains training data preparation.
Runs the training process.
Runs predictions using the trained model.
Stores project settings:
- number of epochs;
- learning rate;
- training data size;
- path for saving the model.
Add model saving:
torch.save(model.state_dict(), "model.pt")And model loading:
model.load_state_dict(torch.load("model.pt"))This will make it possible to reuse the trained model without training it again every time.
Create a separate file:
predict.py
Then run:
python predict.pyExample interaction:
Enter x: 10
Model prediction: 31.98
Add charts using matplotlib:
- loss curve;
- comparison between correct values and model predictions.
This will help visualize how the model learns.
After the linear formula, try a more complex relationship:
y = x² + 2x + 1
For this task, a single nn.Linear(1, 1) model will not be enough.
A small neural network can be used instead:
model = nn.Sequential(
nn.Linear(1, 16),
nn.ReLU(),
nn.Linear(16, 16),
nn.ReLU(),
nn.Linear(16, 1)
)This helps explain why hidden layers and activation functions are useful.
The next educational project can be point classification.
Example task:
If x + y > 0 → class 1
Otherwise → class 0
This will introduce:
- binary classification;
BCEWithLogitsLoss;- accuracy;
- the difference between regression and classification.
After basic regression and classification, the project can move to the MNIST dataset.
Goal:
Recognize handwritten digits from 0 to 9
New topics:
torchvision;DataLoader;- batches;
CrossEntropyLoss;- simple neural networks for images.
- Linear regression:
y = 3x + 2. - Nonlinear regression:
y = x² + 2x + 1. - Binary point classification.
- MNIST classification.
- Saving and loading a model.
- Using a trained model in a separate prediction script.
- Simple formula learning.
- Tabular data.
- Basic object classification.
- Mini recommendation system.
- Image data.
- Small custom dataset.
- Simple inference script.
- Model export.
The model receives the hour of the day and predicts the temperature.
Goal: understand nonlinear relationships.
The model receives object features:
- weight;
- price;
- rarity;
- type;
- level.
Then it predicts the item category.
This can be useful for future game development projects.
The model receives user and game features and predicts whether the user will like the game.
Example features:
- likes strategy games;
- likes RPGs;
- prefers short games;
- the game is turn-based;
- the game is free;
- the game is difficult.
The model predicts whether an item is too strong based on its stats.
Example features:
- damage;
- attack speed;
- rarity;
- cost;
- player level.
Possible tasks:
- circle / square / triangle classification;
- tile type recognition;
- simple game icon classification.
The main goal of this project is not to build a complex neural network, but to understand the fundamental PyTorch workflow:
data → model → prediction → loss → backward → optimizer → improved model
After understanding this cycle, it will be easier to move on to more complex tasks:
- classification;
- image recognition;
- game-related data;
- recommendation systems;
- custom datasets.