-
Notifications
You must be signed in to change notification settings - Fork 7
/
models.py
62 lines (57 loc) · 2.1 KB
/
models.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
import torch
import torch.nn as nn
class Supervised_Encoder(nn.Module):
def __init__(self,input_dim,output_dim,drop_rate):
super(Supervised_Encoder, self).__init__()
self.model = torch.nn.Sequential(
nn.Linear(input_dim, output_dim),
nn.BatchNorm1d(output_dim),
nn.ReLU(),
nn.Dropout(drop_rate)
)
def forward(self, x):
output = self.model(x)
return output
class AutoEncoder(nn.Module):
def __init__(self,input_dim,output_dim,drop_rate):
super(AutoEncoder, self).__init__()
self.encoder = torch.nn.Sequential(
nn.Linear(input_dim, output_dim),
nn.ReLU(),
nn.Dropout(drop_rate),
)
self.decoder = torch.nn.Sequential(
nn.Linear(output_dim, input_dim),
nn.Dropout(drop_rate),
)
def forward(self, x):
encoded_x = self.encoder(x)
decoded_x = self.decoder(encoded_x)
return encoded_x, decoded_x
class Classifier(nn.Module):
def __init__(self,input_dim,output_dim,drop_rate):
super(Classifier, self).__init__()
self.model = torch.nn.Sequential(
nn.Linear(input_dim, output_dim),
nn.Dropout(drop_rate),
nn.Sigmoid()
)
def forward(self, x):
output = self.model(x)
return output
class OnlineTriplet(nn.Module):
def __init__(self, marg, triplet_selector):
super(OnlineTriplet, self).__init__()
self.marg = marg
self.triplet_selector = triplet_selector
def forward(self, embeddings, target):
triplets = self.triplet_selector.get_triplets(embeddings, target)
return triplets
class OnlineTestTriplet(nn.Module):
def __init__(self, marg, triplet_selector):
super(OnlineTestTriplet, self).__init__()
self.marg = marg
self.triplet_selector = triplet_selector
def forward(self, embeddings, target):
triplets = self.triplet_selector.get_triplets(embeddings, target)
return triplets