-
Notifications
You must be signed in to change notification settings - Fork 0
/
bachbayes.py
301 lines (220 loc) · 8.93 KB
/
bachbayes.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
289
290
291
292
293
294
295
296
297
298
299
300
import csv
import numpy as np
import glob
import os
import torch
import torch.nn as nn
import torch.optim as optim
from tqdm import tqdm
# Neural network models
class RNN(nn.Module):
def __init__(self, n_dim, n_layers, n_hidden, unit_type, batch_size=None):
super(RNN, self).__init__()
self.n_dim = n_dim
self.n_layers = n_layers
self.n_hidden = n_hidden
self.unit_type = unit_type
if self.unit_type.lower() == 'elman':
self.rnn = nn.RNN(n_dim, n_hidden, n_layers, batch_first=True)
elif self.unit_type.lower() == 'gru':
self.rnn = nn.GRU(n_dim, n_hidden, n_layers, batch_first=True)
elif self.unit_type.lower() == 'lstm':
self.rnn = nn.LSTM(n_dim, n_hidden, n_layers, batch_first=True)
else:
print('Unit type {self.unit_type} not recognised')
self.out_obs = nn.Linear(n_hidden, n_dim, bias=True)
self.sig_obs = nn.Sigmoid()
self.out_pred = nn.Linear(n_hidden, n_dim, bias=True)
self.sig_pred = nn.Sigmoid()
if batch_size is not None:
self.init_hidden(batch_size)
else:
self.hidden = None
def init_hidden(self, batch_size):
h_size = [self.n_layers, batch_size, self.n_hidden]
self.hidden = torch.zeros(*h_size)
def forward(self, x):
self.init_hidden(x.shape[0])
x, self.hidden = self.rnn(x, self.hidden)
x_obs = self.sig_obs(self.out_obs(x))
x_pred = self.sig_pred(self.out_pred(x))
return x_obs, x_pred
class FeedForwardNN(nn.Module):
def __init__(self, n_dim, n_hidden_layers, n_hidden_units):
super(FeedForwardNN, self).__init__()
self.n_dim = n_dim
self.n_hidden_layers = n_hidden_layers
self.n_hidden_units = n_hidden_units
layers = [nn.Linear(self.n_dim, self.n_hidden_units), nn.ReLU()]
for _ in range(self.n_hidden_layers):
layers.append(nn.Linear(self.n_hidden_units, self.n_hidden_units))
layers.append(nn.ReLU())
layers += [nn.Linear(self.n_hidden_units, self.n_dim), nn.Sigmoid()]
self.feedforward = nn.Sequential(*layers)
def forward(self, x):
x = self.feedforward(x) # value OR probability???????
return x
# Data Loading
class Chunker():
def __init__(self, songs_path, batch_size, chunk_size, chromatic, noise, dev):
self.songs_path = songs_path
self.batch_size = batch_size
self.chunk_size = chunk_size
self.chromatic = chromatic
self.noise = noise
self.dev = dev
self.songs_dict = self._get_songs_()
self.song_pool = list(self.songs_dict.keys())
self.song_list = [np.random.choice(self.song_pool) for _ in range(batch_size)]
self.t0 = [0 for _ in range(batch_size)]
def generate_chunk(self):
n_dims = self.songs_dict[self.song_pool[0]]['target'].shape[1]
target = np.zeros((self.batch_size, self.chunk_size, n_dims))
sample = np.zeros((self.batch_size, self.chunk_size, n_dims))
for n in range(self.batch_size):
t0, t1 = self.t0[n], self.t0[n] + self.chunk_size
target[n, :, :] = self.songs_dict[self.song_list[n]]['target'][t0:t1]
sample[n, :, :] = self.songs_dict[self.song_list[n]]['sample'][t0:t1]
if t1 > self.songs_dict[self.song_list[n]]['target'].shape[1]-self.chunk_size:
self.song_list[n] = np.random.choice(self.song_pool)
self.t0[n] = 0
else:
self.t0[n] += self.chunk_size
target = torch.tensor(target).to(device=self.dev).float()
sample = torch.tensor(sample).to(device=self.dev).float()
return target, sample
def _get_songs_(self):
n_pad_song_end = 20
files = dict()
for filepath in glob.glob(os.path.join(self.songs_path, '*.csv')):
file = os.path.split(filepath)[-1]
if '_' in file:
opera = file.split('_')[0]
else:
opera = file.replace('.csv', '')
files[opera] = glob.glob(os.path.join(self.songs_path, f'{opera}*.csv'))
songs_dict = dict()
for opera in files:
songs_dict[opera] = dict()
targets = []
for file in files[opera]:
with open(file, 'r') as f:
targets += [el for el in list(csv.reader(f)) if el != []]
targets += [[]] * n_pad_song_end
target_array = self._generate_target_array_(targets)
sample_array = self._add_noise_to_target_(target_array)
songs_dict[opera]['target'] = target_array
songs_dict[opera]['sample'] = sample_array
return songs_dict
def _generate_target_array_(self, targets):
if self.chromatic:
n_midi_notes = 12
else:
n_midi_notes = 108
song_len = int(self.chunk_size * np.ceil(len(targets)/self.chunk_size))
target_array = np.zeros((song_len, n_midi_notes))
for tick, target_chord in enumerate(targets):
for target_note in [int(note)-1 for note in target_chord if note != '']:
note = target_note % 12 if self.chromatic else target_note
target_array[tick, note] = 1
return target_array
def _add_noise_to_target_(self, target_array):
sample_array = target_array + self.noise * np.random.randn(*target_array.shape)
return(sample_array)
# Model object
class Bachmodel():
def __init__(self, pars, dev=None):
super(Bachmodel, self).__init__()
if dev is None:
dev = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.pars = pars
self.dev = dev
self.chromatic = pars['chromatic']
self.noise = pars['noise']
self.train_path = os.path.join(pars['datapath'] , 'train')
self.validation_path = os.path.join(pars['datapath'] , 'test')
self.test_path = os.path.join(pars['datapath'] , 'test')
n_dim = 12 if self.chromatic else 108
n_layers = pars['n_layers']
n_hidden = pars['n_hidden']
unit_type = pars['unit_type']
self.rnn = RNN(n_dim, n_layers, n_hidden, unit_type).to(dev)
modname = f'{unit_type}_nhidden-{n_hidden:d}_nlayers-{n_layers:d}'
modname += f'{"_chromatic" if self.chromatic else ""}'
modname += f'_noise{self.noise}'.replace('.', 'p')
self.modname = modname
self.modpath = f'./models/{modname}.pth'
def load_weights(self, suffix=''):
modpath = self.modpath.split('.')
loadpath = modpath[0] + suffix + modpath[1]
self.rnn.load_state_dict(torch.load(loadpath))
def save_weights(self, suffix=''):
modpath = self.modpath.split('.')
savepath = modpath[0] + suffix + modpath[1]
torch.save(self.rnn.state_dict(), self.modpath)
def train(self, lr, chunk_size, batch_size, n_batches, freeze=[], obj=['obs']):
tolerance = 0.5 # loss < validation_loss + tol * std_validation_loss --> early stopping
chunker = Chunker(self.train_path,batch_size,chunk_size,self.chromatic,self.noise,self.dev)
# Select which parameters to train
parameters_to_train = []
for name, parameter in self.rnn.named_parameters():
if name.split('.')[0] in freeze:
print(f'Freezing {name}')
parameter.requires_grad = False
else:
parameter.requires_grad = True
parameters_to_train.append(parameter)
optimizer = optim.Adam(parameters_to_train, lr=lr)
loss_func = nn.BCELoss()
loss_hist = []
for batch in range(n_batches):
optimizer.zero_grad()
target, sample = chunker.generate_chunk()
obs, pred = self.rnn(sample)
loss = 0
if 'obs' in obj:
loss += loss_func(obs, target)
if 'pred' in obj:
loss += loss_func(pred[:, :-1], target[:, 1:])
loss.backward()
optimizer.step()
loss_hist += [float(loss.detach().cpu().numpy())]
if batch % 25 == 0:
print(f"Batch {batch:04}/{n_batches} | Loss: {loss:.4f}")
# Early stopping if we begin to overfit the data when training RNNs
if 'rnn' not in freeze and batch > 0.05 * n_batches:
ve = self.test(chunk_size, batch_size, 6, obj, self.validation_path)
valid_error_m, valid_error_s = np.mean(ve), np.std(ve)
cutoff = valid_error_m - tolerance * valid_error_s
cutoff_str = f'{valid_error_m:.2f}' + u'\u00B1' + f'{valid_error_s:.2f}'
cutoff_str += f' (tol = {tolerance:.2f})'
avg_loss = np.mean(loss_hist[max(0, batch-10):])
if avg_loss < cutoff:
print(f'Stopping -> <Loss> = {avg_loss:.2f}, val_loss = {cutoff_str}')
break
self._write_report_(loss_hist, freeze, obj)
def test(self, chunk_size, batch_size, n_samples=1, obj=['obs'], test_path=None):
if test_path is None:
test_path = self.test_path
chunker = Chunker(test_path, batch_size, chunk_size, self.chromatic, self.noise, self.dev)
loss_func = nn.BCELoss(reduction='none')
performance = np.zeros((n_samples, batch_size))
with torch.no_grad():
for n_sample in (tqdm(range(n_samples)) if n_samples > 20 else range(n_samples)):
target, sample = chunker.generate_chunk()
obs, pred = self.rnn(sample)
loss = 0
if 'obs' in obj:
loss += loss_func(obs, target).mean((1,2))
if 'pred' in obj:
loss += loss_func(pred[:, :-1], target[:, 1:]).mean((1,2))
performance[n_sample, :] = loss.cpu().numpy()
return performance
def _write_report_(self, loss_hist, freeze, obj):
report_name = self.modname
report_name += f'_obj-{"-".join(obj)}_'
report_name += f'freeze-{"none" if freeze is None else "-".join(freeze)}'
report_path = os.path.join(os.path.split(self.modpath)[0], report_name) + '.txt'
history_str = ','.join([f'{l:.4f}' for l in loss_hist])
with open(report_path, 'w') as f:
f.write(history_str)