Python 模块';tensorflow.compat.v2.内部;没有属性';tf2';

Python 模块';tensorflow.compat.v2.内部;没有属性';tf2';,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,我昨天尝试使用TensorFlow作为后端,我可以使用它,但今天当我尝试导入Keras时使用它显示一些错误消息,所以我的代码如下: # Install required libs # NOTE: Run this one code, then restart this runtime and run again for next all... (PENTING!!!) ### please update Albumentations to version>=0.3.0 for `

我昨天尝试使用TensorFlow作为后端,我可以使用它,但今天当我尝试导入Keras时使用它显示一些错误消息,所以我的代码如下:

# Install required libs  
# NOTE: Run this one code, then restart this runtime and run again for next all... (PENTING!!!) 
 
### please update Albumentations to version>=0.3.0 for `Lambda` transform support
!pip install -U segmentation-models

!pip install q tensorflow==2.1
!pip install q keras==2.3.1
!pip install tensorflow-estimator==2.1.

## Imports libs
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'


import cv2
import Keras
import NumPy as np
import matplotlib.pyplot as plt
它显示了这个错误:

AttributeError                            Traceback (most recent call last)

<ipython-input-3-9c78a7be919d> in <module>()
      5 
      6 import cv2
----> 7 import keras
      8 import numpy as np
      9 import matplotlib.pyplot as plt

8 frames

/usr/local/lib/python3.7/dist-packages/keras/initializers/__init__.py in populate_deserializable_objects()
     47 
     48   LOCAL.ALL_OBJECTS = {}
---> 49   LOCAL.GENERATED_WITH_V2 = tf.__internal__.tf2.enabled()
     50 
     51   # Compatibility aliases (need to exist in both V1 and V2).

AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'
AttributeError回溯(最近一次调用)
在()
5.
6进口cv2
---->7进口干酪
8作为np进口numpy
9将matplotlib.pyplot作为plt导入
8帧
/填充反序列化对象()中的usr/local/lib/python3.7/dist-packages/keras/initializers/__-init___;u.py
47
48 LOCAL.ALL_OBJECTS={}
--->49 LOCAL.GENERATED_,其中_V2=tf.uuu internal_uuu.tf2.enabled()
50
51#兼容性别名(需要同时存在于V1和V2中)。
AttributeError:模块“tensorflow.compat.v2.\u_internal\u_”没有属性“tf2”
因此,当我使用TensorFlow 2.2版和Keras 2.3.1版时,昨天我可以运行,但今天似乎不行。我今天导入的Keras和TensorFlow版本是否错误

编辑:
当我使用tensorFlow import keras的
时,我希望使用tensorFlow backend的
输出不会显示,然后当我加载
import segmentation\u模型作为sm时,它会显示与上面类似的错误。

这是您问题的解决方案,我已经在colab上测试过了

!pip install -U -q segmentation-models
!pip install -q tensorflow==2.1
!pip install -q keras==2.3.1
!pip install -q tensorflow-estimator==2.1.

## Imports libs
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
os.environ["SM_FRAMEWORK"] = "tf.keras"

from tensorflow import keras
import segmentation_models as sm

我在尝试导入keras时出现了相同的错误。我的keras是2.4.0,tensorflow也是2.4。0@ymc你解决了吗?我尝试在2.4.0和2.5.0上使用TF和Keras的多个版本,但似乎有相同的错误。尝试:
来自tensorflow import Keras
。@M.Innat我尝试过,但当我使用
导入分段\u模型作为sm
时,似乎显示了相同的错误,当我使用tensorflow import keras的
时,它不会显示文本
使用tensorflow作为后端
。对于
tf 2.x
,请按照说明进行操作,在否决任何问题或答案之前,请添加一些注释,这更有意义。我对我的答案投了反对票(还有OP),没有对原因发表任何评论!我希望SO会提供一些必须的选项,以便对否决票发表评论。我可以知道,为什么当我从import Keras加载时,我不能使用TensorFlow后端?为什么使用TensorFlow后端的输出没有出现?或者我必须使用tf。Keras而不是Keras?无论如何,这是工作,谢谢!在
tf2.x
之后,独立的
keras
不再维护,它成为
tf
的一部分。因此,以前我们使用
import keras
,现在使用(在
tf.2.x
之后,我们应该使用来自10的
。导入keras
。当您使用
tf 2.
时,您应该以这种方式导入。不显示
TensorFlow后端的原因是,在旧的
keras
中,它有三个备份,其中一个是
tf
(AFAIK)但在
tf2.x
之后,只有一个备份,即
tf
,所以消息现在被贬低了
|████████████████████████████████| 51kB 3.3MB/s 
|████████████████████████████████| 421.8MB 42kB/s 
|████████████████████████████████| 450kB 35.7MB/s 
|████████████████████████████████| 3.9MB 33.6MB/s 
Building wheel for gast (setup.py) ... done
ERROR: tensorflow-probability 0.12.1 has requirement gast>=0.3.2, 
but you'll have gast 0.2.2 which is incompatible.
|████████████████████████████████| 378kB 2.1MB/s 
Segmentation Models: using `tf.keras` framework.