tensorflow backend error. AttributeError: module 'tensorflow' has no attribute 'name_scope'

。_饼干妹妹 提交于 2021-02-11 13:51:19

问题


I'm using Version: 2.1.0 of TensorFlow and 2.3.1 of keras. While importing any module of keras i'm facing below tensorflow back-end error.

import pandas as pd, numpy as np, os, re, json, math, time 
from keras.models import Sequential 
from keras.layers import Dense 
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score 
from sklearn.model_selection import KFold 
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline 
from sklearn.model_selection import train_test_split 
---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last) <ipython-input-82-2f712055860b> in <module>
      1 import pandas as pd, numpy as np, os, re, json, math, time
----> 2 from keras.models import Sequential
      3 from keras.layers import Dense
      4 from keras.wrappers.scikit_learn import KerasRegressor
      5 from sklearn.model_selection import cross_val_score

~\Anaconda3\lib\site-packages\keras\__init__.py in <module>
      1 from __future__ import absolute_import
      2 
----> 3 from . import utils
      4 from . import activations
      5 from . import applications

~\Anaconda3\lib\site-packages\keras\utils\__init__.py in <module>
      4 from . import data_utils
      5 from . import io_utils
----> 6 from . import conv_utils
      7 from . import losses_utils
      8 from . import metrics_utils

~\Anaconda3\lib\site-packages\keras\utils\conv_utils.py in <module>
      7 from six.moves import range
      8 import numpy as np
----> 9 from .. import backend as K
     10 
     11 

~\Anaconda3\lib\site-packages\keras\backend\__init__.py in <module>
----> 1 from .load_backend import epsilon
      2 from .load_backend import set_epsilon
      3 from .load_backend import floatx
      4 from .load_backend import set_floatx
      5 from .load_backend import cast_to_floatx

~\Anaconda3\lib\site-packages\keras\backend\load_backend.py in <module>
     88 elif _BACKEND == 'tensorflow':
     89     sys.stderr.write('Using TensorFlow backend.\n')
---> 90     from .tensorflow_backend import *
     91 else:
     92     # Try and load external backend.

~\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in <module>
     54 get_graph = tf_keras_backend.get_graph
     55 # learning_phase_scope = tf_keras_backend.learning_phase_scope  # TODO
---> 56 name_scope = tf.name_scope
     57 
     58 

> AttributeError: module 'tensorflow' has no attribute 'name_scope'

Looking forward to quick solution.


回答1:


I recommend that you switch to using the keras module inside TensorFlow.

It is better maintained and you will not have incompatibility issues.

According to Francois Chollet:

Keras version 2.3.0 is here, and it is the last major multi-backend release. Going forward, users are recommended to switch their code over to tf.keras in TensorFlow 2.0. This release brings API changes and a few breaking changes. Have a look under the hood and see what it includes, as well as what the plans are going forward.

Switching all the imports instead of

from keras.models import Sequential 
from keras.layers import Dense 

to

from tensorflow.keras.models import Sequential 
from tensorflow.keras.layers import Dense 

will solve all your incompatibility issues.

You can uninstall Keras altogether as a separate package since it is already included by default in TensorFlow.

OBSERVATION: If PyCharm does not recognise tensorflow.keras, you will need to update PyCharm to the latest version >=2019.3. Only starting with that version is TensorFlow 2.0 recognised accordingly in PyCharm. Please consult the answer below for more information:

Unable to import Keras(from TensorFlow 2.0) in PyCharm



来源:https://stackoverflow.com/questions/59711670/tensorflow-backend-error-attributeerror-module-tensorflow-has-no-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!