Downloads · 30 days
14
4% of all-time downloads
liujch1998/crystal-11b
crystal-11b is a text generation model from liujch1998. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as mit.
Crystal is an introspective reasoning model commonsense QA. See our paper at: <https://arxiv.org/abs/2310.04921.
Downloads · 30 days
14
4% of all-time downloads
All-time downloads
386
Public
Repo size
55.4 GB
Likes
1
Public
Click a slice to open those files.
.bin22.7 GB · 100%
From the Hugging Face model README
Crystal is an introspective reasoning model commonsense QA. See our paper at: https://arxiv.org/abs/2310.04921.
Crystal can answer a given commonsense question by first generating a relevant knowledge statement, and then predict the final answer by referencing the generated knowledge. We call this process "introspective reasoning", and it improves both the prediction accuracy and the interpretability of neural models at reasoning tasks.
Crystal is intended to answer commonsense questions via an "introspective reasoning" process.
Crystal is a research prototype and may give incorrect answers or reasoning process. Do not use for making critical decisions. It is intended to answer questions about commonsense, and may be unreliable when taking input out of this scope.
See the Limitations section of our paper.
Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model. More information needed for further recommendations.
Use the code below to get started with the model.
from transformers import T5Tokenizer, T5ForConditionalGeneration
tokenizer = AutoTokenizer.from_pretrained('liujch1998/crystal-11b')
model = AutoModelForSeq2SeqLM.from_pretrained('liujch1998/crystal-11b')
model.eval()
max_question_len, max_knowledge_len, max_answer_len = 128, 32, 2
k = 1 # number of knowledge statements to generate
top_p = 0.0001
question = 'If the mass of an object gets bigger what will happen to the amount of matter contained within it? \\n (A) gets bigger (B) gets smaller'
choices = ['A', 'B']
choices_ids = tokenizer(choices, return_tensors='pt', padding='max_length', truncation='longest_first', max_length=max_answer_len).input_ids # (C, AL)
prompt = question + ' \\n Knowledge: '
prompt_tok = tokenizer(prompt, return_tensors='pt', padding='max_length', truncation='longest_first', max_length=max_question_len) # (1, QL)
knowledges_ids = self.model.generate(
input_ids=prompt_tok.input_ids,
attention_mask=prompt_tok.attention_mask,
max_length=max_knowledge_len + 1,
min_length=3,
do_sample=True,
num_return_sequences=k,
top_p=top_p,
) # (K, KL); begins with 0 ([BOS]); ends with 1 ([EOS])
knowledges_ids = knowledges_ids[:, 1:].contiguous() # no beginning; ends with 1 ([EOS])
knowledges = tokenizer.batch_decode(knowledges_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
prompts = [question + (f' \\n Knowledge: {knowledge} \\n Answer: ' if knowledge != '' else ' \\n Answer:') for knowledge in knowledges]
prompts_tok = self.tokenizer(prompts, return_tensors='pt', padding='max_length', truncation='longest_first', max_length=max_question_len + max_knowledge_len) # (K, QL+KL)
output = model(
input_ids=prompts_tok.input_ids,
attention_mask=prompts_tok.attention_mask,
labels=choices_ids[0].unsqueeze(0).repeat(len(knowledges), 1),
)
logitsss = output.logits # (K, AL, V)
logitss = logitsss[:, 0, :] # (K, V)
choice_ids = choices_ids[:, 0] # (C)
answer_logitss = logitss.gather(dim=1, index=choice_ids.unsqueeze(0).expand(len(knowledges), -1)) # (K, C)
answer_probss = answer_logitss.softmax(dim=1) # (K, C)
answer_probs = answer_probss.max(dim=0).values # (C)
pred = answer_probs.argmax(dim=0).item()
pred = choices[pred]
print(f'Question: {question}\nKnowledge: {knowledges[0]}\nAnswer: {pred}')
You may also refer to https://huggingface.co/spaces/liujch1998/crystal/blob/main/app.py#L10-L86 for implementation.
BibTeX:
@article{Liu2023CrystalIR,
title={Crystal: Introspective Reasoners Reinforced with Self-Feedback},
author={Jiacheng Liu and Ramakanth Pasunuru and Hannaneh Hajishirzi and Yejin Choi and Asli Celikyilmaz},
journal={ArXiv},
year={2023},
volume={abs/2310.04921}
}
Jiacheng Liu