我使用的是带有BertForSequenceClassification.from_pretrained(“bert-base-uncased”)模型的huggingface训练器。
简化后,它看起来像这样:
model = BertForSequenceClassification.from_pretrained("bert-base-uncased")
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
training_args = TrainingArguments(
output_dir="bert_results",
num_train_epochs=3,
per_device_train_batch_size=8,
per_device_eval_batch_size=32,
warmup_steps=500,
weight_decay=0.01,
logging_dir="bert_results/logs",
logging_steps=10
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
compute_metrics=compute_metrics
)
日志包含每10步的损失,但我似乎找不到训练精度。有人知道如何获得精度吗,例如,通过改变日志的详细程度?我似乎找不到任何关于它的在线。
谢谢,巴斯警官
5条答案
按热度按时间ruarlubt1#
您可以加载accuracy metric并使其与您的
compute_metrics
函数一起工作。例如,它如下所示:这个
compute_metrics
函数的例子是基于Hugging Face's text classification tutorial的,它在我的测试中工作正常。ztmd8pv52#
我遇到了同样的问题,我通过添加一个自定义回调函数来解决这个问题,该函数在每次回调函数结束时使用train_dataset调用evaluate()方法。
这给出了如下所示的列车指标:
另一种获得训练精度的方法是扩展基本Trainer类并覆盖compute_loss()方法,如下所示:
然后使用CustomTrainer代替培训师,如下所示:
a1o7rhls3#
您可以通过evaluation_strategy training参数来确定培训师的评估间隔,该参数目前接受3个值:
“否”:培训期间不进行评估。
“steps”:每个eval_steps执行一次评估(并记录)。
“epoch”:在每个epoch结束时进行评价。
5jvtdoz24#
函数来返回所需的指标。下面是我编写的一个函数,它返回指标列表(越多越好,对吗?):
此外,如果需要计算每个时期的指标,则需要在训练参数中定义:
最后一步是将其添加到培训师:
57hvy0tb5#
现在说这个有点晚了,但是为了方便那些没有成功回答前面问题的人,我发现了另一个方法,那就是覆盖Transformers库中Trainer类的evaluate方法。这个方法的思想是计算训练集的求值,并将它们添加到日志中。确保在返回时将eval和train字典合并为一个字典。
按如下所示扩展培训器类并覆盖:
记住使用你的定制扩展类训练你的模型,trainer = CTCTrainer(args)and trainer.train().上面的代码将在你的日志历史中产生以下输出。