"""
使用簡單的前饋神經網路feed-forward neural network來訓練並預測mnist資料集,用我自行產生的csv檔來當做資料來源.
see tensorflow-1.6.0/tensorflow/examples/tutorials/mnist/fully_connected_feed.py
"""
import tensorflow as tf
from mnist import mnist_dataset as md
from mnist import mnist_core as mnist
#from tensorflow.examples.tutorials.mnist import mnist
from six.moves import xrange
import time
import os

def placeholder_inputs(batch_size):
  """建立兩個tf placeholder來存於輸入值及標籤.

  These placeholders are used as inputs by the rest of the model building
  code and will be fed from the downloaded data in the .run() loop, below.

  Args:
    batch_size: The batch size will be baked into both placeholders.

  Returns:
    images_placeholder: Images placeholder.
    labels_placeholder: Labels placeholder.
  """
  images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, md.IMAGE_PIXELS))
  labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))
  return images_placeholder, labels_placeholder

def fill_feed_dict(data_set, batch_size, images_pl, labels_pl):
  """Fills the feed_dict for training the given step.

  A feed_dict takes the form of:
  feed_dict = {
      <placeholder>: <tensor of values to be passed for placeholder>,
      ....
  }

  Args:
    data_set: The set of images and labels, from input_data.read_data_sets()
    batch_size: batch size
    images_pl: The images placeholder, from placeholder_inputs().
    labels_pl: The labels placeholder, from placeholder_inputs().

  Returns:
    feed_dict: The feed dictionary mapping from placeholders to values.
  """
  images_feed, labels_feed = data_set.next_batch(batch_size)
  feed_dict = {
      images_pl: images_feed,
      labels_pl: labels_feed,
  }
  return feed_dict

def do_eval(sess,
            eval_correct,
            images_placeholder,
            labels_placeholder,
            data_set,
            batch_size):
  """Runs one evaluation against the full epoch of data.

  Args:
    sess: The session in which the model has been trained.
    eval_correct: The Tensor that returns the number of correct predictions.
    images_placeholder: The images placeholder.
    labels_placeholder: The labels placeholder.
    data_set: The set of images and labels to evaluate, from input_data.read_data_sets().
  """
  # And run one epoch of eval.
  true_count = 0  # Counts the number of correct predictions.
  steps_per_epoch = data_set.num_examples // batch_size
  num_examples = steps_per_epoch * batch_size
  for step in xrange(steps_per_epoch):
    feed_dict = fill_feed_dict(data_set,
                               batch_size,
                               images_placeholder,
                               labels_placeholder)
    true_count += sess.run(eval_correct, feed_dict=feed_dict)
  precision = float(true_count) / num_examples
  print('Num examples: %d  Num correct: %d  Precision @ 1: %0.04f' %
        (num_examples, true_count, precision))

