0

ValueError在尝试微调Hugging Face 的 GPT-2 模型分布时,我在我的 Python 代码中遇到了。具体来说:

ValueError: Dimensions must be equal, but are 64 and 0 for
'{{node Equal_1}} = Equal[T=DT_FLOAT, incompatible_shape_error=true](Cast_18, Cast_19)'
with input shapes: [64,0,1024], [2,0,12,1024].

我有大约 100 个文本文件,我将它们连接成一个名为的字符串变量raw_text,然后传递给以下函数以创建训练和测试 TensorFlow 数据集:

def to_datasets(raw_text):
    # split the raw text in smaller sequences
    seqs = [
        raw_text[SEQ_LEN * i:SEQ_LEN * (i + 1)]
        for i in range(len(raw_text) // SEQ_LEN)
    ]

    # set up Hugging Face GPT-2 tokenizer
    tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
    tokenizer.pad_token = tokenizer.eos_token

    # tokenize the character sequences
    tokenized_seqs = [
        tokenizer(seq, padding="max_length", return_tensors="tf")["input_ids"]
        for seq in seqs
    ]

    # convert tokenized sequences into TensorFlow datasets
    trn_seqs = tf.data.Dataset \
        .from_tensor_slices(tokenized_seqs[:int(len(tokenized_seqs) * TRAIN_PERCENT)])
    tst_seqs = tf.data.Dataset \
        .from_tensor_slices(tokenized_seqs[int(len(tokenized_seqs) * TRAIN_PERCENT):])

    def input_and_target(x):
        return x[:-1], x[1:]

    # map into (input, target) tuples, shuffle order of elements, and batch
    trn_dataset = trn_seqs.map(input_and_target) \
        .shuffle(SHUFFLE_BUFFER_SIZE) \
        .batch(BATCH_SIZE, drop_remainder=True)
    tst_dataset = tst_seqs.map(input_and_target) \
        .shuffle(SHUFFLE_BUFFER_SIZE) \
        .batch(BATCH_SIZE, drop_remainder=True)

    return trn_dataset, tst_dataset

然后我尝试训练我的模型,调用train_model(*to_datasets(raw_text))

def train_model(trn_dataset, tst_dataset):
    # import Hugging Face GPT-2 model
    model = TFGPT2Model.from_pretrained("gpt2")

    model.compile(
        optimizer=tf.keras.optimizers.Adam(),
        loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        metrics=tf.metrics.SparseCategoricalAccuracy()
    )

    model.fit(
        trn_dataset,
        epochs=EPOCHS,
        initial_epoch=0,
        validation_data=tst_dataset
    )

调用ValueError时触发。model.fit()全部大写的变量是从 JSON 文件中提取的设置。目前,它们设置为:

{
    "BATCH_SIZE":64,
    "SHUFFLE_BUFFER_SIZE":10000,
    "EPOCHS":500,
    "SEQ_LEN":2048,
    "TRAIN_PERCENT":0.9
}

任何有关此错误含义的信息或有关如何解决它的想法将不胜感激。谢谢!

4

1 回答 1

0

我遇到了同样的问题,但是当我将批量大小更改为 12(与 gpt-2 配置文件中的 n_layer 参数相同)时,它可以工作。我不知道它为什么会起作用,但您可以尝试一下……如果您设法以不同的方式解决它,我将很高兴听到。

于 2021-07-29T15:43:18.783 回答