jbeno commited on
Commit
73296c7
·
verified ·
1 Parent(s): 44200f1

Added arXiv link, updated citation, edited pip install line

Browse files
Files changed (1) hide show
  1. README.md +395 -391
README.md CHANGED
@@ -1,391 +1,395 @@
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 base discriminator](https://huggingface.co/google/electra-base-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-base-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: **79.29**
89
- - Accuracy: **79.69**
90
- - **DynaSent R1**
91
- - Macro Average F1: **82.10**
92
- - Accuracy: **82.14**
93
- - **DynaSent R2**
94
- - Macro Average F1: **71.83**
95
- - Accuracy: **71.94**
96
- - **SST-3**
97
- - Macro Average F1: **69.95**
98
- - Accuracy: **78.24**
99
-
100
- ## Model Architecture
101
-
102
- - **Base Model**: ELECTRA base discriminator (`google/electra-base-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, 768, padding_idx=0)
112
- (position_embeddings): Embedding(512, 768)
113
- (token_type_embeddings): Embedding(2, 768)
114
- (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
115
- (dropout): Dropout(p=0.1, inplace=False)
116
- )
117
- (encoder): ElectraEncoder(
118
- (layer): ModuleList(
119
- (0-11): 12 x ElectraLayer(
120
- (attention): ElectraAttention(
121
- (self): ElectraSelfAttention(
122
- (query): Linear(in_features=768, out_features=768, bias=True)
123
- (key): Linear(in_features=768, out_features=768, bias=True)
124
- (value): Linear(in_features=768, out_features=768, bias=True)
125
- (dropout): Dropout(p=0.1, inplace=False)
126
- )
127
- (output): ElectraSelfOutput(
128
- (dense): Linear(in_features=768, out_features=768, bias=True)
129
- (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
130
- (dropout): Dropout(p=0.1, inplace=False)
131
- )
132
- )
133
- (intermediate): ElectraIntermediate(
134
- (dense): Linear(in_features=768, out_features=3072, bias=True)
135
- (intermediate_act_fn): GELUActivation()
136
- )
137
- (output): ElectraOutput(
138
- (dense): Linear(in_features=3072, out_features=768, bias=True)
139
- (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
140
- (dropout): Dropout(p=0.1, inplace=False)
141
- )
142
- )
143
- )
144
- )
145
- )
146
- (pooling): PoolingLayer()
147
- (classifier): Classifier(
148
- (layers): Sequential(
149
- (0): Linear(in_features=768, 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
-
168
- ## Custom Model Components
169
-
170
- ### SwishGLU Activation Function
171
-
172
- 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.
173
-
174
- ```python
175
- class SwishGLU(nn.Module):
176
- def __init__(self, input_dim: int, output_dim: int):
177
- super(SwishGLU, self).__init__()
178
- self.projection = nn.Linear(input_dim, 2 * output_dim)
179
- self.activation = nn.SiLU()
180
-
181
- def forward(self, x):
182
- x_proj_gate = self.projection(x)
183
- projected, gate = x_proj_gate.tensor_split(2, dim=-1)
184
- return projected * self.activation(gate)
185
- ```
186
-
187
- ### PoolingLayer
188
-
189
- The PoolingLayer class allows you to choose between different pooling strategies:
190
-
191
- - `cls`: Uses the representation of the \[CLS\] token.
192
- - `mean`: Calculates the mean of the token embeddings.
193
- - `max`: Takes the maximum value across token embeddings.
194
-
195
- **'mean'** pooling was used in the fine-tuned model.
196
-
197
- ```python
198
- class PoolingLayer(nn.Module):
199
- def __init__(self, pooling_type='cls'):
200
- super().__init__()
201
- self.pooling_type = pooling_type
202
-
203
- def forward(self, last_hidden_state, attention_mask):
204
- if self.pooling_type == 'cls':
205
- return last_hidden_state[:, 0, :]
206
- elif self.pooling_type == 'mean':
207
- return (last_hidden_state * attention_mask.unsqueeze(-1)).sum(1) / attention_mask.sum(-1).unsqueeze(-1)
208
- elif self.pooling_type == 'max':
209
- return torch.max(last_hidden_state * attention_mask.unsqueeze(-1), dim=1)[0]
210
- else:
211
- raise ValueError(f"Unknown pooling method: {self.pooling_type}")
212
- ```
213
-
214
- ### Classifier
215
-
216
- The Classifier class is a customizable feed-forward neural network used for the final classification.
217
-
218
- The fine-tuned model had:
219
-
220
- - `input_dim`: 768
221
- - `num_layers`: 2
222
- - `hidden_dim`: 1024
223
- - `hidden_activation`: SwishGLU
224
- - `dropout_rate`: 0.3
225
- - `n_classes`: 3
226
-
227
- ```python
228
- class Classifier(nn.Module):
229
- def __init__(self, input_dim, hidden_dim, hidden_activation, num_layers, n_classes, dropout_rate=0.0):
230
- super().__init__()
231
- layers = []
232
- layers.append(nn.Linear(input_dim, hidden_dim))
233
- layers.append(hidden_activation)
234
- if dropout_rate > 0:
235
- layers.append(nn.Dropout(dropout_rate))
236
-
237
- for _ in range(num_layers - 1):
238
- layers.append(nn.Linear(hidden_dim, hidden_dim))
239
- layers.append(hidden_activation)
240
- if dropout_rate > 0:
241
- layers.append(nn.Dropout(dropout_rate))
242
-
243
- layers.append(nn.Linear(hidden_dim, n_classes))
244
- self.layers = nn.Sequential(*layers)
245
- ```
246
-
247
- ## Model Configuration
248
-
249
- The model's configuration (config.json) includes custom parameters:
250
-
251
- - `hidden_dim`: Size of the hidden layers in the classifier.
252
- - `hidden_activation`: Activation function used in the classifier ('SwishGLU').
253
- - `num_layers`: Number of layers in the classifier.
254
- - `dropout_rate`: Dropout rate used in the classifier.
255
- - `pooling`: Pooling strategy used ('mean').
256
-
257
- ## Performance by Dataset
258
-
259
- ### Merged Dataset
260
-
261
- ```
262
- Merged Dataset Classification Report
263
-
264
- precision recall f1-score support
265
-
266
- negative 0.847081 0.777211 0.810643 2352
267
- neutral 0.704453 0.761072 0.731669 1829
268
- positive 0.828047 0.844615 0.836249 2349
269
-
270
- accuracy 0.796937 6530
271
- macro avg 0.793194 0.794299 0.792854 6530
272
- weighted avg 0.800285 0.796937 0.797734 6530
273
-
274
- ROC AUC: 0.926344
275
-
276
- Predicted negative neutral positive
277
- Actual
278
- negative 1828 331 193
279
- neutral 218 1392 219
280
- positive 112 253 1984
281
-
282
- Macro F1 Score: 0.79
283
- ```
284
-
285
- ### DynaSent Round 1
286
-
287
- ```
288
- DynaSent Round 1 Classification Report
289
-
290
- precision recall f1-score support
291
-
292
- negative 0.901222 0.737500 0.811182 1200
293
- neutral 0.745957 0.922500 0.824888 1200
294
- positive 0.850970 0.804167 0.826907 1200
295
-
296
- accuracy 0.821389 3600
297
- macro avg 0.832716 0.821389 0.820992 3600
298
- weighted avg 0.832716 0.821389 0.820992 3600
299
-
300
- ROC AUC: 0.945131
301
-
302
- Predicted negative neutral positive
303
- Actual
304
- negative 885 201 114
305
- neutral 38 1107 55
306
- positive 59 176 965
307
-
308
- Macro F1 Score: 0.82
309
- ```
310
-
311
- ### DynaSent Round 2
312
-
313
- ```
314
- DynaSent Round 2 Classification Report
315
-
316
- precision recall f1-score support
317
-
318
- negative 0.696154 0.754167 0.724000 240
319
- neutral 0.770408 0.629167 0.692661 240
320
- positive 0.704545 0.775000 0.738095 240
321
-
322
- accuracy 0.719444 720
323
- macro avg 0.723702 0.719444 0.718252 720
324
- weighted avg 0.723702 0.719444 0.718252 720
325
-
326
- ROC AUC: 0.88842
327
-
328
- Predicted negative neutral positive
329
- Actual
330
- negative 181 26 33
331
- neutral 44 151 45
332
- positive 35 19 186
333
-
334
- Macro F1 Score: 0.72
335
- ```
336
-
337
- ### Stanford Sentiment Treebank (SST-3)
338
-
339
- ```
340
- SST-3 Classification Report
341
-
342
- precision recall f1-score support
343
-
344
- negative 0.831878 0.835526 0.833698 912
345
- neutral 0.452703 0.344473 0.391241 389
346
- positive 0.834669 0.916392 0.873623 909
347
-
348
- accuracy 0.782353 2210
349
- macro avg 0.706417 0.698797 0.699521 2210
350
- weighted avg 0.766284 0.782353 0.772239 2210
351
-
352
- ROC AUC: 0.885009
353
-
354
- Predicted negative neutral positive
355
- Actual
356
- negative 762 104 46
357
- neutral 136 134 119
358
- positive 18 58 833
359
-
360
- Macro F1 Score: 0.70
361
- ```
362
-
363
- ## License
364
-
365
- This model is licensed under the MIT License.
366
-
367
- ## Citation
368
-
369
- If you use this model in your work, please consider citing it:
370
-
371
- ```bibtex
372
- @misc{beno-2024-electra_base_classifier_sentiment,
373
- title={Electra Base Classifier for Sentiment Analysis},
374
- author={Jim Beno},
375
- year={2024},
376
- publisher={Hugging Face},
377
- howpublished={\url{https://huggingface.co/jbeno/electra-base-classifier-sentiment}},
378
- }
379
- ```
380
-
381
- ## Contact
382
-
383
- For questions or comments, please open an issue on the repository or contact [Jim Beno](https://huggingface.co/jbeno).
384
-
385
- ## Acknowledgments
386
-
387
- - The [Hugging Face Transformers library](https://github.com/huggingface/transformers) for providing powerful tools for model development.
388
- - The creators of the [ELECTRA model](https://arxiv.org/abs/2003.10555) for their foundational work.
389
- - The authors of the datasets used: [Stanford Sentiment Treebank](https://huggingface.co/datasets/stanfordnlp/sst), [DynaSent](https://huggingface.co/datasets/dynabench/dynasent).
390
- - [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)
391
-
 
 
 
 
 
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 base discriminator](https://huggingface.co/google/electra-base-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
+ import sys
38
+ !{sys.executable} -m pip install electra-classifier
39
+
40
+ # Import libraries
41
+ import torch
42
+ from transformers import AutoTokenizer
43
+ from electra_classifier import ElectraClassifier
44
+
45
+ # Load tokenizer and model
46
+ model_name = "jbeno/electra-base-classifier-sentiment"
47
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
48
+ model = ElectraClassifier.from_pretrained(model_name)
49
+
50
+ # Set model to evaluation mode
51
+ model.eval()
52
+
53
+ # Run inference
54
+ text = "I love this restaurant!"
55
+ inputs = tokenizer(text, return_tensors="pt")
56
+
57
+ with torch.no_grad():
58
+ logits = model(**inputs)
59
+ predicted_class_id = torch.argmax(logits, dim=1).item()
60
+ predicted_label = model.config.id2label[predicted_class_id]
61
+ print(f"Predicted label: {predicted_label}")
62
+ ```
63
+
64
+ ## Requirements
65
+ - Python 3.7+
66
+ - PyTorch
67
+ - Transformers
68
+ - [electra-classifier](https://pypi.org/project/electra-classifier/) - Install with pip, or download electra_classifier.py
69
+
70
+ ## Training Details
71
+
72
+ ### Dataset
73
+
74
+ 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.
75
+
76
+ ### Code
77
+
78
+ The code used to train the model can be found on GitHub:
79
+ - [jbeno/sentiment](https://github.com/jbeno/sentiment)
80
+ - [jbeno/electra-classifier](https://github.com/jbeno/electra-classifier)
81
+
82
+ ### Research Paper
83
+
84
+ The research paper can be found here: [ELECTRA and GPT-4o: Cost-Effective Partners for Sentiment Analysis](http://arxiv.org/abs/2501.00062) (arXiv:2501.00062)
85
+
86
+ ### Performance Summary
87
+
88
+ - **Merged Dataset**
89
+ - Macro Average F1: **79.29**
90
+ - Accuracy: **79.69**
91
+ - **DynaSent R1**
92
+ - Macro Average F1: **82.10**
93
+ - Accuracy: **82.14**
94
+ - **DynaSent R2**
95
+ - Macro Average F1: **71.83**
96
+ - Accuracy: **71.94**
97
+ - **SST-3**
98
+ - Macro Average F1: **69.95**
99
+ - Accuracy: **78.24**
100
+
101
+ ## Model Architecture
102
+
103
+ - **Base Model**: ELECTRA base discriminator (`google/electra-base-discriminator`)
104
+ - **Pooling Layer**: Custom pooling layer supporting 'cls', 'mean', and 'max' pooling types.
105
+ - **Classifier**: Custom classifier with configurable hidden dimensions, number of layers, and dropout rate.
106
+ - **Activation Function**: Custom SwishGLU activation function.
107
+
108
+ ```
109
+ ElectraClassifier(
110
+ (electra): ElectraModel(
111
+ (embeddings): ElectraEmbeddings(
112
+ (word_embeddings): Embedding(30522, 768, padding_idx=0)
113
+ (position_embeddings): Embedding(512, 768)
114
+ (token_type_embeddings): Embedding(2, 768)
115
+ (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
116
+ (dropout): Dropout(p=0.1, inplace=False)
117
+ )
118
+ (encoder): ElectraEncoder(
119
+ (layer): ModuleList(
120
+ (0-11): 12 x ElectraLayer(
121
+ (attention): ElectraAttention(
122
+ (self): ElectraSelfAttention(
123
+ (query): Linear(in_features=768, out_features=768, bias=True)
124
+ (key): Linear(in_features=768, out_features=768, bias=True)
125
+ (value): Linear(in_features=768, out_features=768, bias=True)
126
+ (dropout): Dropout(p=0.1, inplace=False)
127
+ )
128
+ (output): ElectraSelfOutput(
129
+ (dense): Linear(in_features=768, out_features=768, bias=True)
130
+ (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
131
+ (dropout): Dropout(p=0.1, inplace=False)
132
+ )
133
+ )
134
+ (intermediate): ElectraIntermediate(
135
+ (dense): Linear(in_features=768, out_features=3072, bias=True)
136
+ (intermediate_act_fn): GELUActivation()
137
+ )
138
+ (output): ElectraOutput(
139
+ (dense): Linear(in_features=3072, out_features=768, bias=True)
140
+ (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
141
+ (dropout): Dropout(p=0.1, inplace=False)
142
+ )
143
+ )
144
+ )
145
+ )
146
+ )
147
+ (pooling): PoolingLayer()
148
+ (classifier): Classifier(
149
+ (layers): Sequential(
150
+ (0): Linear(in_features=768, out_features=1024, bias=True)
151
+ (1): SwishGLU(
152
+ (projection): Linear(in_features=1024, out_features=2048, bias=True)
153
+ (activation): SiLU()
154
+ )
155
+ (2): Dropout(p=0.3, inplace=False)
156
+ (3): Linear(in_features=1024, out_features=1024, bias=True)
157
+ (4): SwishGLU(
158
+ (projection): Linear(in_features=1024, out_features=2048, bias=True)
159
+ (activation): SiLU()
160
+ )
161
+ (5): Dropout(p=0.3, inplace=False)
162
+ (6): Linear(in_features=1024, out_features=3, bias=True)
163
+ )
164
+ )
165
+ )
166
+ ```
167
+
168
+
169
+ ## Custom Model Components
170
+
171
+ ### SwishGLU Activation Function
172
+
173
+ 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.
174
+
175
+ ```python
176
+ class SwishGLU(nn.Module):
177
+ def __init__(self, input_dim: int, output_dim: int):
178
+ super(SwishGLU, self).__init__()
179
+ self.projection = nn.Linear(input_dim, 2 * output_dim)
180
+ self.activation = nn.SiLU()
181
+
182
+ def forward(self, x):
183
+ x_proj_gate = self.projection(x)
184
+ projected, gate = x_proj_gate.tensor_split(2, dim=-1)
185
+ return projected * self.activation(gate)
186
+ ```
187
+
188
+ ### PoolingLayer
189
+
190
+ The PoolingLayer class allows you to choose between different pooling strategies:
191
+
192
+ - `cls`: Uses the representation of the \[CLS\] token.
193
+ - `mean`: Calculates the mean of the token embeddings.
194
+ - `max`: Takes the maximum value across token embeddings.
195
+
196
+ **'mean'** pooling was used in the fine-tuned model.
197
+
198
+ ```python
199
+ class PoolingLayer(nn.Module):
200
+ def __init__(self, pooling_type='cls'):
201
+ super().__init__()
202
+ self.pooling_type = pooling_type
203
+
204
+ def forward(self, last_hidden_state, attention_mask):
205
+ if self.pooling_type == 'cls':
206
+ return last_hidden_state[:, 0, :]
207
+ elif self.pooling_type == 'mean':
208
+ return (last_hidden_state * attention_mask.unsqueeze(-1)).sum(1) / attention_mask.sum(-1).unsqueeze(-1)
209
+ elif self.pooling_type == 'max':
210
+ return torch.max(last_hidden_state * attention_mask.unsqueeze(-1), dim=1)[0]
211
+ else:
212
+ raise ValueError(f"Unknown pooling method: {self.pooling_type}")
213
+ ```
214
+
215
+ ### Classifier
216
+
217
+ The Classifier class is a customizable feed-forward neural network used for the final classification.
218
+
219
+ The fine-tuned model had:
220
+
221
+ - `input_dim`: 768
222
+ - `num_layers`: 2
223
+ - `hidden_dim`: 1024
224
+ - `hidden_activation`: SwishGLU
225
+ - `dropout_rate`: 0.3
226
+ - `n_classes`: 3
227
+
228
+ ```python
229
+ class Classifier(nn.Module):
230
+ def __init__(self, input_dim, hidden_dim, hidden_activation, num_layers, n_classes, dropout_rate=0.0):
231
+ super().__init__()
232
+ layers = []
233
+ layers.append(nn.Linear(input_dim, hidden_dim))
234
+ layers.append(hidden_activation)
235
+ if dropout_rate > 0:
236
+ layers.append(nn.Dropout(dropout_rate))
237
+
238
+ for _ in range(num_layers - 1):
239
+ layers.append(nn.Linear(hidden_dim, hidden_dim))
240
+ layers.append(hidden_activation)
241
+ if dropout_rate > 0:
242
+ layers.append(nn.Dropout(dropout_rate))
243
+
244
+ layers.append(nn.Linear(hidden_dim, n_classes))
245
+ self.layers = nn.Sequential(*layers)
246
+ ```
247
+
248
+ ## Model Configuration
249
+
250
+ The model's configuration (config.json) includes custom parameters:
251
+
252
+ - `hidden_dim`: Size of the hidden layers in the classifier.
253
+ - `hidden_activation`: Activation function used in the classifier ('SwishGLU').
254
+ - `num_layers`: Number of layers in the classifier.
255
+ - `dropout_rate`: Dropout rate used in the classifier.
256
+ - `pooling`: Pooling strategy used ('mean').
257
+
258
+ ## Performance by Dataset
259
+
260
+ ### Merged Dataset
261
+
262
+ ```
263
+ Merged Dataset Classification Report
264
+
265
+ precision recall f1-score support
266
+
267
+ negative 0.847081 0.777211 0.810643 2352
268
+ neutral 0.704453 0.761072 0.731669 1829
269
+ positive 0.828047 0.844615 0.836249 2349
270
+
271
+ accuracy 0.796937 6530
272
+ macro avg 0.793194 0.794299 0.792854 6530
273
+ weighted avg 0.800285 0.796937 0.797734 6530
274
+
275
+ ROC AUC: 0.926344
276
+
277
+ Predicted negative neutral positive
278
+ Actual
279
+ negative 1828 331 193
280
+ neutral 218 1392 219
281
+ positive 112 253 1984
282
+
283
+ Macro F1 Score: 0.79
284
+ ```
285
+
286
+ ### DynaSent Round 1
287
+
288
+ ```
289
+ DynaSent Round 1 Classification Report
290
+
291
+ precision recall f1-score support
292
+
293
+ negative 0.901222 0.737500 0.811182 1200
294
+ neutral 0.745957 0.922500 0.824888 1200
295
+ positive 0.850970 0.804167 0.826907 1200
296
+
297
+ accuracy 0.821389 3600
298
+ macro avg 0.832716 0.821389 0.820992 3600
299
+ weighted avg 0.832716 0.821389 0.820992 3600
300
+
301
+ ROC AUC: 0.945131
302
+
303
+ Predicted negative neutral positive
304
+ Actual
305
+ negative 885 201 114
306
+ neutral 38 1107 55
307
+ positive 59 176 965
308
+
309
+ Macro F1 Score: 0.82
310
+ ```
311
+
312
+ ### DynaSent Round 2
313
+
314
+ ```
315
+ DynaSent Round 2 Classification Report
316
+
317
+ precision recall f1-score support
318
+
319
+ negative 0.696154 0.754167 0.724000 240
320
+ neutral 0.770408 0.629167 0.692661 240
321
+ positive 0.704545 0.775000 0.738095 240
322
+
323
+ accuracy 0.719444 720
324
+ macro avg 0.723702 0.719444 0.718252 720
325
+ weighted avg 0.723702 0.719444 0.718252 720
326
+
327
+ ROC AUC: 0.88842
328
+
329
+ Predicted negative neutral positive
330
+ Actual
331
+ negative 181 26 33
332
+ neutral 44 151 45
333
+ positive 35 19 186
334
+
335
+ Macro F1 Score: 0.72
336
+ ```
337
+
338
+ ### Stanford Sentiment Treebank (SST-3)
339
+
340
+ ```
341
+ SST-3 Classification Report
342
+
343
+ precision recall f1-score support
344
+
345
+ negative 0.831878 0.835526 0.833698 912
346
+ neutral 0.452703 0.344473 0.391241 389
347
+ positive 0.834669 0.916392 0.873623 909
348
+
349
+ accuracy 0.782353 2210
350
+ macro avg 0.706417 0.698797 0.699521 2210
351
+ weighted avg 0.766284 0.782353 0.772239 2210
352
+
353
+ ROC AUC: 0.885009
354
+
355
+ Predicted negative neutral positive
356
+ Actual
357
+ negative 762 104 46
358
+ neutral 136 134 119
359
+ positive 18 58 833
360
+
361
+ Macro F1 Score: 0.70
362
+ ```
363
+
364
+ ## License
365
+
366
+ This model is licensed under the MIT License.
367
+
368
+ ## Citation
369
+
370
+ If you use this model in your work, please cite:
371
+
372
+ ```bibtex
373
+ @article{beno-2024-electragpt,
374
+ title={ELECTRA and GPT-4o: Cost-Effective Partners for Sentiment Analysis},
375
+ author={James P. Beno},
376
+ journal={arXiv preprint arXiv:2501.00062},
377
+ year={2024},
378
+ eprint={2501.00062},
379
+ archivePrefix={arXiv},
380
+ primaryClass={cs.CL},
381
+ url={https://arxiv.org/abs/2501.00062},
382
+ }
383
+ ```
384
+
385
+ ## Contact
386
+
387
+ For questions or comments, please open an issue on the repository or contact [Jim Beno](https://huggingface.co/jbeno).
388
+
389
+ ## Acknowledgments
390
+
391
+ - The [Hugging Face Transformers library](https://github.com/huggingface/transformers) for providing powerful tools for model development.
392
+ - The creators of the [ELECTRA model](https://arxiv.org/abs/2003.10555) for their foundational work.
393
+ - The authors of the datasets used: [Stanford Sentiment Treebank](https://huggingface.co/datasets/stanfordnlp/sst), [DynaSent](https://huggingface.co/datasets/dynabench/dynasent).
394
+ - [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)
395
+