Text Classification
Transformers
ONNX
Transformers.js
English
bert
disaster-tweets
text-embeddings-inference
Instructions to use dylanqi27/disaster-tweet-bert with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use dylanqi27/disaster-tweet-bert with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="dylanqi27/disaster-tweet-bert")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("dylanqi27/disaster-tweet-bert") model = AutoModelForSequenceClassification.from_pretrained("dylanqi27/disaster-tweet-bert", device_map="auto") - Transformers.js
How to use dylanqi27/disaster-tweet-bert with Transformers.js:
// npm i @huggingface/transformers import { pipeline } from '@huggingface/transformers'; // Allocate pipeline const pipe = await pipeline('text-classification', 'dylanqi27/disaster-tweet-bert'); - Notebooks
- Google Colab
- Kaggle
Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: transformers
|
| 3 |
+
tags:
|
| 4 |
+
- text-classification
|
| 5 |
+
- bert
|
| 6 |
+
- disaster-tweets
|
| 7 |
+
- onnx
|
| 8 |
+
- transformers.js
|
| 9 |
+
license: mit
|
| 10 |
+
datasets:
|
| 11 |
+
- tweet_disaster
|
| 12 |
+
language:
|
| 13 |
+
- en
|
| 14 |
+
metrics:
|
| 15 |
+
- accuracy
|
| 16 |
+
- f1
|
| 17 |
+
base_model: google-bert/bert-base-uncased
|
| 18 |
+
---
|
| 19 |
+
|
| 20 |
+
# disaster-tweet-bert
|
| 21 |
+
|
| 22 |
+
`bert-base-uncased` 微调的二分类灾情推文分类器(DISASTER / NOT_DISASTER),同时提供 Hugging Face `transformers`
|
| 23 |
+
与 `transformers.js`(ONNX)两种推理方式。
|
| 24 |
+
|
| 25 |
+
## 指标
|
| 26 |
+
|
| 27 |
+
测试集 n = 762,正类 = DISASTER。
|
| 28 |
+
|
| 29 |
+
| 版本 | acc | F1 | precision | recall | 体积 |
|
| 30 |
+
|---|---|---|---|---|---|
|
| 31 |
+
| PyTorch (out/best) | 0.8478 | 0.8165 | 0.8459 | 0.7890 | 438 MB |
|
| 32 |
+
| ONNX fp32 | 0.8478 | 0.8165 | 0.8459 | 0.7890 | 438 MB |
|
| 33 |
+
| ONNX int8(逐通道动态量化) | 0.8294 | 0.7727 | 0.9020 | 0.6758 | 111 MB |
|
| 34 |
+
|
| 35 |
+
ONNX fp32 与 PyTorch 输出逐元素一致(max|diff| = 0.0000)。
|
| 36 |
+
|
| 37 |
+
## 训练配置
|
| 38 |
+
|
| 39 |
+
| 项 | 值 |
|
| 40 |
+
|---|---|
|
| 41 |
+
| base model | `bert-base-uncased` |
|
| 42 |
+
| epochs | 3 |
|
| 43 |
+
| batch size | 32 |
|
| 44 |
+
| lr | 2e-5 |
|
| 45 |
+
| warmup ratio | 0.1 |
|
| 46 |
+
| max_len | 128 |
|
| 47 |
+
| seed | 42 |
|
| 48 |
+
| 训练耗时 | 175 s(RTX 4060 Ti 8GB) |
|
| 49 |
+
|
| 50 |
+
## 用法
|
| 51 |
+
|
| 52 |
+
### transformers.js(浏览器)
|
| 53 |
+
|
| 54 |
+
```js
|
| 55 |
+
import { pipeline } from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.5.1";
|
| 56 |
+
|
| 57 |
+
const clf = await pipeline("text-classification", "dylanqi27/disaster-tweet-bert", { dtype: "q8" });
|
| 58 |
+
console.log(await clf("Forest fire near La Ronge Sask. Canada"));
|
| 59 |
+
// [{ label: "DISASTER", score: 0.998 }]
|
| 60 |
+
```
|
| 61 |
+
|
| 62 |
+
`dtype` 可选 `q8`(111MB,默认,对应 `onnx/model_quantized.onnx`)或 `fp32`(438MB,对应 `onnx/model.onnx`)。
|
| 63 |
+
|
| 64 |
+
### Python
|
| 65 |
+
|
| 66 |
+
```python
|
| 67 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 68 |
+
import torch
|
| 69 |
+
|
| 70 |
+
tok = AutoTokenizer.from_pretrained("dylanqi27/disaster-tweet-bert")
|
| 71 |
+
model = AutoModelForSequenceClassification.from_pretrained("dylanqi27/disaster-tweet-bert")
|
| 72 |
+
|
| 73 |
+
enc = tok(["Forest fire near La Ronge Sask. Canada"], return_tensors="pt", truncation=True, max_length=128)
|
| 74 |
+
with torch.no_grad():
|
| 75 |
+
probs = model(**enc).logits.softmax(-1)[0]
|
| 76 |
+
print(model.config.id2label[probs.argmax().item()], round(probs.max().item(), 4))
|
| 77 |
+
```
|
| 78 |
+
|
| 79 |
+
### ONNX Runtime
|
| 80 |
+
|
| 81 |
+
```python
|
| 82 |
+
import onnxruntime as ort
|
| 83 |
+
sess = ort.InferenceSession("onnx/model_quantized.onnx")
|
| 84 |
+
# inputs: input_ids, attention_mask, token_type_ids(int64,dynamic batch/sequence)
|
| 85 |
+
# output: logits [batch, 2]
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
## 文件
|
| 89 |
+
|
| 90 |
+
```
|
| 91 |
+
config.json # id2label: 0=NOT_DISASTER, 1=DISASTER
|
| 92 |
+
tokenizer.json
|
| 93 |
+
tokenizer_config.json
|
| 94 |
+
onnx/model.onnx # fp32, 438MB
|
| 95 |
+
onnx/model_quantized.onnx # int8 per-channel, 111MB
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
在线演示:https://huggingface.co/spaces/dylanqi27/disaster-tweet-demo-static
|