Skip to main content

Analyzing priming effects in a few shot setting environment

Project description

Exploring LLM Priming Strategies for Few-Shot Stance Classification

How does priming affect prompt-based learing? This project aims at analyzing this effect in stance classification. We train a stance classifier on the ibm stance classification dataset by fine-tuning an Alpaca model with a prompt and analyzing how does the selection of the few shots used in the prompt affect the performance of the model.

  1. To evaluate the prompt-fine-tuning, run the following command
  • Hyperparamter optimization
python scripts/run_prompt_fine_tuning.py --validate --optimize 
  • T evaluate the prompt fine-utning approach using best hyperparameters in the config file.
python scripts/run_prompt_fine_tuning.py --validate  
  1. To evaluate the prompting approaches (prompt) setup run
python scripts/run_prompting.py --validate --optimize 
  1. To optmize DeBERTa using the hyperparameters in the config file, run the following
python scripts/optimize_baseline.py 
  1. run the majority baseline
 python baseline.py --vast --majority --offline
  1. Run prompting or prompt-fine tuning approaches with anaylze k as folows
python scripts/run_prompt_fine_tuning.py --analyze-k  

The ks or topic-counts are stored in conf.yaml and the analysis will be run for multiple seeds and the performance will be averaged and saved in the corresponding entry in /bigwork/nhwpajjy. To perform an analysis for one k instnance you can give it as a parameter using --k to produce the results for one seed you can specify the seed using --seed

  1. Store the path of the produced csv file in conf.yaml under analyze-k

The results of the experiments will be logged to your home directory. The parameters can be saved in config.yaml

Priming Sampling strategies

To run an experiment with a priming sampling strategy use the parameter --similar-examples. This will retrieve priming examples tailored toward the test instance. The priming examples are selected using Sentence-Transformers --sentence-transformer. Example,

python scripts/run_prompting.py --validate --topic-similar --sentence-transformer

To run an experiment with a diversification sampling strategy use the parameter --diverse-examples. This will takes precomputed cluster centers of the training set as few shots. The number of clusters is then the few shot count provided in the configuration.

Topic Priming

Topic-priming examples are sampled using a topic similarity, which relies on sentence transformer. the similarities can be used to apply the right sampling strategy.

  1. To compute the similarities between all the validation and training arguments run the following
python scripts/run_develop_similarity_measure.py --compute-similarity --ibmsc

You have to specify the model used to compute the similarity to be --sentence-transformer

To load similarities from the code you can use the

similarities = load_similarities("ibmsc", "validation", similarity)

which returns a dictionary of dictionary where the indices of the first dictionary are the test indices and the indeices of the nested dictionary for each test index are the indices of the training set with the values being the similarity scores.

To find similar or diverse arguments for an argument in the validation or test set, you can use

examples = sample_diverse_examples(experiment, experiment_type, few_shot_size)

similar can be used for sample_similar

examples = sample_similar_examples(test_istance_index, similarities, df_training, few_shot_size)

Similar examples for alll datasets are cached for efficiency and can be created by running the following commands

python argument_sampling/sampling_strategies.py" --ibmsc

to generate similar examples while using percentiles similarity thresholds you can use

python argument_sampling/sampling_strategies.py" "--ibmsc" --percentiles 10

Stance Priming

Stance-priming approaches use stance similarity to retrieve examples with similar stance to an input instance. To train the contrastive similarity measure, run

python scripts/traing_contrastive_learning.py --vast --lr --epochs --batch-size --output-path-model  

The best parameters are also saved in the config file, which will be used as default for each dataset.

To optimize the contrastive similarity measure on the validation set using the hyperparamters in the script, run.

python scripts/traing_contrastive_learning.py --vast --optimize  --results-path "$DATA_PATH/contrastive_learning/models/optimization-contrastive-learning-ibmsc.tsv"

To generate the similarity matrix using the contrastive similarity measure use

python scripts/generate_stance_priming_samples.py --vast --similarity-matrix 

To generate the stance priming samples using most similar , run the following

python scripts/generate_stance_priming_samples.py --vast --different-topics 

to generate similar examples while using percentiles similarity thresholds you can use

python scripts/generate_stance_priming_samples.py" "--ibmsc" --percentiles 10 --different-topics

Diversification strategy

To sample diverse examples, we use ward hierarchical clustering algorithm to cluster the trianing examples into $k$ cluster. The center of each cluster is then taken as an example. To find the diverse examples, we use the following command

python scripts/run_sample_diverse_examples --vast --validate --ibmsc 

This will precompute the cluster centers for k in [2, 4, 8, 16, 32, 64] To sample diverse examples, you can use the following command

examples = sample_diverse_examples(experiment, experiment_type, few_shot_size) where experiment is ibmsc or vast and experiment_type is validation or test.

Analysis

Mainly there are three types of analyses implemented on the prompting and instruction fine-tuning approaches. For this the results for all ks and the specific model and counts should be in the config file

Few shot size or training topic count effect on priming approaches

python script/run_visualize_over_k_performance.py --k --prompting --prompt-fine-tuning
  1. Run the signifance tests. For this you need to store the predictions of the models using --path-predictions for all seeds
python notebooks/signifiance_test.py
  1. Run the prime anaylis for stance priming you can run
 python "$CODE_PATH/scripts/run_prompting.py"  "--alpaca-7b" "--ibmsc" --offline --vllm --analyze-prime-similarity  \
 --similar-examples --path-similar-examples "$DATA_PATH/sampling-strategies/ibmsc-similar-stance-examples.tsv"
  1. Run the prime anaylis for topic priming you can run
 python "$CODE_PATH/scripts/run_prompting.py"  "--alpaca-7b" "--ibmsc" --offline --vllm --analyze-topic-similarity  \
 --similar-examples --path-similar-examples "$DATA_PATH/sampling-strategies/ibmsc-similar-stance-examples.tsv"

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

few_shot_priming-0.3.937.tar.gz (202.5 kB view details)

Uploaded Source

Built Distribution

few_shot_priming-0.3.937-py3-none-any.whl (207.2 kB view details)

Uploaded Python 3

File details

Details for the file few_shot_priming-0.3.937.tar.gz.

File metadata

  • Download URL: few_shot_priming-0.3.937.tar.gz
  • Upload date:
  • Size: 202.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for few_shot_priming-0.3.937.tar.gz
Algorithm Hash digest
SHA256 6aa69c93919b66e5d366a6a143423853d73daf3985f31dab66b795ae6c99d956
MD5 d8604f9b095cdab8a6c0154fa8e12a19
BLAKE2b-256 39b6aebe94f40e7bda828a948e108983fac29d1ffc1c6b10111bdc01f8c7dead

See more details on using hashes here.

File details

Details for the file few_shot_priming-0.3.937-py3-none-any.whl.

File metadata

File hashes

Hashes for few_shot_priming-0.3.937-py3-none-any.whl
Algorithm Hash digest
SHA256 1a2826e3989607675e15d3e34f1bf27cdee60dda4ebdd4c50e6a8147f09e7328
MD5 b0b2e6bebb9676d67267ba164e257ddf
BLAKE2b-256 5de3890d7287314f701b35276a9e75fef526d89329b850aa68f4f015b209729b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page