Getting Started
Beginner
30 minBuild Your First Neural Network
Learn to build, train, and evaluate a simple feedforward neural network from scratch using PyTorch in DLWΛY.
In this tutorial, you'll build a neural network that classifies handwritten digits from the MNIST dataset.
Prerequisites
- Basic Python knowledge
- A DLWΛY account
- A Jupyter or Kaggle kernel attached under Settings → Compute. PyTorch runs there; the in-browser Python runtime covers NumPy, pandas and scikit-learn
Step 1: Open DLWΛY
Open the Studio, create a project, and open the Explorer. Create a Python file for the model.
Step 2: Define the Model
python
import torchimport torch.nn as nn class NeuralNet(nn.Module): def __init__(self): super(NeuralNet, self).__init__() self.flatten = nn.Flatten() self.layers = nn.Sequential( nn.Linear(784, 512), nn.ReLU(), nn.Dropout(0.2), nn.Linear(512, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, 10) ) def forward(self, x): x = self.flatten(x) return self.layers(x) model = NeuralNet()print(model)Step 3: Train the Model
python
criterion = nn.CrossEntropyLoss()optimizer = torch.optim.Adam(model.parameters(), lr=0.001) for epoch in range(10): for images, labels in train_loader: outputs = model(images) loss = criterion(outputs, labels) optimizer.zero_grad() loss.backward() optimizer.step() print(f'Epoch {epoch+1}/10, Loss: {loss.item():.4f}')Step 4: Evaluate
Run the model on the test set and see your accuracy, typically 98%+ after 10 epochs.
Next Steps
- Try changing the hidden layer sizes
- Add batch normalization
- Experiment with different optimizers