ó
ÉÈ÷Xc           @` s[  d  Z  d d l m Z d d l m Z d d l m Z d d l Z d d l Z d d l Z d d l Z	 d d l
 Z
 d d l m Z d d l m Z d d l	 Z d Z d	 Z d
 Z d Z d Z d Z d Z d Z d Z d Z e j j j d e d ƒ e j j j Z d „  Z d „  Z d „  Z  d „  Z! d „  Z" d d „ Z$ e% d k rWe j j& ƒ  n  d S(   s+  Simple, end-to-end, LeNet-5-like convolutional MNIST model example.

This should achieve a test error of 0.8%. Please keep this model as simple and
linear as possible, it is meant as a tutorial for simple convolutional models.
Run with --self_test on the command line to exectute a short self-test.
i    (   t   absolute_import(   t   division(   t   print_functionN(   t   urllib(   t   xranges!   http://yann.lecun.com/exdb/mnist/t   datai   i   iÿ   i
   iˆ  i® i@   t	   self_tests   True if running a self test.c         C` s”   t  j j t ƒ s" t  j t ƒ n  t  j j t |  ƒ } t  j j | ƒ s t j j t	 |  | ƒ \ } } t  j
 | ƒ } t d |  | j d ƒ n  | S(   s@   Download the data from Yann's website, unless it's already here.s   Succesfully downloadeds   bytes.(   t   ost   patht   existst   WORK_DIRECTORYt   mkdirt   joinR   t   requestt   urlretrievet
   SOURCE_URLt   statt   printt   st_size(   t   filenamet   filepatht   _t   statinfo(    (    sm   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/mnist/convolutional.pyt   maybe_download5   s    c      	   C` sŸ   t  d |  ƒ t j |  ƒ } } | j d ƒ | j t t | ƒ } t j | d t j ƒj t j	 ƒ } | t
 d t
 } | j | t t d ƒ } | SWd QXd S(   s   Extract the images into a 4D tensor [image index, y, x, channels].

  Values are rescaled from [0, 255] down to [-0.5, 0.5].
  t
   Extractingi   t   dtypeg       @i   N(   R   t   gzipt   opent   readt
   IMAGE_SIZEt   numpyt
   frombuffert   uint8t   astypet   float32t   PIXEL_DEPTHt   reshape(   R   t
   num_imagest
   bytestreamt   bufR   (    (    sm   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/mnist/convolutional.pyt   extract_dataA   s    $c         C` sŒ   t  d |  ƒ t j |  ƒ ? } | j d ƒ | j d | ƒ } t j | d t j ƒ} Wd QXt j t ƒ | d d … d f k j
 t j ƒ S(   sB   Extract the labels into a 1-hot matrix [image index, label index].R   i   i   R   N(   R   R   R   R   R   R   R    t   aranget
   NUM_LABELSt   NoneR!   R"   (   R   R%   R&   R'   t   labels(    (    sm   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/mnist/convolutional.pyt   extract_labelsP   s    c         C` s©   t  j d |  t t t f d t  j ƒ } t  j d |  t f d t  j ƒ } xT t |  ƒ D]F } | d } | d | | d d … d d … d f <d | | | f <qU W| | f S(   s=   Generate a fake dataset that matches the dimensions of MNIST.t   shapeR   i   g      à?Ni    g      ð?(   R   t   ndarrayR   t   NUM_CHANNELSR"   t   zerosR*   R   (   R%   R   R,   t   imaget   label(    (    sm   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/mnist/convolutional.pyt	   fake_data[   s    	!
&c         C` s>   d d t  j t  j |  d ƒ t  j | d ƒ k ƒ |  j d S(   sB   Return the error rate based on dense predictions and 1-hot labels.g      Y@i   i    (   R   t   sumt   argmaxR.   (   t   predictionsR,   (    (    sm   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/mnist/convolutional.pyt
   error_rateh   s    .c   &      ` sè  t  j rR t d ƒ t d ƒ \ } } t d ƒ \ } } t d ƒ \ } } d } nâ t d ƒ } t d ƒ }	 t d ƒ }
 t d ƒ } t | d	 ƒ } t |	 d	 ƒ } t |
 d
 ƒ } t | d
 ƒ } | d  t … d  d  … d  d  … d  d  … f } | t  } | t d  … d  d  … d  d  … d  d  … f } | t } t } | j	 d } t
 j t
 j d t t t t f ƒ} t
 j t
 j d t t f ƒ} t
 j | ƒ } t
 j | ƒ } t
 j t
 j d d t d g d d d t ƒƒ ‰ t
 j t
 j d g ƒ ƒ ‰  t
 j t
 j d d d d g d d d t ƒƒ ‰ t
 j t
 j d d d g ƒƒ ‰ t
 j t
 j t d t d d d g d d d t ƒƒ ‰ t
 j t
 j d d d g ƒƒ ‰ t
 j t
 j d t g d d d t ƒƒ ‰ t
 j t
 j d d t g ƒƒ ‰ t ‡  ‡ ‡ ‡ ‡ ‡ ‡ ‡ f d † } | | t ƒ } t
 j t
 j j | | ƒ ƒ } t
 j j ˆ ƒ t
 j j ˆ ƒ t
 j j ˆ ƒ t
 j j ˆ ƒ } | d | 7} t
 j d ƒ } t
 j j d | t | d d t ƒ} t
 j j | d ƒ j | d | ƒ} t
 j j  | ƒ } t
 j j  | | ƒ ƒ } t
 j j  | | ƒ ƒ } t
 j! ƒ  µ} t
 j" ƒ  j# ƒ  t d ƒ x6t$ | | t ƒ D] } | t | t } | | | t … d  d  … d  d  … d  d  … f } | | | t !} i | | 6| | 6}  | j# | | | | g d |  ƒ\ }! }" }# }$ | d d k r_t d t% | ƒ t | ƒ t d  |" |# f ƒ t d! t& |$ | ƒ ƒ t d" t& | j' ƒ  | ƒ ƒ t( j) j* ƒ  q_q_Wt& | j' ƒ  | ƒ }% t d# |% ƒ t  j rÞt d$ |% ƒ |% d% k sÞt+ d& |% f ƒ ‚ n  Wd  QXd  S('   Ns   Running self-test.i   i   i   s   train-images-idx3-ubyte.gzs   train-labels-idx1-ubyte.gzs   t10k-images-idx3-ubyte.gzs   t10k-labels-idx1-ubyte.gzi`ê  i'  i    R.   i   i    t   stddevgš™™™™™¹?t   seedi@   i   i   c      	   ` s°  t  j j |  ˆ d d d d d g d d ƒ} t  j j t  j j | ˆ  ƒ ƒ } t  j j | d d d d d g d d d d d g d d ƒ} t  j j | ˆ d d d d d g d d ƒ} t  j j t  j j | ˆ ƒ ƒ } t  j j | d d d d d g d d d d d g d d ƒ} | j ƒ  j ƒ  } t  j | | d | d | d | d g ƒ } t  j j t  j	 | ˆ ƒ ˆ ƒ } | rœt  j j
 | d	 d
 t ƒ} n  t  j	 | ˆ ƒ ˆ S(   s   The Model definition.t   stridesi   t   paddingt   SAMEt   ksizei   i    i   g      à?R:   (   t   tft   nnt   conv2dt   relut   bias_addt   max_poolt	   get_shapet   as_listR$   t   matmult   dropoutt   SEED(   R   t   traint   convRB   t   poolt
   pool_shapeR$   t   hidden(   t   conv1_biasest   conv1_weightst   conv2_biasest   conv2_weightst
   fc1_biasest   fc1_weightst
   fc2_biasest   fc2_weights(    sm   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/mnist/convolutional.pyt   model´   s4    	!		!	'"gü©ñÒMb@?g{®Gáz„?gffffffî?t	   staircasegÍÌÌÌÌÌì?t   global_steps   Initialized!t	   feed_dictid   s
   Epoch %.2fs)   Minibatch loss: %.3f, learning rate: %.6fs   Minibatch error: %.1f%%s   Validation error: %.1f%%s   Test error: %.1f%%t
   test_errorg        s!   expected 0.0 test_error, got %.2f(,   t   FLAGSR   R   R4   R   R(   R-   t   VALIDATION_SIZEt
   NUM_EPOCHSR.   R?   t   placeholderR"   t
   BATCH_SIZER   R0   R*   t   constantt   Variablet   truncated_normalRI   R1   t   Falset   Truet   reduce_meanR@   t!   softmax_cross_entropy_with_logitst   l2_lossRJ   t   exponential_decayt   MomentumOptimizert   minimizet   softmaxt   Sessiont   initialize_all_variablest   runR   t   floatR8   t   evalt   syst   stdoutt   flusht   AssertionError(&   t   argvt
   train_datat   train_labelst   validation_datat   validation_labelst	   test_datat   test_labelst
   num_epochst   train_data_filenamet   train_labels_filenamet   test_data_filenamet   test_labels_filenamet
   train_sizet   train_data_nodet   train_labels_nodet   validation_data_nodet   test_data_nodeRW   t   logitst   losst   regularizerst   batcht   learning_ratet	   optimizert   train_predictiont   validation_predictiont   test_predictiont   st   stept   offsett
   batch_datat   batch_labelsRZ   R   t   lt   lrR7   R[   (    (   RO   RP   RQ   RR   RS   RT   RU   RV   sm   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/mnist/convolutional.pyt   mainp   s¸    	
	.
.
	!!!'*B			
2

	t   __main__('   t   __doc__t
   __future__R    R   R   R   R   Rr   t   tensorflow.python.platformt
   tensorflowR   t	   six.movesR   R   R?   R   R
   R   R0   R#   R*   R]   RI   R`   R^   t   appt   flagst   DEFINE_booleanRd   R\   R   R(   R-   R4   R8   R+   R—   t   __name__Ro   (    (    (    sm   /tmp/pip-build-UG86a1/tensorflow/tensorflow-0.6.0.data/purelib/tensorflow/models/image/mnist/convolutional.pyt   <module>   s>   					±