def run_training(batch_size, hidden1, hidden2, learning_rate, log_dir, max_steps):
  """Train MNIST for a number of steps.
  Args:
      batch_size: 每之送入幾個樣本到網路中做訓練
      hidden1: 第一層隱藏層的神經元個數
      hidden2: 第二層隱藏層的神經元個數
      learning_rate: 學習速率
      log_dir: log檔案的存放目錄
      max_step： 訓練幾次,這個數字並不等於epoch數,一個step會做一次batch訓練,如果batch_size=100,max_step=200,總共只會用到20000個樣本
  """
  # Get the sets of images and labels for training, validation, and
  # test on MNIST.
  trainingSet = md.DataSet('/Users/ken/Development/java/MyJavaDojo/mnist-training-set.csv')
  testSet = md.DataSet('/Users/ken/Development/java/MyJavaDojo/mnist-test-set.csv')

  # 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(
        batch_size)

    # Build a Graph that computes predictions from the inference model.
    logits = mnist.inference(images_placeholder,
                             hidden1,
                             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 = mnist.training(loss, learning_rate)

    # Add the Op to compare the logits to the labels during evaluation.
    eval_correct = mnist.evaluation(logits, labels_placeholder)

    # Build the summary Tensor based on the TF collection of Summaries.
    summary = tf.summary.merge_all()

    # Add the variable initializer Op.
    init = tf.global_variables_initializer()

    # Create a saver for writing training checkpoints.
    saver = tf.train.Saver()

    # Create a session for running Ops on the Graph.
    sess = tf.Session()

    # Instantiate a SummaryWriter to output summaries and the Graph.
    summary_writer = tf.summary.FileWriter(log_dir, sess.graph)

    # And then after everything is built:

    # Run the Op to initialize the variables.
    sess.run(init)

    # Start the training loop.
    for step in xrange(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(trainingSet,
                                 batch_size,
                                 images_placeholder,
                                 labels_placeholder)

      # Run one step of the model.  The return values are the activations
      # from the `train_op` (which is discarded) and the `loss` Op.  To
      # inspect the values of your Ops or variables, you may include them
      # in the list passed to sess.run() and the value tensors will be
      # returned in the tuple from the call.
      _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

      duration = time.time() - start_time

      # Write the summaries and print an overview fairly often.
      if step % 100 == 0:
        # Print status to stdout.
        print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration))
        # Update the events file.
        summary_str = sess.run(summary, feed_dict=feed_dict)
        summary_writer.add_summary(summary_str, step)
        summary_writer.flush()

      # Save a checkpoint and evaluate the model periodically.
      if (step + 1) % 1000 == 0 or (step + 1) == max_steps:
        checkpoint_file = os.path.join(log_dir, 'model.ckpt')
        saver.save(sess, checkpoint_file, global_step=step)
        # Evaluate against the training set.
        print('Training Data Eval:')
        do_eval(sess,
                eval_correct,
                images_placeholder,
                labels_placeholder,
                trainingSet,
                batch_size)

        # I don't have validation set
        # 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:')
        do_eval(sess,
                eval_correct,
                images_placeholder,
                labels_placeholder,
                testSet,
                batch_size)

if __name__ == '__main__':
    #不知道為什麼,這裡必須有足夠大的batch_size,loss才會顯著下降
    run_training(batch_size = 5000, hidden1 = 128, hidden2 = 32, learning_rate=0.01, log_dir='/Users/ken', max_steps=2000)
"""
使用簡單的前饋神經網路feed-forward neural network來訓練並預測mnist資料集,用我自行產生的csv檔來當做資料來源.
see tensorflow-1.6.0/tensorflow/examples/tutorials/mnist/fully_connected_feed.py
"""
import tensorflow as tf
from mnist import mnist_dataset as md
from mnist import mnist_core as mnist
#from tensorflow.examples.tutorials.mnist import mnist
from six.moves import xrange
import time
import os

def placeholder_inputs(batch_size):
  """建立兩個tf placeholder來存於輸入值及標籤.

  These placeholders are used as inputs by the rest of the model building
  code and will be fed from the downloaded data in the .run() loop, below.

  Args:
    batch_size: The batch size will be baked into both placeholders.

  Returns:
    images_placeholder: Images placeholder.
    labels_placeholder: Labels placeholder.
  """
  images_placeholder = tf.placeholder(tf.float32, shape=(batch_size, md.IMAGE_PIXELS))
  labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))
  return images_placeholder, labels_placeholder

def fill_feed_dict(data_set, batch_size, images_pl, labels_pl):
  """Fills the feed_dict for training the given step.

  A feed_dict takes the form of:
  feed_dict = {
      <placeholder>: <tensor of values to be passed for placeholder>,
      ....
  }

  Args:
    data_set: The set of images and labels, from input_data.read_data_sets()
    batch_size: batch size
    images_pl: The images placeholder, from placeholder_inputs().
    labels_pl: The labels placeholder, from placeholder_inputs().

  Returns:
    feed_dict: The feed dictionary mapping from placeholders to values.
  """
  images_feed, labels_feed = data_set.next_batch(batch_size)
  feed_dict = {
      images_pl: images_feed,
      labels_pl: labels_feed,
  }
  return feed_dict

def do_eval(sess,
            eval_correct,
            images_placeholder,
            labels_placeholder,
            data_set,
            batch_size):
  """Runs one evaluation against the full epoch of data.

  Args:
    sess: The session in which the model has been trained.
    eval_correct: The Tensor that returns the number of correct predictions.
    images_placeholder: The images placeholder.
    labels_placeholder: The labels placeholder.
    data_set: The set of images and labels to evaluate, from input_data.read_data_sets().
  """
  # And run one epoch of eval.
  true_count = 0  # Counts the number of correct predictions.
  steps_per_epoch = data_set.num_examples // batch_size
  num_examples = steps_per_epoch * batch_size
  for step in xrange(steps_per_epoch):
    feed_dict = fill_feed_dict(data_set,
                               batch_size,
                               images_placeholder,
                               labels_placeholder)
    true_count += sess.run(eval_correct, feed_dict=feed_dict)
  precision = float(true_count) / num_examples
  print('Num examples: %d  Num correct: %d  Precision @ 1: %0.04f' %
        (num_examples, true_count, precision))

def run_training(batch_size, hidden1, hidden2, learning_rate, log_dir, max_steps):
  """Train MNIST for a number of steps.
  Args:
      batch_size: 每之送入幾個樣本到網路中做訓練
      hidden1: 第一層隱藏層的神經元個數
      hidden2: 第二層隱藏層的神經元個數
      learning_rate: 學習速率
      log_dir: log檔案的存放目錄
      max_step： 訓練幾次,這個數字並不等於epoch數,一個step會做一次batch訓練,如果batch_size=100,max_step=200,總共只會用到20000個樣本
  """
  # Get the sets of images and labels for training, validation, and
  # test on MNIST.
  trainingSet = md.DataSet('/Users/ken/Development/java/MyJavaDojo/mnist-training-set.csv')
  testSet = md.DataSet('/Users/ken/Development/java/MyJavaDojo/mnist-test-set.csv')

  # 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(
        batch_size)

    # Build a Graph that computes predictions from the inference model.
    logits = mnist.inference(images_placeholder,
                             hidden1,
                             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 = mnist.training(loss, learning_rate)

    # Add the Op to compare the logits to the labels during evaluation.
    eval_correct = mnist.evaluation(logits, labels_placeholder)

    # Build the summary Tensor based on the TF collection of Summaries.
    summary = tf.summary.merge_all()

    # Add the variable initializer Op.
    init = tf.global_variables_initializer()

    # Create a saver for writing training checkpoints.
    saver = tf.train.Saver()

    # Create a session for running Ops on the Graph.
    sess = tf.Session()

    # Instantiate a SummaryWriter to output summaries and the Graph.
    summary_writer = tf.summary.FileWriter(log_dir, sess.graph)

    # And then after everything is built:

    # Run the Op to initialize the variables.
    sess.run(init)

    # Start the training loop.
    for step in xrange(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(trainingSet,
                                 batch_size,
                                 images_placeholder,
                                 labels_placeholder)

      # Run one step of the model.  The return values are the activations
      # from the `train_op` (which is discarded) and the `loss` Op.  To
      # inspect the values of your Ops or variables, you may include them
      # in the list passed to sess.run() and the value tensors will be
      # returned in the tuple from the call.
      _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

      duration = time.time() - start_time

      # Write the summaries and print an overview fairly often.
      if step % 100 == 0:
        # Print status to stdout.
        print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration))
        # Update the events file.
        summary_str = sess.run(summary, feed_dict=feed_dict)
        summary_writer.add_summary(summary_str, step)
        summary_writer.flush()

      # Save a checkpoint and evaluate the model periodically.
      if (step + 1) % 1000 == 0 or (step + 1) == max_steps:
        checkpoint_file = os.path.join(log_dir, 'model.ckpt')
        saver.save(sess, checkpoint_file, global_step=step)
        # Evaluate against the training set.
        print('Training Data Eval:')
        do_eval(sess,
                eval_correct,
                images_placeholder,
                labels_placeholder,
                trainingSet,
                batch_size)

        # I don't have validation set
        # 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:')
        do_eval(sess,
                eval_correct,
                images_placeholder,
                labels_placeholder,
                testSet,
                batch_size)

