-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
54 lines (44 loc) · 1.45 KB
/
models.py
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
48
49
50
51
52
53
54
# Code by Natasha
# Last updated: 2023.12.30
import torch
from torch import nn
def create_model(dataset_name):
"""
Input: Dataset name: can be 'femnist' or 'cifar'
"""
if dataset_name=="femnist":
num_channels=1
image_size=28
num_classes=62
elif dataset_name=="cifar":
num_channels=3
image_size=32
num_classes=10
torch.manual_seed(47)
return CNN500k(num_channels, image_size, num_classes)
class CNN500k(nn.Module):
def __init__(self, num_channels, image_size, num_classes):
super().__init__()
self.layer_stack = nn.Sequential(
nn.Conv2d(num_channels, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Conv2d(64, 32, kernel_size=3, stride=1, padding=1),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Flatten(),
nn.Linear(32 * int(image_size/8) * int(image_size/8), 512),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(256, num_classes)
)
def forward(self, x):
return self.layer_stack(x)