Last modified: Oct 24, 2026
Build a Neural Network with PyTorch
PyTorch makes deep learning simple. It is friendly for beginners. It is also powerful for experts. In this guide, you will build a neural network from scratch.
We will cover tensors, layers, training, and evaluation. You will see working code and real output. Let's get started.
Why Use PyTorch?
PyTorch is an open-source machine learning library. It uses dynamic computation graphs. This means you can change your model on the fly.
The code feels like normal Python. That makes debugging easy. Many researchers and companies use it every day.
PyTorch also runs on CPUs and GPUs. You can move your model with one line of code. This flexibility saves time.
Step 1: Install PyTorch
First, you need PyTorch on your machine. Use pip for a quick install. Run this command in your terminal.
pip install torch torchvision
If you have a GPU, visit the official PyTorch site. It gives you the right command for your system.
Once installed, check the version. This confirms everything works.
import torch
print(torch.__version__)
2.2.1
Step 2: Understand Tensors
Tensors are the core data structure in PyTorch. Think of them as multi-dimensional arrays. They work like NumPy arrays but with GPU support.
You can create a tensor from a Python list. You can also create random or zero tensors.
import torch
# Create a simple tensor
x = torch.tensor([1.0, 2.0, 3.0])
# Create a 2x2 matrix of zeros
zeros = torch.zeros(2, 2)
print(x)
print(zeros)
tensor([1., 2., 3.])
tensor([[0., 0.],
[0., 0.]])
If you plan to move data between services, tools like the ones used to produce and consume Kafka messages in Python can help you stream training data.
Step 3: Prepare Your Data
Every neural network needs data. For this tutorial, we will use a small toy dataset. The goal is to learn the pattern, not to win a contest.
We will create random input features and simple labels. Then we wrap them in a DataLoader. The loader handles batching for us.
import torch
from torch.utils.data import TensorDataset, DataLoader
# Fake dataset: 100 samples, 10 features each
X = torch.randn(100, 10)
y = torch.randint(0, 2, (100,))
# Wrap in a dataset and loader
dataset = TensorDataset(X, y)
loader = DataLoader(dataset, batch_size=16, shuffle=True)
# Check one batch
for batch_x, batch_y in loader:
print(batch_x.shape, batch_y.shape)
break
torch.Size([16, 10]) torch.Size([16])
If your data lives in a database, you can first query Oracle Database from Python and then load it into tensors.
Step 4: Define the Neural Network
Now we build the model. We subclass nn.Module. This is the standard way to define networks in PyTorch.
Our network has two hidden layers. It uses ReLU activation. The output layer has two classes.
import torch.nn as nn
class SimpleNet(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(10, 32) # input to hidden
self.relu = nn.ReLU()
self.fc2 = nn.Linear(32, 16) # hidden to hidden
self.fc3 = nn.Linear(16, 2) # hidden to output
def forward(self, x):
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
return self.fc3(x)
model = SimpleNet()
print(model)
SimpleNet(
(fc1): Linear(in_features=10, out_features=32, bias=True)
(relu): ReLU()
(fc2): Linear(in_features=32, out_features=16, bias=True)
(fc3): Linear(in_features=16, out_features=2, bias=True)
)
The forward method defines how data flows through the layers. PyTorch calls it automatically when you pass data to the model.
Step 5: Choose a Loss Function and Optimizer
The loss function measures how wrong the model is. For classification, cross-entropy is a common choice.
The optimizer updates the weights. We will use Adam. It is fast and works well for most tasks.
import torch.optim as optim
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
The learning rate controls step size. A value like 0.001 is a safe starting point.
Step 6: Write the Training Loop
The training loop is the heart of the process. It repeats several steps. Each step updates the model.
Here is the standard pattern. Forward pass, compute loss, backpropagate, and step the optimizer.
epochs = 5
for epoch in range(epochs):
total_loss = 0.0
for batch_x, batch_y in loader:
optimizer.zero_grad() # clear old gradients
outputs = model(batch_x) # forward pass
loss = criterion(outputs, batch_y)
loss.backward() # compute gradients
optimizer.step() # update weights
total_loss += loss.item()
print(f"Epoch {epoch+1}, Loss: {total_loss/len(loader):.4f}")
Epoch 1, Loss: 0.6931
Epoch 2, Loss: 0.6928
Epoch 3, Loss: 0.6920
Epoch 4, Loss: 0.6911
Epoch 5, Loss: 0.6903
The loss slowly drops. This shows the model is learning. With real data and more epochs, the drop is bigger.
If you deploy models in containers, you may also want to manage Kubernetes pods with Python to automate scaling.
Step 7: Evaluate the Model
Training is not enough. You must check the model on new data. Switch to evaluation mode first.
This disables dropout and batch normalization updates. Then compute accuracy.
model.eval()
with torch.no_grad():
outputs = model(X)
predictions = torch.argmax(outputs, dim=1)
accuracy = (predictions == y).float().mean()
print(f"Accuracy: {accuracy.item():.2f}")
Accuracy: 0.53
The result is close to random. That is expected with fake data. Real datasets will give better numbers.
Common Mistakes to Avoid
Beginners often forget to call optimizer.zero_grad. This causes gradients to pile up. Always clear them before each batch.
Another mistake is forgetting model.eval() during testing. It can give unstable results.
Also, keep an eye on tensor shapes. A shape mismatch throws an error. Print shapes when you are unsure.
Going Further
You now know the basics. Next, try a real dataset like MNIST. Then explore convolutional and recurrent networks.
You can also save and load models with torch.save. This lets you reuse trained weights later.
Data pipelines often grow complex. Services that install kafka-python in Python can feed live data into your training jobs.
Conclusion
Building a neural network with PyTorch is easier than it looks. You start with tensors. You define a model. You train it with a loop.
Each step is short and clear. The code stays readable. That is the beauty of PyTorch.
Practice with small examples first. Then scale up. Soon you will build networks that solve real problems.