内容再要
手写数字识别,早在20世纪前,杨立昆(Yann LeCun)就完成这项工作,并在1980年左右利用卷积神经网络完善了手写数字识别
代码实现
import tensorflow as tf
import random
import matplotlib.pyplot as plt
# 例子 教程 手写数字 输入数据
from tensorflow.examples.tutorials.mnist import input_data
# 设置随机种子
tf.set_random_seed(1)
# 读取数据并进行独热编码
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
# 占位符
X = tf.placeholder(tf.float32, shape=[None, 784])
Y = tf.placeholder(tf.float32, shape=[None, 10])
# 初始化参数Wb(两层神经网络)
w1 = tf.Variable(tf.random_normal([784, 512]))
b1 = tf.Variable(tf.random_normal([512]))
w2 = tf.Variable(tf.random_normal([512, 10]))
b2 = tf.Variable(tf.random_normal([10]))
# 前向传播
z1 = tf.matmul(X, w1) + b1
a1 = tf.sigmoid(z1)
z2 = tf.matmul(a1, w2) + b2
# 多分类用softmax
a2 = tf.nn.softmax(z2)
# 计算代价
cost = -tf.reduce_mean(tf.reduce_sum(Y * tf.log(a2), axis=1), axis=0)
# 反向传播
dz2 = a2 - Y
dw2 = tf.matmul(tf.transpose(a1), dz2) / tf.cast(tf.shape(X)[0], tf.float32)
db2 = tf.reduce_mean(dz2, axis=0)
da1 = tf.matmul(dz2, tf.transpose(w2))
dz1 = da1 * a1 * (1 - a1)
dw1 = tf.matmul(tf.transpose(X), dz1) / tf.cast(tf.shape(X)[0], tf.float32)
db1 = tf.reduce_mean(dz1, axis=0)
# 参数更新
learning_rate = 0.1
updata = [
tf.assign(w2, w2 - learning_rate * dw2),
tf.assign(b2, b2 - learning_rate * db2),
tf.assign(w1, w1 - learning_rate * dw1),
tf.assign(b1, b1 - learning_rate * db1),
]
# 准确率
accurary = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(a2, axis=1),tf.argmax(Y, axis=1)), dtype=tf.float32), axis=0)
# 大批次
train_times = 15
# 小批量 6万个数据每次训练100个
batch_size = 100
# 开启会话
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# 先开始大批次的循环
for times in range(train_times):
# 求代价,代价先归0
avg_cost = 0
# 大批量下的小批量次数
n_batch = int(mnist.train.num_examples / batch_size)
# 小批量训练
for i in range(n_batch):
# 每次取出训练集和测试集100个
train_X, train_Y = mnist.train.next_batch(batch_size)
c, _ = sess.run([cost, updata], feed_dict={X: train_X, Y: train_Y})
avg_cost += c / n_batch
print('批次', times+1)
print('代价', avg_cost)
print('训练结束')
# 准确率
print(sess.run(accurary, feed_dict={X: mnist.test.images, Y: mnist.test.labels}))
# 随机预测
r = random.randint(0, mnist.test.num_examples - 1)
print('labels', sess.run(tf.argmax(mnist.test.labels[r: r + 1], axis=1)))
print('prediction:', sess.run(tf.argmax(a2, 1), feed_dict={X: mnist.test.images[r: r+1]}))
# 画图
plt.imshow(mnist.test.images[r: r + 1].reshape(28, 28),
cmap='Greys',
interpolation='nearest'
)
plt.show()
效果实现
批次 1
代价 2.557806055112321
批次 2
代价 1.0456371155652138
批次 3
代价 0.8032650183276691
批次 4
代价 0.6728521455417991
批次 5
代价 0.5812857172976839
批次 6
代价 0.5152520787715907
批次 7
代价 0.46274486312811985
批次 8
代价 0.4205591650916775
批次 9
代价 0.38447663389823633
批次 10
代价 0.35472463608465415
批次 11
代价 0.32847735724327237
批次 12
代价 0.30493005472150747
批次 13
代价 0.2853568002022804
批次 14
代价 0.266866419179873
批次 15
代价 0.2506876078756018
训练结束
0.9179
labels [7]
prediction: [7]
版权声明:本文为joker_shy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。