forked from DavidBert/ModIA_TP1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist_net.py
More file actions
42 lines (35 loc) · 1.15 KB
/
mnist_net.py
File metadata and controls
42 lines (35 loc) · 1.15 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
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch
import torch.nn as nn
import torch.nn.functional as F
class MNISTNet(nn.Module):
def __init__(self):
super(MNISTNet, self).__init__()
self.conv1 = nn.Conv2d(1, 8, kernel_size=5)
self.conv2 = nn.Conv2d(8, 16, kernel_size=5)
self.pool = nn.MaxPool2d(kernel_size=2)
self.fc1 = nn.Linear(256, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, 10)
def forward(self, x):
x = F.relu(self.conv1(x)) # First convolution followed by
x = self.pool(x) # a relu activation and a max pooling#
x = F.relu(self.conv2(x))
x = self.pool(x)
x = torch.flatten(x, start_dim=1)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
def get_features(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
return x
if __name__=='__main__':
x = torch.rand(16,1,28,28)
net = MNISTNet()
y = net(x)
assert y.shape == (16,10)