Views
No views yet
[CLS ; h1 ; h2 ; h1-h2 ; h1*h2]DeBERTa encoder
↓
h1 = hidden state at <tgt> position in sentence 1
h2 = hidden state at <tgt> position in sentence 2
↓
interaction = [CLS, h1, h2, h1-h2, h1*h2] (5 × 1024)
↓
MLP(5120 → 512 → 2)
↓
loss = CrossEntropy + 0.1 × ContrastiveLoss(h1, h2)| Split | Accuracy |
|---|---|
| Validation | 0.7555 |
| Test | 0.7257 |
| Baseline DeBERTa | 0.7279 |
| SenseBERT (SOTA) | 0.7210 |
[CLS] token for classification,
which must implicitly represent both sentence meaning and word sense.
This model explicitly extracts and compares the target word's contextual
representation from both sentences, directly optimising sense similarity
via contrastive loss.1import torch
2from transformers import AutoTokenizer, AutoModel
3import torch.nn as nn
4import torch.nn.functional as F
5
6# Reconstruct model
7tokenizer = AutoTokenizer.from_pretrained("Deehan1866/deberta-wic-contrastive")
8encoder = AutoModel.from_pretrained("Deehan1866/deberta-wic-contrastive")
9
10classifier = nn.Sequential(
11 nn.Linear(5 * 1024, 512), nn.GELU(), nn.Dropout(0.1), nn.Linear(512, 2)
12)
13# load classifier_head.pt separately
14
15word = "bank"
16s1 = "<tgt>bank</tgt> raised its interest rates."
17s2 = "She visited her local <tgt>bank</tgt> to deposit a cheque."
18
19enc = tokenizer(s1, s2, return_tensors="pt", truncation=True, max_length=256)
20with torch.no_grad():
21 hidden = encoder(**enc).last_hidden_state
22 tgt_id = tokenizer.convert_tokens_to_ids("<tgt>")
23 ids = enc["input_ids"][0].tolist()
24 positions = [i for i, t in enumerate(ids) if t == tgt_id]
25 h1 = hidden[0, positions[0]]
26 h2 = hidden[0, positions[1]]
27 cls = hidden[0, 0]
28 interaction = torch.cat([cls, h1, h2, h1-h2, h1*h2]).unsqueeze(0)
29 logits = classifier(interaction)
30 pred = torch.argmax(logits).item()
31print("Same sense" if pred == 1 else "Different sense")