Python Keras module 'keras.backend' has no attribute 'image_data_format'
2024-08-26 08:56:14
问题:
当使用Keras运行示例程序mnist_cnn时,出现如下错误: 'keras.backend' has no attribute 'image_data_format'
程序路径https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py
使用的python conda环境是udacity自动驾驶课程的carnd-term1
故障程序段:
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
完整代码见 https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py
故障分析:
conda环境中的Keras版本比例子程序中的版本旧,因此没有'image_data_format'这个变量
解决方法:
以下两种方法,任选其一
1)如果不升级Keras版本
将 K.image_data_format() == 'channels_first'
替换为 K.image_dim_ordering() == 'th'
2)升级Keras版本到最新
> activate carnd-term1 //激活你的conda环境,我的这个环境叫做carnd-term1
(carnd-term1)> conda list //显示当前环境中安装的包
(carnd-term1)> pip uninstall Keras //卸载旧版本的Keras, Keras是使用pip方式安装的,因此卸载和重装都要使用pip相关命令
(carnd-term1)> pip install Keras //重新安装新版本的Keras
(carnd-term1)> conda list //检查版本是否已经更新
最新文章
- 新的一年快开始了,学点新东西吧,从React开始(一)
- Query Designer:公式冲突
- iOS 设计模式之工厂模式
- 安卓ROOT后禁用/隐藏导航栏/虚拟按键
- Linux ls -l内容详解
- CentOS 7:如何安装防火墙?
- yaffs2文件系统
- android不自动弹出虚拟键盘
- Responsive Table 利用@media
- 设计模式之桥接模式(Bridge)--结构模型
- printf不同格式表示法
- Ubuntu 16.04安装和配置Sublime Text 3
- Java 逆变与协变的名词说明
- 新增加的HTTP状态码 -- 103
- flask学习(一)
- Linux 一块网卡配置多个IP的方法
- PopupWindow下拉列表
- MongoDB With Spark遇到的2个错误,不能初始化和sample重复的key
- chrome driver 下载
- python学习之参数传递