CNN-BiLSTM Classifier
A hybrid CNN+BiLSTM architecture for product classification with attention mechanism, implemented in PyTorch.
Features
- Multi-kernel CNN: Captures local n-gram features with different kernel sizes (2, 3, 4, 5)
- Bidirectional LSTM: Captures sequential dependencies with soft attention mechanism
- Ad-hoc Features: Extracts statistical and linguistic features from text
- Word2Vec Embeddings: Pre-trained word embeddings for better text representation
- Easy Integration: Simple API for training and inference
- HuggingFace Hub Support: Load and save models to/from HuggingFace Hub
Installation
Install from PyPI:
pip install cnn-bilstm-classifier
Or install from source:
git clone https://github.com/turgutguvercin/cnn-bilstm-classifier.git
cd cnn-bilstm-classifier
pip install -e .
Quick Start
Loading a Pre-trained Model
from product-classifier import CNNBiLSTMInference
# Load from HuggingFace Hub
model = CNNBiLSTMInference.from_pretrained("turgutguvercin/product-classifier-v1")
# Make predictions
predictions = model.predict([
"Yataş Bedding BAMBU Yorgan (%20 Bambu) 300 Gr.",
"Arji Ev ve Ofis Çalışma Sandalyesi Bilgisayar Koltuğu"
], top_k=3)
for i, pred in enumerate(predictions):
print(f"Text {i+1} predictions:")
for label, score in pred:
print(f" → {label}: {score:.4f}")
Training a New Model
from product_classifier import CNNBiLSTMTrainer
import pandas as pd
# Load your data
df = pd.read_csv("your_data.csv")
# Initialize trainer
trainer = CNNBiLSTMTrainer()
# Train the model
results = trainer.train(
df,
text_column="product_name",
label_column="category",
config={
'batch_size': 128,
'epochs': 20,
'embedding_dim': 512,
'cnn_filters': 128,
'lstm_hidden': 256
}
)
# Save the model
trainer.save_model("./my_model")
Command Line Interface
Train a model:
cnn-bilstm-train --data data.csv --text-column name --label-column category --output-dir ./model
Make predictions:
cnn-bilstm-predict --model-path ./model --text "Product name to classify"
Model Architecture
The CNN+BiLSTM classifier combines three main components:
-
Multi-kernel CNN Branch:
- Convolutional layers with kernel sizes [2, 3, 4, 5]
- Max pooling over sequence dimension
- Captures local n-gram patterns
-
BiLSTM Branch:
- Bidirectional LSTM layers
- Soft attention mechanism
- Captures long-range dependencies
-
Ad-hoc Features Branch:
- Statistical features (length, character ratios)
- Linguistic features (word count, symbol count)
- Word length histograms
All branches are concatenated and fed to a final classification layer.
API Reference
CNNBiLSTMInference
Main class for model inference.
Methods
from_pretrained(repo_id): Load model from HuggingFace Hubfrom_local(model_dir): Load model from local directorypredict(texts, top_k=1): Predict categories for input textsget_model_info(): Get model architecture information
Example
# Load model
model = CNNBiLSTMInference.from_pretrained("username/model-name")
# Single prediction
prediction = model.predict("Product title", top_k=3)
# Batch prediction
predictions = model.predict(["Title 1", "Title 2"], top_k=1)
# Model info
info = model.get_model_info()
print(f"Vocabulary size: {info['vocab_size']}")
CNNBiLSTMTrainer
Class for training new models.
Methods
train(df, text_column, label_column, config): Train a new modelsave_model(output_dir): Save trained modelsave_to_hub(repo_name): Upload model to HuggingFace Hub
AdHocFeatureExtractor
Extracts statistical and linguistic features from text.
Features Extracted
- Title length
- Uppercase character rate
- Alphabetic character rate
- Digit character rate
- Space count and rate
- Word count
- Maximum word length
- Unique word rate
- Symbol count
- Word length histogram (5 bins)
Configuration
Default training configuration:
config = {
'batch_size': 128,
'epochs': 20,
'learning_rate': 0.001,
'embedding_dim': 512,
'cnn_filters': 128,
'lstm_hidden': 256,
'max_length': 50
}
Requirements
- Python >= 3.8
- PyTorch >= 1.9.0
- NumPy >= 1.21.0
- SafeTensors >= 0.3.0
- HuggingFace Hub >= 0.15.0
- Gensim >= 4.0.0
- scikit-learn >= 1.0.0
- tqdm >= 4.60.0
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Citation
If you use this work in your research, please cite:
@software{product_classifier,
title={CNN-BiLSTM Classifier: Hybrid Architecture for Product Classification},
author={Turgut Guvercin},
year={2025},
url={https://github.com/turgutguvercin/cnn-bilstm-classifier},
note={Implementation based on the methodology from Suzuki et al. (2018)}
}
@inproceedings{suzuki2018cnn,
title={Convolutional Neural Network and Bidirectional LSTM Based Taxonomy Classification Using External Dataset at SIGIR eCom Data Challenge},
author={Suzuki, Shogo D. and Iseki, Yohei and Shiino, Hiroaki and Zhang, Hongwei and Iwamoto, Aya and Takahashi, Fumihiko},
booktitle={Proceedings of ACM SIGIR Workshop on eCommerce (SIGIR 2018 eCom Data Challenge)},
year={2018},
month={July},
address={Ann Arbor, Michigan, USA},
publisher={ACM},
pages={1--5},
url={https://sigir-ecom.github.io/ecom2018/ecom18DCPapers/ecom18DC_paper_1.pdf},
note={Original paper describing CNN+BiLSTM architecture for product taxonomy classification}
}
Acknowledgments
- Based on "Convolutional Neural Network and Bidirectional LSTM Based Taxonomy Classification"
- Built with PyTorch and HuggingFace ecosystem
- Special thanks to the open-source community
Metadata
Release files for product-classifier 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| product_classifier-0.1.0.tar.gz | 33.7 MB | Without provenance | Details for product_classifier-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| product_classifier-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for product_classifier-0.1.0-py3-none-any.whl |
Total release size: 72.5 MB
Release files / product_classifier-0.1.0.tar.gz
| Download URL | product_classifier-0.1.0.tar.gz |
|---|---|
| Size | 33.7 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
68a60d4ba7f263c0f9dcf4f3c4fffb88fffe2ece7b7a6c3e196437960997c75e
|
|
BLAKE2b-256 checksum How to use checksums |
566b14a7ca1cac4e83f6ce48a9770a6a1047b57a7f3f42dc09ad2ee7314bd9b2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.11
|
Release files / product_classifier-0.1.0-py3-none-any.whl
| Download URL | product_classifier-0.1.0-py3-none-any.whl |
|---|---|
| Size | 38.8 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c76ddca2bff3960e62129ff35e017e565eab94b932ce7307a3cb278b59e1aec8
|
|
BLAKE2b-256 checksum How to use checksums |
0dcfbcf20752781015b2f4ca304af537f6ad865f935dfd75ac7366dbcebab7a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.11
|
Security for product-classifier 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.