0
点赞
收藏
分享

微信扫一扫

解决dtypes.py:513: FutureWarning:...系列问题【TensorFlow】

janedaring 03-30 08:30 阅读 3

前情提要

我的TensorFlow版本是2.4.0,python环境是3.8.19

问题

在训练模型时出现以下报错:

D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\framework\dtypes.py:513: FutureWarning: In the future `np.object` will be defined as the corresponding NumPy scalar.
  np.object,
Traceback (most recent call last):
  File "d:/desktop/model code/LPKT-S-main/train_lpkt_s.py", line 9, in <module>
    import tensorflow as tf
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\__init__.py", line 45, in <module>
    from tensorflow.python import data
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\data\__init__.py", line 25, in <module>
    from tensorflow.python.data import experimental
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 96, in <module>
    from tensorflow.python.data.experimental import service
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 21, in <module>
    from tensorflow.python.data.experimental.ops.data_service_ops import distribute
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 25, in <module>
    from tensorflow.python.data.experimental.ops import compression_ops
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 20, in <module>
    from tensorflow.python.data.util import structure
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\data\util\structure.py", line 26, in <module>      
    from tensorflow.python.data.util import nest
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\data\util\nest.py", line 41, in <module>
    from tensorflow.python.framework import sparse_tensor as _sparse_tensor
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 29, in <module>  
    from tensorflow.python.framework import constant_op
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\framework\constant_op.py", line 29, in <module>    
    from tensorflow.python.eager import execute
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\eager\execute.py", line 27, in <module>
    from tensorflow.python.framework import dtypes
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\framework\dtypes.py", line 513, in <module>        
    np.object,
  File "D:\anaconda3\envs\env_tf\lib\site-packages\numpy\__init__.py", line 305, in __getattr__
    raise AttributeError(__former_attrs__[attr])
AttributeError: module 'numpy' has no attribute 'object'.
`np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:    
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations

解决过程

方案一:在报错中ctrl点击报错位置定位到你报错的源代码中,我定位到的是dtypes.py这个源文件里,然后将所有np.object的地方都改成object,如果运行后又报错dtypes.py:516…的问题的话,依照同样的方法定位到源文件中,将所有np.bool的位置都改成np.bool_,亲测改后不会再报这个错,出现其他情况详见方案二

方案二:百度后发现是numpy版本问题,需要更新,遂更新至最新版本(能方案一就不方案二,因为你也不知道你更新后会不会出现下文所说的包版本冲突!!

pip install --upgrade numpy

如果更新完后你的代码可以运行,那恭喜你不用踩后面的坑了(✿✿ヽ(°▽°)ノ✿)

更新完numpy后,虽说安装成功,但出现了如下信息ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. tensorboard 2.10.0 requires protobuf<3.20,>=3.9.2, but you have protobuf 3.20.3 which is incompatible. tensorflow 2.3.0 requires gast==0.3.3, but you have gast 0.4.0 which is incompatible. tensorflow 2.3.0 requires numpy<1.19.0,>=1.16.0, but you have numpy 1.24.4 which is incompatible. tensorflow 2.3.0 requires scipy==1.4.1, but you have scipy 1.10.1 which is incompatible. tensorflow 2.3.0 requires tensorflow-estimator<2.4.0,>=2.3.0, but you have tensorflow-estimator 2.6.0 which is incompatible.

这里的问题其实就是,我们安装的包版本互相冲突了。我就开始根据报错提示,把我的包用如下命令更换成指定的适配的包版本(-i后加个清华源下载更迅速!)
但在这一步,可能有的朋友就会发现了!这个过程中你的numpy包是很有可能把版本号给降低,即安装回旧版本的!!!

pip install 包名==版本号 -i https://pypi.tuna.tsinghua.edu.cn/simple

如果更新后没有报错,恭喜你又不用踩后面的坑了!

在我更新完所有的包后,这时终端也没有其他的报错信息了,我重新运行我的训练代码,结果出现了以下问题:

Traceback (most recent call last):
  File "train_lpkt_s.py", line 9, in <module>
    import tensorflow as tf
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\__init__.py", line 47, in <module>
    from tensorflow.python import keras
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\keras\__init__.py", line 27, in <module>
    from tensorflow.python.keras import models
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\keras\models.py", line 26, in <module>
    from tensorflow.python.keras.engine import functional
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 38, in <module>
    from tensorflow.python.keras.engine import training as training_lib
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\keras\engine\training.py", line 50, in <module>
    from tensorflow.python.keras.engine import data_adapter
  File "D:\anaconda3\envs\env_tf\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py", line 60, in <module>
    import pandas as pd  # pylint: disable=g-import-not-at-top
  File "D:\anaconda3\envs\env_tf\lib\site-packages\pandas\__init__.py", line 22, in <module>
    from pandas.compat import is_numpy_dev as _is_numpy_dev  # pyright: ignore # noqa:F401
  File "D:\anaconda3\envs\env_tf\lib\site-packages\pandas\compat\__init__.py", line 25, in <module>
    from pandas.compat.numpy import (
  File "D:\anaconda3\envs\env_tf\lib\site-packages\pandas\compat\numpy\__init__.py", line 4, in <module>
    from pandas.util.version import Version
  File "D:\anaconda3\envs\env_tf\lib\site-packages\pandas\util\__init__.py", line 2, in <module>
    from pandas.util._decorators import (  # noqa:F401
  File "D:\anaconda3\envs\env_tf\lib\site-packages\pandas\util\_decorators.py", line 14, in <module>
    from pandas._libs.properties import cache_readonly
  File "D:\anaconda3\envs\env_tf\lib\site-packages\pandas\_libs\__init__.py", line 13, in <module>
    from pandas._libs.interval import Interval
  File "pandas\_libs\interval.pyx", line 1, in init pandas._libs.interval
ValueError: numpy.ndarray size changed, may indicate binary incompatibility. Expected 88 from C header, got 80 from PyObject

这个问题经过百度得到答案:我的numpy包版本过低导致的报错!所以我需要升级。。

所以,如果你也和我的情况一样,最好的解决方法就是直接升级TensorFlow的版本!这里详见这篇博主的博客链接: link

当我更新完我的TensorFlow到2.6.0后,相应的包也会更新,再次运行我的训练代码果然没有出现之前的问题了!希望对你有帮助!

举报

相关推荐

0 条评论