-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
104 lines (81 loc) · 3.09 KB
/
Copy pathmodel.py
File metadata and controls
104 lines (81 loc) · 3.09 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
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
import torch
import torch.nn as nn
class DiscriminativeMultilayerPerceptron(nn.Module):
def __init__(self, input_size, hidden_size=256):
super(DiscriminativeMultilayerPerceptron, self).__init__()
self.linear1 = nn.Linear(in_features=input_size, out_features=hidden_size)
self.linear2 = nn.Linear(in_features=hidden_size, out_features=hidden_size)
self.linear3 = nn.Linear(in_features=hidden_size, out_features=hidden_size)
# self.linear4 = nn.Linear(in_features=hidden_size, out_features=hidden_size)
self.linear5 = nn.Linear(in_features=hidden_size, out_features=2)
def forward(self, x):
x = self.linear1(x)
x = torch.relu(x)
x = self.linear2(x)
x = torch.relu(x)
x = self.linear3(x)
x = torch.relu(x)
# x = self.linear4(x)
# x = torch.relu(x)
x = self.linear5(x)
return x
class LeNetEmbeddingModel(nn.Module):
def __init__(self, le_net_base):
super(LeNetEmbeddingModel, self).__init__()
self.conv1 = le_net_base.conv1
self.conv2 = le_net_base.conv2
self.linear1 = le_net_base.linear1
self.linear2 = le_net_base.linear2
def forward(self, x):
x = self.conv1(x)
x = torch.relu(x)
x = torch.max_pool2d(x, kernel_size=2)
x = self.conv2(x)
x = torch.relu(x)
x = torch.max_pool2d(x, kernel_size=2)
x = x.flatten(start_dim=1, end_dim=-1)
x = self.linear1(x)
x = torch.relu(x)
x = self.linear2(x)
return x
class EarlyStop():
def __init__(self, patience, epsilon):
self.patience = patience
self.epsilon = epsilon
self.count = 0
self.min_loss = float('inf')
def early_stop(self, loss):
if loss < self.min_loss:
self.min_loss = loss
self.count = 0
elif loss > (self.min_loss + self.epsilon):
self.count += 1
if self.count > self.patience:
print("STOPPED EARLY")
return True
return False
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
# 1 * 28 * 28
self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=(5, 5))
# 6 * 24 * 24 -> pooling() -> 6 * 12 * 12
self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=(5, 5))
# 16 * 8 * 8 -> pooling() -> 16 * 4 * 4
self.linear1 = nn.Linear(in_features=16 * 4 * 4, out_features=120)
self.linear2 = nn.Linear(in_features=120, out_features=84)
self.linear3 = nn.Linear(in_features=84, out_features=10)
def forward(self, x):
x = self.conv1(x)
x = torch.relu(x)
x = torch.max_pool2d(x, kernel_size=2)
x = self.conv2(x)
x = torch.relu(x)
x = torch.max_pool2d(x, kernel_size=2)
x = x.flatten(start_dim=1, end_dim=-1)
x = self.linear1(x)
x = torch.relu(x)
x = self.linear2(x)
x = torch.relu(x)
x = self.linear3(x)
return x