$git clone https://github.com/csinva/tree-promptReference: ``r @misc{ch2022augmenting, title={Tree Prompting: Efficient Task Adaptation without Fine-Tuning}, year={2023}, archivePrefix={arXiv}, primaryClass={cs.AI} }
| 1 | <h1 align="center"> Tree Prompting </h1> |
| 2 | <p align="center"> Tree Prompting: Efficient Task Adaptation without Fine-Tuning, code for the <a href="">Tree-prompt paper</a>. |
| 3 | </p> |
| 4 | |
| 5 | <p align="center"> |
| 6 | <img src="https://img.shields.io/badge/license-mit-blue.svg"> |
| 7 | <img src="https://img.shields.io/badge/python-3.6+-blue"> |
| 8 | <img src="https://img.shields.io/pypi/v/imodelsx?color=green"> |
| 9 | </p> |
| 10 | |
| 11 | <p align="center"> Tree Prompting uses training examples to learn a tree of prompts to make a classificationg, yielding higher accuracy and better efficiency that baseline ensembles. |
| 12 | </p> |
| 13 | |
| 14 | ### Quickstart |
| 15 | |
| 16 | For a simple scikit-learn interface to use Tree-Prompt, use the <a href="https://github.com/csinva/imodelsX">imodelsX package</a>. Installation: `pip install imodelsx` |
| 17 | |
| 18 | ```python |
| 19 | from imodelsx import TreePromptClassifier |
| 20 | import datasets |
| 21 | import numpy as np |
| 22 | from sklearn.tree import plot_tree |
| 23 | import matplotlib.pyplot as plt |
| 24 | |
| 25 | # set up data |
| 26 | rng = np.random.default_rng(seed=42) |
| 27 | dset_train = datasets.load_dataset('rotten_tomatoes')['train'] |
| 28 | dset_train = dset_train.select(rng.choice( |
| 29 | len(dset_train), size=100, replace=False)) |
| 30 | dset_val = datasets.load_dataset('rotten_tomatoes')['validation'] |
| 31 | dset_val = dset_val.select(rng.choice( |
| 32 | len(dset_val), size=100, replace=False)) |
| 33 | |
| 34 | # set up arguments |
| 35 | prompts = [ |
| 36 | "This movie is", |
| 37 | " Positive or Negative? The movie was", |
| 38 | " The sentiment of the movie was", |
| 39 | " The plot of the movie was really", |
| 40 | " The acting in the movie was", |
| 41 | ] |
| 42 | verbalizer = {0: " Negative.", 1: " Positive."} |
| 43 | checkpoint = "gpt2" |
| 44 | |
| 45 | # fit model |
| 46 | m = TreePromptClassifier( |
| 47 | checkpoint=checkpoint, |
| 48 | prompts=prompts, |
| 49 | verbalizer=verbalizer, |
| 50 | cache_prompt_features_dir=None, # 'cache_prompt_features_dir/gp2', |
| 51 | ) |
| 52 | m.fit(dset_train["text"], dset_train["label"]) |
| 53 | |
| 54 | |
| 55 | # compute accuracy |
| 56 | preds = m.predict(dset_val['text']) |
| 57 | print('\nTree-Prompt acc (val) ->', |
| 58 | np.mean(preds == dset_val['label'])) # -> 0.7 |
| 59 | |
| 60 | # compare to accuracy for individual prompts |
| 61 | for i, prompt in enumerate(prompts): |
| 62 | print(i, prompt, '->', m.prompt_accs_[i]) # -> 0.65, 0.5, 0.5, 0.56, 0.51 |
| 63 | |
| 64 | # visualize decision tree |
| 65 | plot_tree( |
| 66 | m.clf_, |
| 67 | fontsize=10, |
| 68 | feature_names=m.feature_names_, |
| 69 | class_names=list(verbalizer.values()), |
| 70 | filled=True, |
| 71 | ) |
| 72 | plt.show() |
| 73 | ``` |
| 74 | |
| 75 | Reference: |
| 76 | ```r |
| 77 | @misc{ch2022augmenting, |
| 78 | title={Tree Prompting: Efficient Task Adaptation without Fine-Tuning}, |
| 79 | year={2023}, |
| 80 | archivePrefix={arXiv}, |
| 81 | primaryClass={cs.AI} |
| 82 | } |
| 83 | ``` |
| 84 | |
| 85 | |
| 86 | # Reproducing experiments |
| 87 | |
| 88 | ### Organization |
| 89 | - `tprompt`: contains main code for modeling (e.g. model architecture) |
| 90 | - `experiments`: code for runnning experiments (e.g. loading data, training models, evaluating models) |
| 91 | - `scripts`: scripts for running experiments (e.g. python scripts that launch jobs in `experiments` folder with different hyperparams) |
| 92 | - `notebooks`: jupyter notebooks for analyzing results and making figures |
| 93 | - `tests`: unit tests |
| 94 | |
| 95 | ### Setup |
| 96 | - clone and run `pip install -e .`, resulting in a package named `tprompt` that can be imported |
| 97 | - see `setup.py` for dependencies, not all are required |
| 98 | - example run: run `python scripts/01_train_basic_models.py` (which calls `experiments/01_train_model.py` then view the results in `notebooks/01_model_results.ipynb` |