1、导入所需包及训练、测试的数据
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('datasets', one_hot=True)
备注:自动下载所需数据到指定的文件夹,文件夹名称datasets
2、原始数据处理
trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels
2-1、查看数据的形状
print(trainimg.shape)
print(trainlabel.shape)
print(testimg.shape)
print(testlabel.shape)
output:
(55000, 784)
(55000, 10)
(10000, 784)
(10000, 10)
2-2、显示图片
i = 5
curr_img = np.reshape(trainimg[i,:], (28, 28))
plt.imshow(curr_img, cmap=plt.get_cmap('gray'))
plt.show()
output:
备注:
np.reshape() : 为数组提供新形状而不更改其数据
trainimg[i,:] : 提取数组第i行的全部数据
3、创建输入数据的特征向量
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
4、创建模型参数
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
5、正向传播,计算输出值
actv = tf.nn.softmax(tf.matmul(x,W) + b)
6、计算损失函数
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(tf.clip_by_value(actv,1e-10,1.0)) reduction_indices=1))
7、使用梯度下降法进行优化
learning_rate = 0.01
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
8、判断正确结果与预测结果是否一致
pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))
备注:
tf.argmax(input, dimension, name = None):返回最大数值的下标
dimension = 0: 按列找 ; dimension = 1 :按行找
9、计算正确率
accr = tf.reduce_mean(tf.cast(pred, tf.float32))
10、初始化模型参数
init_op = tf.global_variables_initializer()
11、定义常量
training_epochs = 100 #训练的轮数
display_step = 5 #用来比较、输出结果
12、使用batch梯度下降法进行训练
with tf.Session() as sess:
sess.run(init_op)
costs = []
#对于每一轮的训练
for epoch in range(training_epochs):
avg_cost = 0.
sess.run(optm, feed_dict={x: trainimg, y: trainlabel})
feeds = {x: trainimg, y: trainlabel}
avg_cost += sess.run(cost, feed_dict=feeds) / training_epochs
if epoch % display_step == 0:
feed_train = {x: trainimg, y: trainlabel}
feed_test = {x: testimg, y: testlabel}
train_acc = sess.run(accr, feed_dict=feed_train)
test_acc = sess.run(accr, feed_dict=feed_test)
print("Eppoch: %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f" %
(epoch, training_epochs, avg_cost, train_acc, test_acc))
costs.append(avg_cost)
print("Done.")
# plot the loss
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations ')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
output:
Eppoch: 000/100 cost: 0.022917860 train_acc: 0.663 test_acc: 0.670
Eppoch: 005/100 cost: 0.022393265 train_acc: 0.681 test_acc: 0.688
Eppoch: 010/100 cost: 0.021891801 train_acc: 0.695 test_acc: 0.700
Eppoch: 015/100 cost: 0.021410968 train_acc: 0.707 test_acc: 0.712
Eppoch: 020/100 cost: 0.020949018 train_acc: 0.717 test_acc: 0.722
Eppoch: 025/100 cost: 0.020504670 train_acc: 0.725 test_acc: 0.730
Eppoch: 030/100 cost: 0.020076952 train_acc: 0.730 test_acc: 0.737
Eppoch: 035/100 cost: 0.019665074 train_acc: 0.735 test_acc: 0.744
Eppoch: 040/100 cost: 0.019268372 train_acc: 0.740 test_acc: 0.749
Eppoch: 045/100 cost: 0.018886260 train_acc: 0.745 test_acc: 0.753
Eppoch: 050/100 cost: 0.018518188 train_acc: 0.749 test_acc: 0.757
Eppoch: 055/100 cost: 0.018163649 train_acc: 0.752 test_acc: 0.760
Eppoch: 060/100 cost: 0.017822148 train_acc: 0.755 test_acc: 0.763
Eppoch: 065/100 cost: 0.017493213 train_acc: 0.758 test_acc: 0.767
Eppoch: 070/100 cost: 0.017176378 train_acc: 0.761 test_acc: 0.773
Eppoch: 075/100 cost: 0.016871188 train_acc: 0.764 test_acc: 0.777
Eppoch: 080/100 cost: 0.016577202 train_acc: 0.767 test_acc: 0.780
Eppoch: 085/100 cost: 0.016293980 train_acc: 0.770 test_acc: 0.783
Eppoch: 090/100 cost: 0.016021105 train_acc: 0.772 test_acc: 0.786
Eppoch: 095/100 cost: 0.015758159 train_acc: 0.774 test_acc: 0.789
Done.