This project implements a character-level text generation model using PyTorch. We train both RNN and LSTM models on Jules Verne's The Mysterious Island, sourced from Project Gutenberg.
The dataset used is the full text of The Mysterious Island from Project Gutenberg.
- The text is trimmed between:
Start: "THE MYSTERIOUS ISLAND"End: "End of the Project Gutenberg"
- Total characters:
1,112,350 - Unique characters:
80
- Create a character-level vocabulary and mapping (
char2intandchar_array) - Encode the entire text into integers
- Slice into fixed-length chunks of 41 (40 inputs + 1 target)
'THE MYSTERIOUS ISLAND ***\\n\\n\\n\\n\\nProduced b'Two models are implemented using PyTorch:
text Copy Edit
Embedding → LSTM → Fully Connected (FC)text Copy Edit
Embedding → RNN → Fully Connected (FC)Training loop includes:
CrossEntropyLoss
Adam optimizer
Character-level accuracy per epoch
Start Training python Copy Edit
train_model(lstm_model, model_type='lstm')
train_model(rnn_model, model_type='rnn')After training, models are saved as:
lstm_model.pth
rnn_model.pth
To reload:
python Copy Edit
lstm_model.load_state_dict(torch.load('lstm_model.pth'))Automatically detects CUDA-enabled GPUs:
python Copy Edit
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")Example Output text Copy Edit
Input (x): 'THE MYSTERIOUS ISLAND ***\\n\\n\\n\\n\\nProduced b'
Target (y): 'HE MYSTERIOUS ISLAND ***\\n\\n\\n\\n\\nProduced by'Add text sampling/generation logic from trained models
Implement beam search or temperature sampling
Train on other literary texts for comparative results
📦 Requirements Install required libraries:
bash Copy Edit
pip install torch numpy