【发布时间】:2021-07-26 15:16:12
【问题描述】:
我正在尝试使用 PyTorch-Transformers 从示例文本中生成一长串文本。为此,我正在关注this tutorial。因为原始文章仅从给定文本中预测一个单词,所以我修改了该脚本以生成长序列而不是一个。这是代码的修改部分
# 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)
输出
检查可以定义为详细的检查或分析 一个物体或一个人。例如,工程师将检查一个 结构,像一座桥,看它是否安全。医生可以进行 检查病人的身体,看看它是否安全。
医生也可能会检查病人的身体,看看它是否安全。一种 医生可能会对病人的身体进行检查,看它是否 安全。
如您所见,生成的文本不会生成任何独特的文本序列,而是会一遍又一遍地生成相同的句子,并进行细微的更改。
我们应该如何使用 PyTorch-Transformers 创建长序列?
【问题讨论】:
-
请查看generate method的一些参数(例如:
repetition_penalty、temperature、length_penalty)。
标签: deep-learning nlp pytorch huggingface-transformers transformer