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/Convolutional Neural Networks/dummy/__pycache__/input_data.cpython-35.pyc
Views: 13377


�߱Yh�@sdZddlmZddlmZddlmZddlZddlZddlZddl	Z	ddl
mZddl
mZddlZ
dZd	d
�Zdd�Zd
d�Zddd�Zddd�ZGdd�de�Zdde
jdd�ZdS)z1Functions for downloading and reading MNIST data.�)�absolute_import)�division)�print_functionN)�urllib)�xrangez!http://yann.lecun.com/exdb/mnist/cCs�tjj|�stj|�tjj||�}tjj|�s�tjjt||�\}}tj	|�}t
d||jd�|S)z@Download the data from Yann's website, unless it's already here.zSuccessfully downloadedzbytes.)�os�path�exists�mkdir�joinr�request�urlretrieve�
SOURCE_URL�stat�print�st_size)�filename�work_directory�filepath�_Zstatinfo�r�/output/input_data.py�maybe_downloads
rcCs;tjtj�jd�}tj|jd�d|�dS)N�>��dtyper)�numpyr�uint32�newbyteorder�
frombuffer�read)�
bytestream�dtrrr�_read32%sr#cCs�td|�tj|���}t|�}|dkrMtd||f��t|�}t|�}t|�}|j|||�}tj|dtj�}|j	|||d�}|SWdQRXdS)zDExtract the images into a 4D uint8 numpy array [index, y, x, depth].�
Extractingiz/Invalid magic number %d in MNIST image file: %sr�N)
r�gzip�openr#�
ValueErrorr rr�uint8�reshape)rr!�magic�
num_images�rows�cols�buf�datarrr�extract_images(s
r1�
cCsP|jd}tj|�|}tj||f�}d|j||j�<|S)z5Convert class labels from scalars to one-hot vectors.rr%)�shaper�arange�zeros�flat�ravel)�labels_dense�num_classes�
num_labels�index_offset�labels_one_hotrrr�dense_to_one_hot8s

r=FcCs�td|�tj|��|}t|�}|dkrMtd||f��t|�}|j|�}tj|dtj�}|r�t	|�S|SWdQRXdS)z7Extract the labels into a 1D uint8 numpy array [index].r$iz/Invalid magic number %d in MNIST label file: %srN)
rr&r'r#r(r rrr)r=)r�one_hotr!r+�	num_itemsr/�labelsrrr�extract_labels?s

rAc@seZdZddejdd�Zedd��Zedd��Zedd	��Z	ed
d��Z
ddd
�ZdS)�DataSetFcCs:tj|�j}|tjtjfkr:td|��|rUd|_||_n�|jd|jdks�t	d|j|jf��|jd|_|jddks�t	�|j
|jd|jd|jd�}|tjkr|jtj�}tj
|d�}||_||_d|_d|_d
S)z�Construct a DataSet.
    one_hot arg is used only if fake_data is true.  `dtype` can be either
    `uint8` to leave the input as `[0, 255]`, or `float32` to rescale into
    `[0, 1]`.
    z1Invalid image dtype %r, expected uint8 or float32i'rz!images.shape: %s labels.shape: %s�r%�g�?g�o@Ngp?)�tf�as_dtype�
base_dtyper)�float32�	TypeError�
_num_examplesr>r3�AssertionErrorr*�astyper�multiply�_images�_labels�_epochs_completed�_index_in_epoch)�self�imagesr@�	fake_datar>rrrr�__init__Os*
					zDataSet.__init__cCs|jS)N)rN)rRrrrrSoszDataSet.imagescCs|jS)N)rO)rRrrrr@rszDataSet.labelscCs|jS)N)rJ)rRrrr�num_examplesuszDataSet.num_examplescCs|jS)N)rP)rRrrr�epochs_completedxszDataSet.epochs_completedcsA|rudgd�|jr3dgdgd�nd��fdd�t|�D��fdd�t|�D�fS|j}|j|7_|j|jkr|jd7_tj|j�}tjj|�|j	||_	|j
||_
d}||_||jkst�|j}|j	||�|j
||�fS)z9Return the next `batch_size` examples from this data set.r%ir�	csg|]}��qSrr)�.0r)�
fake_imagerr�
<listcomp>�s	z&DataSet.next_batch.<locals>.<listcomp>csg|]}��qSrr)rYr)�
fake_labelrrr[�s	)r>rrQrJrPrr4�random�shufflerNrOrK)rR�
batch_sizerT�start�perm�endr)rZr\r�
next_batch{s(
	 			zDataSet.next_batchN)�__name__�
__module__�__qualname__rErHrU�propertyrSr@rVrWrcrrrrrBNsrBcs�Gdd�dt�}|�}|rb��fdd�}|�|_|�|_|�|_|Sd}d}d}	d}
d	}t||�}t|�}
t||�}t|d
��}t|	|�}t|�}t|
|�}t|d
��}|
d|�}|d|�}|
|d�}
||d�}t|
|d��|_t||d��|_t||d��|_|S)Nc@seZdZdS)z read_data_sets.<locals>.DataSetsN)rdrerfrrrr�DataSets�srhc	stggddd�d��S)NrTTr>r)rBr)rr>rr�fake�szread_data_sets.<locals>.fakeztrain-images-idx3-ubyte.gzztrain-labels-idx1-ubyte.gzzt10k-images-idx3-ubyte.gzzt10k-labels-idx1-ubyte.gzi�r>r)�object�train�
validation�testrr1rArB)�	train_dirrTr>rrhZ	data_setsri�TRAIN_IMAGES�TRAIN_LABELS�TEST_IMAGES�TEST_LABELSZVALIDATION_SIZE�
local_file�train_images�train_labels�test_images�test_labels�validation_images�validation_labelsr)rr>r�read_data_sets�s<	rz)�__doc__�
__future__rrrr&r�tensorflow.python.platform�
tensorflowr�	six.movesrrrErrr#r1r=rArjrBrHrzrrrr�<module>s$
G