【第三章:神经网络原理详解与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)
  • 混淆矩阵评估模型在不同语言上的分类准确率
相关推荐
大千AI助手9 分钟前
直接偏好优化(DPO):原理、演进与大模型对齐新范式
人工智能·神经网络·算法·机器学习·dpo·大模型对齐·直接偏好优化
ReinaXue14 分钟前
大模型【进阶】(四)QWen模型架构的解读
人工智能·神经网络·语言模型·transformer·语音识别·迁移学习·audiolm
zzywxc7871 小时前
AI 驱动的软件测试革新:框架、检测与优化实践
人工智能·深度学习·机器学习·数据挖掘·数据分析
Ronin-Lotus2 小时前
深度学习篇---PaddleDetection模型选择
人工智能·深度学习
Blossom.1182 小时前
基于深度学习的医学图像分析:使用CycleGAN实现图像到图像的转换
人工智能·深度学习·目标检测·机器学习·分类·数据挖掘·语音识别
shangyingying_13 小时前
关于神经网络CNN的搭建过程以及图像卷积的实现过程学习
神经网络·学习·cnn
CoovallyAIHub6 小时前
无人机图像+深度学习:湖南农大团队实现稻瘟病分级检测84%准确率
深度学习·算法·计算机视觉
TiAmo zhang6 小时前
深度学习与图像处理案例 │ 图像分类(智能垃圾分拣器)
图像处理·深度学习·分类
柴 基7 小时前
Visual Studio Code 使用指南 (2025年版)
人工智能·pytorch·python
zzywxc7878 小时前
随着人工智能技术的飞速发展,大语言模型(Large Language Models, LLMs)已经成为当前AI领域最引人注目的技术突破。
人工智能·深度学习·算法·低代码·机器学习·自动化·排序算法