How shivammitra/lstm-and-gru-predicting-next-word works
Purpose and actual repository shape
This is a compact Python NLP experiment for word-level next-word prediction using recurrent neural networks. The checked-in tree differs from the README’s proposed notebook/module layout: the primary executable artifact is app.py, with one experimentation notebook, a Hamlet corpus, and a serialized tokenizer.
Paths: app.py, experiemnts.ipynb, hamlet.txt
Entry points and execution modes
• app.py is the application entry point and likely serves inference or an interactive prediction workflow using the trained/tokenized language-model assets.
• experiemnts.ipynb is the exploratory/training entry point, intended for iterative data preparation, LSTM/GRU experimentation, evaluation, and model comparison.
• Jupyter is a material runtime for reproducing experiments; Python dependencies are defined in requirements.txt.
Paths: app.py, experiemnts.ipynb, requirements.txt
Corpus ingestion and text preprocessing
The model’s source corpus is Shakespeare’s Hamlet in hamlet.txt. The preprocessing stage normalizes/tokenizes corpus text and transforms it into word-index sequences suitable for supervised language-model training. The persisted tokenizer defines the vocabulary-to-index contract and must remain compatible with any model consumed by the app.
Paths: hamlet.txt, tokenizer.pickle, experiemnts.ipynb
Sequence-generation boundary
Training converts tokenized text into fixed-context input sequences and a target next-word label. This is the key boundary between raw NLP data and numeric ML tensors: sequence length, vocabulary size, padding/encoding strategy, and label representation must be identical at training and prediction time.
Paths: experiemnts.ipynb, tokenizer.pickle
Recurrent modeling and comparison
The architecture centers on alternative recurrent language models: LSTM and GRU. Both consume the same encoded word-context inputs and produce a vocabulary-sized next-token prediction distribution. The notebook is the likely location for selecting embedding dimensions, recurrent units, optimization, epochs, and comparing loss/accuracy or qualitative generated outputs.
Paths: experiemnts.ipynb, README.md
Inference flow
Inference accepts prompt text, loads or uses tokenizer.pickle, converts the prompt to the trained index space, takes the relevant context window, obtains next-word scores from the model, and maps the selected index back to a word. The tokenizer is the essential persisted cross-process artifact; model artifact location is not present in the supplied tree and should be verified before deployment.
Paths: app.py, tokenizer.pickle
Runtime and external dependencies
The project runs locally in Python, with TensorFlow/Keras as the expected deep-learning runtime and Jupyter for experimentation. No deployed infrastructure, database, API, queue, or third-party online service is evidenced. Corpus and tokenizer are local files; model persistence/loading is an implicit requirement of app.py but no checked-in model file is shown.
Paths: requirements.txt, app.py, hamlet.txt
Main entry points: experiemnts.ipynb Text preprocessing · tokenizer.pickle Persisted tokenizer · app.py Prompt encoding · requirements.txt Python dependencies
Components 14
Training & experiments
Notebook-driven corpus preparation, model comparison, and artifact generation.
- Hamlet corpus · Local Shakespeare source text for language-model training.
hamlet.txt - Experiment notebook · Prepares data, trains recurrent models, and evaluates alternatives.
experiemnts.ipynb - Text preprocessing · Normalizes corpus text and converts words to indices.
experiemnts.ipynb - Sequence generation · Builds fixed-context inputs and next-word labels.
experiemnts.ipynb - Encoded sequences · Context and label representation shared by recurrent models.
experiemnts.ipynb - LSTM model · Predicts a vocabulary distribution from word context.
experiemnts.ipynb - GRU model · Alternative recurrent model evaluated on the same sequences.
experiemnts.ipynb - Model evaluation · Compares loss, accuracy, and generated output quality.
experiemnts.ipynb
Local inference
Python application predicts the next word from a prompt.
- Prediction app · Loads language-model assets and runs local next-word prediction.
app.py - Prompt encoding · Tokenizes the prompt and selects the trained context window.
app.py - Next-word result · Maps selected token index back to a word.
app.py
Other components
- Python dependencies · Defines local Python, TensorFlow/Keras, and Jupyter dependencies.
requirements.txt - Persisted tokenizer · Vocabulary-to-index contract required by training and inference.
tokenizer.pickle - Trained model artifact · Required by inference, but no checked-in model location is evidenced.
Connections 18
- Python dependencies → Experiment notebook: supports
- Python dependencies → Prediction app: supports
- Hamlet corpus → Text preprocessing: source text
- Experiment notebook → Text preprocessing: runs
- Text preprocessing → Persisted tokenizer: fits and persists
- Text preprocessing → Sequence generation: token indices
- Sequence generation → Encoded sequences: contexts and labels
- Encoded sequences → LSTM model: trains
- Encoded sequences → GRU model: trains
- LSTM model → Model evaluation: metrics and samples
- GRU model → Model evaluation: metrics and samples
- LSTM model → Trained model artifact: selected model saved
- GRU model → Trained model artifact: selected model saved
- Prediction app → Prompt encoding: accepts prompt
- Persisted tokenizer → Prompt encoding: encodes words
- Prompt encoding → Trained model artifact: context indices
- Trained model artifact → Next-word result: next-token scores
- Persisted tokenizer → Next-word result: decodes index