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)
即可运行代码了。
文章讲述了在Mac环境中使用PyCharm编写的深度学习代码遇到`Cannotfindreferencenp_utils`的问题,解决方案是将导入语句从`fromkeras.utilsimportnp_utils`改为`fromtensorflow.python.keras.utils.np_utilsimportto_categorical`,修复了代码运行问题。
1953

被折叠的 条评论
为什么被折叠?



