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")# pip install -U transformers accelerate # 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 files9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344459 unmodified linestags:- bert- minbert- trainsformer- sentiment- tokenizer- classification---# CS 224N Default Final Project - Multitask BERTThis is the default final project for the Stanford CS 224N class. Please refer to the project handout on the course website for detailed instructions and an overview of the codebase.This project comprises two parts. In the first part, you will implement some important components of the BERT model to better understand its architecture.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.In broad strokes, Part 1 of this project targets:* bert.py: Missing code blocks.* classifier.py: Missing code blocks.* optimizer.py: Missing code blocks.And Part 2 targets:* multitask_classifier.py: Missing code blocks.* datasets.py: Possibly useful functions/classes for extensions.* evaluation.py: Possibly useful functions/classes for extensions.## Setup instructionsFollow `setup.sh` to properly setup a conda environment and install dependencies.## AcknowledgementThe BERT implementation part of the project was adapted from the "minbert" assignment developed at Carnegie Mellon University's [CS11-711 Advanced NLP](http://phontron.com/class/anlp2021/index.html),created by Shuyan Zhou, Zhengbao Jiang, Ritam Dutt, Brendon Boldt, Aditya Veerubhotla, and Graham Neubig.Parts of the code are from the [`transformers`](https://github.com/huggingface/transformers) library ([Apache License 2.0](./LICENSE)).9 unmodified lines1011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283849 unmodified linestags:- bert- minbert- transformer- sentiment- tokenizer- classification---# minBERT β Finetuning BERT with Contrastive Learning for Sentiment AnalysisminBERT 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**.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.## Model architectureminBERT follows the original BERT-base design and loads pretrained `bert-base-uncased` weights:- **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.- **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.- **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.- **Pooler** β the final hidden state of the `[CLS]` token is passed through a linear layer with tanh activation to obtain the sentence representation.- **Classifier head** β dropout followed by a single linear layer over the pooled `[CLS]` embedding, producing the predicted sentiment label.The **AdamW optimizer** (with first/second moment estimates, bias correction, decoupled weight decay) is used for all training runs.## SimCSE contrastive finetuningminBERT's embeddings can be refined with the SimCSE framework. Two variants are implemented:- **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**.- **Supervised SimCSE** β positive/negative pairs come from **NLI** entailment/contradiction triplets `(anchor, positive, hard negative)`. Trained on ~265k samples.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`).## WorkflowsEverything runs through `run.py`:### 1. Contrastive finetuning (SimCSE)```bashpython run.py --task finetune --model unsup # unsupervised SimCSE on Amazon Polaritypython run.py --task finetune --model sup # supervised SimCSE on NLI```### 2. Sentiment classifier training & evaluation```bashpython run.py --task train --model base --dataset sst --train-mode last-linearpython run.py --task train --model sup --dataset cfimdb --train-mode full-modelpython run.py --task train --model unsup --dataset sst --train-mode full-model```- `--model`: which encoder to start from β `base` (pretrained BERT), `sup` / `unsup` (SimCSE-finetuned).- `--dataset`: `sst` (Stanford Sentiment Treebank, 5-class) or `cfimdb` (binary movie reviews).- `--train-mode`: `last-linear` freezes BERT and trains only the classifier head; `full-model` finetunes everything.Each run trains for 10 epochs, reports accuracy and macro-F1 on the dev set, and keeps the best model.## ResultsAfter contrastive finetuning, both SimCSE encoders (supervised and unsupervised) β along with the pretrained baseline β were trained on SST and CFIMDB in both modes.| Model | SST (last-linear) | SST (full-model) | CFIMDB (last-linear) | CFIMDB (full-model) ||---|---|---|---|---|| Pretrained minBERT | 0.393 | **0.524** | 0.804 | 0.963 || minBERT + SimCSE (unsup) | 0.423 | 0.516 | 0.833 | 0.963 || minBERT + SimCSE (sup) | 0.464 | 0.523 | **0.931** | **0.971** |The dev-set accuracy shows:- **`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.- **`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.
README.md+58-19