项目实践:手写数字识别
学习目标
通过本课程,学员们将能够使用TensorFlow构建一个手写数字识别模型,从数据准备、模型构建、训练到评估,全面掌握TensorFlow的应用。本课程将通过实践的方式,帮助学员理解深度学习模型的构建过程,以及如何使用TensorFlow框架来实现这些模型。
相关知识点
- 手写数字识别实践
学习内容
1 手写数字识别实践
1.1 数据准备与预处理
在开始构建模型之前,首先需要准备和预处理数据。对于手写数字识别任务,我们将使用MNIST数据集,这是一个广泛用于训练各种图像处理系统的小型数据集。MNIST数据集包含60,000个训练样本和10,000个测试样本,每个样本都是28x28像素的灰度图像。
1.1.1 加载数据
安装tensorflow。
%pip install tensorflow
执行以下命令获取MNIST数据集。MNIST数据集是一个广泛用于机器学习和深度学习领域的手写数字图片数据集。它由美国国家标准与技术研究所(NIST)发起整理,包含来自250个不同人手写的数字图片,其中50%是高中生,50%来自人口普查局的工作人员。MNIST数据集包含70,000张图片,其中60,000张用于训练,10,000张用于测试。每张图片是28×28像素的灰度图像,像素值在0到255之间。图片的标签是0到9的数字,表示图片中的手写数字。
!wget https://model-community-picture.obs.cn-north-4.myhuaweicloud.com/ascend-zone/notebook_datasets/9b6d4852476411f096fbfa163edcddae/mnist.npz --no-check-certificate
首先,我们需要加载MNIST数据集。下面的代码展示了如何使用np.load来加载数据:
import tensorflow as tf
import numpy as np
# 加载本地MNIST数据集文件
path = "mnist.npz"
with np.load(path) as f:
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
# 查看数据的形状
print("Training data shape:", x_train.shape)
print("Training labels shape:", y_train.shape)
print("Test data shape:", x_test.shape)
print("Test labels shape:", y_test.shape)
1.1.2 数据预处理
加载数据后,我们需要对数据进行预处理,以便更好地训练模型。预处理步骤包括:
- 归一化:将图像数据归一化到0到1之间,这有助于模型更快地收敛。
- 重塑:将图像数据重塑为模型所需的输入格式。
# 归一化
x_train = x_train / 255.0
x_test = x_test / 255.0
# 重塑
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
# 查看预处理后的数据形状
print("Processed training data shape:", x_train.shape)
print("Processed test data shape:", x_test.shape)
1.2 模型构建与训练
在数据准备完成后,接下来是构建模型。我们将使用TensorFlow的Keras API来构建一个简单的卷积神经网络(CNN)模型。CNN是处理图像数据的常用模型,特别适合于手写数字识别任务。
1.2.1 构建模型
我们将构建一个包含卷积层、池化层和全连接层的CNN模型。下面的代码展示了如何使用Keras API来构建这个模型:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
# 构建模型
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# 编译模型
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 查看模型结构
model.summary()
1.2.2 训练模型
构建好模型后,接下来是训练模型。我们将使用训练数据集来训练模型,并在训练过程中监控模型的性能。下面的代码展示了如何训练模型:
# 训练模型
history = model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
# 查看训练历史
import matplotlib.pyplot as plt
# 绘制训练和验证的准确率
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
# 绘制训练和验证的损失
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()
plt.show()
1.3 模型评估与优化
训练完成后,我们需要评估模型的性能,并根据评估结果进行优化。我们将使用测试数据集来评估模型的准确率,并尝试一些优化方法来提高模型的性能。
1.3.1 评估模型
首先,我们使用测试数据集来评估模型的性能。下面的代码展示了如何评估模型的准确率:
# 评估模型
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print("Test Accuracy:", test_accuracy)
1.3.2 优化模型
如果模型的性能不理想,我们可以尝试一些优化方法,例如:
- 增加模型复杂度:增加更多的卷积层或全连接层。
- 调整超参数:调整学习率、批量大小等超参数。
- 数据增强:使用数据增强技术来增加训练数据的多样性。
下面的代码展示了如何通过增加模型复杂度来优化模型:
# 构建更复杂的模型
model = Sequential([
Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Conv2D(128, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Flatten(),
Dense(256, activation='relu'),
Dense(128, activation='relu'),
Dense(10, activation='softmax')
])
# 编译模型
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# 训练模型
history = model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
# 评估模型
test_loss, test_accuracy = model.evaluate(x_test, y_test)
print("Test Accuracy:", test_accuracy)
967

被折叠的 条评论
为什么被折叠?



