This project implements and compares Chinese-English machine translation using RNN and Transformer architectures, as required for the NLP course final project.
GitHub Repository: https://github.com/ShiboSusu/SLAI_LLM_Final_Proj
Due to GitHub's file size limitations (100MB), trained model files are not included in this repository.
Coming Soon: Models will be hosted on Hugging Face Hub or Google Drive.
For now, you can:
- Contact via course platform to get model files
- Or retrain the models yourself (see Training section below)
# Quick retrain (uses 100k dataset, ~24 hours on GPU)
bash quick_retrain.sh
# Or train specific models
python train.py --model_type transformer --train_path dataset/.../train_100k.jsonl ...If you obtain the pre-trained models, you'll need:
trans_improved.pt(216MB) - Best Transformer model (BLEU: 13.79)trans_improved_tokenizers.pt(3.4MB) - Corresponding tokenizersrnn_dot_improved.pt(442MB) - Best RNN model (BLEU: 12.57)rnn_dot_improved_tokenizers.pt(3.4MB) - Corresponding tokenizers
# Activate conda environment
source ~/miniconda3/etc/profile.d/conda.sh
conda activate nlp# Easy one-command inference (uses best Transformer model)
./run_inference.sh "你好,世界!"# RNN model with beam search
python inference.py \
--model_type rnn \
--checkpoint rnn_dot_improved.pt \
--tokenizers rnn_dot_improved_tokenizers.pt \
--sentence "你好,世界!" \
--beam_size 5
# Transformer model
python inference.py \
--model_type transformer \
--checkpoint trans_improved.pt \
--tokenizers trans_improved_tokenizers.pt \
--sentence "你好,世界!" \
--beam_size 5
# T5 model
python -c "
from transformers import T5ForConditionalGeneration, T5Tokenizer
import torch
model = T5ForConditionalGeneration.from_pretrained('./t5_nmt_model')
tokenizer = T5Tokenizer.from_pretrained('./t5_nmt_model')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)
text = 'translate Chinese to English: 你好,世界!'
inputs = tokenizer(text, return_tensors='pt').to(device)
outputs = model.generate(inputs.input_ids, max_length=128)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
"nlp/
├── dataset/ # Training data
│ └── AP0004_Midterm&Final_translation_dataset_zh_en/
│ ├── train_10k.jsonl # Small training set
│ ├── train_100k.jsonl # Large training set
│ ├── valid.jsonl # Validation set
│ └── test.jsonl # Test set
├── rnn_model.py # RNN encoder-decoder with attention
├── transformer_model.py # Transformer implementation
├── data_utils.py # Data loading and tokenization
├── train.py # Training script
├── inference.py # Inference with beam search
├── comprehensive_evaluate.py # Comprehensive evaluation
├── t5_finetune.py # T5 fine-tuning
├── evaluate_t5.py # T5 evaluation
├── analyze_results.py # Result visualization
├── quick_retrain.sh # Quick retraining script
└── run_all_experiments.sh # Complete experiment suite
- Encoder: 2-layer bidirectional GRU/LSTM
- Decoder: 2-layer unidirectional GRU/LSTM with attention
- Attention Mechanisms: Dot-product, Multiplicative, Additive
- Features: Teacher forcing, beam search decoding
- Architecture: Standard encoder-decoder Transformer
- Configurations:
- Baseline: LayerNorm + Absolute positional encoding
- Variant 1: RMSNorm + Absolute positional encoding
- Variant 2: LayerNorm + Relative positional encoding (ALiBi)
- Hyperparameters: d_model=256, n_layers=4, n_heads=8
- Base Model: google-t5/t5-small
- Task Format: "translate Chinese to English: [source]"
- Training: Fine-tuned on 100k parallel sentences
bash quick_retrain.shThis script:
- Trains RNN with dot-product attention
- Trains Transformer baseline
- Fine-tunes T5
- Evaluates all models on test set
bash run_all_experiments.shThis runs all ablation studies and comparisons.
RNN Example:
python train.py \
--model_type rnn \
--train_path dataset/AP0004_Midterm\&Final_translation_dataset_zh_en/train_100k.jsonl \
--valid_path dataset/AP0004_Midterm\&Final_translation_dataset_zh_en/valid.jsonl \
--epochs 20 \
--batch_size 64 \
--learning_rate 0.001 \
--emb_dim 256 \
--hid_dim 512 \
--n_layers 2 \
--dropout 0.3 \
--rnn_cell GRU \
--attn_type dot \
--teacher_forcing_ratio 0.5 \
--patience 5 \
--save_path my_rnn_model.ptTransformer Example:
python train.py \
--model_type transformer \
--train_path dataset/AP0004_Midterm\&Final_translation_dataset_zh_en/train_100k.jsonl \
--valid_path dataset/AP0004_Midterm\&Final_translation_dataset_zh_en/valid.jsonl \
--epochs 20 \
--batch_size 64 \
--learning_rate 0.0005 \
--emb_dim 256 \
--hid_dim 1024 \
--n_layers 4 \
--nhead 8 \
--dropout 0.1 \
--norm_type layernorm \
--pos_type absolute \
--patience 5 \
--save_path my_transformer_model.ptpython comprehensive_evaluate.py \
--model_type rnn \
--checkpoint rnn_dot_improved.pt \
--tokenizers rnn_dot_improved_tokenizers.pt \
--test_path dataset/AP0004_Midterm\&Final_translation_dataset_zh_en/test.jsonl \
--beam_sizes 1 3 5 \
--save_samplesThis generates:
- BLEU and chrF scores for different beam sizes
- Inference time measurements
- Sample translations for qualitative analysis
python analyze_results.pyGenerates:
- Loss curve comparisons
- BLEU score bar charts
- Inference time plots
- Comprehensive summary table (results_summary.md)
- Early Stopping: Prevents overfitting with patience-based stopping
- Gradient Clipping: Stabilizes training (max_norm=1.0)
- Learning Rate Scheduling: ReduceLROnPlateau for better convergence
- Beam Search: Improves translation quality (beam sizes: 1, 3, 5, 10)
- Larger Vocabulary: min_freq=1 to reduce unknown tokens
- 100k Dataset: Better vocabulary coverage and model generalization
- BLEU: Primary metric for translation quality
- chrF: Character-level F-score
- Inference Time: Speed measurements
- Qualitative Analysis: Sample translation inspection
Results will be available after training completes. Check:
*_evaluation_results.json- Detailed metrics*_samples.txt- Translation samplesresults_summary.md- Comprehensive summaryplots/- Visualizations
- Python 3.10
- PyTorch 2.1.0+
- transformers
- sacrebleu
- jieba (Chinese tokenization)
- matplotlib, seaborn (visualization)
- tqdm (progress bars)
Reduce batch size:
--batch_size 32 # or 16Use smaller dataset:
--train_path dataset/.../train_10k.jsonl- Check vocabulary size (should be > 10k)
- Ensure sufficient training epochs
- Try larger model or beam search
- Attention is All You Need (Vaswani et al., 2017)
- Neural Machine Translation by Jointly Learning to Align and Translate (Bahdanau et al., 2015)
- Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer (Raffel et al., 2020)
- PyTorch Seq2Seq Tutorial
Submission Date: December 28, 2025