Create a gist now

Instantly share code, notes, and snippets.

import tensorflow as tf
import numpy as np
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
mnist = read_data_sets("MNIST_data/", one_hot=True)
with tf.name_scope("input_x") as scope:
x = tf.placeholder("float", [None, 784])
with tf.name_scope("label_y_") as scope:
y_ = tf.placeholder("float",[None, 10])
W = tf.Variable(tf.zeros([784, 10]),name="Weights")
b = tf.Variable(tf.zeros([10]),name="bias")
with tf.name_scope("output_y") as scope:
y = tf.nn.softmax(tf.matmul(x, W) + b)
w_hist = tf.summary.histogram("weights", W)
b_hist = tf.summary.histogram("biases", b)
y_hist = tf.summary.histogram("y", y)
with tf.name_scope("cross_entropy") as scope:
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
cross_entropy_summary = tf.summary.scalar("cross_entropy", cross_entropy)
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
with tf.name_scope("accuracy") as scope:
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
accuracy_summary = tf.summary.scalar("accuracy", accuracy)
sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("log", sess.graph)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
if i % 10 == 0:
summary_str =,feed_dict={x: batch_xs, y_: batch_ys})
writer.add_summary(summary_str, i)
else:, feed_dict={x: batch_xs, y_: batch_ys})
print (, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment