jbeno commited on
Commit
3e21aa8
·
verified ·
1 Parent(s): b2c2d5c

Added arXiv link, updated citation, edited pip install line

Browse files
Files changed (1) hide show
  1. README.md +394 -390
README.md CHANGED
@@ -1,390 +1,394 @@
1
- ---
2
- license: mit
3
- tags:
4
- - sentiment-analysis
5
- - text-classification
6
- - electra
7
- - pytorch
8
- - transformers
9
- ---
10
-
11
- # ELECTRA Large 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
-
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - sentiment-analysis
5
+ - text-classification
6
+ - electra
7
+ - pytorch
8
+ - transformers
9
+ ---
10
+
11
+ # ELECTRA Large 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
+ 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-large-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: **82.36**
90
+ - Accuracy: **82.96**
91
+ - **DynaSent R1**
92
+ - Macro Average F1: **85.91**
93
+ - Accuracy: **85.83**
94
+ - **DynaSent R2**
95
+ - Macro Average F1: **76.29**
96
+ - Accuracy: **76.53**
97
+ - **SST-3**
98
+ - Macro Average F1: **70.90**
99
+ - Accuracy: **80.36**
100
+
101
+ ## Model Architecture
102
+
103
+ - **Base Model**: ELECTRA large discriminator (`google/electra-large-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, 1024, padding_idx=0)
113
+ (position_embeddings): Embedding(512, 1024)
114
+ (token_type_embeddings): Embedding(2, 1024)
115
+ (LayerNorm): LayerNorm((1024,), eps=1e-12, elementwise_affine=True)
116
+ (dropout): Dropout(p=0.1, inplace=False)
117
+ )
118
+ (encoder): ElectraEncoder(
119
+ (layer): ModuleList(
120
+ (0-23): 24 x ElectraLayer(
121
+ (attention): ElectraAttention(
122
+ (self): ElectraSelfAttention(
123
+ (query): Linear(in_features=1024, out_features=1024, bias=True)
124
+ (key): Linear(in_features=1024, out_features=1024, bias=True)
125
+ (value): Linear(in_features=1024, out_features=1024, bias=True)
126
+ (dropout): Dropout(p=0.1, inplace=False)
127
+ )
128
+ (output): ElectraSelfOutput(
129
+ (dense): Linear(in_features=1024, out_features=1024, bias=True)
130
+ (LayerNorm): LayerNorm((1024,), eps=1e-12, elementwise_affine=True)
131
+ (dropout): Dropout(p=0.1, inplace=False)
132
+ )
133
+ )
134
+ (intermediate): ElectraIntermediate(
135
+ (dense): Linear(in_features=1024, out_features=4096, bias=True)
136
+ (intermediate_act_fn): GELUActivation()
137
+ )
138
+ (output): ElectraOutput(
139
+ (dense): Linear(in_features=4096, out_features=1024, bias=True)
140
+ (LayerNorm): LayerNorm((1024,), eps=1e-12, elementwise_affine=True)
141
+ (dropout): Dropout(p=0.1, inplace=False)
142
+ )
143
+ )
144
+ )
145
+ )
146
+ )
147
+ (custom_pooling): PoolingLayer()
148
+ (classifier): Classifier(
149
+ (layers): Sequential(
150
+ (0): Linear(in_features=1024, 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
+ ## 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`: 1024
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.858503 0.843537 0.850954 2352
267
+ neutral 0.747684 0.750137 0.748908 1829
268
+ positive 0.864513 0.877395 0.870906 2349
269
+
270
+ accuracy 0.829556 6530
271
+ macro avg 0.823567 0.823690 0.823590 6530
272
+ weighted avg 0.829626 0.829556 0.829549 6530
273
+
274
+ ROC AUC: 0.947247
275
+
276
+ Predicted negative neutral positive
277
+ Actual
278
+ negative 1984 256 112
279
+ neutral 246 1372 211
280
+ positive 81 207 2061
281
+
282
+ Macro F1 Score: 0.82
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.913204 0.824167 0.866404 1200
293
+ neutral 0.779433 0.915833 0.842146 1200
294
+ positive 0.905149 0.835000 0.868661 1200
295
+
296
+ accuracy 0.858333 3600
297
+ macro avg 0.865929 0.858333 0.859070 3600
298
+ weighted avg 0.865929 0.858333 0.859070 3600
299
+
300
+ ROC AUC: 0.963133
301
+
302
+ Predicted negative neutral positive
303
+ Actual
304
+ negative 989 156 55
305
+ neutral 51 1099 50
306
+ positive 43 155 1002
307
+
308
+ Macro F1 Score: 0.86
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.764706 0.812500 0.787879 240
319
+ neutral 0.814815 0.641667 0.717949 240
320
+ positive 0.731884 0.841667 0.782946 240
321
+
322
+ accuracy 0.765278 720
323
+ macro avg 0.770468 0.765278 0.762924 720
324
+ weighted avg 0.770468 0.765278 0.762924 720
325
+
326
+ ROC AUC: 0.927688
327
+
328
+ Predicted negative neutral positive
329
+ Actual
330
+ negative 195 19 26
331
+ neutral 38 154 48
332
+ positive 22 16 202
333
+
334
+ Macro F1 Score: 0.76
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.822199 0.877193 0.848806 912
345
+ neutral 0.504237 0.305913 0.380800 389
346
+ positive 0.856144 0.942794 0.897382 909
347
+
348
+ accuracy 0.803620 2210
349
+ macro avg 0.727527 0.708633 0.708996 2210
350
+ weighted avg 0.780194 0.803620 0.786409 2210
351
+
352
+ ROC AUC: 0.904787
353
+
354
+ Predicted negative neutral positive
355
+ Actual
356
+ negative 800 81 31
357
+ neutral 157 119 113
358
+ positive 16 36 857
359
+
360
+ Macro F1 Score: 0.71
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 cite:
370
+
371
+ ```bibtex
372
+ @article{beno-2024-electragpt,
373
+ title={ELECTRA and GPT-4o: Cost-Effective Partners for Sentiment Analysis},
374
+ author={James P. Beno},
375
+ journal={arXiv preprint arXiv:2501.00062},
376
+ year={2024},
377
+ eprint={2501.00062},
378
+ archivePrefix={arXiv},
379
+ primaryClass={cs.CL},
380
+ url={https://arxiv.org/abs/2501.00062},
381
+ }
382
+ ```
383
+
384
+ ## Contact
385
+
386
+ For questions or comments, please open an issue on the repository or contact [Jim Beno](https://huggingface.co/jbeno).
387
+
388
+ ## Acknowledgments
389
+
390
+ - The [Hugging Face Transformers library](https://github.com/huggingface/transformers) for providing powerful tools for model development.
391
+ - The creators of the [ELECTRA model](https://arxiv.org/abs/2003.10555) for their foundational work.
392
+ - The authors of the datasets used: [Stanford Sentiment Treebank](https://huggingface.co/datasets/stanfordnlp/sst), [DynaSent](https://huggingface.co/datasets/dynabench/dynasent).
393
+ - [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)
394
+