Добавь в следующий код комментарии с пояснением того, что происходит from transformers…
Jump to turn (7)
- 1Добавь в следующий код комментарии с пояснением того, что происходит from transf
- 2параметр returndictingenerate за что отвечает
- 3Можно ли как-то получить вероятности по словарю для каждого шага генерации
- 4что делает функция .strip()
- 5Что делает функция computetransitionscores
- 6что делает вот эта строчка
- 7что делает эта строка кода inputlength = 1 if model.config.isencoderdecoder else
- User
Добавь в следующий код комментарии с пояснением того, что происходит from transformers import LlamaForCausalLM, LlamaTokenizer, AutoModelForCausalLM, AutoTokenizer import torch import numpy as np
device = "cuda:0" if torch.cuda.is_available() else "cpu"
model=LlamaForCausalLM.from_pretrained("Llama-2-7b-chat-hf").to(device) tokenizer= LlamaTokenizer.from_pretrained("Llama-2-7b-chat-hf")
prompt = "Could you give me 3 cities located in Europe ?"
inputs = tokenizer([prompt], return_tensors="pt").to(device)
outputs=model.generate(**inputs,return_dict_in_generate=True, output_scores=True,max_new_tokens=75)
transition_scores = model.compute_transition_scores(outputs.sequences, outputs.scores, normalize_logits=True)
input_length = 1 if model.config.is_encoder_decoder else inputs.input_ids.shape[1] generated_tokens = outputs.sequences[:,input_length:]
for tok, score in zip(generated_tokens[0], transition_scores[0]): # | token | token string | logits | probability print(f"| {tok:5d} | {tokenizer.decode(tok):8s} | {score.numpy(force=True):.4f} | {np.exp(score.numpy(force=True)):.2%}")
- Assistant