当前位置: 首页 > news >正文

02、Tensorflow实现手写数字识别(数字0-9)

02、Tensorflow实现手写数字识别(数字0-9)

01、Tensorflow实现二元手写数字识别(二分类问题)
02、Tensorflow实现手写数字识别(数字0-9)

开始学习机器学习啦,已经把吴恩达的课全部刷完了,现在开始熟悉一下复现代码。对这个手写数字实部比较感兴趣,作为入门的素材非常合适。

基于Tensorflow 2.10.0与pycharm

1、识别目标

识别手写仅仅是为了区分手写的0到9,所以实际上是一个多分类问题。
STEP1:导入相关包

import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.metrics import  classification_report
import matplotlib.pyplot as plt
import logging
import warnings

import numpy as np:这是引入numpy库,并为其设置一个缩写np。Numpy是Python中用于大规模数值计算的库,它提供了多维数组对象及一系列操作这些数组的函数。

import tensorflow as tf:这是引入tensorflow库,并为其设置一个缩写tf。TensorFlow是一个开源的深度学习框架,它被广泛用于各种深度学习应用。

from keras.models import Sequential:这是从Keras库中引入Sequential模型。Keras是一个高级神经网络API,它可以运行在TensorFlow之上。Sequential模型是Keras中的线性堆栈模型,允许你简单地堆叠多个网络层。

from keras.layers import Dense:这是从Keras库中引入Dense层。Dense层是神经网络中的全连接层,每个输入节点与输出节点都是连接的。

from sklearn.model_selection import train_test_split:这是从scikit-learn库中引入train_test_split函数。这个函数用于将数据分割为训练集和测试集。

from sklearn.metrics import classification_report 这行代码的主要作用是导入classification_report 函数,以便在后续的代码中使用它来评估分类模型的性能。

import matplotlib.pyplot as plt:这是引入matplotlib的pyplot模块,并为其设置一个缩写plt。Matplotlib是Python中的绘图库,而pyplot是其中的一个模块,用于绘制各种图形和图像。

import warnings:这是引入Python的标准警告库,它可以用来发出警告,或者过滤掉不需要的警告。

import logging:这是引入Python的标准日志库,用于记录日志信息,方便追踪和调试代码。


STEP2:屏蔽无用警告并允许中文

# 使用warnings模块来忽略特定类型的警告  
warnings.simplefilter(action='ignore', category=FutureWarning)  
# 配置tensorflow的日志记录级别  
logging.getLogger("tensorflow").setLevel(logging.ERROR)  
# 设置TensorFlow的autograph模块的详细级别  
tf.autograph.set_verbosity(0)  
# 设置numpy的打印选项  
np.set_printoptions(precision=2)  

STEP3:加载数据集并分割测试集

# load dataset
def load_data():X = np.load("Handwritten_Digit_Recognition_Multiclass_data/X.npy")y = np.load("Handwritten_Digit_Recognition_Multiclass_data/y.npy")return X, y# load dataset
X, y = load_data()print ('The shape of X is: ' + str(X.shape))
print ('The shape of y is: ' + str(y.shape))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)

原始的输入的数据集是5000* 400数组,共包含5000个手写数字的数据,其中400为20*20像素的图片,
在这里插入图片描述


STEP4:模型构建与训练

# 构建模型  
tf.random.set_seed(1234)  # 设置随机种子以确保每次运行的结果是一致的  
model = Sequential([### START CODE HERE ###  tf.keras.Input(shape=(400,)),  # 输入层,输入数据的形状是400维   Dense(100, activation='relu', name="L1"),  # 全连接层,100个神经元,使用ReLU激活函数,命名为"L1"  Dense(75, activation='relu', name="L2"),  # 全连接层,75个神经元,使用ReLU激活函数,命名为"L2"  Dense(10, activation='linear', name="L3"),  # 输出层,10个神经元,使用线性激活函数,命名为"L3"  ### END CODE HERE ###  ], name="my_model"
)  # 定义模型名称为"my_model"  
model.summary()  # 打印模型的概述信息  # 配置模型的训练参数  
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),# 使用稀疏分类交叉熵作为损失函数,且输出是logits(即未经过softmax的原始输出)  optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),  # 使用Adam优化器,并设置学习率为0.001  
)# 训练模型  
history = model.fit(X_train, y_train,  # 使用X_train作为输入数据,y_train作为目标数据  epochs=100  # 训练100轮  
)

STEP5:结果可视化与打印准确度信息

