Upload modeling_te3s_head.py with huggingface_hub
Browse files- modeling_te3s_head.py +29 -22
modeling_te3s_head.py
CHANGED
|
@@ -1,43 +1,50 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
|
| 3 |
import torch
|
| 4 |
import torch.nn as nn
|
| 5 |
-
from
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
-
class
|
| 9 |
-
"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
def __init__(self, config:
|
| 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
|
| 41 |
**kwargs,
|
| 42 |
):
|
| 43 |
logits = self.net(inputs_embeds)
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
+
from typing import Optional
|
| 4 |
+
from transformers.modeling_utils import PreTrainedModel
|
| 5 |
+
from transformers.configuration_utils import PretrainedConfig
|
| 6 |
|
| 7 |
|
| 8 |
+
class TextEmbedding3SmallSentimentHeadConfig(PretrainedConfig):
|
| 9 |
+
model_type = "sentiment-head"
|
| 10 |
|
| 11 |
+
def __init__(
|
| 12 |
+
self,
|
| 13 |
+
input_dim: int = 1536,
|
| 14 |
+
hidden_dim: int = 512,
|
| 15 |
+
dropout: float = 0.2,
|
| 16 |
+
num_labels: int = 3,
|
| 17 |
+
**kwargs,
|
| 18 |
+
) -> None:
|
| 19 |
+
super().__init__(**kwargs)
|
| 20 |
+
self.input_dim = int(input_dim)
|
| 21 |
+
self.hidden_dim = int(hidden_dim)
|
| 22 |
+
self.dropout = float(dropout)
|
| 23 |
+
self.num_labels = int(num_labels)
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class TextEmbedding3SmallSentimentHead(PreTrainedModel):
|
| 27 |
+
config_class = TextEmbedding3SmallSentimentHeadConfig
|
| 28 |
|
| 29 |
+
def __init__(self, config: TextEmbedding3SmallSentimentHeadConfig) -> None:
|
| 30 |
super().__init__(config)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
if config.hidden_dim and config.hidden_dim > 0:
|
| 33 |
self.net = nn.Sequential(
|
| 34 |
+
nn.Linear(config.input_dim, config.hidden_dim),
|
| 35 |
nn.ReLU(),
|
| 36 |
+
nn.Dropout(p=config.dropout),
|
| 37 |
+
nn.Linear(config.hidden_dim, config.num_labels),
|
| 38 |
)
|
| 39 |
else:
|
| 40 |
+
self.net = nn.Linear(config.input_dim, config.num_labels)
|
| 41 |
|
| 42 |
self.post_init()
|
| 43 |
|
| 44 |
def forward(
|
| 45 |
self,
|
| 46 |
inputs_embeds: torch.FloatTensor,
|
| 47 |
+
labels: Optional[torch.LongTensor] = None,
|
| 48 |
**kwargs,
|
| 49 |
):
|
| 50 |
logits = self.net(inputs_embeds)
|