【问题标题】:How do I suppress a specific type of warning message?如何抑制特定类型的警告消息?
【发布时间】:2021-07-17 17:05:24
【问题描述】:

我知道使用 warnings.filterwarnings('ignore') 会抑制警告消息,但我想抑制特定类型的警告。

更具体地说,此警告消息是由我正在使用的第三方库发出的,日志警告消息如下所示:

04/23/2021 21:09:39 - WARNING - pytorch_transformers.tokenization_utils -  Token indices sequence length is \
longer than the specified maximum sequence length for this model (589 > 512). Running this sequence through the \
model will result in indexing errors

我不确定我应该如何通过使用正则表达式或其他方式来抑制警告消息。我不想进入实际脚本并更改内容,我想知道是否有办法找出抑制此警告消息的具体方法。谢谢。

【问题讨论】:

    标签: python warnings


    【解决方案1】:

    您可以在 filterwarnings 方法中将正则表达式分配给消息关键字。

    warnings.filterwarnings('ignore', message='model will result in indexing errors*')
    

    参考: https://docs.python.org/3/library/warnings.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 2010-10-03
      • 2012-02-09
      相关资源
      最近更新 更多