gpt4 book ai didi

python - 属性错误 : module 'tensorflow.compat.v2.__internal__' has no attribute 'dispatch'

转载 作者:行者123 更新时间:2023-12-05 05:31:29 65 4
gpt4 key购买 nike

当我尝试在 Jupyter notebook 中导入 Keras.layers 时,如下所示,我遇到了这个错误。

# Basic Imports
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
from PIL import Image

# Imports for Building CNN
from tensorflow import keras
from keras.layers import Input, Lambda, Dense, Flatten
from keras.models import Model
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, load_model
from tensorflow.keras.preprocessing.image import load_img

# Ignore Warnings
import warnings
warnings.filterwarnings("ignore")

AttributeError: module 'tensorflow.compat.v2.internal' has no attribute 'dispatch'

我从这个错误提要中得到了这个错误:

>     ---------------------------------------------------------------------------
> AttributeError Traceback (most recent call last)
> Input In [1], in <cell line: 9>()
> 7 # Imports for Building CNN
> 8 from tensorflow import keras
> ----> 9 from keras.layers import Input, Lambda, Dense, Flatten
> 10 from keras.models import Model
> 11 from keras.applications.vgg16 import VGG16
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\__init__.py:20,
> in <module>
> 1 # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
> 2 #
> 3 # Licensed under the Apache License, Version 2.0 (the "License");
> (...)
> 13 # limitations under the License.
> 14 # ==============================================================================
> 15 """Implementation of the Keras API, the high-level API of TensorFlow.
> 16
> 17 Detailed documentation and user guides are available at
> 18 [keras.io](https://keras.io).
> 19 """
> ---> 20 from keras import distribute
> 21 from keras import models
> 22 from keras.engine.input_layer import Input
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\distribute\__init__.py:18,
> in <module>
> 1 # Copyright 2019 The TensorFlow Authors. All Rights Reserved.
> 2 #
> 3 # Licensed under the Apache License, Version 2.0 (the "License");
> (...)
> 13 # limitations under the License.
> 14 # ==============================================================================
> 15 """Keras' Distribution Strategy library."""
> ---> 18 from keras.distribute import sidecar_evaluator
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\distribute\sidecar_evaluator.py:22,
> in <module>
> 20 from tensorflow.python.platform import tf_logging as logging
> 21 from tensorflow.python.util import deprecation
> ---> 22 from keras.optimizers.optimizer_experimental import (
> 23 optimizer as optimizer_experimental,
> 24 )
> 25 from tensorflow.python.util.tf_export import keras_export
> 27 _PRINT_EVAL_STEP_EVERY_SEC = 60.0
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\optimizers\__init__.py:25,
> in <module>
> 22 import tensorflow.compat.v2 as tf
> 24 # Imports needed for deserialization.
> ---> 25 from keras import backend
> 26 from keras.optimizers.legacy import adadelta as adadelta_legacy
> 27 from keras.optimizers.legacy import adagrad as adagrad_legacy
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\backend.py:32,
> in <module>
> 29 import numpy as np
> 30 import tensorflow.compat.v2 as tf
> ---> 32 from keras import backend_config
> 33 from keras.distribute import distribute_coordinator_utils as dc
> 34 from keras.engine import keras_tensor
>
> File D:\Black\miniconda3\envs\tensorflow\lib\site-packages\keras\backend_config.py:33,
> in <module>
> 28 # Default image data format, one of "channels_last", "channels_first".
> 29 _IMAGE_DATA_FORMAT = "channels_last"
> 32 @keras_export("keras.backend.epsilon")
> ---> 33 @tf.__internal__.dispatch.add_dispatch_support
> 34 def epsilon():
> 35 """Returns the value of the fuzz factor used in numeric expressions.
> 36
> 37 Returns:
> (...)
> 42 1e-07
> 43 """
> 44 return _EPSILON
>
> AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute 'dispatch'

我对为什么会收到此错误感到困惑。我只想导入它以便运行我的代码。

最佳答案

这为我解决了(来源:https://www.cnblogs.com/wwj321/p/16832760.html)[必须翻译页面]

在我的例子中,我使用的是 Tensorflow 版本 2.4.0,所以我只是在运行模型构建器之前添加了这个(我使用的是 Google Colab):

!pip install -U -q segmentation-models
!pip install -q keras==2.4.3

from tensorflow import keras
import segmentation_models as sm

关于python - 属性错误 : module 'tensorflow.compat.v2.__internal__' has no attribute 'dispatch' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74353697/

65 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com