代码:
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 #当前路径 mnist = input_data.read_data_sets("MNISt_data", one_hot=True)
训练结果:
Extracting MNISt_data/train-images-idx3-ubyte.gz Extracting MNISt_data/train-labels-idx1-ubyte.gz Extracting MNISt_data/t10k-images-idx3-ubyte.gz Extracting MNISt_data/t10k-labels-idx1-ubyte.gz
代码:
#每个批次的大小 #以矩阵的形式放进去 batch_size = 100 #计算一共有多少个批次 n_batch = mnist.train.num_examples // batch_size #定义两个placeholder #28 x 28 = 784 x = tf.placeholder(tf.float32, [None, 784]) y = tf.placeholder(tf.float32, [None, 10]) #创建一个简单的神经网络 #输入层784,没有隐藏层,输出层10个神经元 W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([1, 10])) prediction = tf.nn.softmax(tf.matmul(x, W) + b) #交叉熵 loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction)) #使用梯度下降法 train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) #初始化变量 init = tf.global_variables_initializer() #结果存放在一个布尔型列表中 #tf.argmax(y, 1)与tf.argmax(prediction, 1)相同返回True,不同则返回False #argmax返回一维张量中最大的值所在的位置 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1)) #求准确率 #tf.cast(correct_prediction, tf.float32) 将布尔型转换为浮点型 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) with tf.Session() as sess: sess.run(init) #总共21个周期 for epoch in range(21): #总共n_batch个批次 for batch in range(n_batch): #获得一个批次 batch_xs, batch_ys = mnist.train.next_batch(batch_size) sess.run(train_step, feed_dict={x:batch_xs, y:batch_ys}) #训练完一个周期后准确率 acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels}) print("Iter" + str(epoch) + ", Testing Accuracy" + str(acc))
训练结果:
Iter0, Testing Accuracy0.8505 Iter1, Testing Accuracy0.895 Iter2, Testing Accuracy0.9028 Iter3, Testing Accuracy0.9061 Iter4, Testing Accuracy0.9084 Iter5, Testing Accuracy0.9101 Iter6, Testing Accuracy0.9114 Iter7, Testing Accuracy0.914 Iter8, Testing Accuracy0.9145 Iter9, Testing Accuracy0.916 Iter10, Testing Accuracy0.9178 Iter11, Testing Accuracy0.918 Iter12, Testing Accuracy0.919 Iter13, Testing Accuracy0.9188 Iter14, Testing Accuracy0.9198 Iter15, Testing Accuracy0.9199 Iter16, Testing Accuracy0.9212 Iter17, Testing Accuracy0.9213 Iter18, Testing Accuracy0.9212 Iter19, Testing Accuracy0.9211 Iter20, Testing Accuracy0.922注: 交叉熵能够快速收敛