Text Classification
Transformers
English
bert
minbert
transformer
sentiment
tokenizer
classification
Instructions to use GlowCheese/minBERT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use GlowCheese/minBERT with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="GlowCheese/minBERT")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("GlowCheese/minBERT", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Commit Β·
ae64259
1
Parent(s): 27888bb
upd: Rewrite README as a standalone project introduction
Browse files
README.md
CHANGED
|
@@ -10,36 +10,75 @@ library_name: transformers
|
|
| 10 |
tags:
|
| 11 |
- bert
|
| 12 |
- minbert
|
| 13 |
-
-
|
| 14 |
- sentiment
|
| 15 |
- tokenizer
|
| 16 |
- classification
|
| 17 |
---
|
| 18 |
|
| 19 |
-
#
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
| 24 |
-
In the second part, you will use the embeddings produced by your BERT model on three downstream tasks: sentiment classification, paraphrase detection, and semantic similarity. You will implement extensions to improve your model's performance on the three downstream tasks.
|
| 25 |
|
| 26 |
-
|
| 27 |
-
* bert.py: Missing code blocks.
|
| 28 |
-
* classifier.py: Missing code blocks.
|
| 29 |
-
* optimizer.py: Missing code blocks.
|
| 30 |
|
| 31 |
-
|
| 32 |
-
* multitask_classifier.py: Missing code blocks.
|
| 33 |
-
* datasets.py: Possibly useful functions/classes for extensions.
|
| 34 |
-
* evaluation.py: Possibly useful functions/classes for extensions.
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
##
|
| 41 |
|
| 42 |
-
|
| 43 |
-
created by Shuyan Zhou, Zhengbao Jiang, Ritam Dutt, Brendon Boldt, Aditya Veerubhotla, and Graham Neubig.
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
tags:
|
| 11 |
- bert
|
| 12 |
- minbert
|
| 13 |
+
- transformer
|
| 14 |
- sentiment
|
| 15 |
- tokenizer
|
| 16 |
- classification
|
| 17 |
---
|
| 18 |
|
| 19 |
+
# minBERT β Finetuning BERT with Contrastive Learning for Sentiment Analysis
|
| 20 |
|
| 21 |
+
minBERT is a minimal re-implementation of BERT (Bidirectional Encoder Representations from Transformers), built from scratch in PyTorch and used to study how **contrastive learning (SimCSE)** improves the quality of sentence embeddings for **sentiment analysis**.
|
| 22 |
|
| 23 |
+
The idea: instead of relying on task-specific architectures, we take the contextual embedding of the `[CLS]` token produced by minBERT, attach a single classification layer on top, and compare how well the model performs when its embeddings are refined with contrastive pre-finetuning versus used as-is.
|
|
|
|
| 24 |
|
| 25 |
+
## Model architecture
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
minBERT follows the original BERT-base design and loads pretrained `bert-base-uncased` weights:
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
- **Tokenization** β a WordPiece tokenizer converts raw text into token ids, adds special tokens (`[CLS]` at the start, `[PAD]` for batching, `[UNK]` for out-of-vocabulary words), and produces the attention mask.
|
| 30 |
+
- **Embedding layer** β each token is mapped to the sum of its token embedding and position embedding (plus a segment embedding placeholder), followed by LayerNorm and dropout. Hidden size is 768.
|
| 31 |
+
- **Encoder** β a stack of 12 Transformer layers; each layer applies multi-head self-attention, an add & norm step, a feed-forward network with GELU activation, and another add & norm step.
|
| 32 |
+
- **Pooler** β the final hidden state of the `[CLS]` token is passed through a linear layer with tanh activation to obtain the sentence representation.
|
| 33 |
+
- **Classifier head** β dropout followed by a single linear layer over the pooled `[CLS]` embedding, producing the predicted sentiment label.
|
| 34 |
|
| 35 |
+
The **AdamW optimizer** (with first/second moment estimates, bias correction, decoupled weight decay) is used for all training runs.
|
| 36 |
|
| 37 |
+
## SimCSE contrastive finetuning
|
| 38 |
|
| 39 |
+
minBERT's embeddings can be refined with the SimCSE framework. Two variants are implemented:
|
|
|
|
| 40 |
|
| 41 |
+
- **Unsupervised SimCSE** β a positive pair is the *same* sentence passed through the encoder twice with different dropout masks; other sentences in the batch serve as negatives. Trained on ~115k samples from **Amazon Polarity**.
|
| 42 |
+
- **Supervised SimCSE** β positive/negative pairs come from **NLI** entailment/contradiction triplets `(anchor, positive, hard negative)`. Trained on ~265k samples.
|
| 43 |
+
|
| 44 |
+
Both use a contrastive cross-entropy loss over cosine similarities and are evaluated each epoch on **STS-B** via Spearman correlation (best checkpoint kept). The finetuned weights are included in `minbert-model/` (`unsup-cse-bert.pth`, `sup-cse-bert.pth`).
|
| 45 |
+
|
| 46 |
+
## Workflows
|
| 47 |
+
|
| 48 |
+
Everything runs through `run.py`:
|
| 49 |
+
|
| 50 |
+
### 1. Contrastive finetuning (SimCSE)
|
| 51 |
+
|
| 52 |
+
```bash
|
| 53 |
+
python run.py --task finetune --model unsup # unsupervised SimCSE on Amazon Polarity
|
| 54 |
+
python run.py --task finetune --model sup # supervised SimCSE on NLI
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
### 2. Sentiment classifier training & evaluation
|
| 58 |
+
|
| 59 |
+
```bash
|
| 60 |
+
python run.py --task train --model base --dataset sst --train-mode last-linear
|
| 61 |
+
python run.py --task train --model sup --dataset cfimdb --train-mode full-model
|
| 62 |
+
python run.py --task train --model unsup --dataset sst --train-mode full-model
|
| 63 |
+
```
|
| 64 |
+
|
| 65 |
+
- `--model`: which encoder to start from β `base` (pretrained BERT), `sup` / `unsup` (SimCSE-finetuned).
|
| 66 |
+
- `--dataset`: `sst` (Stanford Sentiment Treebank, 5-class) or `cfimdb` (binary movie reviews).
|
| 67 |
+
- `--train-mode`: `last-linear` freezes BERT and trains only the classifier head; `full-model` finetunes everything.
|
| 68 |
+
|
| 69 |
+
Each run trains for 10 epochs, reports accuracy and macro-F1 on the dev set, and keeps the best model.
|
| 70 |
+
|
| 71 |
+
## Results
|
| 72 |
+
|
| 73 |
+
After contrastive finetuning, both SimCSE encoders (supervised and unsupervised) β along with the pretrained baseline β were trained on SST and CFIMDB in both modes.
|
| 74 |
+
|
| 75 |
+
| Model | SST (last-linear) | SST (full-model) | CFIMDB (last-linear) | CFIMDB (full-model) |
|
| 76 |
+
|---|---|---|---|---|
|
| 77 |
+
| Pretrained minBERT | 0.393 | **0.524** | 0.804 | 0.963 |
|
| 78 |
+
| minBERT + SimCSE (unsup) | 0.423 | 0.516 | 0.833 | 0.963 |
|
| 79 |
+
| minBERT + SimCSE (sup) | 0.464 | 0.523 | **0.931** | **0.971** |
|
| 80 |
+
|
| 81 |
+
The dev-set accuracy shows:
|
| 82 |
+
|
| 83 |
+
- **`last-linear`**: with the encoder frozen (no need to finetune all 12 BERT layers), supervised SimCSE jumps from 0.804 β **0.931** on CFIMDB and 0.393 β **0.464** on SST compared to the baseline. Unsupervised SimCSE also improves on both datasets (0.833 and 0.423), landing between the two.
|
| 84 |
+
- **`full-model` of supervised SimCSE is the strongest overall**: it tops CFIMDB at **0.971**, while the three encoders are roughly on par on SST (~0.52) β full finetuning largely washes out the differences in starting embeddings.
|