如何禁用导入TensorFlow时触发的NumPy (type,1) 传参FutureWarning
导入TensorFlow时触发NumPy兼容类FutureWarning的处理方案
问题触发逻辑
- 执行
import tensorflow as tf导入TensorFlow时,会批量输出NumPy相关的FutureWarning日志 - 根因为TensorFlow 1.14.0与NumPy 1.19.2版本不兼容:高版本NumPy修改了结构化dtype的定义语法,旧版TensorFlow仍使用已被废弃的写法,连续触发警告。
环境版本校验方法
可执行以下命令确认当前环境的组件版本,排查是否存在上述版本组合:
# 校验NumPy版本 python3 -c "import numpy as np;print(np.__version__)" # 预期输出示例:1.19.2 # 校验TensorFlow版本 python3 -c "import tensorflow as tf;print(tf.__version__)" # 预期输出示例:1.14.0
典型警告输出示例
/usr/local/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)]) /usr/local/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)]) /usr/local/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)]) /usr/local/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)]) /usr/local/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)]) /usr/local/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)]) /usr/local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: 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)]) /usr/local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: 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)]) /usr/local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: 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)]) /usr/local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: 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)]) /usr/local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: 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)]) /usr/local/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: 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)])
完全禁用该类警告的可行方案
注意:所有警告过滤逻辑必须写在import tensorflow as tf语句之前,否则导入过程中触发的警告无法被拦截。
- 全局过滤FutureWarning:直接拦截所有FutureWarning类日志,阻断相关输出
在代码最开头添加以下逻辑:import warnings warnings.filterwarnings('ignore', category=FutureWarning) import numpy as np - 精准匹配特定警告文本:仅屏蔽本次遇到的版本兼容类警告,不会误拦截其他有效提示
替换过滤规则为匹配警告的核心文本特征:import warnings warnings.filterwarnings('ignore', message='.*Passing (type, 1) or .1type. as a synonym of type is deprecated.*') - 根源解决版本兼容:降级NumPy到与TensorFlow 1.14.0适配的版本(如1.16.0),可执行
pip install numpy==1.16.0完成安装,从代码层面消除警告触发的条件,无需额外添加过滤规则。
内容的提问来源于stack exchange,提问作者mrgloom
相关产品推荐
相关产品推荐

