报错 raise AttributeError(__former_attrs__[attr], name=None) AttributeError: module ‘numpy‘ has no att
报错
raise AttributeError(former_attrs[attr], name=None)
AttributeError: module ‘numpy’ has no attribute ‘int’.np.intwas a deprecated alias for the builtinint. To avoid this error in existing code, useintby itself. Doing this will not modify any behavior and is safe. When replacingnp.int, you may wish to use e.g.np.int64ornp.int32to specify the precision. If you wish to review your current use, check the release note link for additional information.
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
原因:
在 NumPy 1.20 版本之后,np.int 被正式弃用了,而在 NumPy 2.0+ 之后,这个属性被彻底移除了。代码中使用的 np.int 实际上就是 Python 原生的 int。
解决方法:
修改文件中报错的那几行代码;我的是在datasets.py中报错。
将bi_rgb = np.floor(np.arange(n_rgb) / batch_size).astype(np.int) # batch index
改为bi_rgb = np.floor(np.arange(n_rgb) / batch_size).astype(int) # 或者用 np.int64
可以直接查找.astype(np.int)替换为 .astype(int);
.astype(np.float) 替换为.astype(float)
.astype(np.bool) 替换为.astype(bool)
注:
像np.int16, np.int32, np.int64, np.float32, np.uint8这种带有数字后缀的类型,全部保留,不用修改。
只有单独的 np.int, np.float, np.bool 才需要改成 int, float, bool。
因为np.int 这种简写被废弃了。而 np.int16 属于 NumPy 的明确精度类型(Explicit Precision Types)。
np.int (报错): 这是 Python 内置 int 的别名,NumPy 2.0 删除了它,要求直接用 int。
np.int16 (正常): 这是 NumPy 特有的 16 位整数类型,它在处理图像数据(比如像素值 0-255)时非常常用,在所有 NumPy 版本中都是合法且推荐的。
