# 4-6-1-16. Dropout Solution

import torch
from torchvision import datasets, transforms

# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])

from torch import nn, optim
import torch.nn.functional as F

class Classifier(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 64)
self.fc4 = nn.Linear(64, 10)

def forward(self, x):
# make sure input tensor is flattened
x = x.view(x.shape[0], -1)

x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.log_softmax(self.fc4(x), dim=1)

return x

model = Classifier()

# Get the class probabilities
ps = torch.exp(model(images))
# Make sure the shape is appropriate, we should get 10 class probabilities for 64 examples
print(ps.shape)

## torch.Size([64, 10])

top_p, top_class = ps.topk(1, dim=1)
# Look at the most likely classes for the first 10 examples
print(top_class[:10,:])

"""
tensor([[5],
[3],
[5],
[9],
[5],
[9],
[5],
[5],
[9],
[5]])
"""

equals = top_class == labels.view(*top_class.shape)

accuracy = torch.mean(equals.type(torch.FloatTensor))
print(f'Accuracy: {accuracy.item()*100}%')

"""
Accuracy: 14.0625%
"""

####### solution  #########

model = Classifier()
criterion = nn.NLLLoss(reduction='sum')

epochs = 30

train_losses, test_losses = [], []
for e in range(epochs):
tot_train_loss = 0

log_ps = model(images)
loss = criterion(log_ps, labels)
tot_train_loss += loss.item()

loss.backward()
optimizer.step()
else:
tot_test_loss = 0
test_correct = 0  # Number of correct predictions on the test set

# Turn off gradients for validation, saves memory and computations
log_ps = model(images)
loss = criterion(log_ps, labels)
tot_test_loss += loss.item()

ps = torch.exp(log_ps)
top_p, top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(*top_class.shape)
test_correct += equals.sum().item()

# Get mean loss to enable comparison between train and test sets

# At completion of epoch
train_losses.append(train_loss)
test_losses.append(test_loss)

print("Epoch: {}/{}.. ".format(e+1, epochs),
"Training Loss: {:.3f}.. ".format(train_loss),
"Test Loss: {:.3f}.. ".format(test_loss),

################################

plt.plot(train_losses, label='Training loss')
plt.plot(test_losses, label='Validation loss')
plt.legend(frameon=False)

import matplotlib.pyplot as plt
class Classifier(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 256)
self.fc2 = nn.Linear(256, 128)
self.fc3 = nn.Linear(128, 64)
self.fc4 = nn.Linear(64, 10)

# Dropout module with 0.2 drop probability
self.dropout = nn.Dropout(p=0.2)

def forward(self, x):
# make sure input tensor is flattened
x = x.view(x.shape[0], -1)

# Now with dropout
x = self.dropout(F.relu(self.fc1(x)))
x = self.dropout(F.relu(self.fc2(x)))
x = self.dropout(F.relu(self.fc3(x)))

# output so no dropout here
x = F.log_softmax(self.fc4(x), dim=1)

return x

########################### Solution #####################

model = Classifier()
criterion = nn.NLLLoss()

## TODO: Train your model with dropout, and monitor the training progress with the validation loss and accuracy
epochs = 30
steps = 0

train_losses, test_losses = [], []
for e in range(epochs):
running_loss = 0

log_ps = model(images)
loss = criterion(log_ps, labels)
loss.backward()
optimizer.step()

running_loss += loss.item()

else:
test_loss = 0
accuracy = 0

# Turn off gradients for validation, saves memory and computations
model.eval()
log_ps = model(images)
test_loss += criterion(log_ps, labels)

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))

model.train()

print("Epoch: {}/{}.. ".format(e+1, epochs),
"Training Loss: {:.3f}.. ".format(train_losses[-1]),
"Test Loss: {:.3f}.. ".format(test_losses[-1]),

###############################################################

import matplotlib.pyplot as plt

plt.plot(train_losses, label='Training loss')
plt.plot(test_losses, label='Validation loss')
plt.legend(frameon=False)

# Import helper module (should be in the repo)
import helper

model.eval()

images, labels = dataiter.next()
img = images[0]
# Convert 2D image to 1D vector
img = img.view(1, 784)

# Calculate the class probabilities (softmax) for img
output = model.forward(img)

ps = torch.exp(output)

# Plot the image and probabilities
helper.view_classify(img.view(1, 28, 28), ps, version='Fashion')  

Hi. Here’s my solution for your building and training this network using dropout now.

Just like I showed you before,

we can define our dropout module as

self.dropout and then nn.dropout to give it some drop probability.

So, in this case, 20 percent,

and then just adding it to our forward method now on each of our hidden layers.

Now, our validation code looks basically the same as

before except now we’re using model.eval.

So, again, this turns our model into

evaluation or inference mode which turns off dropout.

Then, like the same way before,

we just go through our data and the test say,

calculate the losses and accuracy and after all that,

we do model.train to set the model back into train mode,

turn dropout back on,

and then continue on in train smart.

So, now, we’re using dropout and if you look at

again the training loss and the validation loss over these epochs that we’re training,

you actually see that the validation loss sticks a

lot closer to the train loss as we train.

So, here, with dropout, we’ve managed to at least reduce overfitting.

So, the validation losses isn’t as low as we got without dropout being is still,

you can see that it’s still dropping.

So, if we kept training for longer,

we would most likely manage to get our validation loss lower than without dropout.

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.