简体   繁体   English

使用 tensorflow 的 Huggingface 变换器将两个文件保存为模型权重

[英]huggingface transformer with tensorflow saves two files as model weights

This is how I build the model for classification task:这就是我为分类任务构建模型的方式:

    def bert_for_classification(transformer_model_name, max_sequence_length, num_labels):
        config = ElectraConfig.from_pretrained(
            transformer_model_name,
            num_labels=num_labels,
            output_hidden_states=False,
            output_attentions=False
        )
        model = TFElectraForSequenceClassification.from_pretrained(transformer_model_name, config=config)
        # This is the input for the tokens themselves(words from the dataset after encoding):
        input_ids = tf.keras.layers.Input(shape=(max_sequence_length,), dtype=tf.int32, name='input_ids')

        # attention_mask - is a binary mask which tells BERT which tokens to attend and which not to attend.
        # Encoder will add the 0 tokens to the some sequence which smaller than MAX_SEQUENCE_LENGTH,
        # and attention_mask, in this case, tells BERT where is the token from the original data and where is 0 pad
        # token:
        attention_mask = tf.keras.layers.Input((max_sequence_length,), dtype=tf.int32, name='attention_mask')

        # Use previous inputs as BERT inputs:
        output = model([input_ids, attention_mask])[0]
        output = tf.keras.layers.Dense(num_labels, activation='softmax')(output)
        model = tf.keras.models.Model(inputs=[input_ids, attention_mask], outputs=output)

        model.compile(loss=keras.losses.CategoricalCrossentropy(),
                      optimizer=keras.optimizers.Adam(3e-05, epsilon=1e-08),
                      metrics=['accuracy'])

        return model

After I trained this model I save it using model.save_weights('model.hd5') But it turns out there are two files that are saved: model.hd5.index and model.hd5.data-00000-of-00001在我训练了这个模型后,我使用model.save_weights('model.hd5')保存它但结果是保存了两个文件: model.hd5.indexmodel.hd5.data-00000-of-00001

How should I load this model from the disk?我应该如何从磁盘加载这个模型?

You have 2 possibilities to save a model, either in keras h5 format or in tensorflow SavedModel format.您有两种保存模型的可能性,可以是 keras h5格式,也可以是 tensorflow SavedModel格式。

You can determine the format by passing the save_format argument and set it to either "h5" or "tf" .您可以通过传递save_format参数并将其设置为"h5""tf"来确定格式。 If you don't specify this argument, the format will be determined by the name you have passed.如果您不指定此参数,则格式将由您传递的名称确定。 If the name has the .h5 suffix, it will be saved in keras, otherwise in the SavedModel format.如果名称带有.h5后缀,则会保存在 keras 中,否则保存为SavedModel格式。

Anyway, since you have specified suffix hd5 instead of h5 , it will be saved in SavedModel format.无论如何,由于您指定了后缀hd5而不是h5 ,它将以 SavedModel 格式保存。

You can simply load them in the same way you have saved them.您可以像保存它们一样简单地加载它们。

model.save_weights("model.h5")   #h5 format
model.load_weights("model.h5")
#or
model.save_weights("mymodel")    #SavedModel format
model.load_weights("mymodel")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM