【发布时间】:2021-01-02 19:22:58
【问题描述】:
尝试在非常大的文本上训练 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: indices[0,1024] = 1024 不在 [0, 第1024章,【操作】
我错过了什么?
【问题讨论】:
标签: tensorflow keras deep-learning nlp huggingface-transformers