Skip to content

fabrisio-ponte/generative_writing_nn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

Character-Level Text Generator using RNN & LSTM

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.

Dataset

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

Preprocessing

  • Create a character-level vocabulary and mapping (char2int and char_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'

Model Architectures

Two models are implemented using PyTorch:

LSTMModel

text Copy Edit

Embedding → LSTM → Fully Connected (FC)

RNNModel

text Copy Edit

Embedding → RNN → Fully Connected (FC)

Training

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')

Model Saving

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'))

Device Support

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'

Future Work

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

About

RNN_JV

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published