Comparative analysis of sequence models (RNN, LSTM, Transformer) with optimization algorithms (Adam, RMSprop, SGD) for Urdu poetry text generation.
Dataset: ReySajju742/Urdu-Poetry-Dataset (1,323 poems, classical Urdu poetry) Task: Next-word prediction using n-gram sequences
├── datasetFormatted/ # Preprocessed train/val/test splits (80-10-10)
├── tokenizer/ # Keras tokenizer & config
├── rnn/baseline/ # RNN experiments
├── lstm/baseline/ # LSTM experiments
├── transformer/baseline/ # Transformer experiments
├── preprocess.py # Data extraction & n-gram creation
├── train_rnn.py # RNN training (3 optimizers)
├── train_lstm.py # LSTM training (3 optimizers)
├── train_transformer.py # Transformer training (3 optimizers)
└── generate_*.py # Text generation scripts
┌─────────────┬──────┬─────────┬─────┐ │ Architecture│ Adam │ RMSprop │ SGD │ ├─────────────┼──────┼─────────┼─────┤ │ RNN │ ✓ │ ✓ │ ✓ │ │ LSTM │ ✓ │ ✓ │ ✓ │ │ Transformer │ ✓ │ ✓ │ ✓ │ └─────────────┴──────┴─────────┴─────┘
- Vocabulary: ~10,500 Urdu tokens
- Sequence length: 20 tokens (capped)
- Batch size: 128
- Early stopping: patience=5
- Dropout: 0.2
- Embedding dim: 128
RNN: 2-layer SimpleRNN (128 units each)
LSTM: 2-layer LSTM (128 units each)
Transformer: 4 attention heads, 2 blocks, FFN=512
- Adam/RMSprop: 0.001
- SGD: 0.01 (momentum=0.9)
45 samples per model (5 seeds × 3 temperatures × 3 optimizers)
Seeds: محبت (love), دل (heart), شام (evening), یاد (memory), خوشی (happiness) Temperatures: 0.7 (conservative), 1.0 (balanced), 1.3 (creative)
Metrics evaluated:
- Vocabulary diversity (unique/total words)
- Repetition rate (repeated bigrams)
- Average word length (characters)
pip install tensorflow numpy pandas scikit-learn datasets
python preprocess.py
python train_rnn.py
python train_lstm.py
python train_transformer.py
python generate_rnn.py python generate_lstm.py python generate_transformer.py
python analyze_results.py # Comparison tables & plots python analyze_generations.py # Generation quality metrics
Results: *_results.pkl (test metrics: perplexity, accuracy, loss, time) Models: *_model.keras (trained weights) Plots: *_training.png (loss/accuracy curves) Generations: generations.csv (45 samples per model) Metrics: generation_metrics.csv (quantitative analysis)
Primary: Perplexity (lower = better) Secondary: Accuracy, training time Qualitative: Fluency, coherence, poetic quality (manual evaluation)
- Models use sparse_categorical_crossentropy (efficient for large vocab)
- N-gram approach eliminates need for causal masking
- Text is tokenized word-level (Keras Tokenizer)
- Urdu RTL direction handled naturally by tokenization order
- Test set (10%) held out for final evaluation only