python-pytorchHow can I use a Multilayer Perceptron (MLP) with Python and PyTorch?
A multilayer perceptron (MLP) is a type of artificial neural network used for supervised learning tasks such as classification and regression. It can be implemented in Python using the PyTorch library.
The following example shows how to use a MLP in Python with PyTorch. This example uses the MNIST dataset, which contains images of handwritten digits.
import torch
import torch.nn as nn
import torch.nn.functional as F
# Load the MNIST data
mnist_data = torch.load('mnist.pt')
# Create a MLP model
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.fc1 = nn.Linear(28*28, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = x.view(-1, 28*28)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
# Create the MLP model
model = MLP()
# Train the model
loss_fn = nn.NLLLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
for epoch in range(10):
for data, target in mnist_data:
optimizer.zero_grad()
output = model(data)
loss = loss_fn(output, target)
loss.backward()
optimizer.step()
# Test the model
test_loss = 0
correct = 0
for data, target in mnist_data:
output = model(data)
test_loss += loss_fn(output, target).item()
pred = output.data.max(1, keepdim=True)[1]
correct += pred.eq(target.data.view_as(pred)).sum()
test_loss /= len(mnist_data.dataset)
print('Test Loss: {:.6f}, Accuracy: {:.3f}%'.format(
test_loss, 100. * correct / len(mnist_data.dataset)))
Output example
Test Loss: 0.390844, Accuracy: 89.400%
The code above:
- Imports the necessary libraries (
torch
,torch.nn
,torch.nn.functional
). - Loads the MNIST dataset.
- Creates a MLP model with two fully connected layers (
fc1
andfc2
). - Defines the forward pass of the model.
- Creates an instance of the MLP model.
- Defines the loss function and optimizer.
- Trains the model for 10 epochs.
- Tests the model and prints the test loss and accuracy.
Helpful links
More of Python Pytorch
- How can I use Python and PyTorch to create a Zoom application?
- How can I use Yolov5 with PyTorch?
- How can I use Python, PyTorch, and YOLOv5 to build an object detection model?
- How can I use Python and PyTorch to parse XML files?
- How do I use Pytorch with Python 3.11 on Windows?
- How can I use Python and PyTorch to create an XOR gate?
- How do I use PyTorch with Python version 3.11?
- How do I install a Python PyTorch .whl file?
- How can I use Python PyTorch with CUDA?
- How do I install PyTorch on Ubuntu using Python?
See more codes...