Giter Site home page Giter Site logo

kid's Introduction

Knowledge Infused Decoding

Knowledge Infused Decoding (KID) is a decoding algorithm that infuses knowledge (from Wikipedia) into each step decoding of text generation.

KID has been described in the following paper:

@inproceedings{liu2022knowledge,
    author = {Liu, Ruibo and Zheng, Guoqing and Gupta, Shashank and Gaonkar, Radhika and Gao, Chongyang and Vosoughi, Soroush and Shokouhi, Milad and Awadallah, Ahmed H.},
    title = {Knowledge Infused Decoding},
    booktitle = {International Conference on Learning Representations (ICLR)},
    year = {2022},
    month = {April},
    publisher = {ICLR},
    url = {https://www.microsoft.com/en-us/research/publication/knowledge-infused-decoding/},
}

Getting Started

Step 0. Installation

You can install the latest version of KID through:

$ pip install git+https://github.com/microsoft/KID.git@main --upgrade

Now you can already run the demo code in scripts/run_demo.py, which examplifies a QA sample in ELI5 (see Step 3 for outputs).

Or try the web demo here Replicate

Step 1. Downloading Datasets

All the datasets used for the evaluation of KID can be downloaded in this link. The statistics of these datasets can be found in the Table A1 (Appendix) of the paper.

Step 2. Constructing Knowledge Tries

To construct the external knowledge trie for each task, we leverage the Wiki passages and corresponding FAISS index used by RAG. We prepare a script in scrtips/build_trie.py to 1) download passages and index, and 2) build the knowledge trie by query the context in the training data of a certain task.

The complete loop of how local knowledge memory and external knowledge trie interact with each other is shown in Figure A1 (Appendix) of the paper.

Step 3. Using KID for Decoding

In the demo script (scripts/run_demo.py), we show how KID enables knowledge infusing even if the language model (GPT-2) is not fine-tuned. You can run with python scripts/run_demo.py with is_kid=True, and the output is:

# KID output
= The context for generation is: Does marijuana impair driving ability?
= KID Decoding: Does marijuana impair driving ability? The answer is yes and we do 
  believe it would be possible to impair the driving abilities of those who smoke 
  marijuana as it causes some people with medical condition to become unable to drive
  for an extended period of time.

While with vanilla sampling decoding (i.e., Nucleus Sampling), the output is:

# Sampling output (is_kid=False)
= The context for generation is: Does marijuana impair driving ability?
= Sampling Decoding: Does marijuana impair driving ability? Professor, Jordan Allison
  and University of New South Wales researchers led by Alan Hayes and Phd in Public 
  Health investigate cannabis vs. alcohol impairment effects in adolescents

Apparently, the language model armed with KID can answer the question in a more informative and coherent way. Note that we can further improve the generation quality by replacing the vanilla GPT-2 with a task fine-tuned one, by setting the model_path in the head of run_demo.py with the path to your pre-trained language model.

License

KID is MIT licensed. See the LICENSE file for details.

kid's People

Contributors

agi-templar avatar chenxwh avatar couturierc avatar microsoftopensource avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

kid's Issues

How to train?

Nice work. Would the training code be open sourced?

Evaluate Scripts

Hi,
This paper is very interesting, can you share the evaluation scripts?
I find some trouble in the evaluation stage.

Thanks

Spacy EntityRuler

Hi, I try to use Spacy V3, but I got a warning that "The component 'entity_ruler' does not have any patterns defined."
So, should I specify a specific pattern for entity_ruler?

Thanks

Question about the KL penalty

In compute_reward() function in kid_policy.py kl penalty is calculated as below.
kl_penalty = self.kl_scale * ( (kid_last_probs * (kid_last_probs / norm_last_probs).log()).sum() )
But the equation in the paper is
image
I am a little bit confused because, as the comment of the function says norm_last_probs is pi, it is considered kid_last_probs and norm_last_probs should be changed. If I am wrong, please let me know.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.