---
title: rnn-zh-en-translator
canonical_url: "https://www.modelscope.cn/models/chenhao12/rnn-zh-en-translator"
md_url: "https://www.modelscope.cn/models/chenhao12/rnn-zh-en-translator.md"
repository: chenhao12/rnn-zh-en-translator
chinese_name: "RNN中英翻译器"
last_updated: 2026-08-06
pipeline_tag: translation
tasks:
  - translation
library_name:
  - pytorch
downloads: 7
stars: 1
license: mit
language:
  - zh
  - en
tags:
  - text-generation
  - machine-translation
  - rnn
  - gru
  - seq2seq
task:
  - text2text-generation
---

# rnn-zh-en-translator

> rnn-zh-en-translator - chenhao12 在 ModelScope 开源的模型。RNN 中英翻译器（GRU + Bahdanau Attention）

- **Repository**: chenhao12/rnn-zh-en-translator
- **Tasks**: translation
- **Downloads**: 7
- **Stars**: 1
- **Last updated**: 2026-08-06

Source: https://www.modelscope.cn/models/chenhao12/rnn-zh-en-translator

---

# RNN 中英翻译器（GRU + Bahdanau Attention）

一个从零构建的 **Seq2Seq + Attention** 中文 → 英文神经机器翻译模型，使用纯 RNN（GRU）实现，不依赖任何预训练模型或外部词向量。模型体积小（约 **339 万参数**，22 MB），CPU 即可运行。

## 模型信息

| 项目 | 内容 |
|------|------|
| 架构 | Seq2Seq：GRU Encoder + Bahdanau Attention + GRU Decoder |
| 参数 | embed=128, hidden=384, 1 层, 参数量 ~3.39M |
| 分词 | 中文按字切分，英文按词切分 |
| 训练数据 | 内置 2335 对中英句子（train=1960 / val=245 / test=130） |
| 训练配置 | Adam lr=1e-3, dropout=0.4, label_smoothing=0.1, weight_decay=1e-5, teacher forcing 线性衰减, early stopping, bucket 采样 |
| 运行环境 | Python 3.10+, PyTorch 2.0+, CPU 即可 |

## 效果指标（固定测试集 130 句）

| 指标 | 值 |
|------|-----|
| BLEU-4 | **0.2759** |
| 精确匹配率 | **10.77%** |
| 最佳 val_loss | 3.4764 |
| 推理速度 | 单句 < 0.1s（CPU） |

> 说明：这是小数据（2335 对）下从零训练的纯 RNN 模型，BLEU 指标与大规模预训练模型不可比。它能稳定翻译语料覆盖的高频日常句式，对未见句式泛化有限（见"局限性"）。

## 快速开始

```bash
# 1. 安装依赖
pip install torch

# 2. 下载本模型到本地目录
pip install modelscope
python -c "
from modelscope import snapshot_download
snapshot_download('chenhao12/rnn-zh-en-translator', local_dir='rnn-zh-en')
"

# 3. 运行示例
cd rnn-zh-en
python example.py
```

示例输出：

```
中文: 你好
英文: hello
中文: 我喜欢吃苹果
英文: i like eating apples
中文: 今天天气很好
英文: the weather is nice today
中文: 洗手间在哪里
英文: where is the restroom
中文: 这个多少钱
英文: how much is this
```

## 在代码中使用

```python
import sys
sys.path.insert(0, "rnn-zh-en")

import torch
from config import Config
from src.vocab import Vocab
from src.evaluate import load_model
from src.translate import translate_text

# 加载模型（结构参数自动从 checkpoint 读取）
cfg = Config()
src_vocab = Vocab.load("rnn-zh-en/vocabs/src_vocab.json")
tgt_vocab = Vocab.load("rnn-zh-en/vocabs/tgt_vocab.json")
model = load_model("rnn-zh-en/best.pt", cfg, src_vocab, tgt_vocab,
                   torch.device("cpu"))

# 翻译（beam_width=0 为贪心；>0 用 beam search）
out = translate_text(model, src_vocab, tgt_vocab, "洗手间在哪里",
                     torch.device("cpu"), beam_width=0)
print(out)  # where is the restroom
```

也可以直接用命令行：

```bash
python main.py translate "你好"
python main.py interactive      # 交互式翻译
```

## 文件说明

```
├── best.pt            # 模型权重（含训练时完整配置）
├── config.py          # 超参数配置
├── main.py            # 命令行入口
├── example.py         # 一键示例脚本
├── requirements.txt
├── vocabs/            # 中英词表
└── src/               # 源码（模型/分词/词表/推理）
```

## 训练过程

- **数据**：2335 对日常中英句子（内置语料），固定 test 集（130 句，无 OOV），保证实验可比
- **分词**：中文按字、英文按词（小语料下 BPE 为负优化，故默认关闭）
- **训练**：Adam + label smoothing + dropout + teacher forcing 线性衰减 + early stopping（22 epoch）
- **加速**：bucket 采样（随机分组 + 组内按句长排序）将目标侧 pad 从 ~44% 降到 ~20%，epoch 提速 30~39%，不改变训练语义

## 局限性

- **数据量小**（2335 对），对未见过句式/生僻词泛化有限，长句生成可能出错或重复
- 只支持中文 → 英文单一方向
- 纯 RNN 架构，长句效果弱于 Transformer 类模型

## License

MIT
