-
Notifications
You must be signed in to change notification settings - Fork 8
/
models.py
136 lines (89 loc) · 3.89 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
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
129
130
131
132
133
134
135
136
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.autograd import Variable
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Linear') != -1:
m.bias.data.fill_(0)
nn.init.xavier_uniform_(m.weight,gain=0.5)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
class encoder_z(nn.Module):
def __init__(self,input_dim, latent_size,device):
super(encoder_z_v5,self).__init__()
self.layer_sizes = [input_dim, 2048, latent_size]
modules = []
for i in range(len(self.layer_sizes)-2):
modules.append(nn.Linear(self.layer_sizes[i],self.layer_sizes[i+1]))
modules.append(nn.LeakyReLU(0.2, True))
self.feature_encoder = nn.Sequential(*modules)
self._mu = nn.Linear(in_features=self.layer_sizes[-2], out_features=latent_size)
self._logvar = nn.Linear(in_features=self.layer_sizes[-2], out_features=latent_size)
self.apply(weights_init)
self.to(device)
def forward(self,x):
z = self.feature_encoder(x)
mu = self._mu(z)
logvar = self._logvar(z)
return mu, logvar
class encoder_template(nn.Module):
def __init__(self,input_dim,output_dim,device):
super(encoder_template_v5,self).__init__()
self.feature_encoder = nn.Sequential(nn.Linear(input_dim,4096),nn.LeakyReLU(0.2, True),nn.Linear(4096,output_dim))
self.apply(weights_init)
self.to(device)
def forward(self,x):
h = self.feature_encoder(x)
return h
class decoder_template(nn.Module):
def __init__(self,input_dim,output_dim,device):
super(decoder_template,self).__init__()
self.layer_sizes = [input_dim, 4096 , output_dim]
self.feature_decoder = nn.Sequential(nn.Linear(input_dim,self.layer_sizes[1]),nn.LeakyReLU(0.2, True),nn.Linear(self.layer_sizes[1],output_dim))
self.apply(weights_init)
self.to(device)
def forward(self,x):
return self.feature_decoder(x)
class decoder_z(nn.Module):
def __init__(self,latent_size, output_dim, device):
super(decoder_z,self).__init__()
self.layer_sizes = [latent_size, 2048, output_dim]
self.feature_decoder = nn.Sequential(nn.Linear(latent_size,self.layer_sizes[1]),nn.LeakyReLU(0.2, True),nn.Linear(self.layer_sizes[1],output_dim))
self.apply(weights_init)
self.to(device)
def forward(self,x):
return self.feature_decoder(x)
class domain_classifier(nn.Module):
def __init__(self, hidden_size,device):
super(domain_classifier, self).__init__()
self.classify = nn.Sequential(nn.Linear(hidden_size,512),nn.LeakyReLU(0.2, True),nn.Linear(512,1),nn.Sigmoid())
self.to(device)
def forward(self, x):
output = self.classify(x)
output = output.view(-1)
return output
class domain_discriminator(nn.Module):
def __init__(self,input_dim,output_dim,hiden_size,device):
super(domain_discriminator, self).__init__()
self.discriminator = nn.Sequential(nn.Linear(input_dim,output_dim),nn.LeakyReLU(0.2, True),nn.Linear(output_dim,output_dim))
self.apply(weights_init)
self.to(device)
def forward(self,x):
return self.discriminator(x)
class class_cls(nn.Module):
def __init__(self, input_dim, nclass):
super(class_cls, self).__init__()
self.fc = nn.Linear(input_dim, nclass)
self.logic = nn.LogSoftmax(dim=1)
self.fc1 = nn.Linear(input_dim, 512)
self.bn1_fc = nn.BatchNorm1d(512)
self.fc2 = nn.Linear(512, nclass)
self.bn_fc2 = nn.BatchNorm1d(nclass)
self.apply(weights_init)
def forward(self, x, att):
x = F.relu(self.bn1_fc(self.fc1(x)))
x = self.fc2(x)
return x