Skip to content

Trouble when used on a fine-tuned on my own dataset: TypeError: argument after ** must be a mapping, not Tensor #149

@Novampe

Description

@Novampe

I fine-tuned a Huggingface model (hfl/chinese-roberta-wwm-ext) to do sequence classification task on my own dataset. The fine-tuning process followed the official getting-started guide (https://transformers.run/) and the fine-tuned model ran successfully completed the sequence classification task. I am now seeking to do interpretable analysis with this package but there are some problems. I would greatly appreciate any help or insights that anyone could provide regarding this issue.

Here is part of my code. I firstly instantiated a model and loaded the weights from the saved fine-tuned model. I also loaded the pretrained tokenizer:

class BertForClas(BertPreTrainedModel):
    def __init__(self, config):
        super().__init__(config)
        self.bert = BertModel(config, add_pooling_layer=False)
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.classifier = nn.Linear(768, 2)
        self.post_init()
    
    def forward(self, x):
        bert_output = self.bert(**x)
        cls_vectors = bert_output.last_hidden_state[:, 0, :]
        cls_vectors = self.dropout(cls_vectors)
        logits = self.classifier(cls_vectors)
        return logits

model_0 = BertForClas(config).to(device)
model_0.load_state_dict(torch.load("/path_of_my_finetuned_model.bin"))

tokenizer = BertTokenizerFast.from_pretrained("/path_of_the_downloaded_original_model")

Then I made an explainer and tried:

cls_explainer = SequenceClassificationExplainer(model_0, tokenizer)
text = 'I love you, I like you'
word_attributions = cls_explainer(text)

Here I got the error:


  File /this_code.py:186 in forward
    bert_output = self.bert(**x)

TypeError: BertModel(
  (embeddings): BertEmbeddings(
    (word_embeddings): Embedding(21128, 768, padding_idx=0)
    (position_embeddings): Embedding(512, 768)
    (token_type_embeddings): Embedding(2, 768)
    (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
    (dropout): Dropout(p=0.1, inplace=False)
  )
  (encoder): BertEncoder(
    (layer): ModuleList(
      (0-11): 12 x BertLayer(
        (attention): BertAttention(
          (self): BertSdpaSelfAttention(
            (query): Linear(in_features=768, out_features=768, bias=True)
            (key): Linear(in_features=768, out_features=768, bias=True)
            (value): Linear(in_features=768, out_features=768, bias=True)
            (dropout): Dropout(p=0.1, inplace=False)
          )
          (output): BertSelfOutput(
            (dense): Linear(in_features=768, out_features=768, bias=True)
            (LayerNorm): LayerNorm((768,), eps=1e-12, elementwise_affine=True)
            (dropout): Dropout(p=0.1, inplace=False)
          )
        )
        (intermediate): BertIntermediate(
          (dense): Linear(in_features=768, out_features=3072, bias=True)
          (intermediate_act_fn): GELUActivation()
        )
        (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.1, inplace=False)
        )
      )
    )
  )
) argument after ** must be a mapping, not Tensor

I guess the inputed text was not converted by the tokenizer into the mapping format ({input_ids, attention_mask, token_type_ids}), but I have no idea how to fix this. Any assistance or guidance on this matter would be greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions