当前位置: 代码网 > it编程>前端脚本>Python > 解决dtypes.py:513: FutureWarning:...系列问题【TensorFlow】

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

2024年08月01日 Python 我要评论
我的TensorFlow版本是2.4.0,python环境是3.8.19。

前情提要

我的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)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com