导入需要的工具包:
# 导入Mnist数据集
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets('data/', one_hot=True)
执行结果:
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
设置参数:
# 输出类别个数
numClasses = 10
# 输入28*28*1
inputSize = 784
# 5W次迭代次数
trainingIterations = 50000
# 一次迭代个数,最好64以上
batchSize = 64
指定X、y大小:
X = tf.placeholder(tf.float32, shape = [None, inputSize])
y = tf.placeholder(tf.float32, shape = [None, numClasses])
参数初始化:
# 随机生成784*10维的W矩阵,stddev:是正态分布的标准差,默认为1.0。
W1 = tf.Variable(tf.random_normal([inputSize, numClasses], stddev=0.1))
B1 = tf.Variable(tf.constant(0.1), [numClasses])
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
构造模型:
y_pred = tf.nn.softmax(tf.matmul(X, W1) + B1)
loss = tf.reduce_mean(tf.square(y - y_pred))
opt = tf.train.GradientDescentOptimizer(learning_rate = .05).minimize(loss)
correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
迭代计算:
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(trainingIterations):
batch = mnist.train.next_batch(batchSize)
batchInput = batch[0]
batchLabels = batch[1]
_, trainingLoss = sess.run([opt, loss], feed_dict={X: batchInput, y: batchLabels})
if i%10000 ==0:
train_accuracy = accuracy.eval(session=sess, feed_dict={X: batchInput, y: batchLabels})
print("step %d, training accuracy %g"%(i, train_accuracy))
执行结果:
step 0, training accuracy 0.125
step 10000, training accuracy 0.90625
step 20000, training accuracy 0.875
step 30000, training accuracy 0.875
step 40000, training accuracy 0.890625
测试结果:
batch = mnist.test.next_batch(batchSize)
testAccuracy = sess.run(accuracy, feed_dict={X: batch[0], y: batch[1]})
print("test accuracy %g"%(testAccuracy))
执行结果:
test accuracy 0.90625