B.Tech Computer Science — Semester 6 Project | 2023 Subject: Neural Networks
This project was developed as part of my Neural Networks course. The goal was to build a chatbot from scratch using a Sequence-to-Sequence (Seq2Seq) neural network architecture with an Attention mechanism — without using any pre-trained models.
The chatbot is trained on the Cornell Movie Dialogs Corpus which contains over 220,000 real conversational exchanges from movie scripts. It uses an LSTM-based Encoder-Decoder model with Bahdanau Attention that allows it to give context-aware responses.
Everything was built from scratch to understand how neural network architectures work internally, not just how to use them as black boxes.
Seq2Seq (Sequence-to-Sequence) is a type of neural network model that takes a sequence of words as input and produces another sequence as output. It was originally developed for machine translation but works well for chatbots too.
It has two main parts:
- Encoder — reads the input sentence and compresses it into a fixed-size "context vector" (like a summary of what was said)
- Decoder — takes that context vector and generates the response word by word
Basic Seq2Seq has one big limitation: the entire input sentence is compressed into a single vector. For long sentences, this loses a lot of information.
Bahdanau Attention (2015) solves this by letting the decoder "look back" at all encoder outputs at every step and decide which input words are most relevant right now. This significantly improves response quality.
Input: "what is your name ?"
↓ ↓ ↓ ↓ ↓
[Encoder: LSTM processes each word]
↓ (attention weights computed at each decoder step)
[Decoder: LSTM generates output word by word]
↓ ↓ ↓
Output: "my name is chatbot"
Intelligent_Chatbot/
│
├── app.py ← Flask web app (run this to chat)
├── train.py ← Training script (run this first)
├── model.py ← Model wrapper exporting build_model
├── seq2seq.py ← Full Seq2Seq Model
├── encoder.py ← LSTM Encoder
├── decoder.py ← LSTM Decoder with Attention
├── attention.py ← Bahdanau Attention Mechanism
├── requirements.txt ← Python dependencies
├── README.md ← This file
│
├── templates/
│ └── index.html ← Chat web interface (Flask template)
│
├── utils/ ← Utility modules package
│ ├── __init__.py ← Package initialization
│ ├── vocabulary.py ← Word-to-index mapping
│ ├── preprocess.py ← Text cleaning + Cornell dataset loader
│ └── evaluate.py ← BLEU score evaluation
│
├── data/ ← Cornell dataset (auto-downloaded during training)
├── checkpoints/ ← Trained model checkpoints
│ ├── seq2seq_best.pt ← Best model weights
│ └── vocabulary.pkl ← Saved vocabulary
└── venv/ ← Python virtual environment
- Python 3.10 or higher
- NVIDIA GPU recommended (training takes ~1 hour on GPU, several hours on CPU)
git clone https://github.com/ram-cs7/Intelligent_Chatbot.git
cd Intelligent_ChatbotWindows:
python -m venv venv
venv\Scripts\activateLinux / Mac:
python3 -m venv venv
source venv/bin/activatepip install -r requirements.txtThis will install:
torch— Deep learning framework (PyTorch)torchvision,torchaudio— PyTorch ecosystemflask— Web framework for the chatbot UInumpy— Numerical computing
Note: For GPU-accelerated training on NVIDIA GPUs, install PyTorch with CUDA support:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
python train.pyThis will automatically:
- Download the Cornell Movie Dialogs dataset (~10 MB)
- Parse ~118,000 conversation pairs from the dataset
- Build the vocabulary (~5,700 words with min frequency ≥ 10)
- Train for up to 30 epochs with early stopping (patience=5)
- Save the best checkpoint based on validation loss
Training Features:
- Learning rate scheduling (ReduceLROnPlateau)
- Early stopping to prevent overfitting
- Gradient clipping for stable training
- Automatic GPU/CPU detection
Training Time:
- GPU (NVIDIA RTX 4050 or similar): ~1 hour
- CPU: ~4-5 hours
python app.pyOpen your browser and navigate to: http://localhost:5000
| Component | Details |
|---|---|
| Architecture | Seq2Seq LSTM with Bahdanau Attention |
| Encoder | 2-layer LSTM, hidden size 512 |
| Decoder | 2-layer LSTM + Attention, hidden size 512 |
| Embedding size | 256 |
| Dropout | 0.3 |
| Vocabulary size | ~5,700 words (min frequency ≥ 10) |
| Total parameters | ~17.8 million |
| Optimizer | Adam (lr=0.0005) |
| LR Scheduler | ReduceLROnPlateau (factor=0.5, patience=2) |
| Loss function | Cross-Entropy (padding ignored) |
| Teacher forcing | 0.7 (70% ground truth during training) |
| Max epochs | 30 (with early stopping, patience=5) |
| Batch size | 64 |
| Decoding strategy | Top-k sampling (k=10, temperature=0.8) |
| Metric | Value |
|---|---|
| Best Val Loss | 4.9929 |
| Best Epoch | 4 |
| Epochs Run | 9 (early stopping triggered) |
| Training Device | NVIDIA GeForce RTX 4050 Laptop GPU |
Note: Open-domain conversational AI is a hard problem. LSTM-based Seq2Seq models produce reasonable but imperfect responses — this is expected behavior and a known limitation of the architecture. The project demonstrates understanding of the neural network concepts, not production-level chatbot quality.
- User types a message in the web interface
- Message is cleaned (lowercase, remove special chars, etc.)
- Words are converted to numbers using vocabulary lookup
- Encoder LSTM processes the input sequence step by step
- Attention weights are computed at each decoder step
- Decoder LSTM generates the response word by word using top-k sampling
- Words are converted back from numbers to text
- Response is displayed in the chat window along with the detected intent
Intent Classification: Rule-based regex patterns label each message (greeting, question, farewell, emotion, etc.)
Decoding Strategy: Instead of greedy decoding (which tends to produce repetitive outputs), we use top-k sampling with temperature scaling. This introduces controlled randomness for more natural and diverse responses.
- LSTM (Long Short-Term Memory) — Recurrent neural network architecture that handles sequential data
- Encoder-Decoder Architecture — Two-part model for sequence-to-sequence tasks
- Bahdanau Attention — Mechanism that lets the decoder focus on relevant parts of the input
- Teacher Forcing — Training technique where ground truth is fed back to the decoder
- Top-k Sampling — Decoding strategy that samples from the k most likely next words
- Gradient Clipping — Prevents exploding gradients during backpropagation
- Learning Rate Scheduling — Automatically reduces learning rate when training plateaus
- Early Stopping — Stops training when validation loss stops improving
- Try Transformer architecture instead of LSTM
- Implement beam search decoding
- Use a larger or domain-specific dataset
- Add pre-trained word embeddings (GloVe/Word2Vec)
- Deploy on a cloud server
- Sutskever, I., Vinyals, O., & Le, Q. V. (2014). Sequence to Sequence Learning with Neural Networks. NeurIPS 2014.
- Bahdanau, D., Cho, K., & Bengio, Y. (2015). Neural Machine Translation by Jointly Learning to Align and Translate. ICLR 2015.
- Fan, A., Lewis, M., & Dauphin, Y. (2018). Hierarchical Neural Story Generation. ACL 2018. (Top-k sampling)
- Danescu-Niculescu-Mizil, C., & Lee, L. (2011). Chameleons in Imagined Conversations. ACL Workshop 2011. (Cornell Movie Dialogs Corpus)
- Papineni, K., et al. (2002). BLEU: a Method for Automatic Evaluation of Machine Translation. ACL 2002.
Sairam Chennaka B.Tech - Computer Science & Engineering, Shoolini University
This project was done purely for learning purposes as part of the Neural Networks course curriculum. All dataset credits go to their respective authors.