gpt4 book ai didi

python - HuggingFace Transformers GPT-2 如何使用过去?

转载 作者:行者123 更新时间:2023-12-03 14:37:46 31 4
gpt4 key购买 nike

我有:

        context = torch.tensor(context, dtype=torch.long, device=self.device)
context = context.unsqueeze(0)
generated = context
with torch.no_grad():
past_outputs = None
for i in trange(num_words):
print(i, num_words)
inputs = {"input_ids": generated}

outputs, past_outputs = self.model(
**inputs,
past=past_outputs
)
next_token_logits = outputs[
0, -1, :] / (temperature if temperature > 0 else 1.0)

# reptition penalty from CTRL
# (https://arxiv.org/abs/1909.05858)
for _ in set(generated.view(-1).tolist()):
next_token_logits[_] /= repetition_penalty

filtered_logits = top_k_top_p_filtering(
next_token_logits, top_k=top_k, top_p=top_p)
if temperature == 0: # greedy sampling:
next_token = torch.argmax(filtered_logits).unsqueeze(0)
else:
next_token = torch.multinomial(
F.softmax(filtered_logits, dim=-1), num_samples=1)

generated = torch.cat(
(generated, next_token.unsqueeze(0)), dim=1)
这适用于第一次迭代,但随后在下一次迭代中出现错误:
  File "/Users/shamoon/Sites/wordblot/packages/ml-server/generator.py", line 143, in sample_sequence
past=past_outputs
File "/Users/shamoon/.local/share/virtualenvs/ml-server-EdimT5-E/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
result = self.forward(*input, **kwargs)
File "/Users/shamoon/.local/share/virtualenvs/ml-server-EdimT5-E/lib/python3.7/site-packages/transformers/modeling_gpt2.py", line 601, in forward
output_hidden_states=output_hidden_states,
File "/Users/shamoon/.local/share/virtualenvs/ml-server-EdimT5-E/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
result = self.forward(*input, **kwargs)
File "/Users/shamoon/.local/share/virtualenvs/ml-server-EdimT5-E/lib/python3.7/site-packages/transformers/modeling_gpt2.py", line 470, in forward
position_embeds = self.wpe(position_ids)
File "/Users/shamoon/.local/share/virtualenvs/ml-server-EdimT5-E/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
result = self.forward(*input, **kwargs)
File "/Users/shamoon/.local/share/virtualenvs/ml-server-EdimT5-E/lib/python3.7/site-packages/torch/nn/modules/sparse.py", line 114, in forward
self.norm_type, self.scale_grad_by_freq, self.sparse)
File "/Users/shamoon/.local/share/virtualenvs/ml-server-EdimT5-E/lib/python3.7/site-packages/torch/nn/functional.py", line 1724, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
IndexError: index out of range in self
有什么我做错了吗?

最佳答案

我相信问题在于context包含超过词汇量的整数值。我的假设基于最后的回溯行:

return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
IndexError: index out of range in self

关于python - HuggingFace Transformers GPT-2 如何使用过去?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63232732/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com