-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (57 loc) · 2.41 KB
/
main.py
File metadata and controls
69 lines (57 loc) · 2.41 KB
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
import pickle
import numpy
from midi_to_statematrix import *
import multi_training
import model
import os
def gen_adaptive(m, pcs, times, keep_thoughts=False, name="final"):
xIpt, xOpt = map(lambda x: numpy.array(x, dtype='int8'),
multi_training.getPieceSegment(pcs))
all_outputs = [xOpt[0]]
if keep_thoughts:
all_thoughts = []
m.start_slow_walk(xIpt[0])
cons = 1
for time in range(multi_training.batch_len * times):
resdata = m.slow_walk_fun(cons)
nnotes = numpy.sum(resdata[-1][:, 0])
if nnotes < 2:
if cons > 1:
cons = 1
cons -= 0.02
else:
cons += (1 - cons) * 0.3
all_outputs.append(resdata[-1])
if keep_thoughts:
all_thoughts.append(resdata)
noteStateMatrixToMidi(numpy.array(all_outputs), 'output/' + name)
if keep_thoughts:
pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))
def fetch_train_thoughts(m, pcs, batches, name="trainthoughts"):
all_thoughts = []
for i in range(batches):
ipt, opt = multi_training.getPieceBatch(pcs)
thoughts = m.update_thought_fun(ipt, opt)
all_thoughts.append((ipt, opt, thoughts))
pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))
def get_last_epoch(model_directory):
# Get all file names in model_directory
files = [file for file in os.listdir(model_directory) if '.p' in file]
# Function that go over a string and create in a list all individual
# numbers and then return all of them joined as a string.
get_number = (lambda string: "".join(list(filter(lambda c: c.isdigit(),
string))))
# Map the get_number over all the names and return them as integers
epochs = list(map(lambda string: int(get_number(string)), files))
epochs.append(0) # Append 0 in case of void list
return max(epochs)
if __name__ == '__main__':
# Directory in which the parameters that have been calculated for the model
# are saved.
model_directory = "output"
os.makedirs(model_directory, exist_ok=True)
pcs = multi_training.loadPieces("Scale")
start = get_last_epoch(model_directory)
m = model.BiaxialRNNModel([300, 300], [100, 50])
m.cuda()
multi_training.trainPiece(m, pcs, 10000, model_directory, start)