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network (PyTorch)
|
| 12 |
+
|
| 13 |
+
[](https://www.python.org/)
|
| 14 |
+
[](https://pytorch.org/)
|
| 15 |
+
[](LICENSE)
|
| 16 |
+
|
| 17 |
+
A **simple, real-world spam detection neural network** built from scratch in **PyTorch**.
|
| 18 |
+
This model classifies SMS / short text messages as **Spam** or **Ham (Not Spam)**.
|
| 19 |
+
|
| 20 |
+
The project is **small, easy to understand, and perfect for learning**.
|
| 21 |
+
You can fork it, fine-tune it, and use it as a **starting point for your own projects**.
|
| 22 |
+
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
## π§ Model Overview
|
| 26 |
+
|
| 27 |
+
- **Framework:** PyTorch
|
| 28 |
+
- **Architecture:** Fully Connected Neural Network (MLP)
|
| 29 |
+
- **Input:** Bag-of-Words text vectors
|
| 30 |
+
- **Output:** Binary classification (Spam / Ham)
|
| 31 |
+
- **Training:** From scratch, small dataset (~5,500 messages)
|
| 32 |
+
|
| 33 |
+
> β οΈ Note: The dataset is intentionally small to keep things simple.
|
| 34 |
+
> You are encouraged to **fork the repo, add more data, and fine-tune the model**.
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
## π Repository Structure
|
| 39 |
+
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
.
|
| 43 |
+
βββ spam_nn.pth # Trained PyTorch model weights
|
| 44 |
+
βββ vectorizer.pkl # CountVectorizer for text preprocessing
|
| 45 |
+
βββ model.py # Neural network architecture
|
| 46 |
+
βββ config.json # Model configuration
|
| 47 |
+
βββ inference.py # Inference / prediction script
|
| 48 |
+
βββ README.md # Documentation
|
| 49 |
+
|
| 50 |
+
````
|
| 51 |
+
|
| 52 |
+
---
|
| 53 |
+
|
| 54 |
+
## π Usage
|
| 55 |
+
|
| 56 |
+
### Load Model
|
| 57 |
+
|
| 58 |
+
```python
|
| 59 |
+
import torch
|
| 60 |
+
from model import SpamNN
|
| 61 |
+
import pickle
|
| 62 |
+
|
| 63 |
+
# Load model architecture + weights
|
| 64 |
+
model = SpamNN()
|
| 65 |
+
model.load_state_dict(torch.load("spam_nn.pth"))
|
| 66 |
+
model.eval()
|
| 67 |
+
|
| 68 |
+
# Load vectorizer
|
| 69 |
+
with open("vectorizer.pkl", "rb") as f:
|
| 70 |
+
vectorizer = pickle.load(f)
|
| 71 |
+
````
|
| 72 |
+
|
| 73 |
+
### Predict Messages
|
| 74 |
+
|
| 75 |
+
```python
|
| 76 |
+
def predict(text):
|
| 77 |
+
vec = vectorizer.transform([text]).toarray()
|
| 78 |
+
vec = torch.tensor(vec, dtype=torch.float32)
|
| 79 |
+
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
output = model(vec)
|
| 82 |
+
|
| 83 |
+
return "Spam" if output.item() > 0.35 else "Ham"
|
| 84 |
+
|
| 85 |
+
# Example
|
| 86 |
+
print(predict("Congratulations! You won $1000. Click now!"))
|
| 87 |
+
```
|
| 88 |
+
|
| 89 |
+
---
|
| 90 |
+
|
| 91 |
+
## π§ Training & Fine-Tuning
|
| 92 |
+
|
| 93 |
+
The model can be **improved and fine-tuned** by:
|
| 94 |
+
|
| 95 |
+
* Adding more data (larger SMS datasets)
|
| 96 |
+
* Increasing n-grams (`ngram_range=(1,2)`)
|
| 97 |
+
* Adjusting class weights in `BCEWithLogitsLoss`
|
| 98 |
+
* Training with more epochs
|
| 99 |
+
* Using embeddings or LSTM for contextual understanding
|
| 100 |
+
|
| 101 |
+
π‘ **Fork this repo and experiment freely**. Make it your own!
|
| 102 |
+
|
| 103 |
+
---
|
| 104 |
+
|
| 105 |
+
## π Support the Project
|
| 106 |
+
|
| 107 |
+
If this project is helpful:
|
| 108 |
+
|
| 109 |
+
β **Give this repository a star**
|
| 110 |
+
π΄ **Fork it and improve it**
|
| 111 |
+
π’ **Share it with others learning PyTorch**
|
| 112 |
+
|
| 113 |
+
> Following and starring helps me keep releasing open-source projects!
|
| 114 |
+
|
| 115 |
+
---
|
| 116 |
+
|
| 117 |
+
## π Source Code & Updates
|
| 118 |
+
|
| 119 |
+
For the **full source code, training scripts, and future updates**,
|
| 120 |
+
please visit the **GitHub repository** linked to this project.
|
| 121 |
+
|
| 122 |
+
---
|
| 123 |
+
|
| 124 |
+
## π License
|
| 125 |
+
|
| 126 |
+
This project is **open-source** and intended for **educational purposes**.
|
| 127 |
+
MIT License applies.
|
| 128 |
+
|
| 129 |
+
---
|
| 130 |
+
|
| 131 |
+
## π€ Hugging Face Friendly
|
| 132 |
+
|
| 133 |
+
You can also **upload this model to Hugging Face Model Hub**.
|
| 134 |
+
Include `spam_nn.pth`, `vectorizer.pkl`, `config.json`, and `inference.py` to make it **ready for inference online**.
|