-
Notifications
You must be signed in to change notification settings - Fork 0
/
convnet_MNIST.py
128 lines (102 loc) · 3.5 KB
/
convnet_MNIST.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Simple convnet on MNIST with PyTorch
"""
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch as T
import torch.nn as nn
from torch.nn.modules import *
from tqdm import tqdm, trange
from torchvision import datasets, transforms
T.set_default_tensor_type('torch.FloatTensor')
batch_size = 32
nb_epochs = 5000
nb_digits = 10
train_loader = T.utils.data.DataLoader(datasets.MNIST(
'./data', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True
)
test_loader = T.utils.data.DataLoader(datasets.MNIST(
'./data', train=False, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=batch_size, shuffle=True
)
class CeliaNet(Module):
def __init__(self):
super(CeliaNet, self).__init__()
self.conv = Sequential(
#Conv2D => (channel_in, channel_out, kernel_size)
Conv2d(1, 5, 5),
#channel_out = Combien de filtre on veut appliquer
#kernel_size = kernel_w*kernel_h, ici = 5, avec kernel_w=kernel_h=5
#nbr_filtre = channel_out
#nbr_param = (channel_in*kernel_w*kernel_h+1)*channel_out = (1*5*5+1)*5
ReLU(),
MaxPool2d(2),
#aucun parametre
Conv2d(5, 16, 9),
#nbr_param = (channel_in*kernel_w*kernel_h+1)*channel_out = (5*9*9+1)*16
ReLU(),
Conv2d(16, 20, 4),
#nbr_param = (channel_in*kernel_w*kernel_h+1)*channel_out = (16*4*4+1)*20
ReLU()
)
self.clf = Sequential(
Linear(20, 10),
#channel_in, channel_out
#channel_in = 20 (la taille du vecteur flatten = sortie de la convolution)
#nbr_param = (channel_in+1)*channel_out
Softmax()
)
def forward(self, x):
out = self.conv(x)
out = out.reshape(out.size(0), -1)
return self.clf(out)
model = CeliaNet()
optimizer = torch.optim.Adam(model.parameters())
loss_function = CrossEntropyLoss()
##inspecter le modele et verifier qu'il marche
#from torchsummary import summary
#summary(model, (1, 28, 28))
nb_epochs = 7
train_history, test_history = [], []
for i in trange(nb_epochs):
model.train()
batch_loss = []
for x, y in train_loader:
optimizer.zero_grad()
yhat = model(x.view([x.shape[0], 1, 28, 28]))# 1: couleur
#pour les convolution batch_size, channel_in, w, h
#pour le linéaire Batch_size, nb_features
loss = loss_function(yhat, y)
loss.backward()
optimizer.step()
batch_loss.append(loss.item())
train_history.append(np.array(batch_loss).mean())
model.eval()# utile pour le dropout, pour ne pas stocker les gradientss
batch_loss = []
for x, y in test_loader:
yhat = model(x.view([x.shape[0], 1, 28, 28]))
loss = loss_function(yhat, y)
batch_loss.append(loss.item())
test_history.append(np.array(batch_loss).mean())
plt.title("Loss MNIST")
plt.plot(train_history, label='train')#, marker="o--")
plt.plot(test_history, label='test')#, marker='r--')
plt.legend()
plt.show()
accuracy = []
for x, y in test_loader:
if x.shape[0] != batch_size:
continue
yhat = model(x.view([batch_size, 1, 28, 28]))
accuracy.append((yhat.argmax(1) == y).float().mean().item())
print(np.mean(accuracy))