Python tensorflow 报错:ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' 解决方法

本文主要介绍Python中,使用tensorflow报错:ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' (/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/eager/context.py)解决方法。

报错信息:

---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-589c442233c5> in <module>()
7 import tensorflow as tf
8 from tensorflow.keras import datasets, layers, models
----> 9 from keras.preprocessing import image
10 from keras_preprocessing.image import ImageDataGenerator #check underscore or not
11 from tensorflow.keras.preprocessing import image_dataset_from_directory
2 frames
/usr/local/lib/python3.7/dist-packages/keras/backend.py in <module>()
35 from tensorflow.python.distribute import distribute_coordinator as dc
36 from tensorflow.python.distribute import distribute_coordinator_context as dc_context
---> 37 from tensorflow.python.eager.context import get_config
38 from tensorflow.python.framework import config
39 from keras import backend_config
ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' (/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/eager/context.py)

我的代码:

!pip install tf-nightly

import tensorflow as tf
from tensorflow.keras import datasets, layers, models
from keras.preprocessing import image
from keras_preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing import image_dataset_from_directory
from keras.callbacks import Callback, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping

解决方法:

尝试更新 tensorflow和tensorflow-gpu

pip install --upgrade tensorflow
pip install --upgrade tensorflow-gpu

尝试将import keras 改成from tensorflow import keras

尝试将from keras.preprocessing import image 改成 from tensorflow.keras.preprocessing import image

推荐阅读
cjavapy编程之路首页