45. Your First Neural Network in PyTorch
45.1 Build a Tiny Sequential Net
Why. Put Chapters 41–44 together: a tiny net in PyTorch code you can run.
What. torch.nn.Sequential stacks layers. For a table with 3 features → hidden 8 → output 1 (binary score before sigmoid).
How.
Build the module
- Import
torchandtorch.nn as nn. - Define Sequential: Linear → ReLU → Linear.
- Print the module.
import torch
import torch.nn as nn
model = nn.Sequential(
nn.Linear(3, 8),
nn.ReLU(),
nn.Linear(8, 1),
)
print(model)
x = torch.randn(4, 3) # batch of 4
print(model(x).shape) # (4, 1)
What you should see. Layer list and output shape (4, 1).