Finetuning BERT on custom data
Asked Answered
W

1

5

I want to train a 21 class text classification model using Bert. But I have very little training data, so a downloaded a similar dataset with 5 classes with 2 million samples.t And finetuned downloaded data with uncased pretrained model provided by bert. And got about 98% validation accuracy. Now, I want to use this model as pretrained model for my small custom data. But I am getting shape mismatch with tensor output_bias from checkpoint reader error as the check-point model has 5 classes and my custom data has 21 classes.

NFO:tensorflow:Calling model_fn.
INFO:tensorflow:Running train on CPU
INFO:tensorflow:*** Features ***
INFO:tensorflow:  name = input_ids, shape = (32, 128)
INFO:tensorflow:  name = input_mask, shape = (32, 128)
INFO:tensorflow:  name = is_real_example, shape = (32,)
INFO:tensorflow:  name = label_ids, shape = (32, 21)
INFO:tensorflow:  name = segment_ids, shape = (32, 128)
Tensor("IteratorGetNext:3", shape=(32, 21), dtype=int32)
WARNING:tensorflow:From /home/user/Spine_NLP/bert/modeling.py:358: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.
Instructions for updating:
Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.
WARNING:tensorflow:From /home/user/Spine_NLP/bert/modeling.py:671: dense (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.
Instructions for updating:
Use keras.layers.dense instead.
INFO:tensorflow:num_labels:21;logits:Tensor("loss/BiasAdd:0", shape=(32, 21), dtype=float32);labels:Tensor("loss/Cast:0", shape=(32, 21), dtype=float32)
INFO:tensorflow:Error recorded from training_loop: Shape of variable output_bias:0 ((21,)) doesn't match with shape of tensor output_bias ([5]) from checkpoint reader.
Well answered 4/5, 2019 at 5:40 Comment(0)
S
8

If you want to fine-tune your own model with the pre-trained model with 5 classes, you probably want to add one more layer to project the 5 classes into your 21 classes.

The error you see is due to the fact that you probably did not define a new set of "output_weights" and "output_bias" but reused them for your new labels with 21 classes. Below I "prefixed" the intermediate tensors for your new labels with "final_".

The code should be something like below:

# These are the logits for the 5 classes. Keep them as is.
logits = tf.matmul(output_layer, output_weights, transpose_b=True)
logits = tf.nn.bias_add(logits, output_bias)

# You want to create one more layer
final_output_weights = tf.get_variable(
  "final_output_weights", [21, 5],
  initializer=tf.truncated_normal_initializer(stddev=0.02))
final_output_bias = tf.get_variable(
  "final_output_bias", [21], initializer=tf.zeros_initializer())

final_logits = tf.matmul(logits, final_output_weights, transpose_b=True)
final_logits = tf.nn.bias_add(final_logits, final_output_bias)

# Below is for evaluating the classification.
final_probabilities = tf.nn.softmax(final_logits, axis=-1)
final_log_probs = tf.nn.log_softmax(final_logits, axis=-1)

# Note labels below should be the 21 class ids.
final_one_hot_labels = tf.one_hot(labels, depth=21, dtype=tf.float32)
final_per_example_loss = -tf.reduce_sum(final_one_hot_labels * final_log_probs, axis=-1)
final_loss = tf.reduce_mean(final_per_example_loss)
Sententious answered 7/5, 2019 at 0:59 Comment(1)
Should I not remove that last layer from pre-trained model, rather than adding one more? That's how transfer learning works, isn't it?Well

© 2022 - 2024 — McMap. All rights reserved.