【问题标题】:更改预训练的拥抱脸模型的最后一层
【发布时间】:2022-01-23 16:22:10
【问题描述】:

我想重新微调变压器模型,但在尝试训练模型时出现未知错误。 我无法在加载模型时更改“num_labels”。 所以,我尝试手动更改它

model_name = "mrm8488/flaubert-small-finetuned-movie-review-sentiment-analysis"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name).to('cuda')

num_labels = 3
model.sequence_summary.summary = torch.nn.Linear(in_features=model.sequence_summary.summary.in_features, out_features=num_labels, bias=True)




trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_train['train'],
    eval_dataset=tokenized_test['train'],
    tokenizer=tokenizer,
    compute_metrics=compute_metrics,
    #data_collator=data_collator,
)

trainer.train()

错误

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-93-8139f38c5ec6> in <module>()
     20 )
     21 
---> 22 trainer.train()

7 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
   2844     if size_average is not None or reduce is not None:
   2845         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2846     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
   2847 
   2848 

ValueError: Expected input batch_size (24) to match target batch_size (16).

【问题讨论】:

    标签: python pytorch torch huggingface-transformers


    【解决方案1】:

    所以,有一个解决方案 加载模型时只需添加ignore_mismatched_sizes=True

    model = AutoModelForSequenceClassification.from_pretrained(model_name,num_labels=3, ignore_mismatched_sizes=True).to('cuda')
    

    【讨论】:

      【解决方案2】:

      这是另一个你可以尝试的例子

      import torch
      import torch.nn as nn
      
      class CustomHFClassifier(nn.Module):
          def __init__(self, num_class):
             super(CustomHFClassifier, self).__init__()
             self.model = AutoModel.from_pretrained('bert-base-cased')
      
             self.dropout = nn.Dropout(0.25)
             self.classifier = nn.Linear(768, n_classes)
      
          def forward(self, input_ids, attention_mask, token_type_ids):
             embeddings = self.model(
                  input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids
              )[1]
              
              embeddings = self.dropout(embeddings)
              embeddings = self.classifier(embeddings)
              return embeddings
      
      model = CustomHFClassifier(10)
      

      【讨论】:

      • 此选项要求您有基础手电筒微调
      猜你喜欢
      • 2021-10-07
      • 2021-12-21
      • 2021-08-16
      • 2021-12-17
      • 1970-01-01
      • 2021-08-10
      • 2021-12-06
      • 2023-01-31
      • 1970-01-01
      相关资源
      最近更新 更多