How to change config parameters when loading the model with `from_pretrained`
I have created a model by extending PreTrainedBertModel:
class BertForMultiLabelClassification(PreTrainedBertModel):
def __init__(self, config, num_labels=2):
super(BertForMultiLabelClassification, self).__init__(config)
self.num_labels = num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.attention_probs_dropout_prob)
self.classifier = nn.Linear(config.hidden_size, num_labels)
self.apply(self.init_bert_weights)
# some code here ...
I am creating an instance of this model:
model = BertForMultiLabelClassification.from_pretrained(args.bert_model,
cache_dir=PYTORCH_PRETRAINED_BERT_CACHE / 'distributed_{}'.format(
args.local_rank),
num_labels=num_labels)
what is an effective way to modify parameters of the default config, when creating an instance of BertForMultiLabelClassification? (say, setting a different value for config.hidden_dropout_prob).
Any thoughts on what is an effective way to do this?
I have the same question. I need to change the hidden_dropout_prob. How is that possible?
Same question. Any best practice?
Oh, I find this code works:
hidden_droput_prob = 0.3
config = BertConfig.from_pretrained("bert-base-uncased", num_labels=num_labels, hidden_dropout_prob=hidden_dropout_prob)
model = BertForMultiLabelClassification.from_pretrained("bert-base-uncased", config=config)
And print(model) could see that drop_out changes:
(output): BertOutput(
(dense): Linear(in_features=3072, out_features=768, bias=True)
(LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
(dropout): Dropout(p=0.3, inplace=False)
Hi @Opdoop - this code overrides the config parameters of the pertained BERT model. Did I understand correctly?
Also, how can we ensure that the num_labels parameter is also updated? I don't see it in the output of print(model).
@kaankork
- For override: Yes. Your understand is correct.
- For
num_labels: It's been a long time. I didn't sure. But you should seenum_labelsby the last layer shape inprint(model).
Any update on this?
I need to pass some values to config as well, will save me a lot of time....
Just use the update method.
For example, if you want to change the number of hidden layers, simply use config.update({'num_hidden_layers': 1}).