-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
131 lines (100 loc) · 3.82 KB
/
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
129
130
131
import time
import torch
from torch import nn
import torch.optim as optim
import numpy as np
import os
import variables as var
class Autoencoder(nn.Module):
def __init__(self,enc_hidden,dec_hidden):
super(Autoencoder,self).__init__()
# encoder
self.enc_list = []
for i in range(1,len(enc_hidden)):
self.enc_list.append(nn.Linear(enc_hidden[i-1],enc_hidden[i]))
self.enc_list.append(nn.ReLU(True))
self.enc_list.pop()
self.enc_list = nn.ModuleList(self.enc_list)
#decoder
self.dec_list = []
for i in range(1,len(dec_hidden)):
self.dec_list.append(nn.Linear(dec_hidden[i-1],dec_hidden[i]))
self.dec_list.append(nn.ReLU(True))
self.dec_list.pop()
self.dec_list = nn.ModuleList(self.dec_list)
def forward(self,x):
for f in self.enc_list:
x = f(x)
encoding = x
for f in self.dec_list:
x = f(x)
reconstruction = x
return encoding, reconstruction
class Decoder(nn.Module):
def __init__(self,dec_hidden):
super(Decoder,self).__init__()
#decoder
self.dec_list = []
for i in range(1,len(dec_hidden)):
self.dec_list.append(nn.Linear(dec_hidden[i-1],dec_hidden[i]))
self.dec_list.append(nn.ReLU(True))
self.dec_list.pop()
self.dec_list = nn.ModuleList(self.dec_list)
def forward(self,x):
for f in self.dec_list:
x = f(x)
reconstruction = x
return reconstruction
def train_model(dataset,net,train_loader,val_loader,save_model=True):
optimizer = optim.Adam(net.parameters(), lr = var.lr, betas=(0.5, 0.999))
loss_fn = nn.SmoothL1Loss(reduction = "none")
train_losses = []
val_losses = []
start = time.time()
for epoch in range(1,var.n_epochs+1):
#training
net.train()
train_batch_loss = []
for x_batch in train_loader:
x_batch = x_batch.to(var.device)
# Makes predictions
_, x_rec = net(x_batch)
# Computes loss
loss = loss_fn(x_batch,x_rec).mean()
# Computes gradients
loss.backward()
# Updates parameters
optimizer.step()
#zero gradient
optimizer.zero_grad()
# Returns the loss
train_losses.append(loss.item())
train_batch_loss.append(loss.item())
#validation
with torch.no_grad():
val_batch_loss = []
net.eval()
for x_batch in val_loader:
x_batch = x_batch.to(var.device)
_, x_rec = net(x_batch.to(var.device))
val_loss = loss_fn(x_batch, x_rec).mean()
val_losses.append(val_loss.item())
val_batch_loss.append(val_loss.item())
#print progress
print("Epoch: %d, Loss %.8f, Validation Loss %.8f" % (epoch, np.mean(train_batch_loss), np.mean(val_batch_loss)))
#early stopping
if val_loss < 0.003:
break
end = time.time()
print("Training time: %.8f minutes" %((end-start)/60))
model_save_file = "saved_models/%s/" %dataset
if not os.path.exists(os.path.dirname(model_save_file)):
os.makedirs(os.path.dirname(model_save_file))
torch.save(
{'model_state_dict': net.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'epoch': epoch,
'loss' : loss,
'val_loss': val_loss
}, "%snet.pth" %model_save_file)
return net