Upload modeling_te3s_head.py with huggingface_hub
Browse files- modeling_te3s_head.py +49 -0
modeling_te3s_head.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn as nn
|
| 5 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class TextEmbedding3SmallSentimentHead(PreTrainedModel):
|
| 9 |
+
"""Lightweight sentiment head for 1536-d OpenAI embeddings.
|
| 10 |
+
|
| 11 |
+
Expects config.json fields:
|
| 12 |
+
- input_dim (int, default 1536)
|
| 13 |
+
- hidden_dim (int, default 512; use 0 for linear-only)
|
| 14 |
+
- dropout (float, default 0.2)
|
| 15 |
+
- num_labels (int, default 3)
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
def __init__(self, config: PretrainedConfig) -> None:
|
| 19 |
+
super().__init__(config)
|
| 20 |
+
input_dim = int(getattr(config, "input_dim", 1536))
|
| 21 |
+
hidden_dim = int(getattr(config, "hidden_dim", 512))
|
| 22 |
+
dropout = float(getattr(config, "dropout", 0.2))
|
| 23 |
+
num_labels = int(getattr(config, "num_labels", 3))
|
| 24 |
+
|
| 25 |
+
if hidden_dim and hidden_dim > 0:
|
| 26 |
+
self.net = nn.Sequential(
|
| 27 |
+
nn.Linear(input_dim, hidden_dim),
|
| 28 |
+
nn.ReLU(),
|
| 29 |
+
nn.Dropout(p=dropout),
|
| 30 |
+
nn.Linear(hidden_dim, num_labels),
|
| 31 |
+
)
|
| 32 |
+
else:
|
| 33 |
+
self.net = nn.Linear(input_dim, num_labels)
|
| 34 |
+
|
| 35 |
+
self.post_init()
|
| 36 |
+
|
| 37 |
+
def forward(
|
| 38 |
+
self,
|
| 39 |
+
inputs_embeds: torch.FloatTensor,
|
| 40 |
+
labels: torch.LongTensor | None = None,
|
| 41 |
+
**kwargs,
|
| 42 |
+
):
|
| 43 |
+
logits = self.net(inputs_embeds)
|
| 44 |
+
loss = None
|
| 45 |
+
if labels is not None:
|
| 46 |
+
loss = nn.CrossEntropyLoss()(logits, labels)
|
| 47 |
+
return {"logits": logits, "loss": loss}
|
| 48 |
+
|
| 49 |
+
|