Upload modeling_te3s_head.py with huggingface_hub
Browse files- modeling_te3s_head.py +1 -112
modeling_te3s_head.py
CHANGED
|
@@ -1,112 +1,3 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import torch.nn as nn
|
| 3 |
-
from typing import Optional
|
| 4 |
-
from transformers import PreTrainedModel, PretrainedConfig
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
class TextEmbedding3SmallSentimentHeadConfig(PretrainedConfig):
|
| 8 |
-
model_type = "sentiment-head"
|
| 9 |
-
|
| 10 |
-
def __init__(
|
| 11 |
-
self,
|
| 12 |
-
input_dim: int = 1536,
|
| 13 |
-
hidden_dim: int = 512,
|
| 14 |
-
dropout: float = 0.2,
|
| 15 |
-
num_labels: int = 3,
|
| 16 |
-
**kwargs,
|
| 17 |
-
) -> None:
|
| 18 |
-
super().__init__(**kwargs)
|
| 19 |
-
self.input_dim = int(input_dim)
|
| 20 |
-
self.hidden_dim = int(hidden_dim)
|
| 21 |
-
self.dropout = float(dropout)
|
| 22 |
-
self.num_labels = int(num_labels)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
class TextEmbedding3SmallSentimentHead(PreTrainedModel):
|
| 26 |
-
config_class = TextEmbedding3SmallSentimentHeadConfig
|
| 27 |
-
|
| 28 |
-
def __init__(self, config: TextEmbedding3SmallSentimentHeadConfig) -> None:
|
| 29 |
-
super().__init__(config)
|
| 30 |
-
|
| 31 |
-
if config.hidden_dim and config.hidden_dim > 0:
|
| 32 |
-
self.net = nn.Sequential(
|
| 33 |
-
nn.Linear(config.input_dim, config.hidden_dim),
|
| 34 |
-
nn.ReLU(),
|
| 35 |
-
nn.Dropout(p=config.dropout),
|
| 36 |
-
nn.Linear(config.hidden_dim, config.num_labels),
|
| 37 |
-
)
|
| 38 |
-
else:
|
| 39 |
-
self.net = nn.Linear(config.input_dim, config.num_labels)
|
| 40 |
-
|
| 41 |
-
self.post_init()
|
| 42 |
-
|
| 43 |
-
def forward(
|
| 44 |
-
self,
|
| 45 |
-
inputs_embeds: torch.FloatTensor,
|
| 46 |
-
labels: Optional[torch.LongTensor] = None,
|
| 47 |
-
**kwargs,
|
| 48 |
-
):
|
| 49 |
-
logits = self.net(inputs_embeds)
|
| 50 |
-
loss = None
|
| 51 |
-
if labels is not None:
|
| 52 |
-
loss = nn.CrossEntropyLoss()(logits, labels)
|
| 53 |
-
return {"logits": logits, "loss": loss}
|
| 54 |
-
|
| 55 |
-
from __future__ import annotations
|
| 56 |
-
|
| 57 |
-
import torch
|
| 58 |
-
import torch.nn as nn
|
| 59 |
-
from transformers import PreTrainedModel, PretrainedConfig
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
class TextEmbedding3SmallSentimentHeadConfig(PretrainedConfig):
|
| 63 |
-
model_type = "sentiment-head"
|
| 64 |
-
|
| 65 |
-
def __init__(
|
| 66 |
-
self,
|
| 67 |
-
input_dim: int = 1536,
|
| 68 |
-
hidden_dim: int = 512,
|
| 69 |
-
dropout: float = 0.2,
|
| 70 |
-
num_labels: int = 3,
|
| 71 |
-
**kwargs,
|
| 72 |
-
) -> None:
|
| 73 |
-
super().__init__(**kwargs)
|
| 74 |
-
self.input_dim = int(input_dim)
|
| 75 |
-
self.hidden_dim = int(hidden_dim)
|
| 76 |
-
self.dropout = float(dropout)
|
| 77 |
-
self.num_labels = int(num_labels)
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
class TextEmbedding3SmallSentimentHead(PreTrainedModel):
|
| 81 |
-
config_class = TextEmbedding3SmallSentimentHeadConfig
|
| 82 |
-
|
| 83 |
-
def __init__(self, config: TextEmbedding3SmallSentimentHeadConfig) -> None:
|
| 84 |
-
super().__init__(config)
|
| 85 |
-
|
| 86 |
-
if config.hidden_dim and config.hidden_dim > 0:
|
| 87 |
-
self.net = nn.Sequential(
|
| 88 |
-
nn.Linear(config.input_dim, config.hidden_dim),
|
| 89 |
-
nn.ReLU(),
|
| 90 |
-
nn.Dropout(p=config.dropout),
|
| 91 |
-
nn.Linear(config.hidden_dim, config.num_labels),
|
| 92 |
-
)
|
| 93 |
-
else:
|
| 94 |
-
self.net = nn.Linear(config.input_dim, config.num_labels)
|
| 95 |
-
|
| 96 |
-
self.post_init()
|
| 97 |
-
|
| 98 |
-
def forward(
|
| 99 |
-
self,
|
| 100 |
-
inputs_embeds: torch.FloatTensor,
|
| 101 |
-
labels: torch.LongTensor | None = None,
|
| 102 |
-
**_: dict,
|
| 103 |
-
):
|
| 104 |
-
logits = self.net(inputs_embeds)
|
| 105 |
-
loss = None
|
| 106 |
-
if labels is not None:
|
| 107 |
-
loss = nn.CrossEntropyLoss()(logits, labels)
|
| 108 |
-
return {"logits": logits, "loss": loss}
|
| 109 |
-
|
| 110 |
from __future__ import annotations
|
| 111 |
|
| 112 |
import torch
|
|
@@ -153,6 +44,4 @@ class TextEmbedding3SmallSentimentHead(PreTrainedModel):
|
|
| 153 |
loss = None
|
| 154 |
if labels is not None:
|
| 155 |
loss = nn.CrossEntropyLoss()(logits, labels)
|
| 156 |
-
return {"logits": logits, "loss": loss}
|
| 157 |
-
|
| 158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
|
| 3 |
import torch
|
|
|
|
| 44 |
loss = None
|
| 45 |
if labels is not None:
|
| 46 |
loss = nn.CrossEntropyLoss()(logits, labels)
|
| 47 |
+
return {"logits": logits, "loss": loss}
|
|
|
|
|
|