Skip to content

This repository implements a GPU-accelerated next-word prediction model using PyTorch and LSTM. It includes data preprocessing with NLTK, vocabulary creation, training on tokenized text, and generating text predictions, starting from a given input phrase.

Notifications You must be signed in to change notification settings

MUHAMMADAKMAL137/GPU-Accelerated-Next-Word-Prediction-Using-LSTM-and-PyTorch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

3 Commits
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”ฎ Next-Word Prediction Using LSTM with PyTorch (GPU-Accelerated)

This repository presents a next-word prediction model built with PyTorch using an LSTM architecture, optimized for GPU acceleration. It includes everything from text preprocessing to model training and next-word generation. This serves as an educational and practical introduction to natural language generation using deep learning.


Features

  • โœ… Tokenization and preprocessing using NLTK
  • โœ… Dynamic vocabulary creation with <UNK> token handling
  • โœ… Training data preparation with padded sequences
  • โœ… Custom PyTorch Dataset and DataLoader
  • โœ… LSTM-based neural network with embedding and linear layers
  • โœ… GPU support via CUDA (if available)
  • โœ… Next-word prediction from user-defined input
  • โœ… Loop-based text generation for full sentence construction

Example Output

Input: "Zero-shot learning"
Generated: "Zero-shot learning is a powerful AI technique that enables models to learn new"
Install dependencies:

bash
Copy
Edit
pip install torch nltk
Download required NLTK data:

python
Copy
Edit
import nltk
nltk.download('punkt')
nltk.download('stopwords')
Usage
Set your training data
Modify the document string in the script with your own dataset (e.g., "Exploring Data Availability in LLM Development").

Train the model
Run the full script to tokenize, create sequences, and train the LSTM model.

Generate predictions
Use the prediction(model, vocab, text) function to predict the next word.

Generate full sequences
The script includes a loop to auto-generate multiple words:

python
Copy
Edit
input_text = "Zero-shot learning"
for _ in range(25):
    output = prediction(model, vocab, input_text)
    print(output)
    input_text = output
Model Architecture
Embedding Layer: Converts token IDs to dense vectors

LSTM Layer: Learns temporal dependencies

Linear Layer: Outputs scores for each vocabulary word

Requirements
Python 3.x

PyTorch

NLTK

Applications
Language modeling

Predictive text systems

Educational demonstrations in NLP and deep learning

Building blocks for advanced generative AI models

About

This repository implements a GPU-accelerated next-word prediction model using PyTorch and LSTM. It includes data preprocessing with NLTK, vocabulary creation, training on tokenized text, and generating text predictions, starting from a given input phrase.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published