CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

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: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Sequence Models/Week 3/Neural Machine Translation/__pycache__/nmt_utils.cpython-36.pyc
Views: 13380
3

��BZ�@s�ddlZddlmZddlZddlmZddlmZddlm	Z	ddl
jZe�Z
e
jd�ejd�ddd	d
ddd
ddddddddgZdgZdd�Zdd�Zdd�Zdd�Zddd d!gZd"d#�Zefd$d%�Zd*d&d'�Zd+d(d)�ZdS),�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_UScCs�tj�}yXt|tjt�tjt�d�}tjdddg�}|dkrF|j�}n|dkrV|j�}|j	�}Wn t
k
r�}zdSd}~XnX|||fS)z�
        Creates some fake dates 
        :returns: tuple containing human readable string, machine readable string, and date object
    )�format�localer��N)NNN)�fake�date_objectr�random�choice�FORMATS�LOCALES�upper�lower�	isoformat�AttributeError)�dtZhuman_readableZcase_changeZmachine_readable�e�r�@/home/jovyan/work/Week 3/Neural Machine Translation/nmt_utils.py�create_date"s
rcs"t�}t�}g}xRtt|��D]B}t�\}}}|dk	r|j||f�|jt|��|jt|��qWttt	|�ddgt	tt
|�d�����ttt	|�ddg��}dd�|j�D��d�t|�\}	}
t
j��fdd	�|	D��}	��fd
d	�|
D�}
t
jt	t�fdd�|
���}
|��||	|
fS)
zx
        Creates a dataset with n_examples and vocabularies
        :n_examples: the number of examples to generate
    Nz<unk>z<pad>rcSsi|]\}}||�qSrr)�.0�k�vrrr�
<dictcomp>Nsz"create_dataset.<locals>.<dictcomp>�csg|]}t|����qSr)�
string_to_int)r�i)�Tx�humanrr�
<listcomp>Tsz"create_dataset.<locals>.<listcomp>csg|]}t|����qSr)r#)r�t)r%�machinerrr'Uscst|t��d�S)N)�num_classes)r�len)�x)r)rr�<lambda>Vsz create_dataset.<locals>.<lambda>)�setr�ranger�append�update�tuple�dict�zip�listr+�	enumerate�items�np�array�map)Z
n_examplesZhuman_vocabZ
machine_vocab�datasetr$�h�m�_Zinv_machine�sources�targetsr)r%r&r)r�create_dataset:s&rAcsXt|�|kr|d|�}tt�fdd�|��}t|�|krT|�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
    Ncs�j|d�S)Nz<unk>)�get)r,)�vocabrrr-mszstring_to_int.<locals>.<lambda>z<pad>)r+r5r:)�string�lengthrC�repr)rCrr#\sr#cs�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)rr$)�	inv_vocabrrr'�sz!int_to_string.<locals>.<listcomp>r)�intsrG�lr)rGr�
int_to_stringusrJz
3 May 1979z5 Apr 09z20th February 2016zWed 10 Jul 2007cCs:t|t|�}|jtj|g��}tj|ddd�}t||�S)Nrr
)�axis�����)r#r%�predictr8r9�argmaxrJ)�model�input_vocabulary�inv_output_vocabulary�text�encoded�
predictionrrr�run_example�srUcCsHg}x>|D]6}|jdjt||||���td|�td|d�q
W|S)N�zinput:zoutput:r
rL)r0�joinrU�print)rOrPrQ�examplesZ	predicted�examplerrr�run_examples�s

r[cCst|dkrtjjd|d�}tjj|||fd�}tjjdd|dfd�}tj|dd�d|f�|dd�|dd�f<||fS)aS
    Data generation. x is purely random except that it's first value equals the target y.
    In practice, the network should learn that the target = x[attention_column].
    Therefore, most of its attention should be focused on the value addressed by attention_column.
    :param m: the number of samples to retrieve.
    :param Tx: the number of time steps of your series.
    :param n_h: the number of dimensions of each element in the series.
    :param attention_column: the column linked to the target. Everything else is purely random.
    :return: x: model inputs, y: model targets
    Nr)�low�high)�sizerr
)r\r]r^)r8r�randint�standard_normal�tile)r=r%Zn_hZattention_columnr,�yrrr�get_data_recurrent�s*rccsxg}|j��dkr$dd�|jD�}n�fdd�|jD�}�fdd�|D�}�fdd�|D�}x|D]}|j|�qbW|S)NcSsg|]
}|j�qSr)�output)r�layerrrrr'�sz#get_activations.<locals>.<listcomp>csg|]}|j�kr|j�qSr)�namerd)rre)�
layer_namerrr'�scs&g|]}tj�gtj�g|g��qSr)�K�function�learning_phase)r�out)�inprrr'�scsg|]}|�dg�d�qS)g�?rr)r�func)�inputsrrr'�s)�input�layersr0)rOrnrg�activations�outputs�funcsZ
layer_outputsZlayer_activationsr)rlrnrgr�get_activations�s
rt)N)N)�numpyr8�fakerrrr�babel.datesr�keras.utilsr�
keras.backend�backendrhr�seedrrrrAr#rJZEXAMPLESrUr[rcrtrrrr�<module>sD


"