Python2.7.17环境下`from keras.utils import to_categorical`语法错误求助
Python 2.7.17 下解决
from keras.utils import to_categorical语法错误的方案 处理Keras版本兼容性问题
Python 2.7仅支持Keras 2.3.1及更早版本,高版本Keras已停止对Python 2的支持。先卸载当前不兼容的Keras,安装指定兼容版本:pip uninstall keras -y pip install keras==2.3.1同时需匹配兼容的TensorFlow版本(Python 2.7最高支持TensorFlow 1.15.x):
pip install tensorflow==1.15.5确认导入语句正确性
在Keras 2.3.1等Python 2.7兼容版本中,to_categorical确实位于keras.utils模块下,原语句from keras.utils import to_categorical本身是正确的,之前报错大概率是因为安装了不兼容Python 2.7的高版本Keras。验证修复效果
安装完成后,可运行以下代码验证:from keras.utils import to_categorical import numpy as np test_labels = np.array([0, 1, 2, 3]) categorical_labels = to_categorical(test_labels, num_classes=4) print(categorical_labels)若能正常输出独热编码矩阵,说明问题已解决。
内容的提问来源于stack exchange,提问作者ym w
相关产品推荐
相关产品推荐

