禁用Tensorflow / Numpy弃用警告消息

时间:2019-09-19 01:27:17

标签: python python-3.x tensorflow

运行使用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.

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

概览

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

方法

在任何 tensorflow 导入之前添加以下内容将抑制由 DeprecationWarnings 引起的开发者导向的 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'。)