CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: amanchadha/coursera-deep-learning-specialization
Path: blob/master/C5 - Sequence Models/Week 1/Jazz improvisation with LSTM/music_utils.py
Views: 4819
1
from __future__ import print_function
2
import tensorflow as tf
3
#import tensorflow.keras.backend as K
4
from tensorflow.keras.layers import RepeatVector
5
import sys
6
from music21 import *
7
import numpy as np
8
from grammar import *
9
from preprocess import *
10
from qa import *
11
12
13
def data_processing(corpus, values_indices, m = 60, Tx = 30):
14
# cut the corpus into semi-redundant sequences of Tx values
15
Tx = Tx
16
N_values = len(set(corpus))
17
np.random.seed(0)
18
X = np.zeros((m, Tx, N_values), dtype=np.bool)
19
Y = np.zeros((m, Tx, N_values), dtype=np.bool)
20
for i in range(m):
21
# for t in range(1, Tx):
22
random_idx = np.random.choice(len(corpus) - Tx)
23
corp_data = corpus[random_idx:(random_idx + Tx)]
24
for j in range(Tx):
25
idx = values_indices[corp_data[j]]
26
if j != 0:
27
X[i, j, idx] = 1
28
Y[i, j-1, idx] = 1
29
30
Y = np.swapaxes(Y,0,1)
31
Y = Y.tolist()
32
return np.asarray(X), np.asarray(Y), N_values
33
34
def next_value_processing(model, next_value, x, predict_and_sample, indices_values, abstract_grammars, duration, max_tries = 1000, temperature = 0.5):
35
"""
36
Helper function to fix the first value.
37
38
Arguments:
39
next_value -- predicted and sampled value, index between 0 and 77
40
x -- numpy-array, one-hot encoding of next_value
41
predict_and_sample -- predict function
42
indices_values -- a python dictionary mapping indices (0-77) into their corresponding unique value (ex: A,0.250,< m2,P-4 >)
43
abstract_grammars -- list of grammars, on element can be: 'S,0.250,<m2,P-4> C,0.250,<P4,m-2> A,0.250,<P4,m-2>'
44
duration -- scalar, index of the loop in the parent function
45
max_tries -- Maximum numbers of time trying to fix the value
46
47
Returns:
48
next_value -- process predicted value
49
"""
50
51
# fix first note: must not have < > and not be a rest
52
if (duration < 0.00001):
53
tries = 0
54
while (next_value.split(',')[0] == 'R' or
55
len(next_value.split(',')) != 2):
56
# give up after 1000 tries; random from input's first notes
57
if tries >= max_tries:
58
#print('Gave up on first note generation after', max_tries, 'tries')
59
# np.random is exclusive to high
60
rand = np.random.randint(0, len(abstract_grammars))
61
next_value = abstract_grammars[rand].split(' ')[0]
62
else:
63
next_value = predict_and_sample(model, x, indices_values, temperature)
64
65
tries += 1
66
67
return next_value
68
69
70
def sequence_to_matrix(sequence, values_indices):
71
"""
72
Convert a sequence (slice of the corpus) into a matrix (numpy) of one-hot vectors corresponding
73
to indices in values_indices
74
75
Arguments:
76
sequence -- python list
77
78
Returns:
79
x -- numpy-array of one-hot vectors
80
"""
81
sequence_len = len(sequence)
82
x = np.zeros((1, sequence_len, len(values_indices)))
83
for t, value in enumerate(sequence):
84
if (not value in values_indices): print(value)
85
x[0, t, values_indices[value]] = 1.
86
return x
87
88