【问题标题】:Disable Tensorflow/Numpy Deprecation Warning Messages禁用 TensorFlow/Numpy 弃用警告消息
【发布时间】:2019-09-19 01:27:17
【问题描述】:

运行使用 tensorflow v1.14.0 的 Python 3.7.4 应用会导致出现大量弃用警告。以下代码清除了大部分内容。

try:
    from tensorflow.python.util import module_wrapper as deprecation
except ImportError:
    from tensorflow.python.util import deprecation_wrapper as deprecation
deprecation._PER_MODULE_WARNING_LIMIT = 0

但是,没有删除任何警告。将tensorflow 更新到 v2.x 目前不是一个选项。

如何删除这些消息?

警告信息:

/anaconda3/envs/ml/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/anaconda3/envs/ml/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/anaconda3/envs/ml/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/anaconda3/envs/ml/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/anaconda3/envs/ml/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/anaconda3/envs/ml/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
WARNING:tensorflow:From /Users/x/foo/dnnlib/tflib/tfutil.py:34: The name tf.Dimension is deprecated. Please use tf.compat.v1.Dimension instead.

WARNING:tensorflow:From /Users/x/foo/dnnlib/tflib/tfutil.py:74: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.

WARNING:tensorflow:From /Users/x/foo/dnnlib/tflib/tfutil.py:128: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

【问题讨论】:

  • 设置保守日志级别 (ERROR) 是一个选项吗?它将禁止所有警告,而不仅仅是弃用警告
  • @Marat 是的,我们如何设置?
  • @Marat tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR) 似乎不起作用
  • 这就是我要建议的。还有原生的 Python 日志记录,你能在前面加上 logging.basicConfig(level=logging.ERROR) 吗?
  • 试过 import logging, logging.basicConfig(level=logging.ERROR) 但没有运气>_

标签: python python-3.x tensorflow


【解决方案1】:

您可以通过将以下代码放在脚本开头 (source) 来忽略所有“FutureWarning”警告:

from warnings import simplefilter 
simplefilter(action='ignore', category=FutureWarning)

请注意,您必须将这些行放在任何其他导入之前,因为其中一些可能已经自行导入依赖项。

【讨论】:

    【解决方案2】:

    概览

    将我带到这里的问题实际上是来自tensorboard 中的代码的最近一个 numpy 弃用警告,但我希望这些原则也适用于 OP 中的问题。

    方法

    在任何tensorflow 导入之前添加以下内容将抑制由that specific module 引起的开发人员指导的DeprecationWarnings(默认情况下会忽略弃用警告,但它们可能会被打开,例如,QA 工具;请改用FutureWarnings如果弃用警告是针对最终用户的):

    from warnings import filterwarnings  # noqa
    warnings.filterwarnings(action='ignore',
                            category=DeprecationWarning,
                            module='tensorflow')  # noqa                      
    

    笔记

    • 如果您正在开发多文件包,您可能会考虑将过滤器代码放在您的 __init__.py 文件的顶部(在任何 __future__ 导入之后)。
    • 自动格式化程序(如blackautopep8)可能会在有问题的导入之后 推送这些行。由于我在 VSCode 中使用了 autopep8-on-save 触发器,因此我还需要将 # noqa 添加到导入和过滤行中,以便它们不会被移动(参见 this post)。为避免需要为以下导入消除 linting 错误,另一种选择是将警告过滤行移至单独的 python 文件,然后从__init__.py 导入(请参阅this post)。
    • (当然,对于我的tensorboard 问题,我使用了module='tensorboard'。)

    【讨论】:

      猜你喜欢
      • 2017-07-16
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 2012-10-07
      • 2015-06-27
      • 2015-09-15
      • 1970-01-01
      • 2023-04-03
      相关资源
      最近更新 更多