【问题标题】:How can we pass a list of strings to a fine tuned bert model?我们如何将字符串列表传递给经过微调的 bert 模型?
【发布时间】:2022-08-18 16:19:44
【问题描述】:

我想将字符串列表而不是单个字符串输入传递给我的微调 bert 问题分类模型。 这是我接受单个字符串输入的代码。

questionclassification_model = tf.keras.models.load_model(\'/content/drive/MyDrive/questionclassification_model\')
tokenizer = BertTokenizer.from_pretrained(\'bert-base-cased\')

def prepare_data(input_text):
    token = tokenizer.encode_plus(
        input_text,
        max_length=256, 
        truncation=True, 
        padding=\'max_length\', 
        add_special_tokens=True,
        return_tensors=\'tf\'
    )
    return {
        \'input_ids\': tf.cast(token[\'input_ids\'], tf.float64),
        \'attention_mask\': tf.cast(token[\'attention_mask\'], tf.float64)
    }

def make_prediction(model, processed_data, classes=[\'Easy\', \'Medium\', \'Hard\']):
    probs = model.predict(processed_data)[0]
    return classes[np.argmax(probs)],probs;

我不想在列表上使用 for 循环,因为它需要更多的执行时间。 当我尝试将列表作为输入传递给标记器时,它为每个输入返回相同的输出。

input_text = [\"What is gandhi commonly considered to be?,Father of the nation in india\",\"What is the long-term warming of the planets overall temperature called?, Global Warming\"]
processed_data = prepare_data(input_text)

{\'input_ids\': <tf.Tensor: shape=(1, 256), dtype=float64, numpy= 数组([[101., 100., 100., 102., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])>, \'attention_mask\': <tf.Tensor: shape=(1, 256), dtype=float64, numpy= 数组([[1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0 ., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]) >}


这不是输入文本的正确标记。

提前致谢...

    标签: python nlp huggingface-transformers bert-language-model huggingface-tokenizers


    【解决方案1】:

    默认情况下已经支持拥抱脸。分词器和模型都接受一个列表。请参阅此处标记器的文档:https://huggingface.co/docs/transformers/main_classes/tokenizer#transformers.PreTrainedTokenizer.__call__

    samples = ["some text1", "some_text2"]
    inputs = tokenizer(samples)
    predictions = questionclassification_model(inputs)
    

    【讨论】:

    • 但是当我尝试传递一个列表时,它为所有输入返回相同的输出。
    • 对于来自 hugginface 的模型,它正在工作。但是,这是我自己创建的微调模型。
    【解决方案2】:

    一句话与批次的不同方法

    编码一个句子和编码一批句子有不同的方法 根据文档 (https://huggingface.co/docs/transformers/v4.21.1/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.encode_plus),encode_plus 方法期望第一个参数是“这可以是一个字符串,一个字符串列表(使用 tokenize 方法标记化的字符串) 或整数列表(使用 convert_tokens_to_ids 方法的标记化字符串 id)。" (强调我的) - 因此,如果您将字符串列表传递给此特定方法,它们将被解释为令牌列表,而不是句子,显然所有那些很长的“标记”,如“通常认为甘地是什么?,印度国家之父”与词汇表中的任何内容都不匹配,因此它们被映射到词汇表外的 id .

    如果要对一批句子进行编码,则需要将字符串列表传递给batch_encode_plus 方法(https://huggingface.co/docs/transformers/v4.21.1/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.batch_encode_plus

    【讨论】:

      猜你喜欢
      • 2020-06-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 2021-04-01
      • 1970-01-01
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多