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/Improving Deep Neural Networks Hyperparameter tuning, Regularization and Optimization/week7/__pycache__/improv_utils.cpython-36.pyc
Views: 13377
3

Y��Y�*�@shddlZddlZddlZddlZdd�Zddd�Zdd�Zd	d
�Z	dd�Z
d
d�Zdd�Zddd�Z
dS)�NcCs�tjdd�}tj|ddd��}tj|ddd��}tjdd�}tj|ddd��}tj|ddd��}tj|ddd��}|jd	|jd
f�}|jd	|jd
f�}|||||fS)Nzdatasets/train_signs.h5�r�train_set_x�train_set_yzdatasets/test_signs.h5�
test_set_x�
test_set_y�list_classes�r)�h5py�File�np�array�reshape�shape)�
train_dataset�train_set_x_orig�train_set_y_orig�test_dataset�test_set_x_orig�test_set_y_orig�classes�r�'/home/jovyan/work/week7/improv_utils.py�load_datasetsr�@cCs"|jd}g}tjj|�ttjj|��}|dd�|f}|dd�|fj|jd|f�}tj||�}	xdt	d|	�D]V}
|dd�|
||
||�f}|dd�|
||
||�f}||f}
|j
|
�qtW||dk�r|dd�|	||�f}|dd�|	||�f}||f}
|j
|
�|S)a�
    Creates a list of random minibatches from (X, Y)
    
    Arguments:
    X -- input data, of shape (input size, number of examples)
    Y -- true "label" vector (containing 0 if cat, 1 if non-cat), of shape (1, number of examples)
    mini_batch_size - size of the mini-batches, integer
    seed -- this is only for the purpose of grading, so that you're "random minibatches are the same as ours.
    
    Returns:
    mini_batches -- list of synchronous (mini_batch_X, mini_batch_Y)
    rNr)rr�random�seed�list�permutationr
�math�floor�range�append)�X�Y�mini_batch_sizer�m�mini_batchesr�
shuffled_X�
shuffled_Y�num_complete_minibatches�k�mini_batch_X�mini_batch_Y�
mini_batchrrr�random_mini_batchess$
   
r.cCstj|�|jd�j}|S)Nr�����)r�eyer
�T)r#�Crrr�convert_to_one_hot?sr3cCs�tj|d�}tj|d�}tj|d�}tj|d�}tj|d�}tj|d�}||||||d�}tjdd	d
g�}	t|	|�}
tj|
�}tj��}|j||	|id�}
WdQRX|
S)N�W1�b1�W2�b2�W3�b3)r4r5r6r7r8r9�floati0r)�	feed_dict)�tf�convert_to_tensor�placeholder�forward_propagation�argmax�Session�run)r"�
parametersr4r5r6r7r8r9�params�x�z3�p�sess�
predictionrrr�predictCs$


rJcCs(tjd|dg�}tjd|dg�}||fS)a[
    Creates the placeholders for the tensorflow session.
    
    Arguments:
    n_x -- scalar, size of an image vector (num_px * num_px = 64 * 64 * 3 = 12288)
    n_y -- scalar, number of classes (from 0 to 5, so -> 6)
    
    Returns:
    X -- placeholder for the data input, of shape [n_x, None] and dtype "float"
    Y -- placeholder for the input labels, of shape [n_y, None] and dtype "float"
    
    Tips:
    - You will use None because it let's us be flexible on the number of examples you will for the placeholders.
      In fact, the number of examples during test/train is different.
    r:N)r<r>)�n_x�n_yr"r#rrr�create_placeholders^srMcCs�tjd�tjdddgtjjjdd�d�}tjdddgtj�d�}tjdd	dgtjjjdd�d�}tjd
d	dgtj�d�}tjddd	gtjjjdd�d�}tjd
ddgtj�d�}||||||d�}|S)a�
    Initializes parameters to build a neural network with tensorflow. The shapes are:
                        W1 : [25, 12288]
                        b1 : [25, 1]
                        W2 : [12, 25]
                        b2 : [12, 1]
                        W3 : [6, 12]
                        b3 : [6, 1]
    
    Returns:
    parameters -- a dictionary of tensors containing W1, b1, W2, b2, W3, b3
    rr4�i0)r)�initializerr5r6�r7r8�r9)r4r5r6r7r8r9)r<�set_random_seed�get_variable�contrib�layersZxavier_initializer�zeros_initializer)r4r5r6r7r8r9rCrrr�initialize_parametersws
   rWcCs.tj|�}tj|�}tjtjj||d��}|S)a
    Computes the cost
    
    Arguments:
    z3 -- output of forward propagation (output of the last LINEAR unit), of shape (10, number of examples)
    Y -- "true" labels vector placeholder, same shape as z3
    
    Returns:
    cost - Tensor of the cost function
    )�logits�labels)r<�	transpose�reduce_mean�nn�!softmax_cross_entropy_with_logits)rFr#rXrY�costrrr�compute_cost�s

r_�-C��6?��� Tc Cs�tj�tjd�d}|j\}	}
|jd}g}t|	|�\}
}t�}t|
|�}t||�}tj	j
|d�j|�}tj�}tj
���x}|j|�x�t|�D]�}d}t|
|�}|d}t||||�}x>|D]6}|\}}|j||g|
|||id�\}}|||7}q�W|dk�r$|ddk�r$td	||f�|dkr�|d
dkr�|j|�q�Wtjtj|��tjd�tjd�tjd
t|��tj�|j|�}td�tjtj|�tj|��}tjtj |d��}td|j!|
|||i��td|j!|
|||i��|SQRXdS)a!
    Implements a three-layer tensorflow neural network: LINEAR->RELU->LINEAR->RELU->LINEAR->SOFTMAX.
    
    Arguments:
    X_train -- training set, of shape (input size = 12288, number of training examples = 1080)
    Y_train -- test set, of shape (output size = 6, number of training examples = 1080)
    X_test -- training set, of shape (input size = 12288, number of training examples = 120)
    Y_test -- test set, of shape (output size = 6, number of test examples = 120)
    learning_rate -- learning rate of the optimization
    num_epochs -- number of epochs of the optimization loop
    minibatch_size -- size of a minibatch
    print_cost -- True to print the cost every 100 epochs
    
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    r�r)�
learning_rateg)r;T�dzCost after epoch %i: %f�r^ziterations (per tens)zLearning rate =zParameters have been trained!r:zTrain Accuracy:zTest Accuracy:N)"�ops�reset_default_graphr<rRrrMrWr?r_�train�
AdamOptimizer�minimize�global_variables_initializerrArBr �intr.�printr!�plt�plotr�squeeze�ylabel�xlabel�title�str�show�equalr@r[�cast�eval) �X_train�Y_train�X_test�Y_testrd�
num_epochs�minibatch_size�
print_costrrKr%rL�costsr"r#rCrFr^�	optimizer�initrH�epoch�minibatch_cost�num_minibatches�minibatches�	minibatch�minibatch_X�minibatch_Y�_�	temp_cost�correct_prediction�accuracyrrr�model�sN









r�)rr)r`rarbT)r	�numpyr�
tensorflowr<rrr.r3rJrMrWr_r�rrrr�<module>s
(#