问题描述
已解决
Traceback (most recent call last):
File "run_sample.py", line 6, in <module>
from misc import pyutils
File "***/ReCAM-main/misc/pyutils.py", line 86, in <module>
def to_one_hot(sparse_integers, maximum_val=None, dtype=np.bool):
File "/usr/local/lib/python3.8/dist-packages/numpy/__init__.py", line 305, in __getattr__
raise AttributeError(__former_attrs__[attr])
AttributeError: module 'numpy' has no attribute 'bool'.
`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
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.bool
被弃用,建议使用内置的bool
或者np.bool_
。将pyutils.py
文件中的第86行中的np.bool
替换为bool
或np.bool_
。
修改后的代码如下:
def to_one_hot(sparse_integers, maximum_val=None, dtype=bool):
# 其他代码保持不变
或者
def to_one_hot(sparse_integers, maximum_val=None, dtype=np.bool_):
# 其他代码保持不变
区别:
np.bool
、bool
和np.bool_
都是用于表示布尔类型的,但存在一些差异:
-
np.bool
:这是NumPy库中的一个数据类型,表示一个布尔数组。它与Python内置的bool
类型具有相同的行为,但在NumPy中,所有的数组都是以np.ndarray
的形式表示的,因此使用np.bool
可以确保代码在NumPy中的行为与在Python中的行为一致。 -
bool
:这是Python内置的一个类型,表示一个布尔值。它在大多数情况下的行为与NumPy中的np.bool
相同,但在某些情况下,例如在进行算术运算时,它可能会被隐式地转换为其他类型(例如整数)。 -
np.bool_
:这是NumPy库中的一个数据类型,表示一个布尔数组。它与np.bool
具有相同的行为,但在某些情况下,例如在进行算术运算时,它可能会被保留为布尔类型,而不是被隐式地转换为其他类型。这可以帮助确保在进行数学运算时,结果的类型始终是布尔类型。