Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T
GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Sequence Models/Week 1/Jazz improvisation with LSTM/__pycache__/music_utils.cpython-36.pyc
15918 views
3

��qZ��@s�ddlmZddlZddljZddlmZddl	Z	ddl
TddlZddl
TddlTddlTddd�Zddd�Zd
d�Zdd�ZdS)�)�print_functionN)�RepeatVector)�*�<�cCs�|}tt|��}tjjd�tj|||ftjd�}tj|||ftjd�}xzt|�D]n}tjjt|�|�}||||�}	xDt|�D]8}
||	|
}|
dkr�d|||
|f<d|||
d|f<q�WqRWtj	|dd�}|j
�}tj|�tj|�|fS)Nr)�dtype�)�len�set�np�random�seed�zeros�bool�range�choice�swapaxes�tolist�asarray)�corpus�values_indices�mZTxZN_values�X�Y�iZ
random_idxZ	corp_data�j�idx�r�D/home/jovyan/work/Week 1/Jazz improvisation with LSTM/music_utils.py�data_processing
s r����?c	Cs~|dkrzd}	xl|jd�ddks2t|jd��dkrx|	|kr`tjjdt|��}
||
jd�d}n|||||�}|	d7}	qW|S)a�
    Helper function to fix the first value.
    
    Arguments:
    next_value -- predicted and sampled value, index between 0 and 77
    x -- numpy-array, one-hot encoding of next_value
    predict_and_sample -- predict function
    indices_values -- a python dictionary mapping indices (0-77) into their corresponding unique value (ex: A,0.250,< m2,P-4 >)
    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>'
    duration -- scalar, index of the loop in the parent function
    max_tries -- Maximum numbers of time trying to fix the value
    
    Returns:
    next_value -- process predicted value
    g�h㈵��>r�,�R�� r)�splitr	rr�randint)�modelZ
next_value�xZpredict_and_sampleZindices_values�abstract_grammars�duration�	max_triesZtemperature�tries�randrrr�next_value_processing"sr/cCsXt|�}tjd|t|�f�}x6t|�D]*\}}||kr>t|�d|d|||f<q&W|S)z�
    Convert a sequence (slice of the corpus) into a matrix (numpy) of one-hot vectors corresponding 
    to indices in values_indices
    
    Arguments:
    sequence -- python list
    
    Returns:
    x -- numpy-array of one-hot vectors 
    rg�?r)r	rr�	enumerate�print)�sequencerZsequence_lenr)�t�valuerrr�sequence_to_matrixFsr5cCs&tj|�}tj|d�}td�|�}|S)N�Nr)�K�argmax�tf�one_hotr)r)rrrr:Xs
r:)rr)r r!)�
__future__rZ
tensorflowr9Z
keras.backend�backendr7�keras.layersr�sys�music21�numpyr�grammar�
preprocess�qarr/r5r:rrrr�<module>s


$