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__/tf_utils.cpython-36.pyc
Views: 13377
3

\�]f�@sNddlZddlZddlZddlZdd�Zd
dd�Zdd�Zd	d
�Z	dd�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�rZtrain_set_xZtrain_set_yzdatasets/test_signs.h5Z
test_set_xZ
test_set_yZlist_classes�r)�h5py�File�np�array�reshape�shape)Z
train_datasetZtrain_set_x_origZtrain_set_y_orig�test_datasetZtest_set_x_origZtest_set_y_orig�classes�r�#/home/jovyan/work/week7/tf_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)r	r�random�seed�list�permutationr�math�floor�range�append)�X�YZmini_batch_sizer�mZmini_batchesrZ
shuffled_XZ
shuffled_YZnum_complete_minibatches�kZmini_batch_XZmini_batch_YZ
mini_batchrrr
�random_mini_batchess$
   
rcCstj|�|jd�j}|S)Nr�����)r�eyer�T)r�Crrr
�convert_to_one_hot?sr!cCs�tj|d�}tj|d�}tj|d�}tj|d�}tj|d�}tj|d�}||||||d�}tjdd	d
g�}	t|	|�}
tj|
�}tj�}|j||	|id�}
|
S)N�W1�b1�W2�b2�W3�b3)r"r#r$r%r&r'�floati0r)�	feed_dict)�tf�convert_to_tensor�placeholder�forward_propagation_for_predict�argmax�Session�run)r�
parametersr"r#r$r%r&r'�params�xZz3�p�sessZ
predictionrrr
�predictDs$

r6c
Cs�|d}|d}|d}|d}|d}|d}tjtj||�|�}tjj|�}	tjtj||	�|�}
tjj|
�}tjtj||�|�}|S)a�
    Implements the forward propagation for the model: LINEAR -> RELU -> LINEAR -> RELU -> LINEAR -> SOFTMAX
    
    Arguments:
    X -- input dataset placeholder, of shape (input size, number of examples)
    parameters -- python dictionary containing your parameters "W1", "b1", "W2", "b2", "W3", "b3"
                  the shapes are given in initialize_parameters

    Returns:
    Z3 -- the output of the last LINEAR unit
    r"r#r$r%r&r')r*�add�matmul�nn�relu)
rr1r"r#r$r%r&r'ZZ1�A1ZZ2�A2ZZ3rrr
r-^sr-)rr)r�numpyr�
tensorflowr*rrrr!r6r-rrrr
�<module>s
(