Deep learning 使用PyTorch变压器生成长序列的正确方法是什么?

Deep learning 使用PyTorch变压器生成长序列的正确方法是什么?,deep-learning,nlp,pytorch,huggingface-transformers,transformer,Deep Learning,Nlp,Pytorch,Huggingface Transformers,Transformer,我试图从一个示例文本中使用PyTorch Transformers生成一个长的文本序列。我是为了这个目的而跟随的。因为原始文章只预测给定文本中的一个单词,所以我修改了脚本以生成长序列而不是一个。这是代码的修改部分 # Encode a text inputs text = """An examination can be defined as a detailed inspection or analysis of an object or person. For

我试图从一个示例文本中使用PyTorch Transformers生成一个长的文本序列。我是为了这个目的而跟随的。因为原始文章只预测给定文本中的一个单词,所以我修改了脚本以生成长序列而不是一个。这是代码的修改部分

# Encode a text inputs
text = """An examination can be defined as a detailed inspection or analysis
 of an object or person. For example, an engineer will examine a structure,
  like a bridge, to see if it is safe. A doctor may conduct"""

indexed_tokens = tokenizer.encode(text)

# Convert indexed tokens in a PyTorch tensor
tokens_tensor = torch.tensor([indexed_tokens])
seq_len = tokens_tensor.shape[1]
tokens_tensor = tokens_tensor.to('cuda')


with torch.no_grad():
    for i in range(50):
        outputs = model(tokens_tensor[:,-seq_len:])
        predictions = outputs[0]
        predicted_index = torch.argmax(predictions[0, -1, :])
        tokens_tensor = torch.cat((tokens_tensor,predicted_index.reshape(1,1)),1)


pred = tokens_tensor.detach().cpu().numpy().tolist()
predicted_text = tokenizer.decode(pred[0])
print(predicted_text)
输出

检查可定义为详细检查或分析 指物体或人。例如,工程师将检查 结构,就像一座桥,看它是否安全。医生可以指导医生 检查病人的身体是否安全

医生也可以检查病人的身体,看是否安全。A. 医生可能会对病人的身体进行检查,看它是否健康 安全的

正如您所看到的,生成的文本不会生成任何唯一的文本序列,但它会一次又一次地生成相同的句子,只是做了一些小的更改


我们应该如何使用PyTorch Transformers创建长序列?

通常不会一次生成完整的句子或完整的文本。在这方面有一些研究方法,但几乎所有最先进的模型都会逐字生成文本。然后,在时间t-1生成下一个单词的同时,将生成的单词用作输入(与其他已生成或给定的单词一起)。所以,它逐字生成是正常的。我不明白你这是什么意思


您使用的是哪种型号?

请检查一些参数(例如:
重复惩罚
温度
长度惩罚
)。