-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
128 lines (107 loc) · 3.84 KB
/
train_model.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
125
126
127
128
from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.optim.lr_scheduler import StepLR
from torch.utils.data.sampler import SubsetRandomSampler
import numpy as np
import matplotlib.pyplot as plt
import itertools
from sklearn.metrics import confusion_matrix
from sklearn.manifold import TSNE
import seaborn as sns
import os
'''
This code is adapted from two sources:
(i) The official PyTorch MNIST example (https://github.com/pytorch/examples/blob/master/mnist/main.py)
(ii) Starter code from Yisong Yue's CS 155 Course (http://www.yisongyue.com/courses/cs155/2020_winter/)
'''
class fcNet(nn.Module):
'''
Design your model with fully connected layers (convolutional layers are not
allowed here). Initial model is designed to have a poor performance. These
are the sample units you can try:
Linear, Dropout, activation layers (ReLU, softmax)
'''
def __init__(self):
# Define the units that you will use in your model
# Note that this has nothing to do with the order in which operations
# are applied - that is defined in the forward function below.
super(fcNet, self).__init__()
self.fc1 = nn.Linear(in_features=784, out_features=20)
self.fc2 = nn.Linear(20, 10)
self.dropout1 = nn.Dropout(p=0.5)
def forward(self, x):
# Define the sequence of operations your model will apply to an input x
x = torch.flatten(x, start_dim=1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout1(x)
x = F.relu(x)
output = F.log_softmax(x, dim=1)
return output
class ConvNet(nn.Module):
'''
Design your model with convolutional layers.
'''
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=8, kernel_size=(3,3), stride=1)
self.conv2 = nn.Conv2d(8, 8, 3, 1)
self.dropout1 = nn.Dropout2d(0.5)
self.dropout2 = nn.Dropout2d(0.5)
self.fc1 = nn.Linear(200, 64)
self.fc2 = nn.Linear(64, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout2(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x)
output = F.log_softmax(x, dim=1)
return output
class Net(nn.Module):
'''
Build the best MNIST classifier.
'''
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=(5,5), stride=1)
self.conv2 = nn.Conv2d(32, 32, kernel_size=(5,5), stride=1)
self.maxpool1 = nn.MaxPool2d((2, 2))
self.conv3 = nn.Conv2d(32, 64, kernel_size=(3,3), stride=1)
self.conv4 = nn.Conv2d(64, 64, kernel_size=(3,3), stride=1)
self.maxpool2 = nn.MaxPool2d((2, 2), stride = (2, 2))
self.dropout = nn.Dropout(0.25)
self.fc1 = nn.Linear(576, 256)
self.fc2 = nn.Linear(256, 10)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = self.maxpool1(x)
x = self.dropout(x)
x = self.conv3(x)
x = F.relu(x)
x = self.conv4(x)
x = F.relu(x)
x = self.maxpool2(x)
x = self.dropout(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout(x)
x = self.fc2(x)
output = F.log_softmax(x, dim=1)
return output