Skip to content

Commit 99d4e6a

Browse files
committed
Update 13_feedforward.py
1 parent cd1d915 commit 99d4e6a

File tree

1 file changed

+76
-48
lines changed

1 file changed

+76
-48
lines changed

13_feedforward.py

Lines changed: 76 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@
33
import torchvision
44
import torchvision.transforms as transforms
55
import matplotlib.pyplot as plt
6+
import ssl
67

7-
# Device configuration
8-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
8+
ssl._create_default_https_context = ssl._create_unverified_context
9+
10+
# GPU device configuration
11+
if torch.cuda.is_available():
12+
device = torch.device('cuda')
13+
print('Using GPU')
14+
elif torch.backends.mps.is_available():
15+
device = torch.device('mps')
16+
print('Using MPS')
17+
else:
18+
device = torch.device('cpu')
19+
print('Using CPU')
920

1021
# Hyper-parameters
1122
input_size = 784 # 28x28
1223
hidden_size = 500
1324
num_classes = 10
14-
num_epochs = 2
25+
num_epochs = 10
1526
batch_size = 100
1627
learning_rate = 0.001
1728

@@ -40,64 +51,81 @@
4051
for i in range(6):
4152
plt.subplot(2,3,i+1)
4253
plt.imshow(example_data[i][0], cmap='gray')
43-
plt.show()
54+
# plt.show()
4455

4556
# Fully connected neural network with one hidden layer
4657
class NeuralNet(nn.Module):
47-
def __init__(self, input_size, hidden_size, num_classes):
48-
super(NeuralNet, self).__init__()
49-
self.input_size = input_size
50-
self.l1 = nn.Linear(input_size, hidden_size)
51-
self.relu = nn.ReLU()
52-
self.l2 = nn.Linear(hidden_size, num_classes)
53-
54-
def forward(self, x):
55-
out = self.l1(x)
56-
out = self.relu(out)
57-
out = self.l2(out)
58-
# no activation and no softmax at the end
59-
return out
58+
def __init__(self, input_size, hidden_size, num_classes):
59+
super(NeuralNet, self).__init__()
60+
self.input_size = input_size
61+
self.l1 = nn.Linear(input_size, hidden_size)
62+
self.relu = nn.ReLU()
63+
self.l2 = nn.Linear(hidden_size, hidden_size)
64+
def forward(self, x):
65+
out = self.l1(x)
66+
out = self.relu(out)
67+
out = self.l2(out)
68+
# no activation and no softmax at the end
69+
return out
6070

6171
model = NeuralNet(input_size, hidden_size, num_classes).to(device)
72+
print(model)
6273

6374
# Loss and optimizer
6475
criterion = nn.CrossEntropyLoss()
65-
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
76+
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
6677

6778
# Train the model
6879
n_total_steps = len(train_loader)
80+
loss_list = []
81+
6982
for epoch in range(num_epochs):
70-
for i, (images, labels) in enumerate(train_loader):
71-
# origin shape: [100, 1, 28, 28]
72-
# resized: [100, 784]
73-
images = images.reshape(-1, 28*28).to(device)
74-
labels = labels.to(device)
75-
76-
# Forward pass
77-
outputs = model(images)
78-
loss = criterion(outputs, labels)
79-
80-
# Backward and optimize
81-
optimizer.zero_grad()
82-
loss.backward()
83-
optimizer.step()
84-
85-
if (i+1) % 100 == 0:
86-
print (f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.4f}')
83+
epoch_loss = 0
84+
for i, (images, labels) in enumerate(train_loader):
85+
# origin shape: [100, 1, 28, 28]
86+
# resized: [100, 784]
87+
images = images.reshape(-1, 28*28).to(device)
88+
labels = labels.to(device)
89+
90+
# Forward pass
91+
outputs = model(images)
92+
loss = criterion(outputs, labels)
93+
94+
# Backward and optimize
95+
optimizer.zero_grad()
96+
loss.backward()
97+
optimizer.step()
98+
99+
epoch_loss += loss.item()
100+
101+
if (i+1) % 200 == 0:
102+
print (f'Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss: {loss.item():.4f}')
103+
104+
avg_epoch_loss = epoch_loss / n_total_steps
105+
loss_list.append(avg_epoch_loss)
106+
107+
# Plot loss as a function of epoch
108+
plt.figure()
109+
plt.plot(range(1, num_epochs + 1), loss_list, label='Training Loss')
110+
plt.xlabel('Epoch')
111+
plt.ylabel('Loss')
112+
plt.title('Training Loss vs. Epoch')
113+
plt.legend()
114+
plt.show()
87115

88116
# Test the model
89117
# In test phase, we don't need to compute gradients (for memory efficiency)
90118
with torch.no_grad():
91-
n_correct = 0
92-
n_samples = 0
93-
for images, labels in test_loader:
94-
images = images.reshape(-1, 28*28).to(device)
95-
labels = labels.to(device)
96-
outputs = model(images)
97-
# max returns (value ,index)
98-
_, predicted = torch.max(outputs.data, 1)
99-
n_samples += labels.size(0)
100-
n_correct += (predicted == labels).sum().item()
101-
102-
acc = 100.0 * n_correct / n_samples
103-
print(f'Accuracy of the network on the 10000 test images: {acc} %')
119+
n_correct = 0
120+
n_samples = 0
121+
for images, labels in test_loader:
122+
images = images.reshape(-1, 28*28).to(device)
123+
labels = labels.to(device)
124+
outputs = model(images)
125+
# max returns (value ,index)
126+
_, predicted = torch.max(outputs.data, 1)
127+
n_samples += labels.size(0)
128+
n_correct += (predicted == labels).sum().item()
129+
130+
acc = 100.0 * n_correct / n_samples
131+
print(f'Accuracy of the network on the 10000 test images: {acc} %')

0 commit comments

Comments
 (0)