Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,67 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
datasets:
|
| 4 |
+
- AdamLucek/apple-environmental-report-QA-retrieval
|
| 5 |
+
base_model: sentence-transformers/all-MiniLM-L6-v2
|
| 6 |
+
pipeline_tag: feature-extraction
|
| 7 |
+
library_name: peft
|
| 8 |
+
---
|
| 9 |
+
# all-MiniLM-L6-v2-query-only-linear-adapter-AppleQA
|
| 10 |
+
|
| 11 |
+
Query-only linear adapter for [sentence-transformers/all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) with the [AdamLucek/apple-environmental-report-QA-retrieval](https://huggingface.co/datasets/AdamLucek/apple-environmental-report-QA-retrieval) dataset.
|
| 12 |
+
|
| 13 |
+
6 adapters trained at 10, 20, 30, and 40 epochs with:
|
| 14 |
+
- Triplet Margin Loss, Margin=1.0, Euclidean Distance=2
|
| 15 |
+
- AdamW Optimizer
|
| 16 |
+
- Random negative sampling from irrelevant document
|
| 17 |
+
- LR: 0.003
|
| 18 |
+
- Batch size: 32
|
| 19 |
+
- Grad Norm: 1.0
|
| 20 |
+
- Warmup Steps: 100
|
| 21 |
+
|
| 22 |
+
Training script and model creation available on [Github Repo](https://github.com/ALucek/query-only-linear-adapter-training)
|
| 23 |
+
|
| 24 |
+
# Assessment
|
| 25 |
+
|
| 26 |
+
Mean Reciprocal Rank & Recall@k=10
|
| 27 |
+
|
| 28 |
+
<img src="https://cdn-uploads.huggingface.co/production/uploads/65ba68a15d2ef0a4b2c892b4/ZsbVzv81cn2XW24eqbicU.png" width=800>
|
| 29 |
+
|
| 30 |
+
# Usage
|
| 31 |
+
|
| 32 |
+
```python
|
| 33 |
+
import torch
|
| 34 |
+
from torch import nn
|
| 35 |
+
from sentence_transformers import SentenceTransformer
|
| 36 |
+
|
| 37 |
+
class LinearAdapter(nn.Module):
|
| 38 |
+
def __init__(self, input_dim):
|
| 39 |
+
super().__init__()
|
| 40 |
+
self.linear = nn.Linear(input_dim, input_dim)
|
| 41 |
+
|
| 42 |
+
def forward(self, x):
|
| 43 |
+
return self.linear(x)
|
| 44 |
+
|
| 45 |
+
# Load the base model
|
| 46 |
+
base_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 47 |
+
|
| 48 |
+
# Load Adapter
|
| 49 |
+
adapter = LinearAdapter(base_model.get_sentence_embedding_dimension())
|
| 50 |
+
adapter.load_state_dict(torch.load('adapters/linear_adapter_30epochs.pth'))
|
| 51 |
+
|
| 52 |
+
# Example function for encoding
|
| 53 |
+
def encode_query(query, base_model, adapter):
|
| 54 |
+
device = next(adapter.parameters()).device
|
| 55 |
+
query_emb = base_model.encode(query, convert_to_tensor=True).to(device)
|
| 56 |
+
adapted_query_emb = adapter(query_emb)
|
| 57 |
+
return adapted_query_emb.cpu().detach().numpy()
|
| 58 |
+
|
| 59 |
+
emb = encode_query("Hello", base_model, adapter)
|
| 60 |
+
|
| 61 |
+
print(emb[:5])
|
| 62 |
+
```
|
| 63 |
+
**output**
|
| 64 |
+
|
| 65 |
+
```
|
| 66 |
+
[-0.13122843 0.02912715 0.07466945 0.09387457 0.13010463]
|
| 67 |
+
```
|