An open API service indexing awesome lists of open source software.

https://github.com/eliask93/instruction-fine-tuned-gemma-2-for-stance-detection

Example application for applying QLoRA-based Parameter-Efficient Fine-Tuning (PEFT) to a Stance Detection task using Gemma-2-9B-Instruct
https://github.com/eliask93/instruction-fine-tuned-gemma-2-for-stance-detection

argument-mining gemma2 lora nlp peft qlora quantization stance-detection

Last synced: 8 months ago
JSON representation

Example application for applying QLoRA-based Parameter-Efficient Fine-Tuning (PEFT) to a Stance Detection task using Gemma-2-9B-Instruct

Awesome Lists containing this project

README

          

## Parameter-Efficient Fine-Tuning of Gemma-2-Instruct-9B for Stance Detection

Example application for fine-tuning the instruction-tuned 9B variant of Google's [Gemma 2](https://blog.google/technology/developers/google-gemma-2/) model ([google/gemma-2-9b-it](https://huggingface.co/google/gemma-2-9b-it)) on a new task (Stance Detection).

To make the fine-tuning feasible on a consumer GPU, a parameter-efficient fine-tuning (PEFT) approach based on [QLoRA](https://arxiv.org/abs/2305.14314) (_Quantized Low-Rank Adaptation_) is applied. While the original model has 9.295.724.032 parameters, this approach works with only 54.018.048 trainable parameters (~0.581%), leaving the original model weights entirely frozen during the fine-tuning process. This way, the model was fine-tuned for two epochs on the task using a single RTX 4080 GPU.


### Corpus

Each model was fine-tuned on a 5,000 sentences Stance Detection corpus that I manually annotated during my Master's Thesis.
Stance Detection aims to classify the stance a sentence takes towards a claim (topic) as either _Pro_, _Contra_ or _Neutral_.
The sentences originate from Reddit's _r/ChangeMyView_ subreddit in the time span between January 2013 and October 2018, as provided in the [ConvoKit subreddit corpus](https://zissou.infosci.cornell.edu/convokit/datasets/subreddit-corpus/corpus-zipped/).
They cover five topics: _abortion_, _climate change_, _gun control_, _minimum wage_ and _veganism_.
The table below shows some examples.

topic
sentence
stance label

There should be more gun control.
It's the only country with a "2nd Amendment", yet 132 countries have a lower murder rate.
Pro

Humanity needs to combat climate change.
The overhwelming evidence could be lies and you would never know because you're content to live your life as a giant appeal to authority.
Contra

Vegans are right.
It's all about finding a system that works for you.
Neutral


For the instruction-based fine-tuning and inference, the sentence pairs are wrapped in the following prompt:

prompt
expected output

The first text is a hypothesis/claim, the second text is a sentence. Determine whether the sentence is a pro argument ("pro"), a contra argument ("con") or doesnt take position at all/is neutral ("neu") towards the hypothesis.

For your answer, just write exactly one of pro, con or neu, not a full text.


Sentence to predict:

hypothesis: \`\`\`There should be more gun control.\`\`\`

sentence: \`\`\`It's the only country with a "2nd Amendment", yet 132 countries have a lower murder rate.\`\`\`

stance:
pro


### Results

| Model | Accuracy | Micro-F1 | Macro-F1 |
|:------------------------------------------------:|:--------:|:--------:|:--------:|
| base model (google/gemma-2-9b-it) | 0.72 | 0.71 | 0.71 |
| fine-tuned model (gemma2-9b-it-stance-finetuned) | 0.90 | 0.89 | 0.90 |


### Requirements

##### - Python >= 3.10

##### - Conda
- `pytorch==2.4.0`
- `cudatoolkit=12.1`

##### - pip
- `transformers`
- `datasets`
- `trl`
- `sentencepiece`
- `protobuf`
- `peft`
- `bitsandbytes`
- `openpyxl`
- `scikit-learn`


### Notes

The dataset files in this repository are cut off after the first 50 rows.
The trained model files `adapter_model.safetensors`, `optimizer.pt` and `tokenizer.json` are omitted in this repository.