σ
UΎχ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 Z d Z	 e	 e	 Z
 d   Z d	   Z d
   Z d   Z d S(   sΛ  Builds the MNIST network.

Implements the inference/loss/training pattern for model building.

1. inference() - Builds the model as far as is required for running the network
forward to make predictions.
2. loss() - Adds to the inference model the layers required to generate loss.
3. training() - Adds to the loss model the Ops required to generate and
apply gradients.

This file is used by the various "fully_connected_*.py" files and not meant to
be run.
i    (   t   absolute_import(   t   division(   t   print_functionNi
   i   c         C` sΊ  t  j d   t  j t  j t | g d d t j t t   d d } t  j t  j | g  d d } t  j	 j
 t  j |  |  |  } Wd QXt  j d   t  j t  j | | g d d t j t |   d d } t  j t  j | g  d d } t  j	 j
 t  j | |  |  } Wd QXt  j d	  y t  j t  j | t g d d t j t |   d d } t  j t  j t g  d d } t  j | |  | } Wd QX| S(
   s)  Build the MNIST model up to where it may be used for inference.

  Args:
    images: Images placeholder, from inputs().
    hidden1_units: Size of the first hidden layer.
    hidden2_units: Size of the second hidden layer.

  Returns:
    softmax_linear: Output tensor with the computed logits.
  t   hidden1t   stddevg      π?t   namet   weightst   biasesNt   hidden2t   softmax_linear(   t   tft
   name_scopet   Variablet   truncated_normalt   IMAGE_PIXELSt   matht   sqrtt   floatt   zerost   nnt   relut   matmult   NUM_CLASSES(   t   imagest   hidden1_unitst   hidden2_unitsR   R   R   R   t   logits(    (    sk   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/examples/tutorials/mnist/mnist.pyt	   inference-   s2    		(		(		c         C` sC   t  j |  } t  j j d | d |  d d  } t  j | d d S(   sί   Calculates the loss from the logits and the labels.

  Args:
    logits: Logits tensor, float - [batch_size, NUM_CLASSES].
    labels: Labels tensor, int32 - [batch_size].

  Returns:
    loss: Loss tensor of type float.
  t   labelsR   R   t   xentropyt   xentropy_mean(   R
   t   to_int64R   t(   sparse_softmax_cross_entropy_with_logitst   reduce_mean(   R   R   t   cross_entropy(    (    sk   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/examples/tutorials/mnist/mnist.pyt   lossV   s    
c         C` sY   t  j j d |   t  j j |  } t  j d d d d t } | j |  d | } | S(   sΏ  Sets up the training Ops.

  Creates a summarizer to track the loss over time in TensorBoard.

  Creates an optimizer and applies the gradients to all trainable variables.

  The Op returned by this function is what must be passed to the
  `sess.run()` call to cause the model to train.

  Args:
    loss: Loss tensor, from loss().
    learning_rate: The learning rate to use for gradient descent.

  Returns:
    train_op: The Op for training.
  R#   i    R   t   global_stept	   trainable(   R
   t   summaryt   scalart   traint   GradientDescentOptimizerR   t   Falset   minimize(   R#   t   learning_ratet	   optimizerR$   t   train_op(    (    sk   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/examples/tutorials/mnist/mnist.pyt   trainingf   s
    c         C` s4   t  j j |  | d  } t  j t  j | t  j   S(   s`  Evaluate the quality of the logits at predicting the label.

  Args:
    logits: Logits tensor, float - [batch_size, NUM_CLASSES].
    labels: Labels tensor, int32 - [batch_size], with values in the
      range [0, NUM_CLASSES).

  Returns:
    A scalar int32 tensor with the number of examples (out of batch_size)
    that were predicted correctly.
  i   (   R
   R   t   in_top_kt
   reduce_sumt   castt   int32(   R   R   t   correct(    (    sk   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/examples/tutorials/mnist/mnist.pyt
   evaluation   s    (   t   __doc__t
   __future__R    R   R   R   t
   tensorflowR
   R   t
   IMAGE_SIZER   R   R#   R/   R5   (    (    (    sk   /tmp/pip-build-h1VYrz/tensorflow/tensorflow-1.0.1.data/purelib/tensorflow/examples/tutorials/mnist/mnist.pyt   <module>   s   
	)		