Simple Neural Network (tf.layers/estimator api) · TensorFlow Examples (aymericdamien)

Neural Network Example

Build a 2-hidden layers fully connected neural network (a.k.a multilayer perceptron) with TensorFlow.

This example is using some of TensorFlow higher-level wrappers (tf.estimators, tf.layers, tf.metrics, …), you can check ‘neural_network_raw’ example for a raw, and more detailed TensorFlow implementation.

  • Author: Aymeric Damien
  • Project: https://github.com/aymericdamien/TensorFlow-Examples/

Neural Network Overview

nn

MNIST Dataset Overview

This example is using MNIST handwritten digits. The dataset contains 60,000 examples for training and 10,000 examples for testing. The digits have been size-normalized and centered in a fixed-size image (28×28 pixels) with values from 0 to 1. For simplicity, each image has been flattened and converted to a 1-D numpy array of 784 features (28*28).

MNIST Dataset

More info: http://yann.lecun.com/exdb/mnist/

from

__future__

import

print_function

from

tensorflow.examples.tutorials.mnist

import

input_data mnist = input_data.read_data_sets(

"/tmp/data/"

, one_hot=

False

)

import

tensorflow

as

tf

import

matplotlib.pyplot

as

plt

import

numpy

as

np
Extracting /tmp/data/train-images-idx3-ubyte.gz
Extracting /tmp/data/train-labels-idx1-ubyte.gz
Extracting /tmp/data/t10k-images-idx3-ubyte.gz
Extracting /tmp/data/t10k-labels-idx1-ubyte.gz
 
learning_rate = 

0.1

num_steps =

1000

batch_size =

128

display_step =

100

n_hidden_1 =

256

n_hidden_2 =

256

num_input =

784

num_classes =

10

 
input_fn = tf.estimator.inputs.numpy_input_fn(
    x={

'images'

: mnist.train.images}, y=mnist.train.labels, batch_size=batch_size, num_epochs=

None

, shuffle=

True

)
 

def

neural_net

(x_dict)

: x = x_dict[

'images'

] layer_1 = tf.layers.dense(x, n_hidden_1) layer_2 = tf.layers.dense(layer_1, n_hidden_2) out_layer = tf.layers.dense(layer_2, num_classes)

return

out_layer
 

def

model_fn

(features, labels, mode)

: logits = neural_net(features) pred_classes = tf.argmax(logits, axis=

1

) pred_probas = tf.nn.softmax(logits)

if

mode == tf.estimator.ModeKeys.PREDICT:

return

tf.estimator.EstimatorSpec(mode, predictions=pred_classes) loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( logits=logits, labels=tf.cast(labels, dtype=tf.int32))) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step()) acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes) estim_specs = tf.estimator.EstimatorSpec( mode=mode, predictions=pred_classes, loss=loss_op, train_op=train_op, eval_metric_ops={

'accuracy'

: acc_op})

return

estim_specs
 
model = tf.estimator.Estimator(model_fn)
INFO:tensorflow:Using default config.
WARNING:tensorflow:Using temporary folder as model directory: /tmp/tmpu7vjLA
INFO:tensorflow:Using config: {'_save_checkpoints_secs': 600, '_session_config': None, '_keep_checkpoint_max': 5, '_tf_random_seed': 1, '_keep_checkpoint_every_n_hours': 10000, '_log_step_count_steps': 100, '_save_checkpoints_steps': None, '_model_dir': '/tmp/tmpu7vjLA', '_save_summary_steps': 100}
 
model.train(input_fn, steps=num_steps)
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /tmp/tmpu7vjLA/model.ckpt.
INFO:tensorflow:loss = 2.44919, step = 1
INFO:tensorflow:global_step/sec: 602.544
INFO:tensorflow:loss = 0.344767, step = 101 (0.167 sec)
INFO:tensorflow:global_step/sec: 618.839
INFO:tensorflow:loss = 0.277633, step = 201 (0.162 sec)
INFO:tensorflow:global_step/sec: 626.418
INFO:tensorflow:loss = 0.407796, step = 301 (0.160 sec)
INFO:tensorflow:global_step/sec: 624.765
INFO:tensorflow:loss = 0.376889, step = 401 (0.160 sec)
INFO:tensorflow:global_step/sec: 624.091
INFO:tensorflow:loss = 0.319697, step = 501 (0.160 sec)
INFO:tensorflow:global_step/sec: 616.907
INFO:tensorflow:loss = 0.39049, step = 601 (0.162 sec)
INFO:tensorflow:global_step/sec: 623.371
INFO:tensorflow:loss = 0.336831, step = 701 (0.161 sec)
INFO:tensorflow:global_step/sec: 617.429
INFO:tensorflow:loss = 0.312776, step = 801 (0.162 sec)
INFO:tensorflow:global_step/sec: 620.825
INFO:tensorflow:loss = 0.312817, step = 901 (0.161 sec)
INFO:tensorflow:Saving checkpoints for 1000 into /tmp/tmpu7vjLA/model.ckpt.
INFO:tensorflow:Loss for final step: 0.24931.





<tensorflow.python.estimator.estimator.Estimator at 0x7f21ac597690>
 
 
input_fn = tf.estimator.inputs.numpy_input_fn(
    x={

'images'

: mnist.test.images}, y=mnist.test.labels, batch_size=batch_size, shuffle=

False

) model.evaluate(input_fn)
INFO:tensorflow:Starting evaluation at 2017-08-21-13:57:02
INFO:tensorflow:Restoring parameters from /tmp/tmpu7vjLA/model.ckpt-1000
INFO:tensorflow:Finished evaluation at 2017-08-21-13:57:02
INFO:tensorflow:Saving dict for global step 1000: accuracy = 0.9189, global_step = 1000, loss = 0.286567





{'accuracy': 0.91890001, 'global_step': 1000, 'loss': 0.28656715}
 
n_images = 

4

test_images = mnist.test.images[:n_images] input_fn = tf.estimator.inputs.numpy_input_fn( x={

'images'

: test_images}, shuffle=

False

) preds = list(model.predict(input_fn))

for

i

in

range(n_images): plt.imshow(np.reshape(test_images[i], [

28

,

28

]), cmap=

'gray'

) plt.show() print(

"Model prediction:"

, preds[i])
INFO:tensorflow:Restoring parameters from /tmp/tmpu7vjLA/model.ckpt-1000

png

Model prediction: 7

png

Model prediction: 2

png

Model prediction: 1

png

Model prediction: 0