Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/C5 - Sequence Models/Week 1/Jazz improvisation with LSTM/music_utils.py
Views: 4819
from __future__ import print_function1import tensorflow as tf2#import tensorflow.keras.backend as K3from tensorflow.keras.layers import RepeatVector4import sys5from music21 import *6import numpy as np7from grammar import *8from preprocess import *9from qa import *101112def data_processing(corpus, values_indices, m = 60, Tx = 30):13# cut the corpus into semi-redundant sequences of Tx values14Tx = Tx15N_values = len(set(corpus))16np.random.seed(0)17X = np.zeros((m, Tx, N_values), dtype=np.bool)18Y = np.zeros((m, Tx, N_values), dtype=np.bool)19for i in range(m):20# for t in range(1, Tx):21random_idx = np.random.choice(len(corpus) - Tx)22corp_data = corpus[random_idx:(random_idx + Tx)]23for j in range(Tx):24idx = values_indices[corp_data[j]]25if j != 0:26X[i, j, idx] = 127Y[i, j-1, idx] = 12829Y = np.swapaxes(Y,0,1)30Y = Y.tolist()31return np.asarray(X), np.asarray(Y), N_values3233def next_value_processing(model, next_value, x, predict_and_sample, indices_values, abstract_grammars, duration, max_tries = 1000, temperature = 0.5):34"""35Helper function to fix the first value.3637Arguments:38next_value -- predicted and sampled value, index between 0 and 7739x -- numpy-array, one-hot encoding of next_value40predict_and_sample -- predict function41indices_values -- a python dictionary mapping indices (0-77) into their corresponding unique value (ex: A,0.250,< m2,P-4 >)42abstract_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>'43duration -- scalar, index of the loop in the parent function44max_tries -- Maximum numbers of time trying to fix the value4546Returns:47next_value -- process predicted value48"""4950# fix first note: must not have < > and not be a rest51if (duration < 0.00001):52tries = 053while (next_value.split(',')[0] == 'R' or54len(next_value.split(',')) != 2):55# give up after 1000 tries; random from input's first notes56if tries >= max_tries:57#print('Gave up on first note generation after', max_tries, 'tries')58# np.random is exclusive to high59rand = np.random.randint(0, len(abstract_grammars))60next_value = abstract_grammars[rand].split(' ')[0]61else:62next_value = predict_and_sample(model, x, indices_values, temperature)6364tries += 16566return next_value676869def sequence_to_matrix(sequence, values_indices):70"""71Convert a sequence (slice of the corpus) into a matrix (numpy) of one-hot vectors corresponding72to indices in values_indices7374Arguments:75sequence -- python list7677Returns:78x -- numpy-array of one-hot vectors79"""80sequence_len = len(sequence)81x = np.zeros((1, sequence_len, len(values_indices)))82for t, value in enumerate(sequence):83if (not value in values_indices): print(value)84x[0, t, values_indices[value]] = 1.85return x868788