```python import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data ``` ```python mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) ``` ```python with tf.name_scope("input_X"): x = tf.placeholder(tf.float32, [None, 784]) ``` ```python with tf.name_scope("Weight_and_bias"): W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) ``` ```python w_h = tf.summary.histogram("weight",W) b_h = tf.summary.histogram("bias",b) ``` ```python with tf.name_scope("Prediction_y"): y = tf.nn.softmax(tf.matmul(x, W) + b) ``` ```python with tf.name_scope("Actual_y"): y_ = tf.placeholder(tf.float32, [None, 10]) ``` ```python with tf.name_scope("Cross_Entropy"): cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) tf.summary.scalar("cost_function",cross_entropy) ``` ```python with tf.name_scope("GradientDescentOptimizer"): train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) ``` ```python with tf.name_scope("accuracy"): correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.summary.scalar("accuracy",accuracy) ``` ```python config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.2 sess = tf.Session(config=config) ``` ```python init = tf.global_variables_initializer() sess.run(init) ``` ```python merged_summary_op = tf.summary.merge_all() writer = tf.summary.FileWriter("output", sess.graph) ``` ```python for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) result = sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) summary_val = sess.run(merged_summary_op, feed_dict={x: batch_xs, y_: batch_ys}) writer.add_summary(summary_val) ``` ```python print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) ``` ```python writer.close() ``` ```python ```