-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnn_optim.py
More file actions
47 lines (41 loc) · 1.43 KB
/
Copy pathnn_optim.py
File metadata and controls
47 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import torchvision
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
dataset = torchvision.datasets.CIFAR10(root='./Dataset', train=False, transform=torchvision.transforms.ToTensor(), download=False)
data_loader = DataLoader(dataset, batch_size=64)
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 5, 1, 2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(64 * 4 * 4, 10)
)
def forward(self, x):
return self.model(x)
model = MyModel().to(device)
loss = nn.CrossEntropyLoss()
optim = torch.optim.SGD(model.parameters(), lr=0.01)
for epoch in range(100):
print(f"Epoch {epoch + 1}")
running_loss = 0.0
for i, (image, label) in enumerate(data_loader):
image, label = image.to(device), label.to(device)
images, labels = image, label
output = model(images)
result_loss = loss(output, labels)
optim.zero_grad()
result_loss.backward()
optim.step()
running_loss += result_loss.item()
if running_loss < 0.01:
break
print(f"Loss: {running_loss / len(data_loader)}")