KeyError: "Unable to open object (object 'bias:0' doesn't exist)"
Author: yuimo
Pull request: no

hi, i have create a new class named ClsNerModel inherited from TFBertPreTrainedModel
i have trained it successfully and save the model using model.save_pretrained to some dir

however, when i loaded from that dir using  ClsNerModel.from_pretrained, it fails and report some error below.
sorry to report this, and any advice is appreciated.

  File "/home/yuanhao/anaconda3/envs/tf2/lib/python3.7/site-packages/transformers/modeling_tf_utils.py", line 410, in from_pretrained
    model.load_weights(resolved_archive_file, by_name=True)
  File "/home/yuanhao/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py", line 250, in load_weights
    return super(Model, self).load_weights(filepath, by_name, skip_mismatch)
  File "/home/yuanhao/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py", line 1264, in load_weights
    f, self.layers, skip_mismatch=skip_mismatch)
  File "/home/yuanhao/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 753, in load_weights_from_hdf5_group_by_name
    weight_values = [np.asarray(g[weight_name]) for weight_name in weight_names]
  File "/home/yuanhao/anaconda3/envs/tf2/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 753, in <listcomp>
    weight_values = [np.asarray(g[weight_name]) for weight_name in weight_names]
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "/home/yuanhao/anaconda3/envs/tf2/lib/python3.7/site-packages/h5py/_hl/group.py", line 264, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to open object (object 'bias:0' doesn't exist)"

my code is something like this:
##############################################################################
class ClsNerModel(TFBertPreTrainedModel):
    def __init__(self, config, *inputs, cls_num_labels:int=2, **kwargs):
        super().__init__(config, *inputs, **kwargs)
        self.num_labels = config.num_labels
        self.cls_num_labels = cls_num_labels

        self.bert = TFBertMainLayer(config, name="bert")
        self.dropout = tf.keras.layers.Dropout(config.hidden_dropout_prob)
        self.classifier = tf.keras.layers.Dense(
            config.num_labels, kernel_initializer=get_initializer(config.initializer_range), name="classifier"
        )
        self.cls_dropout = tf.keras.layers.Dropout(config.hidden_dropout_prob)
        self.cls_classifier = tf.keras.layers.Dense(
            self.cls_num_labels, kernel_initializer=get_initializer(config.initializer_range), name="cls_classifier"
        )

    def call(self, inputs, **kwargs):
        outputs = self.bert(inputs, **kwargs)

        sequence_output = outputs[0] # (b, t, d)
        pool_output = outputs[1] # (b, d) only for cls token

        sequence_output = self.dropout(sequence_output, training=kwargs.get("training", False))
        token_logits = self.classifier(sequence_output)
        pool_output = self.cls_dropout(pool_output, training=kwargs.get("training", False))
        cls_logits = self.cls_classifier(pool_output)

        outputs = (token_logits, cls_logits) + outputs[2:]  # add hidden states and attention if they are here

        return outputs  # scores, (hidden_states), (attentions)

##############################################################################
    config = AutoConfig.from_pretrained(
        model_args.config_name if model_args.config_name else model_args.model_name_or_path,
        num_labels=num_labels,
        id2label=label_map,
        label2id={label: i for i, label in enumerate(label_map)},
        cache_dir=model_args.cache_dir,
    )
    model = ClsNerModel.from_pretrained(
        model_path,
        from_pt=bool(".bin" in model_args.model_name_or_path),
        output_loading_info=True,
        config=config,
        cls_num_labels=cls_num_labels,
        cache_dir=model_args.cache_dir,
    )

Comments:
hi, i tried again, and write the class of ClsNerModel to model_tf_bert.py, and then reinstall transformers again. it works!!
so, if i create my class outside the lib of transformers, what else should i do to make it work, and avoid the mistakes above
thanks a lot!