使用softmax对mnist图片分类,并获取预测的准确度
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/', one_hot=True)
x_data = tf.placeholder(tf.float32, [None, 28 * 28])
y_data = tf.placeholder(tf.float32, [None, 10])
#创建一个隐藏层,输入数据:x_data, 输出10个神经元,激励函数使用softmax
prediction = tf.layers.dense(x_data, 10, tf.nn.softmax)
# tf.reduce_sum的用法
# x is [[1, 1, 1]
# [1, 1, 1]]
# tf.reduce_sum(x) => 6
# tf.reduce_sum(x, 0) => [2, 2, 2]
# tf.reduce_sum(x, 1) => [3, 3]
#损失函数,一般softmax和交叉熵损失配合使用
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_data * tf.log(prediction), reduction_indices=[1]))
#cross_entropy = tf.reduce_mean(-y_data * tf.log(prediction))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
def computer_accuracy(x_input, y_input):
&#