fig, axes = plt.subplots(20, 25, figsize=(20, 25))
fig.tight_layout(pad=0.13, rect=[0, 0.03, 1, 0.91])  # [left, bottom, right, top]
for i, ax in enumerate(axes.flat):# Select random indicesrandom_index = np.random.randint(X_test.shape[0])# Select rows corresponding to the random indices and# reshape the imageX_random_reshaped = X_test[random_index].reshape((20, 20)).T# Display the imageax.imshow(X_random_reshaped, cmap='gray')# Predict using the Neural Networkprediction = model.predict(X_test[random_index].reshape(1, 400))prediction_p = tf.nn.softmax(prediction)yhat = np.argmax(prediction_p)# 错误结果标红if y_test[random_index, 0] == yhat:ax.set_title(f"{y_test[random_index, 0]},{yhat}", fontsize=10)ax.set_axis_off()else:ax.set_title(f"{y_test[random_index, 0]},{yhat}", fontsize=10, color='red')ax.set_axis_off()fig.suptitle("Label, yhat", fontsize=14)
plt.show()# 给出预测的测试集误差
def evaluation(y_test, y_predict):accuracy=classification_report(y_test, y_predict,output_dict=True)['accuracy']s=classification_report(y_test, y_predict,output_dict=True)['weighted avg']precision=s['precision']recall=s['recall']f1_score=s['f1-score']#kappa=cohen_kappa_score(y_test, y_predict)return accuracy,precision,recall,f1_score #, kappay_pred=model.predict(X_test)
prediction_p = tf.nn.softmax(y_pred)
yhat = np.argmax(prediction_p, axis=1)
accuracy,precision,recall,f1_score=evaluation(y_test,yhat)print("测试数据集准确率为:", accuracy)
print("测试数据集精确率为:", precision)
print("测试数据集召回率为:", recall)
print("测试数据集F1_score为:", f1_score)

3、运行结果

在这里插入图片描述

4、工程下载与全部代码

工程链接:Tensorflow实现手写数字识别(数字0-9)

import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn.metrics import  classification_report
import matplotlib.pyplot as plt
import logging
import warnings# 使用warnings模块来忽略特定类型的警告
warnings.simplefilter(action='ignore', category=FutureWarning)
# 配置tensorflow的日志记录级别
logging.getLogger("tensorflow").setLevel(logging.ERROR)
# 设置TensorFlow的autograph模块的详细级别
tf.autograph.set_verbosity(0)
# 设置numpy的打印选项
np.set_printoptions(precision=2)# load dataset
def load_data():X = np.load("Handwritten_Digit_Recognition_Multiclass_data/X.npy")y = np.load("Handwritten_Digit_Recognition_Multiclass_data/y.npy")return X, y# load dataset
X, y = load_data()print ('The shape of X is: ' + str(X.shape))
print ('The shape of y is: ' + str(y.shape))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42)# # 绘图可选
# m, n = X.shape
# fig, axes = plt.subplots(8, 8, figsize=(5, 5))
# fig.tight_layout(pad=0.13, rect=[0, 0.03, 1, 0.91])  # [left, bottom, right, top]
# # fig.tight_layout(pad=0.5)
# for i, ax in enumerate(axes.flat):
#     # Select random indices
#     random_index = np.random.randint(m)
#     # Select rows corresponding to the random indices and
#     # reshape the image
#     X_random_reshaped = X[random_index].reshape((20, 20)).T
#     # Display the image
#     ax.imshow(X_random_reshaped, cmap='gray')
#     # Display the label above the image
#     ax.set_title(y[random_index, 0])
#     ax.set_axis_off()
#     fig.suptitle("Label, image", fontsize=14)
# plt.show()# 构建模型
tf.random.set_seed(1234)  # 设置随机种子以确保每次运行的结果是一致的
model = Sequential([### START CODE HERE ###tf.keras.Input(shape=(400,)),  # 输入层,输入数据的形状是400维Dense(100, activation='relu', name="L1"),  # 全连接层,100个神经元,使用ReLU激活函数,命名为"L1"Dense(75, activation='relu', name="L2"),  # 全连接层,75个神经元,使用ReLU激活函数,命名为"L2"Dense(10, activation='linear', name="L3"),  # 输出层,10个神经元,使用线性激活函数,命名为"L3"### END CODE HERE ###], name="my_model"
)  # 定义模型名称为"my_model"
model.summary()  # 打印模型的概述信息# 配置模型的训练参数
model.compile(loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),# 使用稀疏分类交叉熵作为损失函数,且输出是logits(即未经过softmax的原始输出)optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),  # 使用Adam优化器,并设置学习率为0.001
)# 训练模型
history = model.fit(X_train, y_train,  # 使用X_train作为输入数据,y_train作为目标数据epochs=100  # 训练100轮
)fig, axes = plt.subplots(20, 25, figsize=(20, 25))
fig.tight_layout(pad=0.13, rect=[0, 0.03, 1, 0.91])  # [left, bottom, right, top]
for i, ax in enumerate(axes.flat):# Select random indicesrandom_index = np.random.randint(X_test.shape[0])# Select rows corresponding to the random indices and# reshape the imageX_random_reshaped = X_test[random_index].reshape((20, 20)).T# Display the imageax.imshow(X_random_reshaped, cmap='gray')# Predict using the Neural Networkprediction = model.predict(X_test[random_index].reshape(1, 400))prediction_p = tf.nn.softmax(prediction)yhat = np.argmax(prediction_p)# Display the label above the imageif y_test[random_index, 0] == yhat:ax.set_title(f"{y_test[random_index, 0]},{yhat}", fontsize=10)ax.set_axis_off()else:ax.set_title(f"{y_test[random_index, 0]},{yhat}", fontsize=10, color='red')ax.set_axis_off()fig.suptitle("Label, yhat", fontsize=14)
plt.show()# 给出预测的测试集误差
def evaluation(y_test, y_predict):accuracy=classification_report(y_test, y_predict,output_dict=True)['accuracy']s=classification_report(y_test, y_predict,output_dict=True)['weighted avg']precision=s['precision']recall=s['recall']f1_score=s['f1-score']#kappa=cohen_kappa_score(y_test, y_predict)return accuracy,precision,recall,f1_score #, kappay_pred=model.predict(X_test)
prediction_p = tf.nn.softmax(y_pred)
yhat = np.argmax(prediction_p, axis=1)
accuracy,precision,recall,f1_score=evaluation(y_test,yhat)print("测试数据集准确率为:", accuracy)
print("测试数据集精确率为:", precision)
print("测试数据集召回率为:", recall)
print("测试数据集F1_score为:", f1_score)

