基于pytorch的手写数字识别-训练+使用

python 复制代码
import pandas as pd
import numpy as np
import torch
import matplotlib
import matplotlib.pyplot as plt
from torch.utils.data import TensorDataset, DataLoader

matplotlib.use('tkAgg')

# 设置图形配置
config = {
    "font.family": 'serif',
    "mathtext.fontset": 'stix',
    "font.serif": ['SimSun'],
    'axes.unicode_minus': False
}
matplotlib.rcParams.update(config)

def mymap(labels):
    return np.where(labels < 10, labels, 0)

# 数据加载
path = "d:\\JD\\Documents\\大学等等等\\自学部分\\机器学习自学画图\\手写数字识别\\ex3data1.xlsx"
data = pd.read_excel(path)
data = np.array(data, dtype=np.float32)
x = data[:, :-1]
labels = data[:, -1]
labels = mymap(labels)

# 转换为Tensor
x = torch.tensor(x, dtype=torch.float32)
labels = torch.tensor(labels, dtype=torch.long)

# 创建Dataset和Dataloader
dataset = TensorDataset(x, labels)
train_loader = DataLoader(dataset, batch_size=20, shuffle=True)

# 检查GPU是否可用
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# 定义模型
my_nn = torch.nn.Sequential(
    torch.nn.Linear(400, 128),
    torch.nn.Sigmoid(),
    torch.nn.Linear(128, 256),
    torch.nn.Sigmoid(),
    torch.nn.Linear(256, 512),
    torch.nn.Sigmoid(),
    torch.nn.Linear(512, 10)
).to(device)

# 加载预训练模型
my_nn.load_state_dict(torch.load('model.pth'))
my_nn.eval()  # 切换至评估模式

# 准备选取数据进行预测
sample_indices = np.random.choice(len(dataset), 50, replace=False)  # 随机选择50个样本
sample_images = x[sample_indices].to(device)  # 选择样本并移动到GPU
sample_labels = labels[sample_indices].numpy()  # 真实标签

# 进行预测
with torch.no_grad():  # 禁用梯度计算
    predictions = my_nn(sample_images)
    predicted_labels = torch.argmax(predictions, dim=1).cpu().numpy()  # 获取预测的标签

# 绘制图像
plt.figure(figsize=(10, 10))
for i in range(50):
    plt.subplot(10, 5, i + 1)  # 10行5列的子图
    plt.imshow(sample_images[i].cpu().reshape(20, 20), cmap='gray')  # 还原为20x20图像
    plt.title(f'Predicted: {predicted_labels[i]}', fontsize=8)
    plt.axis('off')  # 关闭坐标轴

plt.tight_layout()  # 调整子图间距
plt.show()

Iteration 0, Loss: 0.8472495079040527

Iteration 20, Loss: 0.014742681756615639

Iteration 40, Loss: 0.00011596851982176304

Iteration 60, Loss: 9.278443030780181e-05

Iteration 80, Loss: 1.3701709576707799e-05

Iteration 100, Loss: 5.019319928578625e-07

Iteration 120, Loss: 0.0

Iteration 140, Loss: 0.0

Iteration 160, Loss: 1.2548344585638915e-08

Iteration 180, Loss: 1.700657230685465e-05

预测准确率: 100.00%

下面使用已经训练好的模型,进行再次测试:

python 复制代码
import pandas as pd
import numpy as np
import torch
import matplotlib
import matplotlib.pyplot as plt
from torch.utils.data import TensorDataset, DataLoader

matplotlib.use('tkAgg')

# 设置图形配置
config = {
    "font.family": 'serif',
    "mathtext.fontset": 'stix',
    "font.serif": ['SimSun'],
    'axes.unicode_minus': False
}
matplotlib.rcParams.update(config)

def mymap(labels):
    return np.where(labels < 10, labels, 0)

# 数据加载
path = "d:\\JD\\Documents\\大学等等等\\自学部分\\机器学习自学画图\\手写数字识别\\ex3data1.xlsx"
data = pd.read_excel(path)
data = np.array(data, dtype=np.float32)
x = data[:, :-1]
labels = data[:, -1]
labels = mymap(labels)

# 转换为Tensor
x = torch.tensor(x, dtype=torch.float32)
labels = torch.tensor(labels, dtype=torch.long)

# 创建Dataset和Dataloader
dataset = TensorDataset(x, labels)
train_loader = DataLoader(dataset, batch_size=20, shuffle=True)

# 检查GPU是否可用
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# 定义模型
my_nn = torch.nn.Sequential(
    torch.nn.Linear(400, 128),
    torch.nn.Sigmoid(),
    torch.nn.Linear(128, 256),
    torch.nn.Sigmoid(),
    torch.nn.Linear(256, 512),
    torch.nn.Sigmoid(),
    torch.nn.Linear(512, 10)
).to(device)

# 加载预训练模型
my_nn.load_state_dict(torch.load('model.pth'))
my_nn.eval()  # 切换至评估模式

# 准备选取数据进行预测
sample_indices = np.random.choice(len(dataset), 50, replace=False)  # 随机选择50个样本
sample_images = x[sample_indices].to(device)  # 选择样本并移动到GPU
sample_labels = labels[sample_indices].numpy()  # 真实标签

# 进行预测
with torch.no_grad():  # 禁用梯度计算
    predictions = my_nn(sample_images)
    predicted_labels = torch.argmax(predictions, dim=1).cpu().numpy()  # 获取预测的标签

plt.figure(figsize=(16, 10))
for i in range(20):
    plt.subplot(4, 5, i + 1)  # 4行5列的子图
    plt.imshow(sample_images[i].cpu().reshape(20, 20), cmap='gray')  # 还原为20x20图像
    plt.title(f'True: {sample_labels[i]}, Pred: {predicted_labels[i]}', fontsize=12)  # 标题中显示真实值和预测值
    plt.axis('off')  # 关闭坐标轴

plt.tight_layout()  # 调整子图间距
plt.show()
相关推荐
B站计算机毕业设计超人15 分钟前
计算机毕业设计Django+Vue.js豆瓣图书推荐系统 图书评论情感分析 豆瓣图书可视化大屏 豆瓣图书爬虫 数据分析 图书大数据 大数据毕业设计 机器学习
大数据·python·深度学习·机器学习·网络爬虫·数据可视化·推荐算法
新手小白勇闯新世界18 分钟前
特征描述子 EFPH(扩展点特征直方图)---既包括几何关系又包括空间关系
人工智能·深度学习·机器学习
墨染辉2 小时前
pdf处理1
人工智能
sp_fyf_20244 小时前
【大语言模型-论文精读】谷歌-BERT:用于语言理解的预训练深度双向Transformers
人工智能·语言模型·bert
算家云5 小时前
PhotoMaker部署文档
人工智能·aigc·conda·图像生成·comfyui·工作流·文本转图像
小猪包3336 小时前
ai论文写作软件哪个好?分享5款ai论文题目生成器
人工智能·深度学习·计算机视觉·ai写作
luthane6 小时前
python 实现algorithm topo卡恩拓扑算法
数据结构·python·算法
云翼时代科技7 小时前
【探索艺术新纪元:Midjourney中文版,让创意无界!】
人工智能
坚持学习的你7 小时前
Jax(Random、Numpy)常用函数
人工智能·pytorch·python·jax
KGback7 小时前
【项目记录】大模型基于llama.cpp在Qemu-riscv64向量扩展指令下的部署
人工智能·llama·riscv