【第三章:神经网络原理详解与Pytorch入门】02.深度学习框架PyTorch入门-(5)PyTorch 实战——使用 RNN 进行人名分类

第三章: 神经网络原理详解与Pytorch入门

第二部分:深度学习框架PyTorch入门

第五节:PyTorch 实战

内容:使用 RNN进行人名分类

任务简介

本任务旨在使用 循环神经网络(RNN) 对输入的英文人名进行分类,判断其所属语言(如English、French、Chinese等)。该项目常用于学习字符级别的RNN序列建模。


一、准备数据

PyTorch 官方教程使用的名字数据集来源于 names.tar.gz,每种语言对应一个文本文件,文件中包含若干名字。

1. 加载依赖

python 复制代码
import os
import unicodedata
import string
import torch

2. 处理字符集与标准化

python 复制代码
all_letters = string.ascii_letters + " .,;'"
n_letters = len(all_letters)

def unicodeToAscii(s):
    return ''.join(
        c for c in unicodedata.normalize('NFD', s)
        if unicodedata.category(c) != 'Mn' and c in all_letters
    )

3. 读取每种语言的名字列表

python 复制代码
category_lines = {}  # dict: language -> [name1, name2, ...]
all_categories = []

def readLines(filename):
    with open(filename, encoding='utf-8') as f:
        return [unicodeToAscii(line.strip()) for line in f]

data_path = './data/names/'
for filename in os.listdir(data_path):
    category = os.path.splitext(filename)[0]
    all_categories.append(category)
    lines = readLines(os.path.join(data_path, filename))
    category_lines[category] = lines

n_categories = len(all_categories)

二、构建输入张量

python 复制代码
def letterToTensor(letter):
    tensor = torch.zeros(1, n_letters)
    tensor[0][all_letters.find(letter)] = 1
    return tensor

def lineToTensor(line):
    tensor = torch.zeros(len(line), 1, n_letters)
    for li, letter in enumerate(line):
        tensor[li][0][all_letters.find(letter)] = 1
    return tensor

三、构建 RNN 模型

python 复制代码
import torch.nn as nn

class RNN(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
        super(RNN, self).__init__()
        self.hidden_size = hidden_size

        self.i2h = nn.Linear(input_size + hidden_size, hidden_size)
        self.i2o = nn.Linear(input_size + hidden_size, output_size)
        self.softmax = nn.LogSoftmax(dim=1)

    def forward(self, input, hidden):
        combined = torch.cat((input, hidden), 1)
        hidden = self.i2h(combined)
        output = self.i2o(combined)
        output = self.softmax(output)
        return output, hidden

    def initHidden(self):
        return torch.zeros(1, self.hidden_size)

四、训练过程

python 复制代码
criterion = nn.NLLLoss()
learning_rate = 0.005
rnn = RNN(n_letters, 128, n_categories)

def train(category_tensor, line_tensor):
    hidden = rnn.initHidden()
    rnn.zero_grad()

    for i in range(line_tensor.size()[0]):
        output, hidden = rnn(line_tensor[i], hidden)

    loss = criterion(output, category_tensor)
    loss.backward()

    for param in rnn.parameters():
        param.data -= learning_rate * param.grad.data

    return output, loss.item()

五、预测

python 复制代码
def predict(input_line):
    with torch.no_grad():
        line_tensor = lineToTensor(input_line)
        hidden = rnn.initHidden()

        for i in range(line_tensor.size()[0]):
            output, hidden = rnn(line_tensor[i], hidden)

        topv, topi = output.topk(1)
        category_index = topi[0].item()
        return all_categories[category_index]

六、总结与扩展

模块 内容
数据格式 文本字符级序列
模型 单层 RNN(可拓展为 LSTM/GRU)
输入 One-hot 字符序列
输出 各语言类别的 LogSoftmax 概率
优化 手动梯度下降,可拓展为使用 optimizer.step()

可视化

  • 可视化训练损失下降曲线
python 复制代码
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation

# 模拟训练损失
epochs = 50
np.random.seed(1)
base_loss = np.linspace(2.0, 0.3, epochs)
noise = np.random.normal(0, 0.05, epochs)
loss_values = np.maximum(base_loss + noise, 0.2)

# 设置图像
fig, ax = plt.subplots(figsize=(8, 4))
ax.set_xlim(1, epochs)
ax.set_ylim(0, 2.2)
line, = ax.plot([], [], lw=2)
point, = ax.plot([], [], 'ro')
title = ax.set_title("Training Loss Animation")

def init():
    line.set_data([], [])
    point.set_data([], [])
    return line, point

def update(frame):
    x = np.arange(1, frame + 2)
    y = loss_values[:frame + 1]
    line.set_data(x, y)
    point.set_data([x[-1]], [y[-1]])
    title.set_text(f"Epoch: {frame + 1}, Loss: {y[-1]:.3f}")
    return line, point, title

ani = animation.FuncAnimation(fig, update, frames=epochs, init_func=init, blit=True, interval=120)
ani.save("training_loss_animation.gif", writer='pillow', fps=10)
  • 混淆矩阵评估模型在不同语言上的分类准确率
相关推荐
我没胡说八道5 小时前
高校论文AI检测优化工具对比研究与实测分析(2026)
人工智能·深度学习·机器学习·计算机视觉·aigc·论文
一叶知秋dong9 小时前
Stable diffusion 工作原理
人工智能·深度学习·stable diffusion
团象科技10 小时前
梳理中小出海独立站落地阶段关于WordPress 海外主机的实操参考路径
人工智能·深度学习
不当菜鸡的程序媛11 小时前
Policy model
深度学习
chlorine512 小时前
【神经网络】——卷积层、池化层、线性层
深度学习·神经网络·cnn
Sirius Wu13 小时前
Agentic端到端&分离式RL技术建设
人工智能·深度学习·机器学习·caffe
Unity官方开发者社区14 小时前
团结引擎动画系统|Event Graph CodeGen:一键编译图逻辑,提升运行时性能
深度学习
湘美书院--湘美谈教育14 小时前
湘美谈教育AI经验集锦:有些东西,它们很难蒸馏
大数据·人工智能·深度学习·机器学习
xixixi7777715 小时前
空天地通信、高速光模块、AI 智能体攻击、同态加密芯片四大事件解读:AI 算力底座攻防与全域通信同步升级
大数据·人工智能·深度学习·ai·大模型·光模块·智能体