相关文章:

  • 在线文库系统 转码功能源代码展示 支持文档在线预览查阅功能
  • Linux “grep“ 命令
  • 【代码随想录】算法训练计划37
  • ctfshow刷题web入门--1--ljcsd
  • 键盘打字盲打练习系列之刻意练习——1
  • 异常数据检测 | Python实现孤立森林(IsolationForest)异常检测
  • RabbitMQ 安装(在docker容器中安装)
  • 微服务保护 Sentinel
  • Webpack的ts的配置详细教程
  • 【MATLAB】异常数据识别
  • C#工程中Form_xx.cs不能在设计器中查看
  • 【docker系列】docker高阶篇
  • 【开源视频联动物联网平台】Node-RED规则引擎
  • thinkphp6生成PDF自动换行
  • 如何设定目标-SMART原则
  • [deviceone开发]-do_Webview的基本示例
  • [译]Python中的类属性与实例属性的区别
  • Android单元测试 - 几个重要问题
  • CentOS从零开始部署Nodejs项目
  • ERLANG 网工修炼笔记 ---- UDP
  • Javascript基础之Array数组API
  • Laravel 实践之路: 数据库迁移与数据填充
  • MD5加密原理解析及OC版原理实现
  • nfs客户端进程变D,延伸linux的lock
  • Quartz实现数据同步 | 从0开始构建SpringCloud微服务(3)
  • SegmentFault 2015 Top Rank
  • SpiderData 2019年2月16日 DApp数据排行榜
  • 对超线程几个不同角度的解释
  • 高性能JavaScript阅读简记(三)
  • 基于组件的设计工作流与界面抽象
  • 前端工程化(Gulp、Webpack)-webpack
  • 前端攻城师
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 使用 Node.js 的 nodemailer 模块发送邮件(支持 QQ、163 等、支持附件)
  • 一、python与pycharm的安装
  • 用Python写一份独特的元宵节祝福
  • 阿里云ACE认证学习知识点梳理
  • 昨天1024程序员节,我故意写了个死循环~
  • ​如何在iOS手机上查看应用日志
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • #vue3 实现前端下载excel文件模板功能
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (NSDate) 时间 (time )比较
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (七)微服务分布式云架构spring cloud - common-service 项目构建过程
  • (转)scrum常见工具列表
  • (转)程序员疫苗:代码注入
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .dwp和.webpart的区别
  • .Net 4.0并行库实用性演练