Instructions to use RLHFlow/RewardModel-Mistral-7B-for-DPA-v1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use RLHFlow/RewardModel-Mistral-7B-for-DPA-v1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="RLHFlow/RewardModel-Mistral-7B-for-DPA-v1", trust_remote_code=True)# Load model directly from transformers import AutoTokenizer, AutoModelForSequenceClassification tokenizer = AutoTokenizer.from_pretrained("RLHFlow/RewardModel-Mistral-7B-for-DPA-v1", trust_remote_code=True) model = AutoModelForSequenceClassification.from_pretrained("RLHFlow/RewardModel-Mistral-7B-for-DPA-v1", trust_remote_code=True) - Notebooks
- Google Colab
- Kaggle
Create modelling_custom.py
Browse files- modelling_custom.py +18 -0
modelling_custom.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from torch.nn import functional as F
|
| 3 |
+
from transformers.models.mistral.modeling_mistral import MistralForSequenceClassification
|
| 4 |
+
|
| 5 |
+
class NormalizedLinear(torch.nn.Linear):
|
| 6 |
+
def forward(self, x):
|
| 7 |
+
x = F.normalize(x, p=2, dim=-1)
|
| 8 |
+
return super().forward(x)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class MistralForAttributePrediction(MistralForSequenceClassification):
|
| 12 |
+
def __init__(self, config):
|
| 13 |
+
super().__init__(config)
|
| 14 |
+
|
| 15 |
+
del self.score
|
| 16 |
+
self.score = NormalizedLinear(config.hidden_size, config.num_labels, bias=True)
|
| 17 |
+
# Initialize weights and apply final processing
|
| 18 |
+
self.post_init()
|