Here is an example of implementing a multilayer perceptron with a negative multinomial target using PyTorch:
import torch
import torch.nn as nn
import torch.optim as optim
class MLP(nn.Module):
def __init__(self):
super(MLP, self).__init__()
self.fc1 = nn.Linear(in_features=784, out_features=64)
self.fc2 = nn.Linear(in_features=64, out_features=32)
self.fc3 = nn.Linear(in_features=32, out_features=10)
def forward(self, x):
x = torch.flatten(x, start_dim=1)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = torch.softmax(self.fc3(x), dim=1)
return x
This model has 3 fully connected layers, with ReLU activation functions and softmax activation function in the last layer. The input is an image of size 28x28 pixels, which is flattened to a 784-dimensional vector.
from torchvision import datasets, transforms
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
trainset = datasets.MNIST('mnist_train', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
testset = datasets.MNIST('mnist_test', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True)
model = MLP()
criterion = nn.NLLLoss()
optimizer = optim.SGD(model.parameters(), lr=0.003)
In this case, we use the negative log-likelihood loss and stochastic gradient descent as the optimizer.
def train(model, trainloader, criterion, optimizer, epochs=5):
for e in range(epochs):
running_loss = 0
for images, labels in trainloader:
optimizer.zero_grad()
log_ps = model(images)
loss = criterion(log_ps, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
else:
print(f"Training loss: {running_loss/len(trainloader)}")
train(model, trainloader, criterion, optimizer, epochs=5)
def test(model, testloader):
accuracy = 0
with torch.no_grad():
for images, labels in testloader:
log_ps = model(images)
ps = torch.exp(log_ps)
top_p, top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(*top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor))
print(f"Accuracy: {accuracy/len(testloader)}")
test(model, testloader)
In this way, we have trained and evaluated a multilayer perceptron model for the MNIST dataset with a negative multinomial target using PyTorch.
Asked: 2023-07-04 14:09:27 +0000
Seen: 10 times
Last updated: Jul 04 '23