12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- '''
- Graph and Loss visualization using Tensorboard.
- This example is using the MNIST database of handwritten digits
- (http://yann.lecun.com/exdb/mnist/)
- Author: Aymeric Damien
- Project: https://github.com/aymericdamien/TensorFlow-Examples/
- '''
- import tensorflow as tf
- # Import MINST data
- from tensorflow.examples.tutorials.mnist import input_data
- mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
- # Parameters
- learning_rate = 0.01
- training_epochs = 25
- batch_size = 100
- display_step = 1
- logs_path = '/tmp/tensorflow_logs'
- # tf Graph Input
- # mnist data image of shape 28*28=784
- x = tf.placeholder(tf.float32, [None, 784], name='InputData')
- # 0-9 digits recognition => 10 classes
- y = tf.placeholder(tf.float32, [None, 10], name='LabelData')
- # Set model weights
- W = tf.Variable(tf.zeros([784, 10]), name='Weights')
- b = tf.Variable(tf.zeros([10]), name='Bias')
- # Construct model and encapsulating all ops into scopes, making
- # Tensorboard's Graph visualization more convenient
- with tf.name_scope('Model'):
- # Model
- pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax
- with tf.name_scope('Loss'):
- # Minimize error using cross entropy
- cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred), reduction_indices=1))
- with tf.name_scope('SGD'):
- # Gradient Descent
- optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
- with tf.name_scope('Accuracy'):
- # Accuracy
- acc = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
- acc = tf.reduce_mean(tf.cast(acc, tf.float32))
- # Initializing the variables
- init = tf.initialize_all_variables()
- # Create a summary to monitor cost tensor
- tf.scalar_summary("loss", cost)
- # Create a summary to monitor accuracy tensor
- tf.scalar_summary("accuracy", acc)
- # Merge all summaries into a single op
- merged_summary_op = tf.merge_all_summaries()
- # Launch the graph
- with tf.Session() as sess:
- sess.run(init)
- # op to write logs to Tensorboard
- summary_writer = tf.train.SummaryWriter(logs_path)
- # Training cycle
- for epoch in range(training_epochs):
- avg_cost = 0.
- total_batch = int(mnist.train.num_examples/batch_size)
- # Loop over all batches
- for i in range(total_batch):
- batch_xs, batch_ys = mnist.train.next_batch(batch_size)
- # Run optimization op (backprop), cost op (to get loss value)
- # and summary nodes
- _, c, summary = sess.run([optimizer, cost, merged_summary_op],
- feed_dict={x: batch_xs, y: batch_ys})
- # Write logs at every iteration
- summary_writer.add_summary(summary, epoch * total_batch + i)
- # Compute average loss
- avg_cost += c / total_batch
- # Display logs per epoch step
- if (epoch+1) % display_step == 0:
- print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)
- print "Optimization Finished!"
- # Test model
- # Calculate accuracy
- print "Accuracy:", acc.eval({x: mnist.test.images, y: mnist.test.labels})
- print "Run the command line:\n" \
- "--> tensorboard --logdir=/tmp/tensorflow_logs " \
- "\nThen open http://0.0.0.0:6006/ into your web browser"
|