Intent-classification chatbot built from scratch · PyTorch feedforward neural network · NLTK preprocessing · Fully retrainable · CLI interface
A trainable intent-classification chatbot built with Python and PyTorch. It processes natural language input using NLP preprocessing techniques and a feedforward neural network to identify user intent and respond accordingly.
User input goes through three stages:
- Preprocessing (
nltk_utils.py) — tokenization and stemming via NLTK, then converted to a bag-of-words vector. - Classification (
model.py) — a 3-layer feedforward neural network (NeuralNet) maps the BoW vector to an intent class using ReLU activations and CrossEntropyLoss. - Response (
chat.py) — if the predicted intent has confidence above 0.75, a random response from that intent's response pool is returned. Otherwise the bot says it doesn't understand.
MachineLearning-ChatBOT/
├── intents.json # Training data: intent tags, patterns, and responses
├── nltk_utils.py # Tokenization, stemming, bag-of-words helpers
├── model.py # NeuralNet definition (3 linear layers + ReLU)
├── train.py # Training loop — reads intents.json, saves model to data.pth
├── chat.py # Inference loop — loads data.pth, runs conversation
└── data.pth # Saved model weights and vocabulary (generated after training)
Input (BoW vector) → Linear(input_size, 8) → ReLU
→ Linear(8, 8) → ReLU
→ Linear(8, num_classes) → (CrossEntropyLoss at training)
Hyperparameters (defaults):
| Parameter | Value |
|---|---|
| Epochs | 1000 |
| Batch size | 8 |
| Learning rate | 0.001 |
| Hidden size | 8 |
| Optimizer | Adam |
- Python 3.7+
- PyTorch
- NLTK
- NumPy
Install dependencies:
pip install torch nltk numpyDownload NLTK data (first time only):
import nltk
nltk.download('punkt')Edit intents.json to define your own intent tags, training patterns, and responses:
{
"intents": [
{
"tag": "greeting",
"patterns": ["Hi", "Hello", "Hey there"],
"responses": ["Hello!", "Hi there!", "Hey! How can I help?"]
}
]
}python train.pyThis generates data.pth containing the trained model weights and vocabulary. Training progress is printed every 100 epochs.
python chat.pyType your message and press Enter. Type quit to exit.
Let's chat! (type 'quit' to exit)
You: hello
Sam: Hi there! How can I help?
You: quit
To add new topics: Add new intent objects to intents.json, then re-run train.py. No code changes needed.
To adjust confidence threshold: In chat.py, change 0.75 in if prob.item() > 0.75 — lower values make the bot more permissive, higher values make it more conservative.
To change the bot's name: In chat.py, update bot_name = "Sam".
- Not generative. Responses are selected from a predefined pool — the model classifies intent only.
- No conversation context. Each input is processed independently; the bot has no memory of prior turns.
- Vocabulary-bound. Words not seen during training are unknown to the model. Adding new topics requires retraining.
- Small hidden size. The default hidden size of 8 is sufficient for toy datasets. Scale up for larger intent sets.
MIT