Some questions about change the BertForSequenceClassification
Author: WenTingTseng
Pull request: no
Labels: name: wontfix

name: Core: Modeling

# 🐛 Bug

## Information

Model I am using Bert:

Language I am using the model on (Chinese ):

The problem arises when using BertForSequenceClassification:
I want to Concatenation pooled_output and word2vec. I change the code of BertForSequenceClassification like this and successfully trained the model.
I use **merge=torch.cat((pooled_output,Word2Vec),1)**
```
def __init__(self, config):
        super(BertForSequenceClassificationEZAI, self).__init__(config)
        self.num_labels = config.num_labels

        self.bert = BertModel(config)#載入預訓練BERT Model
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        # 簡單 linear 層
        self.classifier = nn.Linear(99072, self.config.num_labels)#config.hidden_size
        self.init_weights()

    def forward(self, input_ids=None, attention_mask=None, token_type_ids=None,
                position_ids=None, head_mask=None, inputs_embeds=None, labels=None):
        #BERT 輸入就是 tokens, segments, masks
   
        outputs = self.bert(input_ids,
                            attention_mask=attention_mask,
                            token_type_ids=token_type_ids,
                            position_ids=position_ids,
                            head_mask=head_mask,
                            inputs_embeds=inputs_embeds)
    
        #線性分類器將 dropout 後的 BERT repr. 轉成類別 logits
        pooled_output = outputs[1]
         ####在這裡加上word embedding資訊######
        model = word2vec.load('/share/nas165/Wendy/WordbreakCKIP/corpusWord2Vec.bin')
        Word2Vec=torch.from_numpy(model.vectors.flatten()).cuda().float().expand(len(pooled_output),98304)
        print(Word2Vec.size())
        print(pooled_output.size())
        merge=torch.cat((pooled_output,Word2Vec),1)
       
        pooled_output = self.dropout(merge)
       # pooled_output = self.dropout(pooled_output)
        # print(pooled_output.size())
        logits = self.classifier(pooled_output)
      
        outputs = (logits,) + outputs[1:]  # add hidden states and attention if they are here
        # 輸入有 labels 的話直接計算 Cross Entropy 回傳，方便！
        if labels is not None:
            if self.num_labels == 1:
                #  We are doing regression
                loss_fct = MSELoss()
                loss = loss_fct(logits.view(-1), labels.view(-1))
            else:
                loss_fct = CrossEntropyLoss()      
                loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
            outputs = (loss,) + outputs
    
        return outputs  # (loss), logits, (hidden_states), (attentions)
```

But when I try to predict and import the model like this
```py
bert_config, bert_class, bert_tokenizer = (BertConfig, BertForSequenceClassification, BertTokenizer)
config = bert_config.from_pretrained('/share/nas165/Wendy/BERTYiChen2.0/trained_model/3371筆_word2Vec/config.json')
model = bert_class.from_pretrained('/share/nas165/Wendy/BERTYiChen2.0/trained_model/3371筆_word2Vec/pytorch_model.bin', from_tf=bool('.ckpt' in 'bert-base-chinese'),config=config)
```

It always appears BUG RuntimeError: Error(s) in loading state_dict for BertForSequenceClassification:
        size mismatch for classifier.weight: copying a param with shape torch.Size([267, 99072]) from checkpoint, the shape in current model is torch.Size([267, 768]).

I do not know how to fix it.
Thanks a lot for your help.



Comments:
Please don't post screenshots. Post your code instead. Use code blocks to do that: https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks

sorry, I changed


Could you give us the content of the `/share/nas165/Wendy/BERTYiChen2.0/trained_model/3371筆_word2Vec/config.json` file? I suspect it might set `vocab_size` to 768.

ok, the config file like this , but  vocab_size is 21128
```
{
  "architectures": [
    "BertForMaskedLM"
  ],
  "attention_probs_dropout_prob": 0.1,
  "bos_token_id": 0,
  "directionality": "bidi",
  "do_sample": false,
  "eos_token_ids": 0,
  "finetuning_task": null,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 768,
  "id2label": {
    "0": "LABEL_0",
    "1": "LABEL_1"
  },
  "initializer_range": 0.02,
  "intermediate_size": 3072,
  "is_decoder": false,
  "label2id": {
    "LABEL_0": 0,
    "LABEL_1": 1
  },
  "layer_norm_eps": 1e-12,
  "length_penalty": 1.0,
  "max_length": 20,
  "max_position_embeddings": 512,
  "num_attention_heads": 12,
  "num_beams": 1,
  "num_hidden_layers": 12,
  "num_labels": 267,
  "num_return_sequences": 1,
  "output_attentions": false,
  "output_hidden_states": false,
  "output_past": true,
  "pad_token_id": 0,
  "pooler_fc_size": 768,
  "pooler_num_attention_heads": 12,
  "pooler_num_fc_layers": 3,
  "pooler_size_per_head": 128,
  "pooler_type": "first_token_transform",
  "pruned_heads": {},
  "repetition_penalty": 1.0,
  "temperature": 1.0,
  "top_k": 50,
  "top_p": 1.0,
  "torchscript": false,
  "type_vocab_size": 2,
  "use_bfloat16": false,
  "vocab_size": 21128
}
```


This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.