Text Classification
Transformers
Safetensors
English
tiny_transformer
Michielo commited on
Commit
b780151
·
verified ·
1 Parent(s): 744d042

Added benchmarks

Browse files
Files changed (1) hide show
  1. README.md +94 -6
README.md CHANGED
@@ -2,7 +2,6 @@
2
  library_name: transformers
3
  language:
4
  - en
5
- pipeline_tag: zero-shot-classification
6
  ---
7
 
8
  # Tiny-Toxic-Detector
@@ -12,14 +11,93 @@ A tiny comment toxicity classifier model at only 2M parameters. With only ~10MB
12
  A paper on this model is being released soon.
13
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  ### Usage
16
- This model uses custom architecture and requires some extra custom code to work. Below you can find a script that is fully-usable.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  ```python
18
  import torch
19
  import torch.nn as nn
20
  from transformers import PreTrainedModel, PretrainedConfig, AutoTokenizer
21
- from huggingface_hub import login
22
- import os
23
 
24
  # Define TinyTransformer model
25
  class TinyTransformer(nn.Module):
@@ -112,6 +190,16 @@ def main():
112
  if __name__ == "__main__":
113
  main()
114
  ```
 
115
 
116
-
117
-
 
 
 
 
 
 
 
 
 
 
2
  library_name: transformers
3
  language:
4
  - en
 
5
  ---
6
 
7
  # Tiny-Toxic-Detector
 
11
  A paper on this model is being released soon.
12
 
13
 
14
+ ### Benchmarks
15
+
16
+ The Tiny-Toxic-Detector achieves an impressive 90.26% on the Toxigen benchmark and 87.34% on the Jigsaw-Toxic-Comment-Classification-Challenge. Here we compare our results against other toxic classification models:
17
+
18
+
19
+ | Model | Size (parameters) | Toxigen (%) | Jigsaw (%) |
20
+ | --------------------------------- | ----------------- | ----------- | ---------- |
21
+ | lmsys/toxicchat-t5-large-v1.0 | 738M | 72.67 | 88.82 |
22
+ | s-nlp/roberta toxicity classifier | 124M | 88.41 | 94.92 |
23
+ | unitary/toxic-bert | 109M | 49.50 | 89.70 |
24
+ | mohsenfayyaz/toxicity-classifier | 109M | 81.50 | 83.31 |
25
+ | martin-ha/toxic-comment-model | 67M | 68.02 | 91.56 |
26
+ | Tiny-toxic-detector | 2M | 90.26 | 87.34 |
27
+
28
+
29
  ### Usage
30
+ This model uses custom architecture and requires some extra custom code to work. Below you can find the architecture and a fully-usable example.
31
+
32
+ <details>
33
+ <summary>
34
+ Architecture
35
+ </summary>
36
+
37
+ ```python
38
+ import torch
39
+ import torch.nn as nn
40
+ from transformers import PreTrainedModel, PretrainedConfig, AutoTokenizer
41
+
42
+ # Define TinyTransformer model
43
+ class TinyTransformer(nn.Module):
44
+ def __init__(self, vocab_size, embed_dim, num_heads, ff_dim, num_layers):
45
+ super().__init__()
46
+ self.embedding = nn.Embedding(vocab_size, embed_dim)
47
+ self.pos_encoding = nn.Parameter(torch.zeros(1, 512, embed_dim))
48
+ encoder_layer = nn.TransformerEncoderLayer(d_model=embed_dim, nhead=num_heads, dim_feedforward=ff_dim, batch_first=True)
49
+ self.transformer = nn.TransformerEncoder(encoder_layer, num_layers=num_layers)
50
+ self.fc = nn.Linear(embed_dim, 1)
51
+ self.sigmoid = nn.Sigmoid()
52
+
53
+ def forward(self, x):
54
+ x = self.embedding(x) + self.pos_encoding[:, :x.size(1), :]
55
+ x = self.transformer(x)
56
+ x = x.mean(dim=1) # Global average pooling
57
+ x = self.fc(x)
58
+ return self.sigmoid(x)
59
+
60
+ class TinyTransformerConfig(PretrainedConfig):
61
+ model_type = "tiny_transformer"
62
+
63
+ def __init__(self, vocab_size=30522, embed_dim=64, num_heads=2, ff_dim=128, num_layers=4, max_position_embeddings=512, **kwargs):
64
+ super().__init__(**kwargs)
65
+ self.vocab_size = vocab_size
66
+ self.embed_dim = embed_dim
67
+ self.num_heads = num_heads
68
+ self.ff_dim = ff_dim
69
+ self.num_layers = num_layers
70
+ self.max_position_embeddings = max_position_embeddings
71
+
72
+ class TinyTransformerForSequenceClassification(PreTrainedModel):
73
+ config_class = TinyTransformerConfig
74
+
75
+ def __init__(self, config):
76
+ super().__init__(config)
77
+ self.num_labels = 1
78
+ self.transformer = TinyTransformer(
79
+ config.vocab_size,
80
+ config.embed_dim,
81
+ config.num_heads,
82
+ config.ff_dim,
83
+ config.num_layers
84
+ )
85
+
86
+ def forward(self, input_ids, attention_mask=None):
87
+ outputs = self.transformer(input_ids)
88
+ return {"logits": outputs}
89
+ ```
90
+ </details>
91
+
92
+ <details>
93
+ <summary>
94
+ Full example
95
+ </summary>
96
+
97
  ```python
98
  import torch
99
  import torch.nn as nn
100
  from transformers import PreTrainedModel, PretrainedConfig, AutoTokenizer
 
 
101
 
102
  # Define TinyTransformer model
103
  class TinyTransformer(nn.Module):
 
190
  if __name__ == "__main__":
191
  main()
192
  ```
193
+ </details>
194
 
195
+ Please note that to predict toxicity you can use the following example:
196
+ ```python
197
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128, padding="max_length").to(device)
198
+ if "token_type_ids" in inputs:
199
+ del inputs["token_type_ids"]
200
+
201
+ with torch.no_grad():
202
+ outputs = model(**inputs)
203
+ logits = outputs["logits"].squeeze()
204
+ prediction = "Toxic" if logits > 0.5 else "Not Toxic"
205
+ ```