Python Keras module 'keras. backend' has no attribute 'image _ data_format ', keraskeras. backend
Problem:
When the sample program mnist_cnn is run using Keras, the following error occurs: 'keras. backend' has no attribute 'image _ data_format'
Program path https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py
The python conda environment used is the carnd-term1 of the udacity self-driving course
Fault Section:
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)
Complete code see https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py
Fault analysis:
The Keras version ratio subprogram in the conda environment has an old version, so there is no 'image _ data_format 'variable.
Solution:
Either of the following two methods
1) if you do not upgrade the Keras version
Set K. image_data_format () = 'channels _ first'
ReplaceK.image_dim_ordering() == 'th'
2) upgrade Keras to the latest version.
> Activate carnd-term1 // activate your conda environment, which is called carnd-term1
(Carnd-term1)> conda list // display packages installed in the current environment
(Carnd-term1)> pip uninstall Keras // uninstall the older version of Keras, which is installed using pip, so both uninstall and reinstall use pip-related commands
(Carnd-term1)> pip install Keras // reinstall the new version of Keras
(Carnd-term1)> conda list // check if version is updated