This repository has been archived by the owner on Jan 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
train.py
254 lines (198 loc) · 7.93 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
import torch
from torch import nn
from torch import optim
from torch.autograd import Variable
import numpy as np
import os
import sys
import time
import optparse
import utils
import config
from data import Dataset
from model import PerformanceRNN
from sequence import NoteSeq, EventSeq, ControlSeq
# pylint: disable=E1102
# pylint: disable=E1101
#========================================================================
# Settings
#========================================================================
def get_options():
parser = optparse.OptionParser()
parser.add_option('-s', '--session',
dest='sess_path',
type='string',
default='save/train.sess')
parser.add_option('-d', '--dataset',
dest='data_path',
type='string',
default='dataset/processed/')
parser.add_option('-i', '--saving-interval',
dest='saving_interval',
type='float',
default=60.)
parser.add_option('-b', '--batch-size',
dest='batch_size',
type='int',
default=config.train['batch_size'])
parser.add_option('-l', '--learning-rate',
dest='learning_rate',
type='float',
default=config.train['learning_rate'])
parser.add_option('-w', '--window-size',
dest='window_size',
type='int',
default=config.train['window_size'])
parser.add_option('-S', '--stride-size',
dest='stride_size',
type='int',
default=config.train['stride_size'])
parser.add_option('-c', '--control-ratio',
dest='control_ratio',
type='float',
default=config.train['control_ratio'])
parser.add_option('-T', '--teacher-forcing-ratio',
dest='teacher_forcing_ratio',
type='float',
default=config.train['teacher_forcing_ratio'])
parser.add_option('-t', '--use-transposition',
dest='use_transposition',
action='store_true',
default=config.train['use_transposition'])
parser.add_option('-p', '--model-params',
dest='model_params',
type='string',
default='')
parser.add_option('-r', '--reset-optimizer',
dest='reset_optimizer',
action='store_true',
default=False)
parser.add_option('-L', '--enable-logging',
dest='enable_logging',
action='store_true',
default=False)
return parser.parse_args()[0]
options = get_options()
#------------------------------------------------------------------------
sess_path = options.sess_path
data_path = options.data_path
saving_interval = options.saving_interval
learning_rate = options.learning_rate
batch_size = options.batch_size
window_size = options.window_size
stride_size = options.stride_size
use_transposition = options.use_transposition
control_ratio = options.control_ratio
teacher_forcing_ratio = options.teacher_forcing_ratio
reset_optimizer = options.reset_optimizer
enable_logging = options.enable_logging
event_dim = EventSeq.dim()
control_dim = ControlSeq.dim()
model_config = config.model
model_params = utils.params2dict(options.model_params)
model_config.update(model_params)
device = config.device
print('-' * 70)
print('Session path:', sess_path)
print('Dataset path:', data_path)
print('Saving interval:', saving_interval)
print('-' * 70)
print('Hyperparameters:', utils.dict2params(model_config))
print('Learning rate:', learning_rate)
print('Batch size:', batch_size)
print('Window size:', window_size)
print('Stride size:', stride_size)
print('Control ratio:', control_ratio)
print('Teacher forcing ratio:', teacher_forcing_ratio)
print('Random transposition:', use_transposition)
print('Reset optimizer:', reset_optimizer)
print('Enabling logging:', enable_logging)
print('Device:', device)
print('-' * 70)
#========================================================================
# Load session and dataset
#========================================================================
def load_session():
global sess_path, model_config, device, learning_rate, reset_optimizer
try:
sess = torch.load(sess_path)
if 'model_config' in sess and sess['model_config'] != model_config:
model_config = sess['model_config']
print('Use session config instead:')
print(utils.dict2params(model_config))
model_state = sess['model_state']
optimizer_state = sess['model_optimizer_state']
print('Session is loaded from', sess_path)
sess_loaded = True
except:
print('New session')
sess_loaded = False
model = PerformanceRNN(**model_config).to(device)
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
if sess_loaded:
model.load_state_dict(model_state)
if not reset_optimizer:
optimizer.load_state_dict(optimizer_state)
return model, optimizer
def load_dataset():
global data_path
dataset = Dataset(data_path, verbose=True)
dataset_size = len(dataset.samples)
assert dataset_size > 0
return dataset
print('Loading session')
model, optimizer = load_session()
print(model)
print('-' * 70)
print('Loading dataset')
dataset = load_dataset()
print(dataset)
print('-' * 70)
#------------------------------------------------------------------------
def save_model():
global model, optimizer, model_config, sess_path
print('Saving to', sess_path)
torch.save({'model_config': model_config,
'model_state': model.state_dict(),
'model_optimizer_state': optimizer.state_dict()}, sess_path)
print('Done saving')
#========================================================================
# Training
#========================================================================
if enable_logging:
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
last_saving_time = time.time()
loss_function = nn.CrossEntropyLoss()
try:
batch_gen = dataset.batches(batch_size, window_size, stride_size)
for iteration, (events, controls) in enumerate(batch_gen):
if use_transposition:
offset = np.random.choice(np.arange(-6, 6))
events, controls = utils.transposition(events, controls, offset)
events = torch.LongTensor(events).to(device)
assert events.shape[0] == window_size
if np.random.random() < control_ratio:
controls = torch.FloatTensor(controls).to(device)
assert controls.shape[0] == window_size
else:
controls = None
init = torch.randn(batch_size, model.init_dim).to(device)
outputs = model.generate(init, window_size, events=events[:-1], controls=controls,
teacher_forcing_ratio=teacher_forcing_ratio, output_type='logit')
assert outputs.shape[:2] == events.shape[:2]
loss = loss_function(outputs.view(-1, event_dim), events.view(-1))
model.zero_grad()
loss.backward()
norm = utils.compute_gradient_norm(model.parameters())
nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
if enable_logging:
writer.add_scalar('model/loss', loss.item(), iteration)
writer.add_scalar('model/norm', norm.item(), iteration)
print(f'iter {iteration}, loss: {loss.item()}')
if time.time() - last_saving_time > saving_interval:
save_model()
last_saving_time = time.time()
except KeyboardInterrupt:
save_model()