File size: 1,499 Bytes
0292bfe
 
 
 
 
 
 
 
 
1c082de
 
0292bfe
 
 
 
 
 
 
 
1481eeb
 
0292bfe
 
 
1481eeb
 
0292bfe
 
1c082de
1481eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0292bfe
1481eeb
 
 
0292bfe
1481eeb
0292bfe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
---
license: mit
library_name: pytorch
tags:
  - bilstm
  - lstm
  - pytorch
  - text-classification
  - spam-detection
model_details:
  parameters: 4403585
task_categories:
  - text-classification
datasets:
  - ucirvine/sms_spam
language:
  - en
---

# BiLSTM Text Classifier

Simple BiLSTM model PyTorch trained for SPAM detection on SMS Spam Collection  
(Almeida, Tiago and Jos Hidalgo. 2011. *SMS Spam Collection*.  
UCI Machine Learning Repository. https://doi.org/10.24432/C5CC84).

## Important Notes
- The model returns **logits** as output; to obtain probabilities, apply `torch.sigmoid`.
- The model uses the `bert-base-uncased` tokenizer **only for tokenization** (the encoder is NOT BERT).
- Number of parameters: ~4.4M
## Files
- `BiLSTMClassifier.safetensors`: trained weights
- `BiLSTMClassifier.py`: model definition
- `config.json`: hyperparameters

## Usage

```python
import json
import torch
from transformers import BertTokenizer
from safetensors.torch import load_file
from BiLSTMClassifier import BiLSTMClassifier

with open("config.json") as f:
    cfg = json.load(f)

model = BiLSTMClassifier(**cfg)

state_dict = load_file("BiLSTMClassifier.safetensors")
model.load_state_dict(state_dict)
model.eval()

sample_text = "URGENT HIRING! Earn $500/day working from home. No experience needed."

tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
tokens = tokenizer(sample_text, return_tensors="pt")

logits = model(tokens["input_ids"])
prob = torch.sigmoid(logits)