https://github.com/amazon-science/characterizing-measuring-linguistic-drift

https://github.com/amazon-science/characterizing-measuring-linguistic-drift

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.6%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: amazon-science
  • License: apache-2.0
  • Language: Python
  • Default Branch: main
  • Size: 220 KB
Statistics
  • Stars: 2
  • Watchers: 2
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created about 3 years ago · Last pushed about 3 years ago
Metadata Files
Readme Contributing License Code of conduct

README.md

Characterizing and Measuring Linguistic Dataset Drift

Experiments assessing different drift metrics in their ability to predict NLP model performance on novel data. Drift metrics can be computed for any text datasets, and the full experiments (e.g. model fine-tuning) can be run for any sequence classification task. Steps 2a, 2b, and 2c below can be run largely independently from one another, unless otherwise specified (e.g. if only the model-agnostic metrics are desired, then 2b can be omitted). The specific drift outputs from step 2 do not need to be understood in detail, because they will be compiled automatically in step 3a. More detailed usage for each script can be found in the Python script file headers.


0. Setup.

Tested on Python 3.7.12, Pytorch 1.9.1, and Transformers 4.20.1. To create the conda environment: bash conda create --name drift_experiments python=3.7 conda activate drift_experiments conda config --remove channels conda-forge conda install pytorch=1.9.1 -c pytorch conda config --add channels conda-forge conda install --file requirements.txt Note that removing conda-forge is initially necessary to ensure that pytorch installs from the pytorch channel, for GPU support. Then, download the spaCy model: bash python3 -m spacy download en_core_web_sm


1. Collect or upload datasets.

We include scripts to pull the Amazon Reviews datasets by product category, the Amazon Reviews datasets by review year, and the MNLI dataset by source domain. These scripts can be slow due to the size of the Amazon Reviews dataset.

These datasets are placed in the "datasets" directory, and our code will automatically use the correct settings for these datasets if the task flag is set to "sentimentamazoncategories", "sentimentamazonyears", or "mnli" respectively, and the dataset_dir flag points to the "datasets" directory.

To use your own datasets, you can set the variables in nlp_drift/custom_settings.py to set your training and evaluation domains, the custom task name, and your dataset settings. By default, these custom settings use the "mnlitiny" task in the `sampledatasets` directory.

To pull Amazon Reviews data by product category: bash python3 get_amazon_reviews_datasets.py --output_dir="datasets" \ --max_per_category=100000 --max_per_category_year=5000 To compile Amazon Reviews data by year as well: bash python3 compile_amazon_reviews_years.py --dataset_dir="datasets" \ --n_eval=5000 --n_test=5000 To pull the MNLI data by source domain: bash python3 get_mnli_datasets.py --output_dir="datasets" \ --max_per_domain=-1


2a. Run frequency divergence metrics and spaCy metrics.

Compute token frequency divergence metrics at both the example and dataset level. Dataset-level drift values are outputted to experiment_output_dir/task/frequency_metrics.tsv. Example-level drift values are outputted into each train domain output directory experiment_output_dir/task/train_domain/eval_frequency_annotations.npy, with shape (nevalexamples, 3), concatenating all evaluation domains. Columns correspond to JS-distance, cross-entropy, and raw example sequence length. These metrics use the specified Hugging Face model tokenizer. bash python3 compute_drift_metrics.py \ --tokenizer_name="roberta-base" --metrics="frequency" \ --experiment_output_dir="experiment_output" --task="mnli_tiny" \ --dataset_dir="sample_datasets" \ --max_seq_length=512

Then, compute spaCy drift metrics (e.g. POS n-gram divergences, content word frequency divergences, etc) at the dataset level. Dataset-level drift values are outputted to experiment_output_dir/task/spacy_metrics.tsv. bash python3 compute_drift_metrics.py \ --metrics="spacy" \ --experiment_output_dir="experiment_output" --task="mnli_tiny" \ --dataset_dir="sample_datasets" --max_seq_length=512 For debugging, the content word and POS n-gram frequencies can be viewed per evaluation domain, outputted to experiment_output_dir/task/eval_domain_data by running the script below. bash python3 compile_frequencies_ngrams.py \ --task="mnli_tiny" \ --experiment_output_dir="experiment_output"

We can also compute spaCy metrics at the example level (i.e. example-level vocabulary and structural drift). Example-level drift values are outputted into each train domain output directory experiment_output_dir/task/train_domain/eval_structural_vocab_xent_annotations.npy, with shape (nevalexamples, 2). Columns correspond to structural and vocabulary cross-entropy. bash python3 compute_structural_vocab_drift.py \ --experiment_output_dir="experiment_output" --task="mnli_tiny" \ --dataset_dir="sample_datasets" --max_seq_length=512


2b. Fine-tune models.

Fine-tune a language model on each training domain for a sequence classification task. Outputs each model to experiment_output_dir/task/train_domain. bash python3 finetune.py \ --model_name_or_path="roberta-base" --max_seq_length=512 \ --output_dir="placeholder" --save_strategy="no" \ --do_train --do_eval --evaluation_strategy="steps" --eval_steps=1000 \ --per_device_train_batch_size=8 --gradient_accumulation_steps=4 \ --lr_scheduler_type="linear" --learning_rate=0.00002 --warmup_ratio=0.10 \ --num_train_epochs=4 --per_device_eval_batch_size=8 \ --task="mnli_tiny" --dataset_dir="sample_datasets" \ --experiment_output_dir="experiment_output" --seed=42

Evaluate the fine-tuned models on all evaluation domains. Outputs the tsv of cross-domain accuracies to experiment_output_dir/task/all_domain_eval_results.tsv. Outputs the raw predictions for each model to experiment_output_dir/task/train_domain/domain_eval_predictions.npy, with shape: (nevalexamples, nclasses+1). The first columns correspond to class logits, and the last column is the true label. ```bash python3 evaluate.py \ --maxseqlength=512 \ --perdeviceevalbatchsize=16 --outputdir="placeholder" \ --task="mnlitiny" --datasetdir="sampledatasets" \ --experimentoutputdir="experimentoutput" ```


2c. Compute embedding cosine metrics.

Compute pre-trained embedding cosine similarities. Outputs the tsv of mean cosine similarities between training/eval domains to experiment_output_dir/task/all_pretrained_cosine_metrics.tsv. Includes dataset-level lexical semantic similarities as well (eval token frequency-weighted lexical semantic similarity). Outputs the example-level cosine similarities into each train domain output directory experiment_output_dir/task/train_domain/eval_pretrained_cosine_annotations.npy, with shape: (nevalexamples). The example-level lexical semantic similarities are outputted into experiment_output_dir/task/train_domain/eval_pretrained_semantic_annotations.npy, with shape: (nevalexamples, 2). Columns correspond to lexical semantic similarities when unfiltered or filtered to content tokens. bash python3 compute_cosine_metrics.py \ --max_seq_length 512 \ --per_device_eval_batch_size 16 --output_dir="placeholder" \ --task="mnli_tiny" --dataset_dir="sample_datasets" \ --experiment_output_dir="experiment_output" \ --pos_dict_path="English_pos_dict.tsv" \ --model_name_or_path="roberta-base"

Then, if step 2b has been run, we can compute fine-tuned embedding cosine similarities. Similar to above, outputs the tsv of mean cosine similarities between training/eval domains to experiment_output_dir/task/all_finetuned_cosine_metrics.tsv. Outputs the example-level cosine similarities into each train domain output directory experiment_output_dir/task/train_domain/eval_finetuned_cosine_annotations.npy, with shape: (nevalexamples). bash python3 compute_cosine_metrics.py \ --max_seq_length 512 \ --per_device_eval_batch_size 16 --output_dir="placeholder" \ --task="mnli_tiny" --dataset_dir="sample_datasets" \ --experiment_output_dir="experiment_output"


3a. Compile drift metrics (optional).

If some subset of the commands in 2a, 2b, and 2c has been run, then the example-level drift metrics can be compiled into one file experiment_output_dir/task/eval_example_drift_metrics.tsv. This step is optional, but it can be useful to see the drift metrics all in one place. Each row corresponds to a single evaluation example relative to one training domain. Depending on the steps run above, columns can include the training and evaluation domains, the various drift metrics, whether the trained model correctly predicted the example, the task-specific text fields, and the ground truth label. Note that the dataset-level drift metrics are already organized in the output tsv files from the steps above. Then, all drift metrics can be found in experiment_output_dir/task. bash python3 compile_drift_metrics.py \ --task="mnli_tiny" --dataset_dir="sample_datasets" \ --experiment_output_dir="experiment_output"


3b. Run logistic regressions.

Run a logistic regression for each model, and each drift metric(s), predicting whether the model will get each example correct. Outputs predicted accuracies to experiment_output_dir/task/eval_performance_predictions.tsv. Outputs individual example probability predictions to a subdirectory in each train domain directory experiment_output_dir/task/train_domain/individual_performance_predictions. This contains one npy file for each regression, containing the predicted probabilities of getting each evaluation example correct (using the model trained on traindomain) based on the drift metric(s) for that regression. ```bash python3 runregressions.py \ --experimentoutputdir="experimentoutput" \ --task="mnlitiny" --datasetdir="sampledatasets" \ --outfilename="evalperformance_predictions.tsv" ```


3c. Evaluate performance predictions.

Compute the RMSE for each drift metric(s). Outputs to: experiment_output_dir/task/rmses.tsv. bash python3 get_rmse_scores.py \ --experiment_output_dir="experiment_output" \ --task="mnli_tiny"

Compute the mean ROC AUC for each drift metric(s). Outputs to: experiment_output_dir/task/roc_aucs.tsv. bash python3 get_roc_scores.py \ --experiment_output_dir="experiment_output" \ --dataset_dir="sample_datasets" \ --task="mnli_tiny"


Citation.

If you use these sources, please consider citing the following work: @inproceedings{chang-etal-2023-characterizing, title = "Characterizing and Measuring Linguistic Dataset Drift", author = "Chang, Tyler and Halder, Kishaloy and Anna John, Neha and Vyas, Yogarshi and Benajiba, Yassine and Ballesteros, Miguel and Roth, Dan", booktitle = "Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics", month = jul, year = "2023", publisher = "Association for Computational Linguistics" }

Owner

  • Name: Amazon Science
  • Login: amazon-science
  • Kind: organization

GitHub Events

Total
Last Year

Issues and Pull Requests

Last synced: 11 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels