Skip to content

Repository files navigation

中英机器翻译系统

课程: NLP and LLMs | 学号: 250010064 | 姓名: Li Duojia

基于 RNN (with Attention)、Transformer 和 T5 的中文到英文神经机器翻译实现。

📄 完整报告: 250010064_Li Duojia.md
🔗 GitHub: https://github.com/liduojia1/NLP-LLM
💾 模型权重: Google Drive

项目特点

  • 多种模型架构:RNN Seq2Seq (GRU/LSTM)、Transformer、T5 微调
  • 多种注意力机制:Dot-product、Multiplicative、Additive (Bahdanau)
  • 消融实验支持:位置编码 (Sinusoidal / T5-Relative)、归一化方式 (LayerNorm / RMSNorm)
  • 超参数敏感性分析:Batch Size、Learning Rate、Model Scale
  • 完整训练/评估/推理流程:一键脚本、BLEU 评估、交互式翻译

一键推理

前置说明:仓库已包含 spm_models/ 分词模型,无需额外下载。

下载模型权重

Google Drive 下载模型文件夹,解压到项目根目录:

checkpoints/
├── transformer/tf_bs32/      # 最佳模型 (BLEU-4: 7.80)
├── rnn/rnn_lr2e3/            # RNN 模型 (BLEU-4: 4.66)
└── t5/t5_base/               # T5 模型 (BLEU-4: 3.01)

推理示例

# 单句翻译(推荐使用最佳模型)
python inference.py \
    --model transformer \
    --checkpoint checkpoints/transformer/tf_bs32/best_model.pt \
    --input "今天天气很好"
# 输出: The weather is nice today

# 交互模式(连续翻译)
python inference.py \
    --model transformer \
    --checkpoint checkpoints/transformer/tf_bs32/best_model.pt \
    --interactive

# 批量翻译
python inference.py \
    --model transformer \
    --checkpoint checkpoints/transformer/tf_bs32/best_model.pt \
    --input_file sentences.txt \
    --output translations.txt

# 使用其他模型
python inference.py --model rnn \
    --checkpoint checkpoints/rnn/rnn_lr2e3/best_model.pt \
    --input "机器学习"

python inference.py --model t5 \
    --checkpoint checkpoints/t5/t5_base/best_model \
    --input "深度学习"

实验结果

模型 配置 BLEU-4
Transformer tf_bs32 7.80 🏆
RNN rnn_lr2e3 4.66
T5 t5_base 3.01

详见完整报告:250010064_Li Duojia.md

快速开始(训练)

# 1. 训练分词器(如需从头训练)
python scripts/preprocess.py

# 2. 训练模型(可选,已提供训练好的权重)
python scripts/train_rnn.py --config configs/rnn_base.yaml
python scripts/train_transformer.py --config configs/transformer_base.yaml
python scripts/train_t5.py

# 3. 评估模型
python scripts/evaluate.py --model transformer \
    --checkpoint checkpoints/transformer/tf_bs32/best_model.pt

项目结构

NLP/
├── data/           # 数据处理 (tokenizer, dataset)
├── models/         # 模型定义 (rnn/, transformer/, t5/)
├── trainers/       # 训练器 (base, rnn, transformer)
├── utils/          # 工具 (metrics, decoding, visualization)
├── scripts/        # 脚本 (preprocess, train_*, evaluate)
├── configs/        # YAML配置文件
├── tests/          # 测试套件
├── inference.py    # ⭐ 一键推理脚本
└── spm_models/     # SentencePiece 分词模型

模型架构

RNN with Attention

  • 2层单向 GRU/LSTM 编码器
  • 带注意力机制的解码器 (dot / multiplicative / additive)
  • 训练策略:Teacher Forcing / Free Running
  • 解码策略:Greedy / Beam Search

Transformer

  • 6层编码器 + 6层解码器
  • 位置编码:Sinusoidal (绝对) / T5-style Relative Bias
  • 归一化:LayerNorm / RMSNorm
  • Pre-LN 架构,Label Smoothing

T5 (Fine-tuning)

  • HuggingFace t5-small / t5-base
  • Task prefix: "translate Chinese to English: "

消融实验

实验类型 变量 命令示例
注意力机制 dot/mult/add --attention additive
训练策略 TF/FR --training_mode free_running
位置编码 sin/rel --pos_encoding relative
归一化 LN/RMS --norm_type rmsnorm
模型规模 d_model --d_model 256

评估指标

使用字符串级别 BLEU 评估:

  1. spm.decode() 将 token IDs 解码为文本
  2. nltk.word_tokenize() 分词
  3. corpus_bleu() 计算 BLEU-1/2/3/4

依赖环境

pip install torch transformers sentencepiece nltk pyyaml tqdm

测试

python -m pytest tests/ -v

许可

本项目为课程作业。

About

NLP+LLM Final Code

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages