【发布时间】:2021-02-26 13:33:59
【问题描述】:
我正在尝试获得 T5 变压器模型的可重现结果:
import torch
from transformers import T5ForConditionalGeneration,T5Tokenizer
def set_seed(seed):
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
set_seed(42)
t5model = T5ForConditionalGeneration.from_pretrained('ramsrigouthamg/t5_paraphraser')
tokenizer = T5Tokenizer.from_pretrained('t5-base')
device = torch.device("cpu")
print ("device ",device)
t5model = t5model.to(device)
max_len = 256
text = "paraphrase: " + txt + " </s>"
encoding = tokenizer.encode_plus(text,pad_to_max_length=True, return_tensors="pt")
input_ids, attention_masks = encoding["input_ids"].to(device), encoding["attention_mask"].to(device)
beam_outputs = t5model.generate(
input_ids=input_ids, attention_mask=attention_masks,
do_sample=True,
max_length=max_len,
top_k=50,
top_p=0.98,
early_stopping=True,
num_return_sequences=10,
)
虽然我设置了种子编号,但t5model.generate 每次运行时都会给我不同的结果。
设置种子号的正确方法是什么,以便在多次执行后得到t5model.generate的相同结果?
【问题讨论】:
标签: python nlp torch huggingface-transformers transformer