Tensorflow逻辑回归 分类

codes

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

# Parameters
learning_rate = 0.5
training_epoch = 25
batch_size = 100
display_step = 1

# placeholder input output
X = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])

# hyperparameters
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
pred = tf.nn.softmax(tf.matmul(X,W)+b)

# cost function and optimizer
# cost function is cross-entropy
cost = tf.reduce_mean(-tf.reduce_sum(y * tf.log(pred),reduction_indices=1))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

#initializer
init = tf.global_variables_initializer()

# Session
with tf.Session() as sess:
    sess.run(init)

    #Epoch
    for epoch in range(training_epoch):
        avg_cost = 0
        total_batch = int(mnist.train.num_examples/batch_size)#how many batches
        for i in range(total_batch):
            batch_xs,batch_ys = mnist.train.next_batch(batch_size)
            _,c = sess.run([optimizer,cost],feed_dict={X:batch_xs,y:batch_ys})

            avg_cost += c / total_batch #everage costs

        # output cost value
        if (epoch+1) % display_step == 0:
            print('Epoch:%4d' % (epoch+1),'cost:{:.9f}'.format(avg_cost))

    print('Optimization Finished!')

    # Accuracy
    correct_prediction = tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

    print('Accuracy:',sess.run(accuracy,feed_dict={X:mnist.test.images,y:mnist.test.labels}))

Output:

Accuracy: 0.9135

References

[1] 《TensorFlow实战》黄文坚 唐源 著

你可能感兴趣的:(Tensorflow逻辑回归 分类)