Python Keras module & #39; keras. backend & #39; has no attribute & #39; image_data_format & #39;, keraskeras. backend

Source: Internet
Author: User
Tags keras

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.