An end-to-end deep NLP pipeline for text classification using Bidirectional LSTM.
Covers text cleaning, tokenization, sequence modeling, evaluation, and custom inference.
- Overview
- Tech Stack
- Dataset
- Project Structure
- NLP Pipeline
- Model Architecture
- Getting Started
- Model Performance
- Custom Inference
- Author
This project builds a complete deep learning NLP pipeline to classify text responses from a custom dataset. Unlike traditional ML approaches, this project leverages a Bidirectional LSTM (BiLSTM) to capture contextual information from both directions of a text sequence β leading to richer and more accurate representations.
Key highlights:
- Full text preprocessing pipeline (cleaning, tokenization, padding)
- BiLSTM model trained with TensorFlow/Keras
- Evaluation with accuracy, precision, recall, F1-score & confusion matrix
- Predict labels on brand-new custom text inputs
- Full experiment available as a Kaggle Notebook
| Category | Tools |
|---|---|
| Language | Python 3.8+ |
| Deep Learning | TensorFlow, Keras (BiLSTM, Embedding, Dense, Dropout) |
| Data Handling | pandas, numpy |
| ML Utilities | scikit-learn (split, metrics) |
| Visualization | matplotlib, seaborn |
| Environment | Kaggle Notebook, Jupyter, VS Code |
| Field | Description |
|---|---|
| File | Sheet_1.csv |
response_text |
Raw input text response |
class |
Target label β flagged or not_flagged |
| Split | 80% Train / 20% Validation (stratified) |
Unused columns like
Unnamed: ...are automatically dropped during preprocessing.
deep-nlp-text-classification-bilstm/
β
βββ π deep_nlp_bilstm.ipynb # Main notebook: preprocessing, training & evaluation
βββ π Sheet_1.csv # Custom dataset (text responses + labels)
βββ π€ bilstm_model.h5 # Saved trained BiLSTM model
βββ π€ tokenizer.pkl # Saved Keras tokenizer
βββ π predict.py # Script for inference on new text
βββ π requirements.txt # Python dependencies
βββ π README.md # Project documentation
Raw Text (response_text)
β
βΌ
ββββββββββββββββββββββββββββββββ
β Text Preprocessing β
β β’ Lowercase β
β β’ Remove URLs & @mentions β
β β’ Remove special characters β
β β’ Strip extra whitespace β
ββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββ
β Tokenization & Padding β
β β’ Vocab size: 20,000 words β
β β’ Convert text β sequences β
β β’ Pad to fixed max length β
ββββββββββββββββββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββ
β BiLSTM Neural Network β
β β’ Embedding Layer β
β β’ Bidirectional LSTM β
β β’ Dense + Dropout β
β β’ Sigmoid / Softmax output β
ββββββββββββββββββββββββββββββββ
β
βΌ
Prediction + Confidence
(flagged / not_flagged)
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Embedding (None, max_len, 128) 2,560,000
Bidirectional LSTM (None, 256) 263,168
Dropout (0.5) (None, 256) 0
Dense (64, ReLU) (None, 64) 16,448
Dropout (0.3) (None, 64) 0
Dense (1, Sigmoid) (None, 1) 65
=================================================================
| Hyperparameter | Value |
|---|---|
| Vocabulary Size | 20,000 |
| Embedding Dim | 128 |
| LSTM Units | 128 (Γ 2 bidirectional) |
| Optimizer | Adam |
| Loss | Binary Crossentropy |
| Epochs | ~10 |
| Batch Size | 32 |
git clone https://github.com/Musawir456/Deep-NLP-Text-Classifier-BiLSTM-.git
cd Deep-NLP-Text-Classifier-BiLSTM-python -m venv venv
# Windows
venv\Scripts\activate
# macOS/Linux
source venv/bin/activatepip install -r requirements.txtjupyter notebook deep_nlp_bilstm.ipynb| Metric | Score |
|---|---|
| Accuracy | ~XX% |
| Precision | ~XX% |
| Recall | ~XX% |
| F1-Score | ~XX% |
π Update this table with your actual results after training.
Run predictions on any new text:
import pickle
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
# Load model and tokenizer
model = load_model("bilstm_model.h5")
with open("tokenizer.pkl", "rb") as f:
tokenizer = pickle.load(f)
MAX_LEN = 100 # same as training
def predict(text):
seq = tokenizer.texts_to_sequences([text])
padded = pad_sequences(seq, maxlen=MAX_LEN, padding='post')
prob = model.predict(padded)[0][0]
label = "π© flagged" if prob >= 0.5 else "β
not_flagged"
print(f"Prediction : {label}")
print(f"Confidence : {prob:.4f}")
predict("Your custom text goes here...")β If you found this project helpful, please give it a star! β
Made with β€οΈ by Abdul Musawir