Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 在大文本上微调GPT-2以生成域文本_Tensorflow_Keras_Deep Learning_Nlp_Huggingface Transformers - Fatal编程技术网

Tensorflow 在大文本上微调GPT-2以生成域文本

Tensorflow 在大文本上微调GPT-2以生成域文本,tensorflow,keras,deep-learning,nlp,huggingface-transformers,Tensorflow,Keras,Deep Learning,Nlp,Huggingface Transformers,尝试在非常大的文本上训练GPT-2,以便从特定域生成文本 使用tensorflow2 例如,假设我有所有的哈利波特系列书籍:) 我想在他们身上训练GPT-2,这样我可以在以后从哈利波特域名生成文本 from tensorflow.keras.utils import get_file from transformers import GPT2Tokenizer, TFGPT2Model text = '...' # Length of text: 474429 characters # 84

尝试在非常大的文本上训练GPT-2,以便从特定域生成文本 使用tensorflow2

例如,假设我有所有的哈利波特系列书籍:)
我想在他们身上训练GPT-2,这样我可以在以后从哈利波特域名生成文本

from tensorflow.keras.utils import get_file
from transformers import GPT2Tokenizer, TFGPT2Model

text = '...'
# Length of text: 474429 characters
# 84 unique characters

tokenizer = GPT2Tokenizer.from_pretrained('gpt2-medium')
model = TFGPT2Model.from_pretrained('gpt2-medium')

encoded_input = tokenizer(text, return_tensors='tf') # ERROR
output = model(encoded_input)

input_ids = tokenizer.encode('severus snape', return_tensors='tf')
greedy_output = model.generate(input_ids, max_length=50)
print(tokenizer.decode(greedy_output[0], skip_special_tokens=True))
错误:令牌索引序列长度大于指定的长度 此型号的最大序列长度(149887>1024)。运行这个 模型中的顺序将导致索引错误

那么我该如何让它工作呢?
如何为模型输入大量新文本进行训练

编辑:
尝试concat时,标记器可以工作,但模型不能:

from textwrap import wrap
text_batches = wrap(text, 1000)

encoded_input = None

for tb in text_batches:
    current = tokenizer(tb, return_tensors='tf')
  
    if encoded_input == None:
        encoded_input = current
    else:
        encoded_input['input_ids']      = tf.concat([encoded_input['input_ids'], current['input_ids']], axis=-1)
        encoded_input['attention_mask'] = tf.concat([encoded_input['attention_mask'], current['attention_mask']], axis=1)

output = model(encoded_input) # ERROR
错误:InvalidArgumentError:索引[01024]=1024不在[0]中, 1024)[作品:ResourceGather]


我遗漏了什么?

您的问题与不同领域的培训无关。相反,您只是提供了一个文本长度(显然是149887个令牌),该长度超过了模型可以支持的最大长度(1024)。您有三个选项:

  • 手动将输入字符串截断为令牌的最大长度

  • 在对标记器的调用中设置
    max_length
    参数,例如
    tokenizer(text,max_length=1024,…)
    。确保阅读
    标记器
    类的所有可用选项

  • 重温一下为什么你需要一个149K标记的文本字符串。这是文本的全部吗?你应该改用句子吗


  • 感谢您的帮助。1.尝试过-不起作用。将其添加到Q中。2.加载预训练模型时,其设置不能超过1024。3.在句子中如何设置?我希望知道如何操作:)如果错误显示
    1024不在[0,1024)
    ,那么你必须能够阅读简单的数学符号。1024)意味着最多1024个,但不包括1024个,所以试试1023。如果你想把大文本解析成句子,那么你有三个选择:(a)使用正则表达式(我不会选择这种方法);(b)使用NLTK句子解析器;或者(c)使用Spacy句子解析器。所有三个选项都在一篇StackOverflow文章中讨论过:欣赏输入,但每个petrain模型都有自己的标记。使用不同的标记器不是一个好办法。尤其是在使用嵌入时。也许我需要弄清楚如何在1024个大小的批中训练模型?我没有说使用不同的标记器。问题是,您需要将每个示例的令牌数量限制为1024(或给定模型的限制)。