简体   繁体   English

如何在我的 model 中使用预训练的 bert model 作为嵌入层?

[英]How to using the pre-trained bert model as embedding layer in my model?

I'm using this pre-trained model by bert-for-tf2 in my functional model like following:我在我的功能 model 中使用由 bert-for-tf2 预训练的 model ,如下所示:

def build_model(model_dir, batch_size, max_seq_num, max_seq_len):
    bert_params = bert.params_from_pretrained_ckpt(model_dir)
    l_bert = bert.BertModelLayer.from_params(bert_params, name="bert", trainable=False)

    input_ids = tf.keras.layers.Input(shape=(max_seq_num, max_seq_len,), dtype='int32', name='input_ids')
    reshaped_input_ids = tf.reshape(input_ids, (batch_size * max_seq_num, max_seq_len))

    token_type_ids = tf.keras.layers.Input(shape=(max_seq_num, max_seq_len,), dtype='int32', name='token_type')
    reshaped_token_type_ids = tf.reshape(token_type_ids, (batch_size * max_seq_num, max_seq_len))

    mask_ids = tf.keras.layers.Input(shape=(max_seq_num, max_seq_len,), dtype='int32', name='mask_ids')
    reshaped_mask_ids = tf.reshape(mask_ids, (batch_size * max_seq_num, max_seq_len))

    # provide a custom token_type/segment id as a layer input
    bert_embedd = l_bert([reshaped_input_ids, reshaped_token_type_ids], mask=reshaped_mask_ids)  # [batch_size*max_seq_num, max_seq_len, hidden_size]
    model = tf.keras.models.Model(inputs=[input_ids, token_type_ids, mask_ids], outputs=bert_embedd)
    model.build(input_shape=[(batch_size, max_seq_num, max_seq_len), 
                                                  (batch_size, max_seq_num, max_seq_len),
                                                  (batch_size, max_seq_num, max_seq_len)])
    bert.load_bert_weights(l_bert, os.path.join(model_dir, "bert_model.ckpt"))  # should be called after model.build()
    model.summary()
    tf.keras.utils.plot_model(model, show_shapes=True)
    learning_rate = 1e-2
    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=learning_rate), 
                              loss=tf.keras.losses.MeanSquaredError(), metrics=['mse'])
    return model

I can build the model succeed.我可以成功构建 model。 But when I fed data to the model:但是当我将数据输入 model 时:

model = build_model(path, 16, 16, 16)
x_input = np.random.randint(0, 10000, size=[16, 16, 16])
x_token_type = [[[i] * 16 for i in range(16)] for _ in range(16)]
x_mask = np.ones(shape=[16, 16, 16])
y_predict = model(x_input, x_token_type, x_mask)

the error appears:出现错误:

ValueError: Layer model expects 2 input(s), but it received 1 input tensors. Inputs received: ...

In the last instruction:在最后一条指令中:

"y_predict = model(x_input, x_token_type, x_mask)" “y_predict = 模型(x_input,x_token_type,x_mask)”

2 inputs have been expected, but you provide one input.预期有 2 个输入,但您提供了 1 个输入。

I think you have 3 inputs x_input, x_token_type, and x_mask.我认为您有 3 个输入 x_input、x_token_type 和 x_mask。 If you want to train your model try this:如果你想训练你的 model 试试这个:

model.fit([x_input, x_token_type, x_mask]) model.fit([x_input, x_token_type, x_mask])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问 Huggingface 的预训练 BERT model 的特定层? - How to access a particular layer of Huggingface's pre-trained BERT model? 预训练的 BERT model 的权重未初始化 - Weights of pre-trained BERT model not initialized 如何使用预训练的 BERT 模型进行下一句标注? - How to use pre-trained BERT model for next sentence labeling? 在 Keras 中嵌入预训练模型的问题 - Issue with embedding pre-trained model in Keras 如何在 MLM 任务上训练 Tensorflow 的预训练 BERT? (仅在 Tensorflow 中使用预训练的 model) - How to train Tensorflow's pre trained BERT on MLM task? ( Use pre-trained model only in Tensorflow) 如何从预训练的 TensorFlow model 中删除层? - How to remove layer from pre-trained TensorFlow model? 如何在 Keras 中更改预训练 CNN model 中层的 output? - How to change output of a layer in a pre-trained CNN model in Keras? 如何在张量流预训练模型中将输入输入到一层? - How to feed input into one layer in a tensorflow pre-trained model? 使用预先训练的模型训练模型 - Training a model using a pre-trained model RuntimeError,在 IA tryna 上工作使用预训练的 BERT 模型 - RuntimeError, working on IA tryna use a pre-trained BERT model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM