transformers icon indicating copy to clipboard operation
transformers copied to clipboard

How to change config parameters when loading the model with `from_pretrained`

Open danyaljj opened this issue 7 years ago • 8 comments

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?

danyaljj avatar Feb 19 '19 18:02 danyaljj

I have the same question. I need to change the hidden_dropout_prob. How is that possible?

MeenaAlfons avatar May 09 '19 15:05 MeenaAlfons

Same question. Any best practice?

Opdoop avatar Apr 03 '21 13:04 Opdoop

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)

Opdoop avatar Apr 03 '21 14:04 Opdoop

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 avatar Jul 30 '21 11:07 kaankork

@kaankork

  • For override: Yes. Your understand is correct.
  • For num_labels: It's been a long time. I didn't sure. But you should see num_labels by the last layer shape in print(model) .

Opdoop avatar Aug 03 '21 07:08 Opdoop

Any update on this?

pratikchhapolika avatar Aug 04 '21 08:08 pratikchhapolika

I need to pass some values to config as well, will save me a lot of time....

cs-ark avatar Nov 23 '21 11:11 cs-ark

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}).

guoyang9 avatar Jul 08 '22 03:07 guoyang9