-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtensorflow_classification.py
More file actions
34 lines (30 loc) · 1.35 KB
/
tensorflow_classification.py
File metadata and controls
34 lines (30 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
This script works on TensorFlow 1.x :)
"""
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)
# Train
training_iteration = 10000
batch_size = 100
display_step = 50
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
for iteration in range(training_iteration):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
train_step.run({x: batch_xs, y_: batch_ys})
if iteration % display_step == 0:
print('Iteration: %5d | Training accuracy: %.6f' %
(iteration + 1, sess.run(accuracy, feed_dict={x: batch_xs, y_: batch_ys})))
print('Test accuracy: %.6f' % sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))