微调 GPT-2,用于回答多个类似问题

问题描述 投票:0回答:1

我一直在微调 GPT-2 的 Keras 实现以进行问答。总体而言,结果似乎很有希望,但如果我包含非常相似的问题,我就会发现问题。我看到的行为是模型感到困惑并对两个问题给出相同的答案。

import keras_nlp
import tensorflow as tf
import keras 
import json

    
    preprocessor = keras_nlp.models.GPT2CausalLMPreprocessor(
        sequence_length=128,
    )

    gpt2_lm = keras_nlp.models.GPT2CausalLM.from_preset(
        "gpt2_base_en", preprocessor=preprocessor
    )

    questionsWithAnswers = []
    ... // populate with several lines of text

    questionsWithAnswers.append("What is Joe's phone number? Joe's phone number is 555-555-5555")

    questionsWithAnswers.append("What is Mary's phone number? Mary's phone number is 444-444-4444")

    loss = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
    
    gpt2_lm.compile(
        optimizer='Adam',
        loss=loss,
        weighted_metrics=["accuracy"],
    )

    gpt2_lm.fit(epochs=50, verbose=2, batch_size=2, x=questionsWithAnswers)
    output = gpt2_lm.generate("What is Joe's phone number?", max_length=200)

在上面的示例中,我可以问:“乔的电话号码是什么?”和“玛丽的电话号码是什么?”。不幸的是,该模型似乎只选择了一个答案,并对两个问题生成相同的答案。

我认为问题在于问题之间的相似性,唯一的区别是人名。

有什么想法如何教模型区分两个问题/答案吗?

python keras large-language-model fine-tuning gpt-2
1个回答
0
投票

不知道为什么这个问题被反复否决,但无论如何,我正在分享我想出的解决方案。

我怀疑这个问题是由提出非常相似的问题引起的,因此为了解决这个问题,我最终在提示和训练数据中添加了一个额外的上下文参数。

我没有问原来的问题:“Joe 的电话号码是什么?”,而是在上下文参数中添加了一个前缀。新问题是“朋友:乔的电话号码是什么?”和“同事:玛丽的电话号码是什么”。

看来添加额外的上下文参数足以让模型区分这些问题并回答正确的答案。

© www.soinside.com 2019 - 2024. All rights reserved.