【问题标题】:Keras that does not support TensorFlow 2.0. We recommend using `tf.keras`, or alternatively, downgrading to TensorFlow 1.14不支持 TensorFlow 2.0 的 Keras。我们建议使用 `tf.keras`,或者降级到 TensorFlow 1.14
【发布时间】:2019-11-28 13:51:23
【问题描述】:

关于(不支持 TensorFlow 2.0 的 Keras。我们建议使用 tf.keras,或者降级到 TensorFlow 1.14。)任何建议时,我都遇到了错误。

谢谢

import keras
#For building the Neural Network layer by layer
from keras.models import Sequential
#To randomly initialize the weights to small numbers close to 0(But not 0)
from keras.layers import Dense

classifier=tf.keras.Sequential()

classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))




RuntimeError: It looks like you are trying to use a version of multi-backend Keras that does not support TensorFlow 2.0. We recommend using `tf.keras`, or alternatively, downgrading to TensorFlow 1.14.

【问题讨论】:

    标签: python tensorflow keras neural-network tf.keras


    【解决方案1】:

    您应该只需要更改顶部的导入:

    from tensorflow.python.keras.layers import Dense
    from tensorflow.python.keras import Sequential
    
    classifier = Sequential()
    classifier.add(Dense(6, init = 'uniform', activation = 'relu', input_dim = 11))
    

    【讨论】:

    • 我必须指出这一点。我已经完全按照您列出的方式完成了。但我收到以下错误 TypeError: __init__() missing 1 required positional argument: 'units' 谢谢
    • 这是密集层构造中的错误,与您目前遇到的导入错误不同(因此您在上面提供的代码)。简而言之,所有层都有一个定义神经元数量的必需单位参数。更多详情可以在documentation
    • 你的意思是units=6作为输入层classifier.add(Dense(units = 6, init = 'uniform', activation = 'relu', input_dim = 11))
    • 更像classifier.add(Dense(6, init = 'uniform', activation = 'relu', input_shape = (11,)))。根据文档,输入形状需要是一个元组。这是一个单独的问题,因此您可能需要打开一个新问题或检查使用 keras 的 MLP 实现的现有示例。
    【解决方案2】:

    TensorFlow 2.0+ 只兼容 Keras 2.3.0+,所以如果你想使用 Keras 2.2.5-,你需要 TensorFlow 1.15.0-。或者,是的,您可以使用from tensorflow.keras import ...,但这根本不会使用您的keras 包,您不妨将其卸载。

    【讨论】:

    • “可以”和实际支持有很大区别,只有Keras 2.3.x支持TensorFlow 2.0,所以不建议用2.2.5。
    • @MatiasValdenegro 还好那句话还有后半部分
    • 是的,这就是为什么我建议不要提及部分支持的 TF 版本。
    • @MatiasValdenegro 如果有的话,它明确不鼓励使用 K2.2.5+TF2 - 否则用户可能会运行它而没有错误并认为它很好。但是好吧,我想我可以让它更明确 - 答案已更新
    • 不,现在我发现Keras 2.2.5实际上并不支持TF 2.0的证据,看看this commit,所以说“可以”实际上是错误的。
    【解决方案3】:

    如果你想使用tensorflow 2.0+,你必须有keras 2.3+
    尝试升级你的 keras 它对我有用:

    pip install -U keras
    

    或者您可以将 keras 版本指定为 2.3

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题。使用以下命令将我的 TensorFlow 降级到 1.14 版:

      !pip install tensorflow==1.14.0
      

      修正了错误。

      【讨论】:

        【解决方案5】:

        首先,导入张量流:

        import tensorflow as tf
        

        接下来,代替这个,

        classifier.add(Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))
        

        使用:

        classifier.add(tf.keras.layers.Dense(output_dim = 6, init = 'uniform', activation = 'relu', input_dim = 11))
        

        让我知道它是否有效。

        【讨论】:

          【解决方案6】:

          第一个单元格上的这行代码对我有用

          %tensorflow_version 1.x

          【讨论】:

            【解决方案7】:

            我通过运行解决了问题

            pip install --ignore-installed --upgrade keras
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-11-14
              • 2020-04-27
              • 1970-01-01
              • 1970-01-01
              • 2020-10-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多