Python Keras module 'keras.backend' has no attribute 'image_data_format'

问题:

当使用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 //检查版本是否已经更新

 

转载于:https://www.cnblogs.com/lion-zheng/p/6816751.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值