tensorflow实现softmax分类

使用tensorflow实现softmax分类

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

import tensorflow as tf

#创建输入和输出的占位符,数据类型为float
x  = tf.placeholder("float", [None, 784])
y_ = tf.placeholder("float", [None, 10])

#创建权值变量w和b
#y = xw+b,所以w的维度为[784,10], b的维度为[,10]
w = tf.Variable(tf.random_normal([784, 10], name="weights"))
b = tf.Variable(tf.random_normal([10], name="bias"))

#softmax回归模型
#tf.multiply(x,y)是x和y的元素级别的相乘;tf.matmul为矩阵相乘
#y = tf.nn.softmax(tf.matmul(x, w)+b)
y = tf.matmul(x,w)+b

#定义损失函数,使用交叉熵作为损失
#cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y), reduction_indices=1))
#cross_entropy = -tf.reduce_sum(y_*tf.log(y), reduction_indices=1)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y)

#训练
lr_rate = 0.01
#train_step = tf.train.AdamOptimizer(lr_rate).minimize(cost)
train_step = tf.train.GradientDescentOptimizer(lr_rate).minimize(cross_entropy)
init= tf.initialize_all_variables()
with tf.Session() as sess:
    sess.run(init)

    for epoch in range(100000):
        x_batch, y_batch = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={x : x_batch, y_ : y_batch })
        #训练精确度, training acc
        correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(y,1))
        acc = tf.reduce_mean(tf.cast(correct_prediction, "float"))
        if epoch%1000==0:
            print("step : "+str(epoch)+" training accuracy is : "+str(acc.eval(feed_dict={x:x_batch, y_:y_batch})))

    #评估
    ##比较一下预测值和真实值,如果一致则返回true,否则返回false
    correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(y,1))
    ##计算准确率tf.cast是转换数据格式,如下是转换为float
    ##计算均值tf.reduce_mean
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print("accuracy : "+str(sess.run(accuracy, feed_dict={x : mnist.test.images, y_ : mnist.test.labels})))