【问题标题】:Hugging Face Trainer: Error in the model initHugging Face Trainer:模型初始化错误
【发布时间】:2021-06-23 23:26:13
【问题描述】:

我收到以下错误:

You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
Traceback (most recent call last):
  File "./run_hyperparameter_search.py", line 74, in <module>
    trainer = Trainer(
  File "/ext3/miniconda3/lib/python3.8/site-packages/transformers/trainer.py", line 273, in __init__
    model = self.call_model_init()
  File "/ext3/miniconda3/lib/python3.8/site-packages/transformers/trainer.py", line 737, in call_model_init
    raise RuntimeError("model_init should have 0 or 1 argument.")
RuntimeError: model_init should have 0 or 1 argument.
~                                                                       

这是我在拥抱面部训练器中所说的:

#Initialising the model
trainer = Trainer(
    args = training_args,
    tokenizer = tokenizer,
    train_dataset = train_data,
    eval_dataset = val_data,
    # maybe there is a () in the init, but not in compute metrics for sure. Will test
    model_init = finetuning_utils.model_init(),
    compute_metrics = finetuning_utils.compute_metrics,
)

问题显然出在 model_init 中。

finetuning_utils.model_init() 包含以下内容:

def model_init():
    """Returns an initialized model for use in a Hugging Face Trainer."""
    ## TODO: Return a pretrained RoBERTa model for sequence classification.
    ## See https://huggingface.co/transformers/model_doc/roberta.html#robertaforsequenceclassification.
    model = RobertaForSequenceClassification.from_pretrained("roberta-base")
    #model = model.to('cuda')
    return model

请帮助解决错误。

【问题讨论】:

  • 来自huggingface.co/transformers/_modules/transformers/trainer.html 看起来 model_init 需要一个可调用对象。您是否尝试过删除括号,即 finetuning_utils.model_init
  • 或者也许使用model = RobertaForSequenceClassification.from_pretrained("roberta-base")而不是model_init
  • @ML_Engine 谢谢!
  • 我已将其添加为答案,如果有帮助将不胜感激:-)

标签: nlp huggingface-transformers


【解决方案1】:

Huggingface trainer docs 看来,model_init 需要一个可调用对象。因此,与其实例化参数,不如采用可调用对象本身,即不带括号:

model_init = finetuning_utils.model_init

或者,您可以删除model_init 并使用model 参数,其效果与finetuning_utils.model_init 中包含的代码相同,如下所示:

model = RobertaForSequenceClassification.from_pretrained("roberta-base")

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 2020-10-19
    • 2022-01-15
    • 2021-07-15
    • 2021-10-22
    • 2021-11-19
    • 2022-08-07
    • 2021-07-12
    • 2021-03-10
    相关资源
    最近更新 更多