尝试在一个非常大的文本上训练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)。在模型中运行此序列将导致索引错误。
那我该怎么做呢?
如何给模型一个大的新文本进行训练?
编辑:
当尝试连接时,令牌程序可以工作,但是模型不工作:
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: indices0,1024 = 1024不在[0,1024]操作中:ResourceGather
我遗漏了什么?
发布于 2020-09-16 02:05:23
您的问题与不同领域的培训无关。相反,您只是提供了一个文本长度(显然是149887标记),这个长度比模型所能支持的最大长度(1024)要长。你有三个选择:
max_length
参数,例如tokenizer(text, max_length=1024, ...)
。确保读取Tokenizer
类的所有可用选项.https://stackoverflow.com/questions/63911955
复制相似问题