python - T5 微调模型输出 而不是大括号和其他特殊字符

标签 python machine-learning huggingface-transformers huggingface-tokenizers t5-transformer

首先我要说的是,当涉及到整个机器学习和变压器时,我是一个初学者,所以如果这是一个愚蠢的问题,我很抱歉。 我一直在微调 t5 以生成 mongodb 查询的任务,但我遇到了这个奇怪的输出,它看起来不像预期的那样。

inputs = tokenizer(inputs, max_length=max_input_length, truncation=True, return_tensors="pt")

output = model.generate(**inputs, num_beams=8, do_sample=True, min_length=10, max_length=64)

decoded_output = tokenizer.batch_decode(output, skip_special_tokens=False)[0]

print(decoded_output)

predicted_Query = nltk.sent_tokenize(decoded_output.strip())[0]

print(predicted_Query)

给出以下输出:

<pad> db.movies.find(<unk>"title": "The Poor Little Rich Girl"<unk>, <unk>"writers": 1<unk>)</s>

<pad> db.movies.find(<unk>"title": "The Poor Little Rich Girl"<unk>, <unk>"writers": 1<unk>)</s>

该查询在很大程度上是正确的,我假设 <unk> token 应该是花括号,但模型无法理解它们(如 OOV 情况)。请注意,用于微调它的数据集在输出中包含大括号,因此我对它在测试期间如何无法识别它感到困惑。 标记器会出现问题吗?如果是这样,我可以通过添加一些新的标记来扩展词汇吗? 我并不是要求答案(尽管这是受欢迎的),但如果您提供一些指导,我们将不胜感激。 感谢您抽出时间。

我测试了分词器是否可以处理花括号,结果表明它可以。同样,我对此很陌生,所以我不确定我是否很好地理解了这个问题。

最佳答案

经过一番研究,我找到了解决方案。 T5 分词器词汇缺少一些字符,例如大括号和其他字符,因此我使用以下内容来添加它们。

from transformers import  AutoModel
new_words = ['{', '}']
 
model = AutoModel.from_pretrained("t5-base")

tokenizer.add_tokens(new_words)

model.resize_token_embeddings(len(tokenizer))

关于python - T5 微调模型输出 而不是大括号和其他特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75851029/

相关文章:

machine-learning - 卷积神经网络在二值图像上运行得更快吗

python - Huggingface 分类与预测作斗争

named-entity-recognition - 如何使用经过训练的 BERT NER(命名实体识别)模型来预测新示例?

python - 如果 DataFrame 包含特定字符串,则创建新列

python - 没有 "verbose"参数的 sklearn 模型的进度条

python - 如何在 TensorFlow 2 中写入 TensorBoard

r - 我是否排除训练集中使用的数据来运行预测()模型?

python - Huggingface 上的 mT5 模型可以用于机器翻译吗?

python - 反转 Python 字符串中的单词(包括标点符号)

python - 如何根据来自 for 循环的值创建多维列表