Initial model files and config for custom ELECTRA Large classifier for sentiment
Browse files- README.md +387 -0
- config.json +44 -0
- electra_classifier.py +96 -0
- model.safetensors +3 -0
- pytorch_model.bin +3 -0
- special_tokens_map.json +7 -0
- tokenizer.json +0 -0
- tokenizer_config.json +55 -0
- vocab.txt +0 -0
README.md
CHANGED
@@ -1,3 +1,390 @@
|
|
1 |
---
|
2 |
license: mit
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
license: mit
|
3 |
+
tags:
|
4 |
+
- sentiment-analysis
|
5 |
+
- text-classification
|
6 |
+
- electra
|
7 |
+
- pytorch
|
8 |
+
- transformers
|
9 |
---
|
10 |
+
|
11 |
+
# Electra Base Classifier for Sentiment Analysis
|
12 |
+
|
13 |
+
This is an [ELECTRA large discriminator](https://huggingface.co/google/electra-large-discriminator) fine-tuned for sentiment analysis of reviews. It has a mean pooling layer and a classifier head (2 layers of 1024 dimension) with SwishGLU activation and dropout (0.3). It classifies text into three sentiment categories: 'negative' (0), 'neutral' (1), and 'positive' (2). It was fine-tuned on the [Sentiment Merged](https://huggingface.co/datasets/jbeno/sentiment_merged) dataset, which is a merge of Stanford Sentiment Treebank (SST-3), and DynaSent Rounds 1 and 2.
|
14 |
+
|
15 |
+
|
16 |
+
## Labels
|
17 |
+
|
18 |
+
The model predicts the following labels:
|
19 |
+
|
20 |
+
- `0`: negative
|
21 |
+
- `1`: neutral
|
22 |
+
- `2`: positive
|
23 |
+
|
24 |
+
## How to Use
|
25 |
+
|
26 |
+
### Install package
|
27 |
+
|
28 |
+
This model requires the classes in `electra_classifier.py`. You can download the file, or you can install the package from PyPI.
|
29 |
+
|
30 |
+
```bash
|
31 |
+
pip install electra-classifier
|
32 |
+
```
|
33 |
+
|
34 |
+
### Load classes and model
|
35 |
+
```python
|
36 |
+
# Install the package in a notebook
|
37 |
+
!pip install electra-classifier
|
38 |
+
|
39 |
+
# Import libraries
|
40 |
+
import torch
|
41 |
+
from transformers import AutoTokenizer
|
42 |
+
from electra_classifier import ElectraClassifier
|
43 |
+
|
44 |
+
# Load tokenizer and model
|
45 |
+
model_name = "jbeno/electra-large-classifier-sentiment"
|
46 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
47 |
+
model = ElectraClassifier.from_pretrained(model_name)
|
48 |
+
|
49 |
+
# Set model to evaluation mode
|
50 |
+
model.eval()
|
51 |
+
|
52 |
+
# Run inference
|
53 |
+
text = "I love this restaurant!"
|
54 |
+
inputs = tokenizer(text, return_tensors="pt")
|
55 |
+
|
56 |
+
with torch.no_grad():
|
57 |
+
logits = model(**inputs)
|
58 |
+
predicted_class_id = torch.argmax(logits, dim=1).item()
|
59 |
+
predicted_label = model.config.id2label[predicted_class_id]
|
60 |
+
print(f"Predicted label: {predicted_label}")
|
61 |
+
```
|
62 |
+
|
63 |
+
## Requirements
|
64 |
+
- Python 3.7+
|
65 |
+
- PyTorch
|
66 |
+
- Transformers
|
67 |
+
- [electra-classifier](https://pypi.org/project/electra-classifier/) - Install with pip, or download electra_classifier.py
|
68 |
+
|
69 |
+
## Training Details
|
70 |
+
|
71 |
+
### Dataset
|
72 |
+
|
73 |
+
The model was trained on the [Sentiment Merged](https://huggingface.co/datasets/jbeno/sentiment_merged) dataset, which is a mix of Stanford Sentiment Treebank (SST-3), DynaSent Round 1, and DynaSent Round 2.
|
74 |
+
|
75 |
+
### Code
|
76 |
+
|
77 |
+
The code used to train the model can be found on GitHub:
|
78 |
+
- [jbeno/sentiment](https://github.com/jbeno/sentiment)
|
79 |
+
- [jbeno/electra-classifier](https://github.com/jbeno/electra-classifier)
|
80 |
+
|
81 |
+
### Research Paper
|
82 |
+
|
83 |
+
The research paper can be found here: [ELECTRA and GPT-4o: Cost-Effective Partners for Sentiment Analysis](https://github.com/jbeno/sentiment/research_paper.pdf)
|
84 |
+
|
85 |
+
### Performance Summary
|
86 |
+
|
87 |
+
- **Merged Dataset**
|
88 |
+
- Macro Average F1: **82.36**
|
89 |
+
- Accuracy: **82.96**
|
90 |
+
- **DynaSent R1**
|
91 |
+
- Macro Average F1: **85.91**
|
92 |
+
- Accuracy: **85.83**
|
93 |
+
- **DynaSent R2**
|
94 |
+
- Macro Average F1: **76.29**
|
95 |
+
- Accuracy: **76.53**
|
96 |
+
- **SST-3**
|
97 |
+
- Macro Average F1: **70.90**
|
98 |
+
- Accuracy: **80.36**
|
99 |
+
|
100 |
+
## Model Architecture
|
101 |
+
|
102 |
+
- **Base Model**: ELECTRA large discriminator (`google/electra-large-discriminator`)
|
103 |
+
- **Pooling Layer**: Custom pooling layer supporting 'cls', 'mean', and 'max' pooling types.
|
104 |
+
- **Classifier**: Custom classifier with configurable hidden dimensions, number of layers, and dropout rate.
|
105 |
+
- **Activation Function**: Custom SwishGLU activation function.
|
106 |
+
|
107 |
+
```
|
108 |
+
ElectraClassifier(
|
109 |
+
(electra): ElectraModel(
|
110 |
+
(embeddings): ElectraEmbeddings(
|
111 |
+
(word_embeddings): Embedding(30522, 1024, padding_idx=0)
|
112 |
+
(position_embeddings): Embedding(512, 1024)
|
113 |
+
(token_type_embeddings): Embedding(2, 1024)
|
114 |
+
(LayerNorm): LayerNorm((1024,), eps=1e-12, elementwise_affine=True)
|
115 |
+
(dropout): Dropout(p=0.1, inplace=False)
|
116 |
+
)
|
117 |
+
(encoder): ElectraEncoder(
|
118 |
+
(layer): ModuleList(
|
119 |
+
(0-23): 24 x ElectraLayer(
|
120 |
+
(attention): ElectraAttention(
|
121 |
+
(self): ElectraSelfAttention(
|
122 |
+
(query): Linear(in_features=1024, out_features=1024, bias=True)
|
123 |
+
(key): Linear(in_features=1024, out_features=1024, bias=True)
|
124 |
+
(value): Linear(in_features=1024, out_features=1024, bias=True)
|
125 |
+
(dropout): Dropout(p=0.1, inplace=False)
|
126 |
+
)
|
127 |
+
(output): ElectraSelfOutput(
|
128 |
+
(dense): Linear(in_features=1024, out_features=1024, bias=True)
|
129 |
+
(LayerNorm): LayerNorm((1024,), eps=1e-12, elementwise_affine=True)
|
130 |
+
(dropout): Dropout(p=0.1, inplace=False)
|
131 |
+
)
|
132 |
+
)
|
133 |
+
(intermediate): ElectraIntermediate(
|
134 |
+
(dense): Linear(in_features=1024, out_features=4096, bias=True)
|
135 |
+
(intermediate_act_fn): GELUActivation()
|
136 |
+
)
|
137 |
+
(output): ElectraOutput(
|
138 |
+
(dense): Linear(in_features=4096, out_features=1024, bias=True)
|
139 |
+
(LayerNorm): LayerNorm((1024,), eps=1e-12, elementwise_affine=True)
|
140 |
+
(dropout): Dropout(p=0.1, inplace=False)
|
141 |
+
)
|
142 |
+
)
|
143 |
+
)
|
144 |
+
)
|
145 |
+
)
|
146 |
+
(custom_pooling): PoolingLayer()
|
147 |
+
(classifier): Classifier(
|
148 |
+
(layers): Sequential(
|
149 |
+
(0): Linear(in_features=1024, out_features=1024, bias=True)
|
150 |
+
(1): SwishGLU(
|
151 |
+
(projection): Linear(in_features=1024, out_features=2048, bias=True)
|
152 |
+
(activation): SiLU()
|
153 |
+
)
|
154 |
+
(2): Dropout(p=0.3, inplace=False)
|
155 |
+
(3): Linear(in_features=1024, out_features=1024, bias=True)
|
156 |
+
(4): SwishGLU(
|
157 |
+
(projection): Linear(in_features=1024, out_features=2048, bias=True)
|
158 |
+
(activation): SiLU()
|
159 |
+
)
|
160 |
+
(5): Dropout(p=0.3, inplace=False)
|
161 |
+
(6): Linear(in_features=1024, out_features=3, bias=True)
|
162 |
+
)
|
163 |
+
)
|
164 |
+
)
|
165 |
+
```
|
166 |
+
|
167 |
+
## Custom Model Components
|
168 |
+
|
169 |
+
### SwishGLU Activation Function
|
170 |
+
|
171 |
+
The SwishGLU activation function combines the Swish activation with a Gated Linear Unit (GLU). It enhances the model's ability to capture complex patterns in the data.
|
172 |
+
|
173 |
+
```python
|
174 |
+
class SwishGLU(nn.Module):
|
175 |
+
def __init__(self, input_dim: int, output_dim: int):
|
176 |
+
super(SwishGLU, self).__init__()
|
177 |
+
self.projection = nn.Linear(input_dim, 2 * output_dim)
|
178 |
+
self.activation = nn.SiLU()
|
179 |
+
|
180 |
+
def forward(self, x):
|
181 |
+
x_proj_gate = self.projection(x)
|
182 |
+
projected, gate = x_proj_gate.tensor_split(2, dim=-1)
|
183 |
+
return projected * self.activation(gate)
|
184 |
+
```
|
185 |
+
|
186 |
+
### PoolingLayer
|
187 |
+
|
188 |
+
The PoolingLayer class allows you to choose between different pooling strategies:
|
189 |
+
|
190 |
+
- `cls`: Uses the representation of the \[CLS\] token.
|
191 |
+
- `mean`: Calculates the mean of the token embeddings.
|
192 |
+
- `max`: Takes the maximum value across token embeddings.
|
193 |
+
|
194 |
+
**'mean'** pooling was used in the fine-tuned model.
|
195 |
+
|
196 |
+
```python
|
197 |
+
class PoolingLayer(nn.Module):
|
198 |
+
def __init__(self, pooling_type='cls'):
|
199 |
+
super().__init__()
|
200 |
+
self.pooling_type = pooling_type
|
201 |
+
|
202 |
+
def forward(self, last_hidden_state, attention_mask):
|
203 |
+
if self.pooling_type == 'cls':
|
204 |
+
return last_hidden_state[:, 0, :]
|
205 |
+
elif self.pooling_type == 'mean':
|
206 |
+
return (last_hidden_state * attention_mask.unsqueeze(-1)).sum(1) / attention_mask.sum(-1).unsqueeze(-1)
|
207 |
+
elif self.pooling_type == 'max':
|
208 |
+
return torch.max(last_hidden_state * attention_mask.unsqueeze(-1), dim=1)[0]
|
209 |
+
else:
|
210 |
+
raise ValueError(f"Unknown pooling method: {self.pooling_type}")
|
211 |
+
```
|
212 |
+
|
213 |
+
### Classifier
|
214 |
+
|
215 |
+
The Classifier class is a customizable feed-forward neural network used for the final classification.
|
216 |
+
|
217 |
+
The fine-tuned model had:
|
218 |
+
|
219 |
+
- `input_dim`: 1024
|
220 |
+
- `num_layers`: 2
|
221 |
+
- `hidden_dim`: 1024
|
222 |
+
- `hidden_activation`: SwishGLU
|
223 |
+
- `dropout_rate`: 0.3
|
224 |
+
- `n_classes`: 3
|
225 |
+
|
226 |
+
```python
|
227 |
+
class Classifier(nn.Module):
|
228 |
+
def __init__(self, input_dim, hidden_dim, hidden_activation, num_layers, n_classes, dropout_rate=0.0):
|
229 |
+
super().__init__()
|
230 |
+
layers = []
|
231 |
+
layers.append(nn.Linear(input_dim, hidden_dim))
|
232 |
+
layers.append(hidden_activation)
|
233 |
+
if dropout_rate > 0:
|
234 |
+
layers.append(nn.Dropout(dropout_rate))
|
235 |
+
|
236 |
+
for _ in range(num_layers - 1):
|
237 |
+
layers.append(nn.Linear(hidden_dim, hidden_dim))
|
238 |
+
layers.append(hidden_activation)
|
239 |
+
if dropout_rate > 0:
|
240 |
+
layers.append(nn.Dropout(dropout_rate))
|
241 |
+
|
242 |
+
layers.append(nn.Linear(hidden_dim, n_classes))
|
243 |
+
self.layers = nn.Sequential(*layers)
|
244 |
+
```
|
245 |
+
|
246 |
+
## Model Configuration
|
247 |
+
|
248 |
+
The model's configuration (config.json) includes custom parameters:
|
249 |
+
|
250 |
+
- `hidden_dim`: Size of the hidden layers in the classifier.
|
251 |
+
- `hidden_activation`: Activation function used in the classifier ('SwishGLU').
|
252 |
+
- `num_layers`: Number of layers in the classifier.
|
253 |
+
- `dropout_rate`: Dropout rate used in the classifier.
|
254 |
+
- `pooling`: Pooling strategy used ('mean').
|
255 |
+
|
256 |
+
## Performance by Dataset
|
257 |
+
|
258 |
+
### Merged Dataset
|
259 |
+
|
260 |
+
```
|
261 |
+
Merged Dataset Classification Report
|
262 |
+
|
263 |
+
precision recall f1-score support
|
264 |
+
|
265 |
+
negative 0.858503 0.843537 0.850954 2352
|
266 |
+
neutral 0.747684 0.750137 0.748908 1829
|
267 |
+
positive 0.864513 0.877395 0.870906 2349
|
268 |
+
|
269 |
+
accuracy 0.829556 6530
|
270 |
+
macro avg 0.823567 0.823690 0.823590 6530
|
271 |
+
weighted avg 0.829626 0.829556 0.829549 6530
|
272 |
+
|
273 |
+
ROC AUC: 0.947247
|
274 |
+
|
275 |
+
Predicted negative neutral positive
|
276 |
+
Actual
|
277 |
+
negative 1984 256 112
|
278 |
+
neutral 246 1372 211
|
279 |
+
positive 81 207 2061
|
280 |
+
|
281 |
+
Macro F1 Score: 0.82
|
282 |
+
```
|
283 |
+
|
284 |
+
### DynaSent Round 1
|
285 |
+
|
286 |
+
```
|
287 |
+
DynaSent Round 1 Classification Report
|
288 |
+
|
289 |
+
precision recall f1-score support
|
290 |
+
|
291 |
+
negative 0.913204 0.824167 0.866404 1200
|
292 |
+
neutral 0.779433 0.915833 0.842146 1200
|
293 |
+
positive 0.905149 0.835000 0.868661 1200
|
294 |
+
|
295 |
+
accuracy 0.858333 3600
|
296 |
+
macro avg 0.865929 0.858333 0.859070 3600
|
297 |
+
weighted avg 0.865929 0.858333 0.859070 3600
|
298 |
+
|
299 |
+
ROC AUC: 0.963133
|
300 |
+
|
301 |
+
Predicted negative neutral positive
|
302 |
+
Actual
|
303 |
+
negative 989 156 55
|
304 |
+
neutral 51 1099 50
|
305 |
+
positive 43 155 1002
|
306 |
+
|
307 |
+
Macro F1 Score: 0.86
|
308 |
+
```
|
309 |
+
|
310 |
+
### DynaSent Round 2
|
311 |
+
|
312 |
+
```
|
313 |
+
DynaSent Round 2 Classification Report
|
314 |
+
|
315 |
+
precision recall f1-score support
|
316 |
+
|
317 |
+
negative 0.764706 0.812500 0.787879 240
|
318 |
+
neutral 0.814815 0.641667 0.717949 240
|
319 |
+
positive 0.731884 0.841667 0.782946 240
|
320 |
+
|
321 |
+
accuracy 0.765278 720
|
322 |
+
macro avg 0.770468 0.765278 0.762924 720
|
323 |
+
weighted avg 0.770468 0.765278 0.762924 720
|
324 |
+
|
325 |
+
ROC AUC: 0.927688
|
326 |
+
|
327 |
+
Predicted negative neutral positive
|
328 |
+
Actual
|
329 |
+
negative 195 19 26
|
330 |
+
neutral 38 154 48
|
331 |
+
positive 22 16 202
|
332 |
+
|
333 |
+
Macro F1 Score: 0.76
|
334 |
+
```
|
335 |
+
|
336 |
+
### Stanford Sentiment Treebank (SST-3)
|
337 |
+
|
338 |
+
```
|
339 |
+
SST-3 Classification Report
|
340 |
+
|
341 |
+
precision recall f1-score support
|
342 |
+
|
343 |
+
negative 0.822199 0.877193 0.848806 912
|
344 |
+
neutral 0.504237 0.305913 0.380800 389
|
345 |
+
positive 0.856144 0.942794 0.897382 909
|
346 |
+
|
347 |
+
accuracy 0.803620 2210
|
348 |
+
macro avg 0.727527 0.708633 0.708996 2210
|
349 |
+
weighted avg 0.780194 0.803620 0.786409 2210
|
350 |
+
|
351 |
+
ROC AUC: 0.904787
|
352 |
+
|
353 |
+
Predicted negative neutral positive
|
354 |
+
Actual
|
355 |
+
negative 800 81 31
|
356 |
+
neutral 157 119 113
|
357 |
+
positive 16 36 857
|
358 |
+
|
359 |
+
Macro F1 Score: 0.71
|
360 |
+
```
|
361 |
+
|
362 |
+
## License
|
363 |
+
|
364 |
+
This model is licensed under the MIT License.
|
365 |
+
|
366 |
+
## Citation
|
367 |
+
|
368 |
+
If you use this model in your work, please consider citing it:
|
369 |
+
|
370 |
+
```bibtex
|
371 |
+
@misc{beno-2024-electra_base_classifier_sentiment,
|
372 |
+
title={Electra Large Classifier for Sentiment Analysis},
|
373 |
+
author={Jim Beno},
|
374 |
+
year={2024},
|
375 |
+
publisher={Hugging Face},
|
376 |
+
howpublished={\url{https://huggingface.co/jbeno/electra-large-classifier-sentiment}},
|
377 |
+
}
|
378 |
+
```
|
379 |
+
|
380 |
+
## Contact
|
381 |
+
|
382 |
+
For questions or comments, please open an issue on the repository or contact [Jim Beno](https://huggingface.co/jbeno).
|
383 |
+
|
384 |
+
## Acknowledgments
|
385 |
+
|
386 |
+
- The [Hugging Face Transformers library](https://github.com/huggingface/transformers) for providing powerful tools for model development.
|
387 |
+
- The creators of the [ELECTRA model](https://arxiv.org/abs/2003.10555) for their foundational work.
|
388 |
+
- The authors of the datasets used: [Stanford Sentiment Treebank](https://huggingface.co/datasets/stanfordnlp/sst), [DynaSent](https://huggingface.co/datasets/dynabench/dynasent).
|
389 |
+
- [Stanford Engineering CGOE](https://cgoe.stanford.edu), [Chris Potts](https://stanford.edu/~cgpotts/), and the Course Facilitators of [XCS224U](https://online.stanford.edu/courses/xcs224u-natural-language-understanding)
|
390 |
+
|
config.json
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"ElectraClassifier"
|
4 |
+
],
|
5 |
+
"attention_probs_dropout_prob": 0.1,
|
6 |
+
"classifier_dropout": null,
|
7 |
+
"dropout_rate": 0.3,
|
8 |
+
"embedding_size": 1024,
|
9 |
+
"hidden_act": "gelu",
|
10 |
+
"hidden_activation": "SwishGLU",
|
11 |
+
"hidden_dim": 1024,
|
12 |
+
"hidden_dropout_prob": 0.1,
|
13 |
+
"hidden_size": 1024,
|
14 |
+
"id2label": {
|
15 |
+
"0": "negative",
|
16 |
+
"1": "neutral",
|
17 |
+
"2": "positive"
|
18 |
+
},
|
19 |
+
"initializer_range": 0.02,
|
20 |
+
"intermediate_size": 4096,
|
21 |
+
"label2id": {
|
22 |
+
"negative": 0,
|
23 |
+
"neutral": 1,
|
24 |
+
"positive": 2
|
25 |
+
},
|
26 |
+
"layer_norm_eps": 1e-12,
|
27 |
+
"max_position_embeddings": 512,
|
28 |
+
"model_type": "electra",
|
29 |
+
"num_attention_heads": 16,
|
30 |
+
"num_hidden_layers": 24,
|
31 |
+
"num_layers": 2,
|
32 |
+
"pad_token_id": 0,
|
33 |
+
"pooling": "mean",
|
34 |
+
"position_embedding_type": "absolute",
|
35 |
+
"summary_activation": "gelu",
|
36 |
+
"summary_last_dropout": 0.1,
|
37 |
+
"summary_type": "first",
|
38 |
+
"summary_use_proj": true,
|
39 |
+
"torch_dtype": "float32",
|
40 |
+
"transformers_version": "4.37.1",
|
41 |
+
"type_vocab_size": 2,
|
42 |
+
"use_cache": true,
|
43 |
+
"vocab_size": 30522
|
44 |
+
}
|
electra_classifier.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
from transformers import ElectraPreTrainedModel, ElectraModel
|
4 |
+
|
5 |
+
# Custom activation function
|
6 |
+
class SwishGLU(nn.Module):
|
7 |
+
def __init__(self, input_dim: int, output_dim: int):
|
8 |
+
super(SwishGLU, self).__init__()
|
9 |
+
self.projection = nn.Linear(input_dim, 2 * output_dim)
|
10 |
+
self.activation = nn.SiLU()
|
11 |
+
|
12 |
+
def forward(self, x):
|
13 |
+
x_proj_gate = self.projection(x)
|
14 |
+
projected, gate = x_proj_gate.tensor_split(2, dim=-1)
|
15 |
+
return projected * self.activation(gate)
|
16 |
+
|
17 |
+
|
18 |
+
# Custom pooling layer
|
19 |
+
class PoolingLayer(nn.Module):
|
20 |
+
def __init__(self, pooling_type='cls'):
|
21 |
+
super().__init__()
|
22 |
+
self.pooling_type = pooling_type
|
23 |
+
|
24 |
+
def forward(self, last_hidden_state, attention_mask):
|
25 |
+
if self.pooling_type == 'cls':
|
26 |
+
return last_hidden_state[:, 0, :]
|
27 |
+
elif self.pooling_type == 'mean':
|
28 |
+
# Mean pooling over the token embeddings
|
29 |
+
return (last_hidden_state * attention_mask.unsqueeze(-1)).sum(1) / attention_mask.sum(-1).unsqueeze(-1)
|
30 |
+
elif self.pooling_type == 'max':
|
31 |
+
# Max pooling over the token embeddings
|
32 |
+
return torch.max(last_hidden_state * attention_mask.unsqueeze(-1), dim=1)[0]
|
33 |
+
else:
|
34 |
+
raise ValueError(f"Unknown pooling method: {self.pooling_type}")
|
35 |
+
|
36 |
+
|
37 |
+
# Custom classifier
|
38 |
+
class Classifier(nn.Module):
|
39 |
+
def __init__(self, input_dim, hidden_dim, hidden_activation, num_layers, n_classes, dropout_rate=0.0):
|
40 |
+
super().__init__()
|
41 |
+
layers = []
|
42 |
+
layers.append(nn.Linear(input_dim, hidden_dim))
|
43 |
+
layers.append(hidden_activation)
|
44 |
+
if dropout_rate > 0:
|
45 |
+
layers.append(nn.Dropout(dropout_rate))
|
46 |
+
|
47 |
+
for _ in range(num_layers - 1):
|
48 |
+
layers.append(nn.Linear(hidden_dim, hidden_dim))
|
49 |
+
layers.append(hidden_activation)
|
50 |
+
if dropout_rate > 0:
|
51 |
+
layers.append(nn.Dropout(dropout_rate))
|
52 |
+
|
53 |
+
layers.append(nn.Linear(hidden_dim, n_classes))
|
54 |
+
self.layers = nn.Sequential(*layers)
|
55 |
+
|
56 |
+
def forward(self, x):
|
57 |
+
return self.layers(x)
|
58 |
+
|
59 |
+
|
60 |
+
# Custom Electra classifier model
|
61 |
+
class ElectraClassifier(ElectraPreTrainedModel):
|
62 |
+
def __init__(self, config):
|
63 |
+
super().__init__(config)
|
64 |
+
self.electra = ElectraModel(config)
|
65 |
+
|
66 |
+
if hasattr(self.electra, 'pooler'):
|
67 |
+
self.electra.pooler = None
|
68 |
+
|
69 |
+
self.pooling = PoolingLayer(pooling_type=config.pooling)
|
70 |
+
|
71 |
+
# Handle custom activation functions
|
72 |
+
activation_name = config.hidden_activation
|
73 |
+
if activation_name == 'SwishGLU':
|
74 |
+
hidden_activation = SwishGLU(
|
75 |
+
input_dim=config.hidden_dim,
|
76 |
+
output_dim=config.hidden_dim
|
77 |
+
)
|
78 |
+
else:
|
79 |
+
activation_class = getattr(nn, activation_name)
|
80 |
+
hidden_activation = activation_class()
|
81 |
+
|
82 |
+
self.classifier = Classifier(
|
83 |
+
input_dim=config.hidden_size,
|
84 |
+
hidden_dim=config.hidden_dim,
|
85 |
+
hidden_activation=hidden_activation,
|
86 |
+
num_layers=config.num_layers,
|
87 |
+
n_classes=config.num_labels,
|
88 |
+
dropout_rate=config.dropout_rate
|
89 |
+
)
|
90 |
+
self.init_weights()
|
91 |
+
|
92 |
+
def forward(self, input_ids=None, attention_mask=None, **kwargs):
|
93 |
+
outputs = self.electra(input_ids, attention_mask=attention_mask, **kwargs)
|
94 |
+
pooled_output = self.pooling(outputs.last_hidden_state, attention_mask)
|
95 |
+
logits = self.classifier(pooled_output)
|
96 |
+
return logits
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:becf9a8ff41dc560d132c6430079e745cffd96e9cfe23c6100022c1bf68ba0b0
|
3 |
+
size 1353223676
|
pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4f138e3d706d1972f0859d0f0385a19c98ac2713c9a9ff8f13f28494986d402c
|
3 |
+
size 1353307070
|
special_tokens_map.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cls_token": "[CLS]",
|
3 |
+
"mask_token": "[MASK]",
|
4 |
+
"pad_token": "[PAD]",
|
5 |
+
"sep_token": "[SEP]",
|
6 |
+
"unk_token": "[UNK]"
|
7 |
+
}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"added_tokens_decoder": {
|
3 |
+
"0": {
|
4 |
+
"content": "[PAD]",
|
5 |
+
"lstrip": false,
|
6 |
+
"normalized": false,
|
7 |
+
"rstrip": false,
|
8 |
+
"single_word": false,
|
9 |
+
"special": true
|
10 |
+
},
|
11 |
+
"100": {
|
12 |
+
"content": "[UNK]",
|
13 |
+
"lstrip": false,
|
14 |
+
"normalized": false,
|
15 |
+
"rstrip": false,
|
16 |
+
"single_word": false,
|
17 |
+
"special": true
|
18 |
+
},
|
19 |
+
"101": {
|
20 |
+
"content": "[CLS]",
|
21 |
+
"lstrip": false,
|
22 |
+
"normalized": false,
|
23 |
+
"rstrip": false,
|
24 |
+
"single_word": false,
|
25 |
+
"special": true
|
26 |
+
},
|
27 |
+
"102": {
|
28 |
+
"content": "[SEP]",
|
29 |
+
"lstrip": false,
|
30 |
+
"normalized": false,
|
31 |
+
"rstrip": false,
|
32 |
+
"single_word": false,
|
33 |
+
"special": true
|
34 |
+
},
|
35 |
+
"103": {
|
36 |
+
"content": "[MASK]",
|
37 |
+
"lstrip": false,
|
38 |
+
"normalized": false,
|
39 |
+
"rstrip": false,
|
40 |
+
"single_word": false,
|
41 |
+
"special": true
|
42 |
+
}
|
43 |
+
},
|
44 |
+
"clean_up_tokenization_spaces": true,
|
45 |
+
"cls_token": "[CLS]",
|
46 |
+
"do_lower_case": true,
|
47 |
+
"mask_token": "[MASK]",
|
48 |
+
"model_max_length": 512,
|
49 |
+
"pad_token": "[PAD]",
|
50 |
+
"sep_token": "[SEP]",
|
51 |
+
"strip_accents": null,
|
52 |
+
"tokenize_chinese_chars": true,
|
53 |
+
"tokenizer_class": "ElectraTokenizer",
|
54 |
+
"unk_token": "[UNK]"
|
55 |
+
}
|
vocab.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|