`NewForSequenceClassification` cannot load for 2 classes classification

#14
by nicozck - opened

Failed for 2 calsses

    config = NewConfig.from_pretrained(
        r"D:\download_models\gte-multilingual-base",
        problem_type="single_label_classification",
        label2id={"0": 0, "1": 1 },
        id2label={0: "0", 1: "1" },
        num_labels=2,
    )
    model = NewForSequenceClassification.from_pretrained(
        r"D:\download_models\gte-multilingual-base",
        config=config,
        ignore_mismatched_sizes=True,
    )
    print(model)

Runable for 3 classes

    config = NewConfig.from_pretrained(
        r"D:\download_models\gte-multilingual-base",
        problem_type="single_label_classification",
        label2id={"0": 0, "1": 1, "2": 2},
        id2label={0: "0", 1: "1", 2: "2"},
        num_labels=3,
    )
    model = NewForSequenceClassification.from_pretrained(
        r"D:\download_models\gte-multilingual-base",
        config=config,
        ignore_mismatched_sizes=True,
    )
    print(model)

error message:

    model = NewForSequenceClassification.from_pretrained(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "\.conda\envs\dev\Lib\site-packages\transformers\modeling_utils.py", line 3960, in from_pretrained
    ) = cls._load_pretrained_model(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "\.conda\envs\dev\Lib\site-packages\transformers\modeling_utils.py", line 4492, in _load_pretrained_model
    raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")
RuntimeError: Error(s) in loading state_dict for NewForSequenceClassification:
        size mismatch for classifier.bias: copying a param with shape torch.Size([1]) from checkpoint, the shape in current model is torch.Size([2]).
        You may consider adding `ignore_mismatched_sizes=True` in the model `from_pretrained` method.

Sign up or log in to comment