-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
288 lines (227 loc) · 9.8 KB
/
train.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from src.params import *
from src.model import VariationalAutoencoder
from src.data_utils import MidiDataset, BarTransform
from src.loss import ELBO_loss, ELBO_loss2, ELBO_loss_Multi
from src.new_model import VAECell
from torch.utils.tensorboard import SummaryWriter
import time
from datetime import datetime
import os
import math
from torch.autograd import Variable #deprecated!!!
if __name__ == "__main__":
now = datetime.now()
date_time = now.strftime("%Y_%m_%d_%H_%M_%S")
print("date and time:", date_time)
writer = SummaryWriter("runs/" + date_time)
params_dict = {
"NOTESPERBAR": NOTESPERBAR,
"totalbars": totalbars,
"NUM_PITCHES": NUM_PITCHES,
"batch_size": batch_size,
"learning_rate": learning_rate,
"num_epochs": num_epochs,
"m_key_count": m_key_count,
"use_new_model": use_new_model,
"use_attention": use_attention,
"use_dependency_tree_vertical": use_dependency_tree_vertical,
"use_dependency_tree_horizontal": use_dependency_tree_horizontal,
"use_permutation_loss": use_permutation_loss
}
writer.add_hparams(params_dict, {})
#cut the music piece into several bars, each bar comtains equal number of note sequences
transform = BarTransform(
bars=totalbars,
note_count=NUM_PITCHES) #configures number of input bars
#Load dataset
midi_dataset = MidiDataset(csv_file=data_file,
transform=transform) #imports dataset
print("Train.py Memory Usage for Training Data: ",
midi_dataset.get_mem_usage(), "MB")
#Set random seed
if random_seed is not None:
np.random.seed(random_seed)
dataset_size = len(midi_dataset) #number of musics on dataset
test_size = int(test_split * dataset_size) #test size length
train_size = dataset_size - test_size #train data length
#Split dataset into training/testing
train_dataset, test_dataset = random_split(midi_dataset,
[train_size, test_size])
train_loader = DataLoader(train_dataset,
shuffle=shuffle,
batch_size=batch_size,
num_workers=1) #, sampler=train_sampler)
test_loader = DataLoader(test_dataset,
shuffle=shuffle,
batch_size=batch_size,
num_workers=1) #, sampler=test_sampler)
print("Train.py Train size: {}, Test size: {}".format(
train_size, test_size))
#Model
if use_new_model:
net = VAECell(latent_features)
else:
net = VariationalAutoencoder(latent_features, TEACHER_FORCING, eps_i=1)
if use_cuda:
net = net.cuda()
print("Train.py The model looks like this:\n", net)
# define our optimizer
# The Adam optimizer works really well with VAEs.
optimizer = optim.Adam(net.parameters(), lr=learning_rate)
if use_new_model and use_new_loss:
loss_function = ELBO_loss_Multi
else:
loss_function = ELBO_loss2
#Learning rate: warmup and decay
warmup_lerp = 1 / warmup_epochs
if warmup_epochs > num_epochs - pre_warmup_epochs:
warmup_epochs = num_epochs - pre_warmup_epochs
warmup_w = 0
scheduled_decay_rate = 40
def lin_decay(i, mineps=0):
return np.max([mineps, 1 - (1 / len(train_loader)) * i])
def inv_sigmoid_decay(i, rate=40):
return rate / (rate + np.exp(i / rate))
eps_i = 1
use_scheduled_sampling = False
train_loss, valid_loss = [], []
train_kl, valid_kl, train_klw = [], [], []
start = time.time()
# epochs loop
for epoch in range(num_epochs):
print("Training epoch {}".format(epoch))
batch_loss, batch_kl, batch_klw = [], [], []
net.train()
running_loss = 0.0
kl_loss = 0.0
tpr = 0.0
tnr = 0.0
ppv = 0.0
npv = 0.0
for i_batch, sample_batched in enumerate(train_loader):
if i_batch % 5 == 0:
print("i_batch", i_batch)
# break
x = sample_batched['piano_rolls']
x = x.type('torch.FloatTensor')
# if i_batch%10==0:
# print("batch:",i_batch)
x = Variable(x)
# This is an alternative way of putting
# a tensor on the GPU
x = x.to(device)
## Calc the sched sampling rate:
if epoch >= pre_warmup_epochs and use_scheduled_sampling:
eps_i = inv_sigmoid_decay(i_batch, rate=scheduled_decay_rate)
if use_new_model:
pass
else:
net.set_scheduled_sampling(eps_i)
outputs = net(x)
mu, log_var = outputs['mu'], outputs['log_var']
#elbo, kl, kl_w = loss_function(x_hat, x, mu, log_var, warmup_w, with_logits=False)
if use_new_model and use_new_loss:
multi_notes = outputs["multi_notes"]
elbo, kl, kl_w = loss_function(multi_notes, x, mu, log_var,
warmup_w)
else:
x_hat = outputs['x_hat']
elbo, kl, kl_w = loss_function(x_hat, x, mu, log_var, warmup_w)
optimizer.zero_grad()
elbo.backward()
optimizer.step()
batch_loss.append(elbo.item())
batch_kl.append(kl.item())
batch_klw.append(kl_w.item())
#loss
running_loss += elbo.item()
kl_loss += kl.item()
if i_batch % log_frequency == log_frequency - 1:
# ...log the running loss
writer.add_scalar('ELBO loss', running_loss / log_frequency,
epoch * len(train_loader) + i_batch)
writer.add_scalar('KL loss', kl_loss / log_frequency,
epoch * len(train_loader) + i_batch)
running_loss = 0.0
kl_loss = 0.0
writer.add_scalar('TPR', tpr / log_frequency,
epoch * len(train_loader) + i_batch)
tpr = 0.0
writer.add_scalar('TNR', tnr / log_frequency,
epoch * len(train_loader) + i_batch)
tnr = 0.0
writer.add_scalar('PPV', ppv / log_frequency,
epoch * len(train_loader) + i_batch)
ppv = 0.0
writer.add_scalar('NPV', npv / log_frequency,
epoch * len(train_loader) + i_batch)
npv = 0.0
x_real = x.view(-1, NUM_PITCHES)
x_index = torch.arange(x_real.size(0), requires_grad=False)
x_hat = torch.zeros_like(x_real)
if use_new_model:
multi_notes = outputs["multi_notes"]
multi_notes = multi_notes.view(-1, m_key_count,
NUM_PITCHES) + 1e-6
for j in range(m_key_count):
multi_notes_j = multi_notes[:, j, :]
max_index = torch.argmax(multi_notes_j, dim=-1)
x_hat[x_index, max_index] = 1
else:
notes = outputs['x_hat']
notes = notes.view(-1, NUM_PITCHES) + 1e-6
max_index = torch.argmax(notes, dim=-1)
x_hat[x_index, max_index] = 1
tpr += torch.mean(x_hat[x_real == 1]).item() # true positive rate
tnr += (1 - torch.mean(x_hat[x_real == 0]).item()
) # true negative rate
ppv += torch.mean(
x_real[x_hat == 1]).item() # postive predictive rate
npv += (1 - torch.mean(x_real[x_hat == 0]).item()
) # negative predictive rate
# print("what is wrong with: ")
# print(torch.mean(x_hat[x_real == 1]).item())
# print(torch.mean(x_hat[x_real == 0]).item())
# print(torch.mean(x_real[x_hat == 1]).item())
# print(torch.mean(x_real[x_hat == 0]).item())
train_loss.append(np.mean(batch_loss))
train_kl.append(np.mean(batch_kl))
train_klw.append(np.mean(batch_klw))
# Evaluate, do not propagate gradients
with torch.no_grad():
net.eval()
# Just load a single batch from the test loader
x = next(iter(test_loader))
x = Variable(x['piano_rolls'].type('torch.FloatTensor'))
x = x.to(device)
if use_new_model:
pass
else:
net.set_scheduled_sampling(1)
outputs = net(x)
x_hat = outputs['x_hat']
mu, log_var = outputs['mu'], outputs['log_var']
z = outputs["z"]
if use_new_model and use_new_loss:
multi_notes = outputs["multi_notes"]
elbo, kl, klw = loss_function(multi_notes, x, mu, log_var,
warmup_w)
else:
elbo, kl, klw = loss_function(x_hat, x, mu, log_var, warmup_w)
# We save the latent variable and reconstruction for later use
# we will need them on the CPU to plot
#x = x.to("cpu")
#x_hat = x_hat.to("cpu")
#z = z.detach().to("cpu").numpy()
valid_loss.append(elbo.item())
valid_kl.append(kl.item())
if epoch >= pre_warmup_epochs:
warmup_w = warmup_w + warmup_lerp
if warmup_w > 1:
warmup_w = 1.
#if epoch == 0:
# continue
print("train_loss:", train_loss[-1], np.mean(train_loss))
print("valid_loss:", valid_loss[-1], np.mean(valid_loss))
torch.save(net.state_dict(), "records/" + date_time + ".pt")
writer.close()