Transformers documentation

Logging

You are viewing v4.39.2 version. A newer version v4.47.1 is available.
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Logging

🤗 Transformers拥有一个集中式的日志系统,因此您可以轻松设置库输出的日志详细程度。

当前库的默认日志详细程度为WARNING

要更改日志详细程度,只需使用其中一个直接的setter。例如,以下是如何将日志详细程度更改为INFO级别的方法:

import transformers

transformers.logging.set_verbosity_info()

您还可以使用环境变量TRANSFORMERS_VERBOSITY来覆盖默认的日志详细程度。您可以将其设置为以下级别之一:debuginfowarningerrorcritical。例如:

TRANSFORMERS_VERBOSITY=error ./myprogram.py

此外,通过将环境变量TRANSFORMERS_NO_ADVISORY_WARNINGS设置为true(如1),可以禁用一些warnings。这将禁用logger.warning_advice记录的任何警告。例如:

TRANSFORMERS_NO_ADVISORY_WARNINGS=1 ./myprogram.py

以下是如何在您自己的模块或脚本中使用与库相同的logger的示例:

from transformers.utils import logging

logging.set_verbosity_info()
logger = logging.get_logger("transformers")
logger.info("INFO")
logger.warning("WARN")

此日志模块的所有方法都在下面进行了记录,主要的方法包括 logging.get_verbosity() 用于获取logger当前输出日志详细程度的级别和 logging.set_verbosity() 用于将详细程度设置为您选择的级别。按照顺序(从最不详细到最详细),这些级别(及其相应的整数值)为:

  • transformers.logging.CRITICALtransformers.logging.FATAL(整数值,50):仅报告最关键的errors。
  • transformers.logging.ERROR(整数值,40):仅报告errors。
  • transformers.logging.WARNINGtransformers.logging.WARN(整数值,30):仅报告error和warnings。这是库使用的默认级别。
  • transformers.logging.INFO(整数值,20):报告error、warnings和基本信息。
  • transformers.logging.DEBUG(整数值,10):报告所有信息。

默认情况下,将在模型下载期间显示tqdm进度条。logging.disable_progress_bar()logging.enable_progress_bar() 可用于禁止或启用此行为。

logging vs warnings

Python有两个经常一起使用的日志系统:如上所述的logging,和对特定buckets中的警告进行进一步分类的warnings,例如,FutureWarning用于输出已经被弃用的功能或路径,DeprecationWarning用于指示即将被弃用的内容。

我们在transformers库中同时使用这两个系统。我们利用并调整了loggingcaptureWarning方法,以便通过上面的详细程度setters来管理这些警告消息。

对于库的开发人员,这意味着什么呢?我们应该遵循以下启发法则:

  • 库的开发人员和依赖于transformers的库应优先使用warnings
  • logging应该用于在日常项目中经常使用它的用户

以下是captureWarnings方法的参考。

transformers.utils.logging.captureWarnings

< >

( capture )

Calls the captureWarnings method from the logging library to enable management of the warnings emitted by the warnings library.

Read more about this method here: https://docs.python.org/3/library/logging.html#integration-with-the-warnings-module

All warnings will be logged through the py.warnings logger.

Careful: this method also adds a handler to this logger if it does not already have one, and updates the logging level of that logger to the library’s root logger.

Base setters

transformers.utils.logging.set_verbosity_error

< >

( )

Set the verbosity to the ERROR level.

transformers.utils.logging.set_verbosity_warning

< >

( )

Set the verbosity to the WARNING level.

transformers.utils.logging.set_verbosity_info

< >

( )

Set the verbosity to the INFO level.

transformers.utils.logging.set_verbosity_debug

< >

( )

Set the verbosity to the DEBUG level.

Other functions

transformers.utils.logging.get_verbosity

< >

( ) int

Returns

int

The logging level.

Return the current level for the 🤗 Transformers’s root logger as an int.

🤗 Transformers has following logging levels:

  • 50: transformers.logging.CRITICAL or transformers.logging.FATAL
  • 40: transformers.logging.ERROR
  • 30: transformers.logging.WARNING or transformers.logging.WARN
  • 20: transformers.logging.INFO
  • 10: transformers.logging.DEBUG

transformers.utils.logging.set_verbosity

< >

( verbosity: int )

Parameters

  • verbosity (int) — Logging level, e.g., one of:

    • transformers.logging.CRITICAL or transformers.logging.FATAL
    • transformers.logging.ERROR
    • transformers.logging.WARNING or transformers.logging.WARN
    • transformers.logging.INFO
    • transformers.logging.DEBUG

Set the verbosity level for the 🤗 Transformers’s root logger.

transformers.utils.logging.get_logger

< >

( name: Optional = None )

Return a logger with the specified name.

This function is not supposed to be directly accessed unless you are writing a custom transformers module.

transformers.utils.logging.enable_default_handler

< >

( )

Enable the default handler of the HuggingFace Transformers’s root logger.

transformers.utils.logging.disable_default_handler

< >

( )

Disable the default handler of the HuggingFace Transformers’s root logger.

transformers.utils.logging.enable_explicit_format

< >

( )

Enable explicit formatting for every HuggingFace Transformers’s logger. The explicit formatter is as follows:

    [LEVELNAME|FILENAME|LINE NUMBER] TIME >> MESSAGE
All handlers currently bound to the root logger are affected by this method.

transformers.utils.logging.reset_format

< >

( )

Resets the formatting for HuggingFace Transformers’s loggers.

All handlers currently bound to the root logger are affected by this method.

transformers.utils.logging.enable_progress_bar

< >

( )

Enable tqdm progress bar.

transformers.utils.logging.disable_progress_bar

< >

( )

Disable tqdm progress bar.