README.md 1.08 KB
Newer Older
Patrick von Platen's avatar
Patrick von Platen committed
1
2
3
## RAG

This is the RAG-Sequence Model of the the paper [Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks](https://arxiv.org/pdf/2005.11401.pdf) 
Patrick von Platen's avatar
Patrick von Platen committed
4
by Patrick Lewis, Ethan Perez, Aleksandara Piktus et al.
Patrick von Platen's avatar
Patrick von Platen committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

## Usage:

```python

from transformers import RagTokenizer, RagRetriever, RagSequenceForGeneration

tokenizer = RagTokenizer.from_pretrained("facebook/rag-token-nq")
retriever = RagRetriever.from_pretrained("facebook/rag-token-nq", index_name="exact", use_dummy_dataset=True)
model = RagSequenceForGeneration.from_pretrained("facebook/rag-token-nq", retriever=retriever)

input_dict = tokenizer.prepare_seq2seq_batch("How many people live in Paris?", "In Paris, there are 10 million people.", return_tensors="pt")
outputs = model(input_ids=input_dict["input_ids"], labels=input_dict["labels"])

# outputs.loss should give 76.2978

Patrick von Platen's avatar
Patrick von Platen committed
21
generated = model.generate(input_ids=input_dict["input_ids"])
Patrick von Platen's avatar
Patrick von Platen committed
22
23
24
25
generated_string = tokenizer.batch_decode(generated, skip_special_tokens=True)

# generated_string should give 270,000,000 -> not quite correct the answer, but it also only uses a dummy index
```