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 1/Dinosaur Island -- Character-level language model/__pycache__/shakespeare_utils.cpython-36.pyc
Views: 13420
3

��qZ��@sfddlmZddlmZddlmZmZmZddlm	Z	m
Z
mZmZm
Z
ddlmZddlmZddlmZddlZddlZddlZddlZd#dd�Zd$d
d�Zd%dd�Zdd�Zed�ejddd�j�j�Z d	Z!e"e#e$e ���Z%e&dd�e'e%�D��Z(e&dd�e'e%�D��Z)ed�ee e!d
d�\Z*Z+ed�ee*e+e,e%�e(d�\Z-Z.ed�ed �Z/d!d"�Z0dS)&�)�print_function)�LambdaCallback)�Model�
load_model�
Sequential)�Dense�
Activation�Dropout�Input�Masking)�LSTM)�get_file)�
pad_sequencesN�(�cCsdg}g}xDtdt|�||�D],}|j||||��|j|||�qWtdt|��||fS)a�
    Create a training set by scanning a window of size Tx over the text corpus, with stride 3.
    
    Arguments:
    text -- string, corpus of Shakespearian poem
    Tx -- sequence length, number of time-steps (or characters) in one training example
    stride -- how much the window shifts itself while scanning
    
    Returns:
    X -- list of training examples
    Y -- list of training labels
    rznumber of training examples:)�range�len�append�print)�text�Tx�stride�X�Y�i�r�_/home/jovyan/work/Week 1/Dinosaur Island -- Character-level language model/shakespeare_utils.py�
build_datasrcCs�t|�}tj|||ftjd�}tj||ftjd�}xPt|�D]D\}}	x&t|	�D]\}
}d|||
||f<qNWd|||||f<q<W||fS)a	
    Convert X and Y (lists) into arrays to be given to a recurrent neural network.
    
    Arguments:
    X -- 
    Y -- 
    Tx -- integer, sequence length
    
    Returns:
    x -- array of shape (m, Tx, len(chars))
    y -- array of shape (m, len(chars))
    )�dtype�)r�np�zeros�bool�	enumerate)rr�n_x�char_indicesr�m�x�yr�sentence�t�charrrr�
vectorization*sr,��?cCsftj|�jd�}tj|�|}tj|�}|tj|�}tjjd|d�}tjjt	t
t��|j�d�}|S)N�float64r)�p)
r �asarray�astype�log�exp�sum�random�multinomial�choicerr�chars�ravel)�preds�temperature�	exp_preds�probas�outrrr�sampleCs
r?cCsdS)Nr)�epoch�logsrrr�on_epoch_endNsrBzLoading text data...zshakespeare.txtzutf-8)�encodingccs|]\}}||fVqdS)Nr)�.0r�crrr�	<genexpr>{srFccs|]\}}||fVqdS)Nr)rDrrErrrrF|szCreating training set...)rzVectorizing training set...)r$r%zLoading model...z+models/model_shakespeare_kiank_350_epoch.h5c
Cs�d}td�}dtt�dj|�j�}||7}tjjd�tjj|�x�td�D]�}t	j
dttt�f�}x.t
|�D]"\}}|dkrrd	|d
|t|f<qrWtj|d
d�d
}t|d	d�}t|}	||	7}|dd�|	}tjj|	�tjj�|	d
krPqPqPWdS)N�z[Write the beginning of your poem, the Shakespeare machine will complete it. Your input is: z{0:0>�}z

Here is your poem: 

i�r�0g�?r)�verbose)r;�
)�input�strr�format�lower�sys�stdout�writerr r!rr8r#r%�model�predictr?�indices_char�flush)
�	generated�	usr_inputr)r�x_predr*r+r:�
next_index�	next_charrrr�generate_output�s(
r\)rr)r)r-)1�
__future__r�keras.callbacksr�keras.modelsrrr�keras.layersrrr	r
rr�keras.utils.data_utilsr
�keras.preprocessing.sequencer�numpyr r5rP�iorr,r?rBr�open�readrOrr�sorted�list�setr8�dictr#r%rUrrrr'r(rSr\rrrr�<module>s6


'