mac 深度学习代码 报错 Cannot find reference ‘np_utils’ in ‘init.py’
配置环境
编辑器: PyCharm
keras 版本: 2.15.0
tensorflow 版本: 2.15.0
from keras.utils import np_utils
自己尝试复现实验代码时,显示此错误
解决方法
- 修改导包语句
from tensorflow.python.keras.utils.np_utils import to_categorical
- 将代码中原来使用 np.utils 进行方法调用的代码修改,将 np.utils 删除
修改前
y_train = np_utils.to_categorical(y_train, NB_CLASSES)
y_valid = np_utils.to_categorical(y_valid, NB_CLASSES)
y_test = np_utils.to_categorical(y_test, NB_CLASSES)
修改后
y_train = to_categorical(y_train, NB_CLASSES)
y_valid = to_categorical(y_valid, NB_CLASSES)
y_test = to_categorical(y_test, NB_CLASSES)
即可运行代码了。