A neural network-based text generator trained on Shakespeare's complete works. The model uses LSTM architecture to generate Shakespeare-style text.
- Downloads Shakespeare's complete works automatically
- Uses PyTorch with CUDA/MPS/CPU support
- Optimized for different platforms (Windows, Mac, Linux)
- Supports Apple Silicon (M-series) processors
- Automatic worker optimization for data loading
- Learning rate scheduling for better training
- Python 3.8-3.11 (tested with 3.11.9)
- PyTorch 2.0.1
- CUDA 11.8
- MPS 1.4
- Apple Silicon (M-series) processors
- TensorFlow 2.12.0 (for TF implementation)
pip install -r requirements.txtA pre-trained model (best_model.pt) is included in the repository. To generate text immediately:
python generate.py- Train the model:
python train.pyThis will download Shakespeare's text, preprocess it, and train the model. The model will be saved as best_model.pt. That means you will over write the pre-trained model. If you want to keep the pre-trained model, you can copy that file to a different name.
- Generate text:
python generate.py- LSTM-based neural network
- Character-level text generation
- Embedding layer for character encoding
- Dropout for regularization
- AdamW optimizer with OneCycleLR scheduler
data_loader.py: Downloads and preprocesses Shakespeare's textmodel.py: Neural network model definitiontrain.py: Training script with optimizationgenerate.py: Text generation scriptrequirements.txt: Required Python packagesbest_model.pt: Pre-trained model
The model automatically selects the best available hardware:
- CUDA for NVIDIA GPUs
- MPS for Apple Silicon
- CPU for other systems
Data loading is optimized based on the platform:
- Windows: Single worker for stability
- Other platforms: Automatically uses 80% of available CPU cores