Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T
GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Sequence Models/Week 3/Machine Translation/__pycache__/nmt_utils.cpython-36.pyc
14376 views
3

ڞqZ��@sddlZddlmZddlZddlmZddlmZddlm	Z	ddl
jZddl
jZe�Zejd�ejd�ddd	d
d
d
d
d
d
d
d
d
d
ddd
ddddddddgZdgZdd�Zdd�Zdd�Zdd�Zdd�Zd d!d"d#gZd$d%�Zefd&d'�Zd1d)d*�Zd2d/d0�ZdS)3�N)�Faker)�tqdm)�format_date)�to_categoricali90�short�medium�long�fullz	d MMM YYYz
d MMMM YYYz
dd MMM YYYz
d MMM, YYYzd MMMM, YYYzdd, MMM YYYzd MM YYz
MMMM d YYYzMMMM d, YYYzdd.MM.YY�en_UScCshtj�}y4t|tjt�dd�}|j�}|jdd�}|j�}Wn t	k
r\}zdSd}~XnX|||fS)z�
        Loads some fake dates 
        :returns: tuple containing human readable string, machine readable string, and date object
    r
)�format�locale�,�N)NNN)
�fake�date_objectr�random�choice�FORMATS�lower�replace�	isoformat�AttributeError)�dtZhuman_readableZmachine_readable�e�r�9/home/jovyan/work/Week 3/Machine Translation/nmt_utils.py�	load_date+srcCs�t�}t�}g}d}xRtt|��D]B}t�\}}}|dk	r"|j||f�|jt|��|jt|��q"Wttt	|�ddgt
tt|�d����}ttt	|���}	dd�|	j
�D�}
|||
|	fS)zm
        Loads a dataset with m examples and vocabularies
        :m: the number of examples to generate
    �Nz<unk>z<pad>�cSsi|]\}}||�qSrr)�.0�k�vrrr�
<dictcomp>Ssz load_dataset.<locals>.<dictcomp>)�setr�ranger�append�update�tuple�dict�zip�sorted�list�len�	enumerate�items)�m�human_vocab�
machine_vocab�dataset�Tx�i�h�_ZhumanZinv_machine�machinerrr�load_dataset=sr8c	s�t|�\}}tj��fdd�|D��}��fdd�|D�}tjtt�fdd�|���}tjtt�fdd�|���}|tj|�||fS)Ncsg|]}t|����qSr)�
string_to_int)rr4)r3r0rr�
<listcomp>[sz#preprocess_data.<locals>.<listcomp>csg|]}t|����qSr)r9)r�t)�Tyr1rrr:\scst|t��d�S)N)�num_classes)rr,)�x)r0rr�<lambda>^sz!preprocess_data.<locals>.<lambda>cst|t��d�S)N)r=)rr,)r>)r1rrr?_s)r)�np�arrayr+�map)	r2r0r1r3r<�X�YZXohZYohr)r3r<r0r1r�preprocess_dataWsrEcsl|j�}|jdd�}t|�|kr,|d|�}tt�fdd�|��}t|�|krh|�dg|t|�7}|S)a$
    Converts all strings in the vocabulary into a list of integers representing the positions of the
    input string's characters in the "vocab"
    
    Arguments:
    string -- input string, e.g. 'Wed 10 Jul 2007'
    length -- the number of time steps you'd like, determines if the output will be padded or cut
    vocab -- vocabulary, dictionary used to index every character of your "string"
    
    Returns:
    rep -- list of integers (or '<unk>') (size = length) representing the position of the string's character in the vocabulary
    r
rNcs�j|d�S)Nz<unk>)�get)r>)�vocabrrr?xszstring_to_int.<locals>.<lambda>z<pad>)rrr,r+rB)�string�lengthrG�repr)rGrr9csr9cs�fdd�|D�}|S)a�
    Output a machine readable list of characters based on a list of indexes in the machine's vocabulary
    
    Arguments:
    ints -- list of integers representing indexes in the machine's vocabulary
    inv_vocab -- dictionary mapping machine readable indexes to machine readable characters 
    
    Returns:
    l -- list of characters corresponding to the indexes of ints thanks to the inv_vocab mapping
    csg|]}�|�qSrr)rr4)�	inv_vocabrrr:�sz!int_to_string.<locals>.<listcomp>r)�intsrK�lr)rKr�
int_to_string�srNz
3 May 1979z5 Apr 09z20th February 2016zWed 10 Jul 2007cCs:t|t|�}|jtj|g��}tj|ddd�}t||�S)Nr�)�axis�����)r9Z
TIME_STEPS�predictr@rA�argmaxrN)�model�input_vocabulary�inv_output_vocabulary�text�encoded�
predictionrrr�run_example�srZcCsHg}x>|D]6}|jdjt||||���td|�td|d�q
W|S)Nrzinput:zoutput:rOrQ)r%�joinrZ�print)rTrUrV�examplesZ	predicted�examplerrr�run_examples�s

r_rOcCsbtj|�}|dkrtj|�S|dkrVtj|tj||dd��}tj||dd�}||Std��dS)aSoftmax activation function.
    # Arguments
        x : Tensor.
        axis: Integer, axis along which the softmax normalization is applied.
    # Returns
        Tensor, output of softmax transformation.
    # Raises
        ValueError: In case `dim(x) == 1`.
    rT)rP�keepdimsz+Cannot apply softmax to a tensor that is 1DN)�K�ndim�softmax�exp�max�sum�
ValueError)r>rPrbr�srrrrc�s


rc��r�
cstjd�}|j\}}tjd|f�}	tjd|f�}
|j|�tjt||���jd�}tjtt�fdd�|���}t	j
|j�fdd�t|�D��}|||	|
g�}
x<t|�D]0}x*t|�D]}|
|d|df|||<q�Wq�W|j
||	|
g�}g}x2tt|��D]"}|jttj||dd	����q�Wt|�}t||�}t|�}t|�}|}tj�tjdd�}|jddd�}|j|d
dd�}|jddddg�}|j||dd�}|jjddd�|jt|��|j|d|��|jt|��|j|d|�dd�|jd�|j d�|j!�|S) z$
    Plot the attention map.
  
    rkrrOcst|t��d�S)N)r=)rr,)r>)rUrrr?�sz$plot_attention_map.<locals>.<lambda>csg|]}�j|��qSr)�
get_output_at)rr;)�layerrrr:�sz&plot_attention_map.<locals>.<listcomp>r)rP��!@)�figsize�nearestZBlues)�
interpolation�cmapg�������?g333333�?g���Q��?�
horizontal)Zcax�orientationz1Alpha value (Probability output of the "softmax")r)ZlabelpadN�-)�rotationzInput SequencezOutput Sequence)rkr)rOr)rnro)"r@�zeros�shape�layersrAr9�reshaper+rBra�function�inputsr$rRr,r%�intrSrN�pltZclf�figure�add_subplot�imshowZadd_axes�colorbar�ax�
set_xlabel�
set_yticks�set_yticklabels�
set_xticks�set_xticklabels�
set_ylabel�grid)rTrUrVrW�n_s�numr3r<Z
attention_map�s0�c0rX�f�rr;Zt_primerYZpredicted_textr4Ztext_�input_length�
output_lengthr�Zcbaxes�cbarr)rUrmr�plot_attention_map�sH


 ""


r�)rO)rirjrrk)�numpyr@�fakerrrr�babel.datesr�keras.utilsr�
keras.backend�backendra�matplotlib.pyplot�pyplotrr�seedrZLOCALESrr8rEr9rNZEXAMPLESrZr_rcr�rrrr�<module>sZ