导入错误:无法从“tensorflow.python.eager.context”导入名称“get_config”

Jeh*_*oor 12 python keras tensorflow

我的笔记本一直工作到今天。在我的 colab 笔记本开始时,我每晚安装 tf-night,但现在它给了我这个错误

---------------------------------------------------------------------------
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)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

!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
Run Code Online (Sandbox Code Playgroud)

我已经尝试安装 tensorflow==2.1.0,但这也没有奏效。如果我注释掉那个导入,我只会在下一行收到另一个错误,而且我在网上找不到太多关于此的信息。任何帮助,将不胜感激!

MD *_*din 26

尝试Keras 2.3.1版本

pip install keras==2.3.1

然后也安装它

pip install git+https://www.github.com/keras-team/keras-contrib.git


小智 13

代替

import keras
Run Code Online (Sandbox Code Playgroud)

尝试

from tensorflow import keras 
Run Code Online (Sandbox Code Playgroud)

这对我有用!


小智 6

我能够通过运行以下“命令”来解决问题:

pip install --upgrade tensorflow
pip install --upgrade tensorflow-gpu
Run Code Online (Sandbox Code Playgroud)

这些“命令”可以在您的控制台中执行。


小智 6

就我使用 Google colab 的情况而言,我将 TensorFlow 降级到 2.2,并将全部替换import keras.xxxximport tensorflow.keras.xxxx. 这解决了它。


bar*_*lid 6

而不是 from keras.preprocessing import image 这样做: from tensorflow.keras.preprocessing import image


小智 6

我已经更新了

from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import img_to_array, load_img
from keras import layers, models, optimizers
from keras import backend as K
Run Code Online (Sandbox Code Playgroud)

from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.preprocessing.image import img_to_array, load_img
from tensorflow.keras import layers, models, optimizers
from tensorflow.keras import backend as K
Run Code Online (Sandbox Code Playgroud)

它有效:)