Knowledgeable Prompt-tuning: Incorporating Knowledge into Prompt Verbalizer for Text Classification

Overview

KPT source code

This is KPT's source code (Paper)

Environments

python 3.7.10
torch==1.9.0
transformers==4.10.0
tqdm==4.48.1
numpy==1.18.5
sklearn==0.24.2

Zero-shot text classification

bash scripts/run_zs_KPT_mean.sh

Few-shot text classification

bash scripts/run_fs_KPT.sh

Comments

Other scripts are for different experiments (including ablation study). Please refer to the paper for details. The impletation includes some unnecessary and dirty codes in prior experiments. I will clean it in the future or release a new version together with our future work. Due to some change in the version of package, the replicated results may differ slightly with results in the paper, but general trend is preversed.

Link to originial experiment record

If you are interested, you can comment on the doc. (But sincerely speaking, I can't remember all the meaning of the numbers.) https://docs.google.com/spreadsheets/d/124SaGGElKGv9Spdn05tDj5rKn9-gmiv8B_MEQqsMfXU/edit?usp=sharing

Citation

@article{hu2021knowledgeable,
  title={Knowledgeable prompt-tuning: Incorporating knowledge into prompt verbalizer for text classification},
  author={Hu, Shengding and Ding, Ning and Wang, Huadong and Liu, Zhiyuan and Li, Juanzi and Sun, Maosong},
  journal={arXiv preprint arXiv:2108.02035},
  year={2021}
}
Comments
  • 实验相关问题

    实验相关问题

    (1)在根据频次优化时(Frequency Refinement),体现在代码中,是否为: myverbalizer = KnowledgeableVerbalizer(tokenizer, classes=class_labels, candidate_frac=cutoff, pred_temp=args.pred_temp, max_token_split=args.max_token_split).from_file(f"{args.openprompt_path}/scripts/{scriptsbase}/knowledgeable_verbalizer.{scriptformat}"),其中的cutoff是否为实验中提到的阈值呢?不知道理解的对不对。 (2)在few-shot实验中,为何没有将support set的label值remove掉呢? 我看论文中提到的是“ . Our proposed Contextualized Calibration utilizes a limited amount of unlabeled support data to yield significantly better results”, 但是,在实验中是,却将其注释掉了,这里有些不解。 image **

    opened by Hou-jing 2
  • 运行fewshot.py出现错误

    运行fewshot.py出现错误

    OSError: Can't load the configuration of '../plm_cache/roberta-large'. If you were trying to load it from 'https://huggingface.co/models', make sure you don't have a local directory with the same name. Otherwise, make sure '../plm_cache/roberta-large' is the correct path to a directory containing a config.json file

    opened by qinglongheu 1
  • Is it correct? Manual prompt is better than KPT ?

    Is it correct? Manual prompt is better than KPT ?

    for the up-to-date code of KPT, I get the bellow result on dataset Agnews for Zeroshot:

    KPT: Acc: 0.5125 Manual: Acc: 0.6589473684210526

    Is it correct ?

    opened by znsoftm 1
  • Question

    Question

    我下载解压roberta-large,但是依然提示: OSError: ../plm_cache/roberta-large is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models' If this is a private repository, make sure to pass a token having permission to this repo with use_auth_token or log in with huggingface-cli login and pass use_auth_token=True.

    opened by xiao432 1
  • How to obatain knowledgeable_verbalizer.json?

    How to obatain knowledgeable_verbalizer.json?

    Thank you for sharing your work. In my understanding, the knowledgeable_verbalizer.json is coming from the "OpenPrompt/scripts/TextClassification/yahoo_answers_topics/knowledgeable_verbalizer.json" (yahoo for instance).

    I think the high-quailty knowledgeable_verbalizer is very important. Could you generate knowledgeable_verbalizer.json automanticlly? When I searched "Society & Culture" in https://relatedwords.org/, I found that the returned words are slightly different with the verbalizer in knowledgeable_verbalizer.json of yahoo dataset. So I just want to confirm that knowledgeable_verbalizer.json is from the original returned results from https://relatedwords.org/? How to obatain the relevance scores from https://relatedwords.org/?

    Maybe it is an unimportant part in your method. I will be appreciate if you can answer my simple question.

    opened by unlabeledData 0
  • A very simple question, how to do ablaiton experiment

    A very simple question, how to do ablaiton experiment

    Thanks for your excellent work! As I'm a totally beginer about prompt learning, I want to know how to do the ablation experiment "-RR""-LR" and "PT+CC" QQ截图20221014180044

    Thanks to anyone who answer my stupid question

    opened by passermyh 0
  • Received a fatal error!!

    Received a fatal error!!

    test fewshot.py, get the below fatal error.

    pytorch 1.10.0

    for step, inputs in enumerate(train_dataloader):
                if use_cuda:
                    inputs = inputs.cuda()
                logits = prompt_model(inputs)
                labels = inputs['label']
                loss = loss_func(logits, labels)
                loss.backward()  # it causes the fatal error.
                torch.nn.utils.clip_grad_norm_(prompt_model.parameters(), 1.0)
            
                tot_loss += loss.item()
                
                optimizer1.step()
    
    

    one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [20, 1076]], which is output 0 of SoftmaxBackward0, is at version 1; expected version 0 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient. The variable in question was changed in there or anywhere later. Good luck!

    opened by znsoftm 7
  • Error of running zero-shot on dbpedia dataset

    Error of running zero-shot on dbpedia dataset

    I have already installed the latest openprompt package. And the script for KPT is

    PYTHONPATH=python3
    BASEPATH="./"
    DATASET=dbpedia #agnews dbpedia imdb amazon yahoo
    TEMPLATEID=0 # 1 2 3
    SEED=144 # 145 146 147 148
    SHOT=0 # 0 1 10 20
    VERBALIZER=kpt #
    CALIBRATION="--calibration" # ""
    FILTER=tfidf_filter # none
    MODEL_NAME_OR_PATH="roberta-large"
    RESULTPATH="results_zeroshot"
    OPENPROMPTPATH="../OpenPrompt"
    
    cd $BASEPATH
    
    CUDA_VISIBLE_DEVICES=0 $PYTHONPATH zeroshot.py \
    --model_name_or_path $MODEL_NAME_OR_PATH \
    --result_file $RESULTPATH \
    --openprompt_path $OPENPROMPTPATH \
    --dataset $DATASET \
    --template_id $TEMPLATEID \
    --seed $SEED \
    --verbalizer $VERBALIZER $CALIBRATION \
    --filter $FILTER
    

    And I got the error:

    ##Num of label words for each label: [217, 256, 214, 211, 324, 294, 215, 766, 345, 408, 880, 181, 246, 666]
    Traceback (most recent call last):
      File "/mnt/data2/xuandong/prompt/KPT/zeroshot.py", line 113, in <module>
        support_dataloader = PromptDataLoader(dataset=dataset["support"], template=mytemplate, tokenizer=tokenizer,
      File "/mnt/data2/xuandong/prompt/KPT/openprompt/pipeline_base.py", line 100, in __init__
        self.wrap()
      File "/mnt/data2/xuandong/prompt/KPT/openprompt/pipeline_base.py", line 126, in wrap
        wrapped_example = self.template.wrap_one_example(example)
      File "/mnt/data2/xuandong/prompt/KPT/openprompt/prompt_base.py", line 207, in wrap_one_example
        text = self.incorporate_text_example(example)
      File "/mnt/data2/xuandong/prompt/KPT/openprompt/prompt_base.py", line 107, in incorporate_text_example
        text[i] = d["add_prefix_space"] + d.get("post_processing", lambda x:x)(getattr(example, d['placeholder']))
    TypeError: 'str' object is not callable
    

    Could you please check about it?

    opened by XuandongZhao 5
Owner
DingDing
A student interested in NLP in Tsinghua University, China.
DingDing
Pytorch implementation for the EMNLP 2020 (Findings) paper: Connecting the Dots: A Knowledgeable Path Generator for Commonsense Question Answering

Path-Generator-QA This is a Pytorch implementation for the EMNLP 2020 (Findings) paper: Connecting the Dots: A Knowledgeable Path Generator for Common

Peifeng Wang 33 Dec 5, 2022
[ACL 2022] LinkBERT: A Knowledgeable Language Model 😎 Pretrained with Document Links

LinkBERT: A Knowledgeable Language Model Pretrained with Document Links This repo provides the model, code & data of our paper: LinkBERT: Pretraining

Michihiro Yasunaga 264 Jan 1, 2023
GEP (GDB Enhanced Prompt) - a GDB plug-in for GDB command prompt with fzf history search, fish-like autosuggestions, auto-completion with floating window, partial string matching in history, and more!

GEP (GDB Enhanced Prompt) GEP (GDB Enhanced Prompt) is a GDB plug-in which make your GDB command prompt more convenient and flexibility. Why I need th

Alan Li 23 Dec 21, 2022
The Power of Scale for Parameter-Efficient Prompt Tuning

The Power of Scale for Parameter-Efficient Prompt Tuning Implementation of soft embeddings from https://arxiv.org/abs/2104.08691v1 using Pytorch and H

Kip Parker 208 Dec 30, 2022
EMNLP 2021 Adapting Language Models for Zero-shot Learning by Meta-tuning on Dataset and Prompt Collections

Adapting Language Models for Zero-shot Learning by Meta-tuning on Dataset and Prompt Collections Ruiqi Zhong, Kristy Lee*, Zheng Zhang*, Dan Klein EMN

Ruiqi Zhong 42 Nov 3, 2022
Implementation of "The Power of Scale for Parameter-Efficient Prompt Tuning"

Prompt-Tuning Implementation of "The Power of Scale for Parameter-Efficient Prompt Tuning" Currently, we support the following huggigface models: Bart

Andrew Zeng 36 Dec 19, 2022
Codes for "Template-free Prompt Tuning for Few-shot NER".

EntLM The source codes for EntLM. Dependencies: Cuda 10.1, python 3.6.5 To install the required packages by following commands: $ pip3 install -r requ

null 77 Dec 27, 2022
Incorporating Transformer and LSTM to Kalman Filter with EM algorithm

Deep learning based state estimation: incorporating Transformer and LSTM to Kalman Filter with EM algorithm Overview Kalman Filter requires the true p

zshicode 57 Dec 27, 2022
Black-Box-Tuning - Black-Box Tuning for Language-Model-as-a-Service

Black-Box-Tuning Source code for paper "Black-Box Tuning for Language-Model-as-a

Tianxiang Sun 149 Jan 4, 2023
Saeed Lotfi 28 Dec 12, 2022
code for the ICLR'22 paper: On Robust Prefix-Tuning for Text Classification

On Robust Prefix-Tuning for Text Classification Prefix-tuning has drawed much attention as it is a parameter-efficient and modular alternative to adap

Zonghan Yang 12 Nov 30, 2022
a reccurrent neural netowrk that when trained on a peice of text and fed a starting prompt will write its on 250 character text using LSTM layers

RNN-Playwrite a reccurrent neural netowrk that when trained on a peice of text and fed a starting prompt will write its on 250 character text using LS

Arno Barton 1 Oct 29, 2021
Ensemble Knowledge Guided Sub-network Search and Fine-tuning for Filter Pruning

Ensemble Knowledge Guided Sub-network Search and Fine-tuning for Filter Pruning This repository is official Tensorflow implementation of paper: Ensemb

Seunghyun Lee 12 Oct 18, 2022
[IJCAI-2021] A benchmark of data-free knowledge distillation from paper "Contrastive Model Inversion for Data-Free Knowledge Distillation"

DataFree A benchmark of data-free knowledge distillation from paper "Contrastive Model Inversion for Data-Free Knowledge Distillation" Authors: Gongfa

ZJU-VIPA 47 Jan 9, 2023
TF2 implementation of knowledge distillation using the "function matching" hypothesis from the paper Knowledge distillation: A good teacher is patient and consistent by Beyer et al.

FunMatch-Distillation TF2 implementation of knowledge distillation using the "function matching" hypothesis from the paper Knowledge distillation: A g

Sayak Paul 67 Dec 20, 2022
Source Code for our paper: Understand me, if you refer to Aspect Knowledge: Knowledge-aware Gated Recurrent Memory Network

KaGRMN-DSG_ABSA This repository contains the PyTorch source Code for our paper: Understand me, if you refer to Aspect Knowledge: Knowledge-aware Gated

XingBowen 4 May 20, 2022
Code for EMNLP 2021 main conference paper "Text AutoAugment: Learning Compositional Augmentation Policy for Text Classification"

Text-AutoAugment (TAA) This repository contains the code for our paper Text AutoAugment: Learning Compositional Augmentation Policy for Text Classific

LancoPKU 105 Jan 3, 2023
Code and dataset for ACL2018 paper "Exploiting Document Knowledge for Aspect-level Sentiment Classification"

Aspect-level Sentiment Classification Code and dataset for ACL2018 [paper] ‘‘Exploiting Document Knowledge for Aspect-level Sentiment Classification’’

Ruidan He 146 Nov 29, 2022
《K-Adapter: Infusing Knowledge into Pre-Trained Models with Adapters》(2020)

K-Adapter: Infusing Knowledge into Pre-Trained Models with Adapters This repository is the implementation of the paper "K-Adapter: Infusing Knowledge

Microsoft 118 Dec 13, 2022