# python
import tensorflow as tf
import input_data
if __name__ == "__main__":
# Logistic Regression
# use sigmoid
# Input
# MNIST
# Cross Entropy
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
learning_rate = 0.01
x = tf.placeholder(tf.float32, shape=(None, 28 * 28))
y = tf.placeholder(tf.float32, shape=(None, 10))
# x = n * (28 * 28)
# weight = (28 * 28) * 10
weight = tf.Variable(tf.zeros((28 * 28, 10)))
bias = tf.Variable(tf.zeros((10,)))
# h = n * 10
h = tf.sigmoid(tf.matmul(x, weight) + bias)
cost = tf.reduce_mean(-(tf.reduce_sum(y * tf.log(h), 1) + tf.reduce_sum((1. -y) * tf.log(1. -h), 1)))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
init = tf.initialize_all_variables()
batch_size = 100
display_step = 1
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(10):
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)
# Fit training using batch data
sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
# Compute average loss
avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})/total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print( "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))
# Test model
correct_prediction = tf.equal(tf.argmax(h, 1), tf.argmax(y, 1))
# Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))