Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, below is my code for MPL image classification, I am running the code and I forgot to define the name, device. (Highlighted in bold)

Hello, below is my code for MPL image classification, I am running the code and I forgot to define the name, "device." (Highlighted in bold) Can someone look over it to see where it would be best to include the definition? Thank You.

import torchvision.datasets as datasets

import torch.optim as optim

import torch.utils.data as data

import torch.nn as nn

from torchvision import transforms

train_transform = transforms.Compose([

transforms.RandomCrop(32, padding=4),

transforms.RandomHorizontalFlip(),

transforms.ToTensor(),

])

test_transform = transforms.Compose([

transforms.ToTensor(),

])

train_data = datasets.CIFAR100(root='data', train=True, transform=train_transform, download=True)

test_data = datasets.CIFAR100(root='data', train=False, transform=test_transform, download=True)

class MLP(nn.Module):

def __init__(self, input_size, hidden_size, num_classes):

super(MLP, self).__init__()

self.fc1 = nn.Linear(input_size, hidden_size)

self.relu1 = nn.ReLU()

self.fc2 = nn.Linear(hidden_size, hidden_size)

self.relu2 = nn.ReLU()

self.fc3 = nn.Linear(hidden_size, num_classes)

def forward(self, x):

out = self.fc1(x)

out = self.relu1(out)

out = self.fc2(out)

out = self.relu2(out)

out = self.fc3(out)

return out

input_size = 32 * 32 * 3

hidden_size = 512

num_classes = 100

learning_rate = 0.001

batch_size = 128

num_epochs = 10

model = MLP(input_size, hidden_size, num_classes)

train_loader = data.DataLoader(train_data, batch_size=batch_size, shuffle=True)

test_loader = data.DataLoader(test_data, batch_size=batch_size, shuffle=False)

criterion = nn.CrossEntropyLoss()

optimizer = optim.Adam(model.parameters(), lr=learning_rate)

for epoch in range(num_epochs):

for i, (images, labels) in enumerate(train_loader):

images = images.reshape(-1, input_size).to(device)

labels = labels.to(device)

optimizer.zero_grad()

outputs = model(images)

loss = criterion(outputs, labels)

loss.backward()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions

Question

4 of 1 7 The id is the

Answered: 1 week ago

Question

Recommend the key methods to improve service productivity.

Answered: 1 week ago