使用
Trainer()
训练 Transformer 模型时,文档显示以下用法:
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased", num_labels=5)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=small_train_dataset,
eval_dataset=small_eval_dataset,
compute_metrics=compute_metrics,
)
如果您选中
model.training
标志,则默认情况下它设置为 false
,但是 Trainer
有一个逻辑调用 model.train()
将其设置为 True
,这是有道理的。
使用此微调模型进行推理时,您可以利用
transformers.pipeline
,它接受模型对象作为参数。但是,pipeline
没有检查模型是否处于训练模式的逻辑。我没有在源代码中找到它,并且在文档中的任何地方都没有看到它。当我使用 pipeline
进行预测时,结果不是确定性的,这是模型未处于评估模式的另一个指标。
generator = pipeline(
"some_task",
model=model,
tokenizer=tokenizer,
aggregation_strategy=aggregation_strategy,
ignore_labels=[],
)
generator("Example") # returns score X
generator("Example") # returns score Y
# but if model.eval() is used before creating pipeline object
generator("Example") # returns score X
generator("Example") # returns score X
我应该在
model.eval()
中使用模型之前调用 pipeline
还是应该 pipeline
自己处理它,但由于某种原因它没有处理?
pipeline
在进行前向传递之前会调用 torch.no_grad 。
https://github.com/huggingface/transformers/blob/v4.39.3/src/transformers/pipelines/base.py#L1103
def get_inference_context(self):
return torch.no_grad
def forward(self, model_inputs, **forward_params):
with self.device_placement():
if self.framework == "tf":
model_inputs["training"] = False
model_outputs = self._forward(model_inputs, **forward_params)
elif self.framework == "pt":
inference_context = self.get_inference_context()
with inference_context():
model_inputs = self._ensure_tensor_on_device(model_inputs, device=self.device)
model_outputs = self._forward(model_inputs, **forward_params)
model_outputs = self._ensure_tensor_on_device(model_outputs, device=torch.device("cpu"))
else:
raise ValueError(f"Framework {self.framework} is not supported")
return model_outputs