手写数字识别(使用tensorflow2.2.0框架)
程序员文章站
2022-10-11 22:43:38
手写数字识别(使用tensorflow2.2.0框架)手写数字识别是最经典的分类实战(1)首先导入我们需要用到的模块import matplotlibfrom matplotlib import pyplot as pltimport tensorflow as tffrom tensorflow import kerasfrom tensorflow.keras import datasets, layers, optimizers#用下面二句代码可以去除警告import osos.e...
手写数字识别(使用tensorflow2.2.0框架)
手写数字识别是最经典的分类实战
(1)首先导入我们需要用到的模块
import matplotlib
from matplotlib import pyplot as plt
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import datasets, layers, optimizers
#用下面二句代码可以去除警告
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
(2)加载MNIST数据集
下载下来的数据集被分成两部分:60000行的训练数据集(mnist.train)和10000行的测试数据集(mnist.test)。这样的切分很重要,在机器学习模型设计时必须有一个单独的测试数据集不用于训练而是用来评估这个模型的性能,从而更加容易把设计的模型推广到其他数据集上(泛化)。每一个MNIST数据单元有两部分组成:一张包含手写数字的图片x和一个对应的标签y(例如9的标签就是9)。
(x, y), (x_test, y_test) = datasets.mnist.load_data()
print('x:', x.shape, 'y:', y.shape, 'x test:', x_test.shape, 'y test:', y_test)
(3)预处理数据
def preprocess(x, y):
# [b, 28, 28], [b]
print(x.shape, y.shape)
x = tf.cast(x, dtype=tf.float32) / 255.
x = tf.reshape(x, [-1, 28 * 28])
y = tf.cast(y, dtype=tf.int32)
y = tf.one_hot(y, depth=10)
return x, y
batchsz = 512
train_db = tf.data.Dataset.from_tensor_slices((x, y))
train_db = train_db.shuffle(1000)
train_db = train_db.batch(batchsz)
train_db = train_db.map(preprocess)
train_db = train_db.repeat(20)
# %将数据切分为训练集合测试集%
test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test))
test_db = test_db.shuffle(1000).batch(batchsz).map(preprocess)
x, y = next(iter(train_db))
print('train sample:', x.shape, y.shape)
(4)模型搭建和训练
模型的输入是28*28=728的图片
728(输入层)---->512(隐藏层第一层)----->256(隐藏层第二层)------->10(输出层对应0-9十个数字的分类)
def main():
# learning rate
lr = 1e-2
accs, losses = [], []
# 784 => 512
w1, b1 = tf.Variable(tf.random.normal([784, 256], stddev=0.1)), tf.Variable(tf.zeros([256]))
# 512 => 256
w2, b2 = tf.Variable(tf.random.normal([256, 128], stddev=0.1)), tf.Variable(tf.zeros([128]))
# 256 => 10
w3, b3 = tf.Variable(tf.random.normal([128, 10], stddev=0.1)), tf.Variable(tf.zeros([10]))
for step, (x, y) in enumerate(train_db):
# [b, 28, 28] => [b, 784]
x = tf.reshape(x, (-1, 784))
with tf.GradientTape() as tape:
# layer1.
h1 = x @ w1 + b1
h1 = tf.nn.relu(h1)
# layer2
h2 = h1 @ w2 + b2
h2 = tf.nn.relu(h2)
# output
out = h2 @ w3 + b3
# out = tf.nn.relu(out)
# compute loss
# [b, 10] - [b, 10]
loss = tf.square(y - out)
# [b, 10] => scalar
loss = tf.reduce_mean(loss)
grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])
for p, g in zip([w1, b1, w2, b2, w3, b3], grads):
p.assign_sub(lr * g)
# print
if step % 80 == 0:
print(step, 'loss:', float(loss))
losses.append(float(loss))
if step % 80 == 0:
# evaluate/test
total, total_correct = 0., 0
for x, y in test_db:
# layer1.
h1 = x @ w1 + b1
h1 = tf.nn.relu(h1)
# layer2
h2 = h1 @ w2 + b2
h2 = tf.nn.relu(h2)
# output
out = h2 @ w3 + b3
# [b, 10] => [b]
pred = tf.argmax(out, axis=1)
# convert one_hot y to number y
y = tf.argmax(y, axis=1)
# bool type
correct = tf.equal(pred, y)
# bool tensor => int tensor => numpy
total_correct += tf.reduce_sum(tf.cast(correct, dtype=tf.int32)).numpy()
total += x.shape[0]
print(step, 'Evaluate Acc:', total_correct / total)
accs.append(total_correct / total)
(5)训练结果展示:MSE和准确率
plt.figure()
x = [i * 80 for i in range(len(losses))]
plt.plot(x, losses, color='C0', marker='s', label='训练')
plt.ylabel('MSE')
plt.xlabel('Step')
plt.legend()
plt.savefig('train.svg')
plt.show()
plt.figure()
plt.plot(x, accs, color='C1', marker='s', label='测试')
plt.ylabel('准确率')
plt.xlabel('Step')
plt.legend()
plt.savefig('test.svg')
plt.show()
if __name__ == '__main__':
main()
(6)运行结果展示
本文地址:https://blog.csdn.net/wdxgj123/article/details/107065348
推荐阅读
-
基于MNIST手写数字数据集的数字识别小程序
-
机器学习python实战之手写数字识别
-
PyTorch CNN实战之MNIST手写数字识别示例
-
Python实现识别手写数字大纲
-
python tensorflow基于cnn实现手写数字识别
-
python使用KNN算法手写体识别
-
手写数字识别 ----Softmax回归模型官方案例注释(基于Tensorflow,Python)
-
手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)
-
Python实现识别手写数字 Python图片读入与处理
-
python实现识别手写数字 python图像识别算法