-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bastien.py
More file actions
92 lines (67 loc) · 3.08 KB
/
Copy pathtest_bastien.py
File metadata and controls
92 lines (67 loc) · 3.08 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModel
from adapters import AutoAdapterModel, AdapterConfig
import re
def load_hate_speech_dataset():
"""
Load the hate speech dataset from the UCBerkeley DLab.
"""
mhs = load_dataset("ucberkeley-dlab/measuring-hate-speech", split='train')
return mhs
def dataset_test():
mhs = load_hate_speech_dataset()
mhs = load_dataset("ucberkeley-dlab/measuring-hate-speech", split="train")
# 2. Convert to a DataFrame for easy grouping
df = mhs.to_pandas()
# 3. Group by comment_id
agg_df = (
df.groupby("comment_id")
.agg(
hate_speech_score=("hate_speech_score", "mean"), # average score
platform=("platform", "first"), # same for every annotator row
text=("text", "first") # same for every annotator row
)
.reset_index()
)
platform_names = {0: "Reddit", 1: "Reference", 2: "Twitter", 3: "Youtube"}
platform_names = {0: "Reddit", 1: "Reference", 2: "Twitter", 3: "Youtube"}
# Count comments per platform
keyword = "rt" # ← put any word or phrase here
pattern = rf"\b{re.escape(keyword)}\b"
mask = agg_df["text"].str.contains(pattern, case=False, na=False, regex=True)
hits = agg_df[mask]
platform_counts = (
agg_df["platform"]
agg_df["platform"]
.value_counts() # counts for each integer code
.sort_index()
)
#print(f"{len(hits)} comments contain “{keyword}” (case‑insensitive).")
#print(f"{len(hits)} comments contain “{keyword}” (case‑insensitive).")
print(platform_counts)
def test_model(): # adapter-aware class
model = AutoAdapterModel.from_pretrained("roberta-base")
# 2 – add a Houlsby bottleneck adapter
cfg = AdapterConfig.load("houlsby", reduction_factor=16)
model.add_adapter("hate_reg", config=cfg)
# 3 – add a 768→1 linear head (treated as “classification” with num_labels=1)
model.add_classification_head(
"hate_reg", # head name, same as adapter name = convenient
num_labels=1, # 👉 one scalar output
layers=1, # one linear layer is enough
activation_function="identity", # keep it linear for regression
)
# 4 – freeze backbone, keep only adapter + head trainable
model.train_adapter(["hate_reg"])
model.set_active_adapters("hate_reg")
print(model)
print("Active head:", model.active_head)
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print(f"{trainable_params:,} / {total_params:,} parameters are trainable "
f"→ {trainable_params / total_params:.2%} of the model.")
print(model.heads["hate_reg"].config)
f"→ {trainable_params / total_params:.2%} of the model.")
print(model.heads["hate_reg"].config)
if __name__ == "__main__":
dataset_test()