ORED基准实验
MATH
提示不同的区别
这两段代码分别表示了两种不同的提示(CoT 和 TIR)格式,它们是用于与语言模型进行交互的提示,目的是引导模型按特定的方式进行推理或解决问题
CoT (Chain of Thought):要求模型展示逐步推理的过程,通常适用于需要分步推理的任务,如数学题目。
TIR (Task-Integrated Reasoning):要求模型结合自然语言推理和程序(代码)来解决问题,这对于需要程序执行的任务(如编程或更复杂的数学计算)更为适用。
两者的主要区别在于 TIR 要求在推理过程中结合程序执行,而 CoT 只是让模型按步骤思考问题
Qwen推理代码
from transformers import AutoModelForCausalLM, AutoTokenizer
model_path = "/kaggle/input/qwen2.5-math/transformers/1.5b/1"
#device = "cuda" # the device to load the model onto
model = AutoModelForCausalLM.from_pretrained(
    model_path,
    torch_dtype="auto",
    device_map="auto",
    trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
    model_path,
    trust_remote_code=True
)
prompt = "what is the integral of $x^2$ from 0 to 2?"
# CoT
messages = [
    {"role": "system", "content": "Please reason step by step, and put your final answer within \\boxed{}."},
    {"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True
)
model_inputs = tokenizer(
    [text],
    return_tensors="pt"
).to(model.device)
print(f"开始推理{prompt}")
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=512,
)
print("推理完成.")
generated_ids = [
    output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(
    generated_ids,
    skip_special_tokens=True
)[0]
print(f"推理结果{response}")

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号