forked from ZJUCDSYangKaifan/GEVit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn.py
More file actions
58 lines (54 loc) · 2.04 KB
/
Copy pathcnn.py
File metadata and controls
58 lines (54 loc) · 2.04 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
48
49
50
51
52
53
54
55
56
57
58
import torch.nn as nn
class CNN(nn.Module):
def __init__(
self,
in_channels: int,
num_channels: int,
bn_epsilon: float,
dropout_rate: float,
use_bias: bool = False,
):
"""
Simple CNN used for rotMNIST. Built upon Cohen & Welling, 2016.
"""
super().__init__()
self.sequential = nn.Sequential(
# Layer 1
nn.Conv2d(in_channels, num_channels, kernel_size=3, bias=use_bias),
nn.BatchNorm2d(num_features=num_channels, eps=bn_epsilon),
nn.ReLU(),
nn.Dropout(dropout_rate),
# Layer 2
nn.Conv2d(num_channels, num_channels, kernel_size=3, bias=use_bias),
nn.BatchNorm2d(num_features=num_channels, eps=bn_epsilon),
nn.ReLU(),
# Max Pooling
nn.MaxPool2d(kernel_size=2, stride=2),
# Layer 3
nn.Conv2d(num_channels, num_channels, kernel_size=3, bias=use_bias),
nn.BatchNorm2d(num_features=num_channels, eps=bn_epsilon),
nn.ReLU(),
nn.Dropout(dropout_rate),
# Layer 4
nn.Conv2d(num_channels, num_channels, kernel_size=3, bias=use_bias),
nn.BatchNorm2d(num_features=num_channels, eps=bn_epsilon),
nn.ReLU(),
nn.Dropout(dropout_rate),
# Layer 5
nn.Conv2d(num_channels, num_channels, kernel_size=3, bias=use_bias),
nn.BatchNorm2d(num_features=num_channels, eps=bn_epsilon),
nn.ReLU(),
nn.Dropout(dropout_rate),
# Layer 6
nn.Conv2d(num_channels, num_channels, kernel_size=3, bias=use_bias),
nn.BatchNorm2d(num_features=num_channels, eps=bn_epsilon),
nn.ReLU(),
nn.Dropout(dropout_rate),
# Layer 7
nn.Conv2d(num_channels, 10, kernel_size=4, bias=use_bias),
)
def forward(self, x):
batch_size = x.shape[0]
out = self.sequential(x)
out = out.view(batch_size, 10)
return out