Update README.md
Browse files
README.md
CHANGED
@@ -9,6 +9,101 @@ tags:
|
|
9 |
- text classification
|
10 |
---
|
11 |
|
12 |
-
This
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
- text classification
|
10 |
---
|
11 |
|
12 |
+
This is rubert-tiny fine-tuned for classification messages type from telegram marketplaces.
|
13 |
+
|
14 |
+
Labels:
|
15 |
+
|
16 |
+
- **supply**: somebody willing to sell something or provide service
|
17 |
+
- **demand**: somebody wants to buy something or hire somebody
|
18 |
+
- **noise**: messages unrelated to topic.
|
19 |
+
|
20 |
+
## Usage
|
21 |
+
|
22 |
+
``` python
|
23 |
+
from transformers import AutoTokenizer
|
24 |
+
|
25 |
+
HF_MODEL_NAME = 'poc-embeddings/rubert-tiny-turbo-godeal'
|
26 |
+
MODEL_NAME = 'sergeyzh/rubert-tiny-turbo'
|
27 |
+
|
28 |
+
id2label = {0:'noise', 1:'demand', 2:'noise'}
|
29 |
+
|
30 |
+
class SupplyDemandTrader(
|
31 |
+
Module,
|
32 |
+
PyTorchModelHubMixin,
|
33 |
+
repo_url=HF_MODEL_NAME,
|
34 |
+
library_name="torch",
|
35 |
+
tags=["PyTorch", "sentence-transformers", "NLP", "text classification"],
|
36 |
+
docs_url="https://pytorch.org/docs/stable/index.html"
|
37 |
+
):
|
38 |
+
def __init__(self,
|
39 |
+
num_labels: Optional[int] = 3,
|
40 |
+
use_adapter: bool = False
|
41 |
+
):
|
42 |
+
super().__init__()
|
43 |
+
self.use_adapter = use_adapter
|
44 |
+
self.num_labels = num_labels
|
45 |
+
self.backbone = AutoModel.from_pretrained(MODEL_NAME)
|
46 |
+
|
47 |
+
# Adapter layer
|
48 |
+
if self.use_adapter:
|
49 |
+
self.adapter = TransformerEncoderLayer(
|
50 |
+
d_model=self.backbone.config.hidden_size,
|
51 |
+
nhead=self.backbone.config.num_attention_heads,
|
52 |
+
dim_feedforward=self.backbone.config.intermediate_size,
|
53 |
+
activation="gelu",
|
54 |
+
dropout=0.1,
|
55 |
+
batch_first=True # I/O shape: batch, seq, feature
|
56 |
+
)
|
57 |
+
else:
|
58 |
+
self.adapter = None
|
59 |
+
|
60 |
+
# Classification head
|
61 |
+
self.separator_head = Linear(self.backbone.config.hidden_size, num_labels)
|
62 |
+
self.loss = CrossEntropyLoss()
|
63 |
+
|
64 |
+
def forward(self,
|
65 |
+
input_ids: torch.Tensor,
|
66 |
+
attention_mask: torch.Tensor,
|
67 |
+
labels: Optional[torch.Tensor] = None
|
68 |
+
) -> dict[str, torch.Tensor]:
|
69 |
+
outputs = self.backbone(input_ids=input_ids, attention_mask=attention_mask)
|
70 |
+
last_hidden_state = outputs.last_hidden_state
|
71 |
+
|
72 |
+
if self.use_adapter:
|
73 |
+
last_hidden_state = self.adapter(last_hidden_state)
|
74 |
+
cls_embedding = last_hidden_state[:, 0]
|
75 |
+
|
76 |
+
logits = self.separator_head(cls_embedding)
|
77 |
+
|
78 |
+
if labels is not None:
|
79 |
+
loss = self.loss(logits, labels)
|
80 |
+
return {
|
81 |
+
"loss": loss,
|
82 |
+
"logits": logits,
|
83 |
+
"embedding": cls_embedding
|
84 |
+
}
|
85 |
+
return {
|
86 |
+
"logits": logits,
|
87 |
+
"embedding": cls_embedding
|
88 |
+
}
|
89 |
+
|
90 |
+
|
91 |
+
model = SupplyDemandTrader.from_pretrained(HF_MODEL_NAME)
|
92 |
+
tokenizer = AutoTokenizer.from_pretrained(HF_MODEL_NAME)
|
93 |
+
model.eval()
|
94 |
+
|
95 |
+
with torch.inference_mode():
|
96 |
+
ids = tokenizer("Куплю Iphone 8", return_tensors="pt")
|
97 |
+
logits = checkpoint.forward(ids['input_ids'], ids['attention_mask']))
|
98 |
+
preds = torch.argmax(logits)
|
99 |
+
print(id2label[int(preds)])
|
100 |
+
```
|
101 |
+
## Training
|
102 |
+
Backbone was trained on clustered dataset for matching problem. Partially unfreezed model with classification head on custom dataset containing exports from different telegram chats.
|
103 |
+
```
|
104 |
+
weighted average precision : 0.946
|
105 |
+
weighted average f1-score : 0.945
|
106 |
+
macro average precision : 0.943
|
107 |
+
macro average f1-score : 0.945
|
108 |
+
```
|
109 |
+
|