“不推荐使用类型的同义词;在 numpy 的未来版本中,它将被理解为 (type, (1,)) / '(1,)type'。” TensorFlow 中的问题

Posted

技术标签:

【中文标题】“不推荐使用类型的同义词;在 numpy 的未来版本中,它将被理解为 (type, (1,)) / \'(1,)type\'。” TensorFlow 中的问题【英文标题】:"synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'." problem in TensorFlow“不推荐使用类型的同义词;在 numpy 的未来版本中,它将被理解为 (type, (1,)) / '(1,)type'。” TensorFlow 中的问题 【发布时间】:2019-12-14 07:48:21 【问题描述】:

我安装了 TensorFlow 1.10.1,但是当我尝试导入 TensorFlow 时,它说我需要 TensorFlow 版本 1.10.0。因此,我安装了它,现在我收到以下警告:

>>> import tensorflow
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])
C:\Users\PC\Anaconda3\envs\tut\lib\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)])

【问题讨论】:

张量流 10.1 ?我想你的意思是 1.10.1 【参考方案1】:

如果您使用的是 TF 2.0 快速解决方案是将您的 numpy 降级到 1.16.4。 (我使用 1.17 并收到相同的警告消息)。

1. pip uninstall numpy 
2. pip install numpy==1.16.4

见here(感谢 ymodak)

【讨论】:

适用于 tensorflow-gpu=1.14 这应该是公认的答案,提供具体版本。 这不是问题的答案。这是一种解决方法。 我在运行 CPU tensorflow==1.14.0 时也恢复为 numpy==1.16.4 以消除这些警告。【参考方案2】:

这只是一个警告,而不是错误。这是因为您当前的 numpy libray 版本与 tensorflow 版本不兼容。您需要降级 numpy 版本。

tensorflow 1.10.0 需要 numpy<=1.14.5,>=1.13.3,但您必须安装更高版本(此警告消息出现在最新的 numpy 版本 1.17.0 中)。

【讨论】:

我应该将 numpy 降级到哪个版本? 顺便说一句,您当前的 numpy 版本是什么? 我的numpy版本是1.17.0 降级到以上版本 我不会说绝对需要从 v1.17 降级。这里的一切看起来都像FutureWarning 而不是Error。如果像我一样,代码仅使用 FutureWarning 即可正常运行,那么坚持使用当前版本可能是可以接受的。【参考方案3】:

或者,也可以让警告静音:

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import tensorflow as tf

这里建议使用此方法:How to suppress Pandas Future warning ?

【讨论】:

【参考方案4】:
pip install "numpy<1.17"

恢复到 Numpy 版本 1.16.4

【讨论】:

1.16.6是最新版本【参考方案5】:

我的 Linux 笔记本电脑在 python3 v3.6 中使用 tensorflow 时遇到了同样的问题

其实你只需要修改2个文件中的一些行:

1

~/.local/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py

在你的情况下:

C:\Users\PC\Anaconda3\envs\tut\lib\site-packages\tensorflow\python\framework\dtypes.py

现在更改此代码:(第 516 行)

_np_qint8 = np.dtype([("qint8", np.int8, 1)])
_np_quint8 = np.dtype([("quint8", np.uint8, 1)])
_np_qint16 = np.dtype([("qint16", np.int16, 1)])
_np_quint16 = np.dtype([("quint16", np.uint16, 1)])
_np_qint32 = np.dtype([("qint32", np.int32, 1)])


# _np_bfloat16 is defined by a module import.

# Custom struct dtype for directly-fed ResourceHandles of supported type(s).
np_resource = np.dtype([("resource", np.ubyte, 1)])

通过此代码:

_np_qint8 = np.dtype([("qint8", np.int8, (1,))])
_np_quint8 = np.dtype([("quint8", np.uint8, (1,))])
_np_qint16 = np.dtype([("qint16", np.int16, (1,))])
_np_quint16 = np.dtype([("quint16", np.uint16, (1,))])
_np_qint32 = np.dtype([("qint32", np.int32, (1,))])

# _np_bfloat16 is defined by a module import.

# Custom struct dtype for directly-fed ResourceHandles of supported type(s).
np_resource = np.dtype([("resource", np.ubyte, (1,))])

你必须对这个文件做同样的事情:

2

~/.local/lib/python3.6/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py

在你的情况下:

C:\Users\PC\Anaconda3\envs\tut\lib\site-packages\tensorboard/compat/tensorflow_stub/dtypes.py

它会起作用的。

【讨论】:

【参考方案6】:

最新的 numpy 发行说明 (1.17) 有:

Future Changes

Shape-1 fields in dtypes won’t be collapsed to scalars in a future version
Currently, a field specified as [(name, dtype, 1)] or "1type" is interpreted 
as a scalar field (i.e., the same as [(name, dtype)] or [(name, dtype, ()]). 
This now raises a FutureWarning; in a future version, it will be interpreted 
as a shape-(1,) field, i.e. the same as [(name, dtype, (1,))] or "(1,)type" 
(consistently with [(name, dtype, n)] / "ntype" with n>1, which is already 
equivalent to [(name, dtype, (n,)] / "(n,)type").

https://docs.scipy.org/doc/numpy/release.html

你的表达方式是这样的:

In [123]: np.dtype([("qint8", np.int8, 1)])                                                                  
/usr/local/bin/ipython3:1: 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'.
  #!/usr/bin/python3
Out[123]: dtype([('qint8', 'i1')])

In [124]: np.dtype([("qint8", np.int8, (1,))])                                                               
Out[124]: dtype([('qint8', 'i1', (1,))])

In [125]: np.dtype([("qint8", np.int8)])                                                                     
Out[125]: dtype([('qint8', 'i1')])

In [126]: np.dtype([("qint8", np.int8, 2)])                                                                  
Out[126]: dtype([('qint8', 'i1', (2,))])

In [127]: np.__version__                                                                                     
Out[127]: '1.17.0'

【讨论】:

【参考方案7】:

您是否安装了多个版本的 numpy 或有与 tensorflow 不兼容的新 numpy 版本?

>pip show numpy

import tensorflow as tf
print(tf.__version__)
import numpy as np
print(np.__version__)

将 numpy 版本从 >1.17 降级到 1.16.4 将解决 Tensorflow 1.14.0 的问题

如何降级numpy?

pip uninstall numpy (till you uninstall all versions)
pip install numpy==1.16.4

之后您可以检查兼容性: 之后,请重新启动运行时并执行以下代码

import tensorflow as tf
print(tf.__version__)
import numpy as np
print(np.__version__)

【讨论】:

【参考方案8】:

这是因为您的 TensorFlow 版本与 numpy 不兼容。尝试重新安装 numpy 以前的版本。就我而言,我尝试了 1.16.4 : pip install numpy==1.16.4

但首先,您需要关闭所有正在运行的内核并使用以下命令卸载 numpy: pip 卸载 numpy

【讨论】:

以上是关于“不推荐使用类型的同义词;在 numpy 的未来版本中,它将被理解为 (type, (1,)) / '(1,)type'。” TensorFlow 中的问题的主要内容,如果未能解决你的问题,请参考以下文章