相关疑难解决方法(0)

使用拥抱面变压器模型作为第一层的保存和加载张量流模型的问题

您好,我在保存和加载张量流模型时遇到一些严重的问题,该模型是拥抱面部变压器+一些自定义层进行分类的组合。我正在使用最新的 Huggingface Transformers Tensorflow keras 版本。其想法是使用 distilbert 提取特征,然后通过 CNN 运行特征来进行分类和提取。为了获得正确的分类,我已经做好了一切工作。

问题在于训练后保存模型,然后再次加载模型。

我正在使用tensorflow keras和tensorflow版本2.2

以下是设计模型、训练模型、评估模型然后保存和加载模型的代码


    bert_config = DistilBertConfig(dropout=0.2, attention_dropout=0.2, output_hidden_states=False)
    bert_config.output_hidden_states = False
    transformer_model = TFDistilBertModel.from_pretrained(DISTIL_BERT, config=bert_config)

    input_ids_in = tf.keras.layers.Input(shape=(BERT_LENGTH,), name='input_token', dtype='int32')
    input_masks_in = tf.keras.layers.Input(shape=(BERT_LENGTH,), name='masked_token', dtype='int32')

    embedding_layer = transformer_model(input_ids_in, attention_mask=input_masks_in)[0]
    x = tf.keras.layers.Bidirectional(
        tf.keras.layers.LSTM(50, return_sequences=True, dropout=0.1,
                             recurrent_dropout=0, recurrent_activation="sigmoid",
                             unroll=False, use_bias=True, activation="tanh"))(embedding_layer)
    x = tf.keras.layers.GlobalMaxPool1D()(x)

    outputs = []
    # lots of code here to define the dense layers to generate the outputs
    # .....
    # .....

    model = Model(inputs=[input_ids_in, input_masks_in], outputs=outputs) …
Run Code Online (Sandbox Code Playgroud)

machine-learning keras tensorflow huggingface-transformers

3
推荐指数
1
解决办法
2111
查看次数