if __name__ == '__main__':
    #不知道為什麼,這裡必須有足夠大的batch_size,loss才會顯著下降
    run_training(batch_size = 5000, hidden1 = 128, hidden2 = 32, learning_rate=0.01, log_dir='/Users/ken', max_steps=2000)
使用簡單的前饋神經網路feed-forward neural network來訓練並預測mnist資料集,用我自行產生的csv檔來當做資料來源. see tensorflow-1.6.0/tensorflow/examples/tutorials/mnist/fully_connected_feed.py #from tensorflow.examples.tutorials.mnist import mnist 建立兩個tf placeholder來存於輸入值及標籤.    These placeholders are used as inputs by the rest of the model building   code and will be fed from the downloaded data in the .run() loop, below.    Args:     batch_size: The batch size will be baked into both placeholders.    Returns:     images_placeholder: Images placeholder.     labels_placeholder: Labels placeholder. Fills the feed_dict for training the given step.    A feed_dict takes the form of:   feed_dict = {       <placeholder>: <tensor of values to be passed for placeholder>,       ....   }    Args:     data_set: The set of images and labels, from input_data.read_data_sets()     batch_size: batch size     images_pl: The images placeholder, from placeholder_inputs().     labels_pl: The labels placeholder, from placeholder_inputs().    Returns:     feed_dict: The feed dictionary mapping from placeholders to values. Runs one evaluation against the full epoch of data.    Args:     sess: The session in which the model has been trained.     eval_correct: The Tensor that returns the number of correct predictions.     images_placeholder: The images placeholder.     labels_placeholder: The labels placeholder.     data_set: The set of images and labels to evaluate, from input_data.read_data_sets(). # And run one epoch of eval. # Counts the number of correct predictions. Train MNIST for a number of steps.   Args:       batch_size: 每之送入幾個樣本到網路中做訓練       hidden1: 第一層隱藏層的神經元個數       hidden2: 第二層隱藏層的神經元個數       learning_rate: 學習速率       log_dir: log檔案的存放目錄       max_step： 訓練幾次,這個數字並不等於epoch數,一個step會做一次batch訓練,如果batch_size=100,max_step=200,總共只會用到20000個樣本 # Get the sets of images and labels for training, validation, and # test on MNIST. # Tell TensorFlow that the model will be built into the default Graph. # Generate placeholders for the images and labels. # Build a Graph that computes predictions from the inference model. # Add to the Graph the Ops for loss calculation. # Add to the Graph the Ops that calculate and apply gradients. # Add the Op to compare the logits to the labels during evaluation. # Build the summary Tensor based on the TF collection of Summaries. # Add the variable initializer Op. # Create a saver for writing training checkpoints. # Create a session for running Ops on the Graph. # Instantiate a SummaryWriter to output summaries and the Graph. # And then after everything is built: # Run the Op to initialize the variables. # Start the training loop. # Fill a feed dictionary with the actual set of images and labels # for this particular training step. # Run one step of the model.  The return values are the activations # from the `train_op` (which is discarded) and the `loss` Op.  To # inspect the values of your Ops or variables, you may include them # in the list passed to sess.run() and the value tensors will be # returned in the tuple from the call. # Write the summaries and print an overview fairly often. # Print status to stdout. # Update the events file. # Save a checkpoint and evaluate the model periodically. # Evaluate against the training set. # I don't have validation set # 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. #不知道為什麼,這裡必須有足夠大的batch_size,loss才會顯著下降