parameters: ()
docstring: Train MNIST for a number of steps.
docstring_summary: Train MNIST for a number of steps.
docstring_tokens: Train MNIST for a number of steps .
function: def run_training():
    """Train MNIST for a number of steps."""
    # Get the sets of images and labels for training, validation, and
    # test on MNIST.
    train_dir = tempfile.mkdtemp()
    data_sets = input_data.read_data_sets(train_dir, FLAGS.fake_data)
  
    # Tell TensorFlow that the model will be built into the default Graph.
    with tf.Graph().as_default():
      # Generate placeholders for the images and labels.
      images_placeholder, labels_placeholder = placeholder_inputs()
  
      # Build a Graph that computes predictions from the inference model.
      logits, clustering_loss, kmeans_init, kmeans_training_op = inference(
          images_placeholder,
          FLAGS.num_clusters,
          FLAGS.hidden1,
          FLAGS.hidden2)
  
      # Add to the Graph the Ops for loss calculation.
      loss = mnist.loss(logits, labels_placeholder)
  
      # Add to the Graph the Ops that calculate and apply gradients.
      train_op = tf.group(mnist.training(loss, FLAGS.learning_rate),
                          kmeans_training_op)
  
      # Add the Op to compare the logits to the labels during evaluation.
      eval_correct = mnist.evaluation(logits, labels_placeholder)
  
      # Add the variable initializer Op.
      init = tf.global_variables_initializer()
  
      # Create a session for running Ops on the Graph.
      sess = tf.Session()
  
      # Run the Op to initialize the variables.
      sess.run(init)
  
      feed_dict = fill_feed_dict(data_sets.train,
                                 images_placeholder,
                                 labels_placeholder,
                                 batch_size=max(FLAGS.batch_size, 5000))
      # Run the Op to initialize the clusters.
      sess.run(kmeans_init, feed_dict=feed_dict)
  
      # Start the training loop.
      max_test_prec = 0
      for step in xrange(FLAGS.max_steps):
        start_time = time.time()
  
        # Fill a feed dictionary with the actual set of images and labels
        # for this particular training step.
        feed_dict = fill_feed_dict(data_sets.train,
                                   images_placeholder,
                                   labels_placeholder,
                                   FLAGS.batch_size)
  
        # Run one step of the model.
        _, loss_value, clustering_loss_value = sess.run([train_op,
                                                         loss,
                                                         clustering_loss],
                                                        feed_dict=feed_dict)
  
        duration = time.time() - start_time
        if step % 100 == 0:
          # Print status to stdout.
          print('Step %d: loss = %.2f, clustering_loss = %.2f (%.3f sec)' % (
              step, loss_value, clustering_loss_value, duration))
  
        # Save a checkpoint and evaluate the model periodically.
        if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps:
          # Evaluate against the training set.
          print('Training Data Eval:')
          do_eval(sess,
                  eval_correct,
                  images_placeholder,
                  labels_placeholder,
                  data_sets.train)
          # Evaluate against the validation set.
          print('Validation Data Eval:')
          do_eval(sess,
                  eval_correct,
                  images_placeholder,
                  labels_placeholder,
                  data_sets.validation)
          # Evaluate against the test set.
          print('Test Data Eval:')
          test_prec = do_eval(sess,
                              eval_correct,
                              images_placeholder,
                              labels_placeholder,
                              data_sets.test)
          max_test_prec = max(max_test_prec, test_prec)
      return max_test_prec
function_tokens: def run_training ( ) : # Get the sets of images and labels for training, validation, and # test on MNIST. train_dir = tempfile . mkdtemp ( ) data_sets = input_data . read_data_sets ( train_dir , FLAGS . fake_data ) # Tell TensorFlow that the model will be built into the default Graph. with tf . Graph ( ) . as_default ( ) : # Generate placeholders for the images and labels. images_placeholder , labels_placeholder = placeholder_inputs ( ) # Build a Graph that computes predictions from the inference model. logits , clustering_loss , kmeans_init , kmeans_training_op = inference ( images_placeholder , FLAGS . num_clusters , FLAGS . hidden1 , FLAGS . hidden2 ) # Add to the Graph the Ops for loss calculation. loss = mnist . loss ( logits , labels_placeholder ) # Add to the Graph the Ops that calculate and apply gradients. train_op = tf . group ( mnist . training ( loss , FLAGS . learning_rate ) , kmeans_training_op ) # Add the Op to compare the logits to the labels during evaluation. eval_correct = mnist . evaluation ( logits , labels_placeholder ) # Add the variable initializer Op. init = tf . global_variables_initializer ( ) # Create a session for running Ops on the Graph. sess = tf . Session ( ) # Run the Op to initialize the variables. sess . run ( init ) feed_dict = fill_feed_dict ( data_sets . train , images_placeholder , labels_placeholder , batch_size = max ( FLAGS . batch_size , 5000 ) ) # Run the Op to initialize the clusters. sess . run ( kmeans_init , feed_dict = feed_dict ) # Start the training loop. max_test_prec = 0 for step in xrange ( FLAGS . max_steps ) : start_time = time . time ( ) # Fill a feed dictionary with the actual set of images and labels # for this particular training step. feed_dict = fill_feed_dict ( data_sets . train , images_placeholder , labels_placeholder , FLAGS . batch_size ) # Run one step of the model. _ , loss_value , clustering_loss_value = sess . run ( [ train_op , loss , clustering_loss ] , feed_dict = feed_dict ) duration = time . time ( ) - start_time if step % 100 == 0 : # Print status to stdout. print ( 'Step %d: loss = %.2f, clustering_loss = %.2f (%.3f sec)' % ( step , loss_value , clustering_loss_value , duration ) ) # Save a checkpoint and evaluate the model periodically. if ( step + 1 ) % 1000 == 0 or ( step + 1 ) == FLAGS . max_steps : # Evaluate against the training set. print ( 'Training Data Eval:' ) do_eval ( sess , eval_correct , images_placeholder , labels_placeholder , data_sets . train ) # Evaluate against the validation set. print ( 'Validation Data Eval:' ) do_eval ( sess , eval_correct , images_placeholder , labels_placeholder , data_sets . validation ) # Evaluate against the test set. print ( 'Test Data Eval:' ) test_prec = do_eval ( sess , eval_correct , images_placeholder , labels_placeholder , data_sets . test ) max_test_prec = max ( max_test_prec , test_prec ) return max_test_prec