kpe / bert-for-tf2

A Keras TensorFlow 2.0 implementation of BERT, ALBERT and adapter-BERT.
https://github.com/kpe/bert-for-tf2
MIT License
802 stars 193 forks source link

ValueError: Found unexpected keys that do not correspond to any Model output #84

Open callzhang opened 3 years ago

callzhang commented 3 years ago

System information

Describe the bug

When using tfa.losses.SigmoidFocalCrossEntropy(from_logits=True, reduction='auto') the model raised ValueError: ValueError: Found unexpected keys that do not correspond to any Model output: dict_keys(['class_name', 'config']). Expected: ['dense']

Code to reproduce the issue

...
loss = tfa.losses.SigmoidFocalCrossEntropy(from_logits=True, reduction='auto')
l_bert = bert.BertModelLayer.from_params(bert_params, name='albert' if albert else "bert")
l_input_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype='int32', name="input_ids")
l_mask_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype='int32', name="mask_ids")
l_token_type_ids = tf.keras.layers.Input(shape=(max_seq_length,), dtype='int32', name="token_type_ids")
output = l_bert([l_input_ids, l_token_type_ids], mask=l_mask_ids)
logits = tf.keras.layers.Dense(num_labels, kernel_initializer=tf.keras.initializers.TruncatedNormal(bert_params.initializer_range))(output)
model = tf.keras.Model(inputs=[l_input_ids, l_mask_ids, l_token_type_ids], outputs=logits)
model.build(input_shape=(None, max_seq_length))
model.fit(...)
...

Other info / logs