簡體   English   中英

模塊“tensorflow.compat.v2.__internal__”沒有屬性“tf2”

[英]module 'tensorflow.compat.v2.__internal__' has no attribute 'tf2'

昨天我嘗試使用 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'

所以我用的是TensorFlow 2.2版和Keras 2.3.1版,昨天還能跑,今天好像跑不了了。 我是不是今天為我的 Keras 和 TensorFlow 導入了錯誤的版本?

編輯:當我使用from tensorFlow import keras時,我想using tensorflow backend的 output 沒有顯示,然后當我import segmentation_models as sm時,當我像上面那樣使用import Keras時,它顯示相同的錯誤。

這是您問題的解決方案,我已經在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
|████████████████████████████████| 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.

在下面指定,在導入分割模型之前,單獨在 colab 中為我工作

os.environ["SM_FRAMEWORK"] = "tf.keras"
import tensorflow as tf
import keras

print(tf.__version__, keras.__version__)

output: 2.7.0 2.7.0

我嘗試了很多答案,但沒有一個對我有用。 錯誤原因:AttributeError: module 'tensorflow.compat.v2. internal .distribute' has no attribute 'strategy_supports_no_merge_call' 在我的例子中是我的設備上安裝了 tensorflow 2.7.0 和 keras 2.6.0。

output while getting this error: 2.7.0 2.6.0

只需匹配版本,它對我有用。

我就是這樣解決的!

  1. 只需禁用版本 2 並使用 tensorflow 的版本 1
 import tensorflow.compat.v1 as tf tf.disable_v2_behavior()
  1. 而不是“從 keras.models 導入順序”和“從 keras.layers 導入密集”,使用以下導入
from tensorflow.python.keras.layers import Input, Dense from tensorflow.python.keras.models import Sequential

將 Tensorflow 升級到 2.7.0 后,我收到此錯誤消息。 降級到 2.5.0 是一個臨時的工作修復。

pip install tensorflow==2.5.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM