0

基本上我试图让 gpt2 响应变量 {text} 中的提示,我遇到了这个错误:

ValueError:具有多个元素的数组的真值不明确。使用 a.any() 或 a.all()

到目前为止,这是我的代码:

import gradio as gr
from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')#gpt2-xl #for very powerful model
model = GPT2LMHeadModel.from_pretrained('gpt2', pad_token_id=tokenizer.eos_token_id)

text = "what is natural language processing?"
encoded_input = tokenizer.encode(text, return_tensors='pt')

#print(tokenizer.decode((encoded_input[0][0]))) # works well to here

def generate_text(inp):
    input_ids = tokenizer.encode(inp, return_tensors='tf')
    beam_output = model.generate(input_ids, max_length=100, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
    output = tokenizer.decode(beam_output[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
    return ".".join(output.split(".")[:-1]) + "."

output_text = gr.outputs.Textbox() # works well to here
text1 = generate_text(text) # BREAKS HERE

谁能帮我弄清楚我做错了什么?谢谢。

4

1 回答 1

0

看来您正在使用return_tensors='tf'而不是return_tensors='pt'.

根据文档链接

return_tensors (str, optional, defaults to None) – Can be set to ‘tf’ or ‘pt’ to return respectively TensorFlow tf.constant or PyTorch torch.Tensor instead of a list of python integers.

以下代码适用于我:

import gradio as gr
from transformers import pipeline, GPT2Tokenizer, GPT2LMHeadModel

tokenizer = GPT2Tokenizer.from_pretrained('gpt2')#gpt2-xl #for very powerful model
model = GPT2LMHeadModel.from_pretrained('gpt2', pad_token_id=tokenizer.eos_token_id)

text = "what is natural language processing?"
encoded_input = tokenizer.encode(text, return_tensors='pt')


def generate_text(inp):
    input_ids = tokenizer.encode(inp, return_tensors='pt')
    beam_output = model.generate(input_ids, max_length=100, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
    output = tokenizer.decode(beam_output[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
    return ".".join(output.split(".")[:-1]) + "."

output_text = gr.outputs.Textbox() # works well to here
text1 = generate_text(text) # NOW IT WORKS!!!

从模型生成的文本:

什么是自然语言处理?

这是一个已经争论了很长时间的问题,我认为理解我们在这里讨论的是什么很重要。这不会在一夜之间发生,而是会在非常非常短的时间内发生。我们必须非常小心我们所说的和如何谈论它,因为如果我们不这样做,它可能会被误解为软弱的表现。

Colab 演示

于 2022-02-24T19:27:43.903 回答