neural_network_eager_api.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """ Neural Network with Eager API.
  2. A 2-Hidden Layers Fully Connected Neural Network (a.k.a Multilayer Perceptron)
  3. implementation with TensorFlow's Eager API. This example is using the MNIST database
  4. of handwritten digits (http://yann.lecun.com/exdb/mnist/).
  5. This example is using TensorFlow layers, see 'neural_network_raw' example for
  6. a raw implementation with variables.
  7. Links:
  8. [MNIST Dataset](http://yann.lecun.com/exdb/mnist/).
  9. Author: Aymeric Damien
  10. Project: https://github.com/aymericdamien/TensorFlow-Examples/
  11. """
  12. from __future__ import print_function
  13. import tensorflow as tf
  14. # Set Eager API
  15. tf.enable_eager_execution()
  16. tfe = tf.contrib.eager
  17. # Import MNIST data
  18. from tensorflow.examples.tutorials.mnist import input_data
  19. mnist = input_data.read_data_sets("/tmp/data/", one_hot=False)
  20. # Parameters
  21. learning_rate = 0.001
  22. num_steps = 1000
  23. batch_size = 128
  24. display_step = 100
  25. # Network Parameters
  26. n_hidden_1 = 256 # 1st layer number of neurons
  27. n_hidden_2 = 256 # 2nd layer number of neurons
  28. num_input = 784 # MNIST data input (img shape: 28*28)
  29. num_classes = 10 # MNIST total classes (0-9 digits)
  30. # Using TF Dataset to split data into batches
  31. dataset = tf.data.Dataset.from_tensor_slices(
  32. (mnist.train.images, mnist.train.labels))
  33. dataset = dataset.repeat().batch(batch_size).prefetch(batch_size)
  34. dataset_iter = tfe.Iterator(dataset)
  35. # Define the neural network. To use eager API and tf.layers API together,
  36. # we must instantiate a tfe.Network class as follow:
  37. class NeuralNet(tfe.Network):
  38. def __init__(self):
  39. # Define each layer
  40. super(NeuralNet, self).__init__()
  41. # Hidden fully connected layer with 256 neurons
  42. self.layer1 = self.track_layer(
  43. tf.layers.Dense(n_hidden_1, activation=tf.nn.relu))
  44. # Hidden fully connected layer with 256 neurons
  45. self.layer2 = self.track_layer(
  46. tf.layers.Dense(n_hidden_2, activation=tf.nn.relu))
  47. # Output fully connected layer with a neuron for each class
  48. self.out_layer = self.track_layer(tf.layers.Dense(num_classes))
  49. def call(self, x):
  50. x = self.layer1(x)
  51. x = self.layer2(x)
  52. return self.out_layer(x)
  53. neural_net = NeuralNet()
  54. # Cross-Entropy loss function
  55. def loss_fn(inference_fn, inputs, labels):
  56. # Using sparse_softmax cross entropy
  57. return tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
  58. logits=inference_fn(inputs), labels=labels))
  59. # Calculate accuracy
  60. def accuracy_fn(inference_fn, inputs, labels):
  61. prediction = tf.nn.softmax(inference_fn(inputs))
  62. correct_pred = tf.equal(tf.argmax(prediction, 1), labels)
  63. return tf.reduce_mean(tf.cast(correct_pred, tf.float32))
  64. # SGD Optimizer
  65. optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
  66. # Compute gradients
  67. grad = tfe.implicit_gradients(loss_fn)
  68. # Training
  69. average_loss = 0.
  70. average_acc = 0.
  71. for step in range(num_steps):
  72. # Iterate through the dataset
  73. d = dataset_iter.next()
  74. # Images
  75. x_batch = d[0]
  76. # Labels
  77. y_batch = tf.cast(d[1], dtype=tf.int64)
  78. # Compute the batch loss
  79. batch_loss = loss_fn(neural_net, x_batch, y_batch)
  80. average_loss += batch_loss
  81. # Compute the batch accuracy
  82. batch_accuracy = accuracy_fn(neural_net, x_batch, y_batch)
  83. average_acc += batch_accuracy
  84. if step == 0:
  85. # Display the initial cost, before optimizing
  86. print("Initial loss= {:.9f}".format(average_loss))
  87. # Update the variables following gradients info
  88. optimizer.apply_gradients(grad(neural_net, x_batch, y_batch))
  89. # Display info
  90. if (step + 1) % display_step == 0 or step == 0:
  91. if step > 0:
  92. average_loss /= display_step
  93. average_acc /= display_step
  94. print("Step:", '%04d' % (step + 1), " loss=",
  95. "{:.9f}".format(average_loss), " accuracy=",
  96. "{:.4f}".format(average_acc))
  97. average_loss = 0.
  98. average_acc = 0.
  99. # Evaluate model on the test image set
  100. testX = mnist.test.images
  101. testY = mnist.test.labels
  102. test_acc = accuracy_fn(neural_net, testX, testY)
  103. print("Testset Accuracy: {:.4f}".format(test_acc))