【问题标题】:module 'tensorflow_hub' has no attribute 'KerasLayer'模块“tensorflow_hub”没有属性“KerasLayer”
【发布时间】:2019-09-12 05:19:53
【问题描述】:

当我尝试使用 tensorflow 重新训练模型时,它会显示错误:

**error module 'tensorflow_hub' has no attribute 'KerasLayer'**

代码是:

print("Building model with", MODULE_HANDLE)
model = tf.keras.Sequential([
    hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
    trainable=do_fine_tuning),
    tf.keras.layers.Dropout(rate=0.2),
    tf.keras.layers.Dense(train_generator.num_classes,
    activation='softmax',
    kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
model.build((None,)+IMAGE_SIZE+(3,))
model.summary()

错误是这样的:

      1 print("Building model with", MODULE_HANDLE)
      2 model = tf.keras.Sequential([
----> 3     hub.KerasLayer(MODULE_HANDLE, output_shape=[FV_SIZE],
      4                    trainable=do_fine_tuning),
      5     tf.keras.layers.Dropout(rate=0.2),

AttributeError: module 'tensorflow_hub' has no attribute 'KerasLayer'

通过使用 tensorflow 集线器通过添加新的 dence 全连接层重新训练以前的集线器模型。当运行代码时,它显示上述错误。是否有任何想法。请帮助

【问题讨论】:

    标签: python-3.x tensorflow keras tensorflow-hub


    【解决方案1】:

    请检查 tensorflow 版本。它应该是最近的夜间版本。

    当我使用像 1.13.1 这样的版本时,我在错误之前看到以下警告,没有属性 'KerasLayer':

    W0423 20:04:16.453974 139707130586880 __init__.py:56] Some hub symbols are not available because TensorFlow version is less than 1.14
    

    之后,执行pip install "tf-nightly",一切正常。

    https://www.tensorflow.org/hub

    对于 BatchNormalizationv1 问题,您可以每晚使用 tf2.0,它也应该处理原始问题。

    pip install -U tf-nightly-2.0-preview
    

    https://github.com/tensorflow/tfjs/issues/1255

    【讨论】:

    • 我将 tensorflow 更改为 tf-nighty 但现在它显示错误 [ValueError: Unknown layer: BatchNormalizationV1] 。如何修复它
    • thanks.now am install "pip install -U tf-nightly-2.0-preview"。现在它显示一个错误,如 [AttributeError: module 'tensorflow' has no attribute 'placeholder']
    • 能否请您发布我可以在本地运行的完整代码?
    • thankyou.by 使用 'pip install -U tf-nightly-2.0-preview' 解决了这个问题,现在我得到了模型 h5 文件。但是当我使用 load_model('/content/drive/My Drive/model.h5',custom_objects={"GlorotUniform": tf.keras.initializers.glorot_uniform}) 加载该文件时。它显示一个错误,如 ** [AttributeError: module 'tensorflow' has no attribute 'placeholder']**
    猜你喜欢
    • 2020-06-27
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2021-12-24
    • 2015-05-08
    • 2020-10-17
    • 2020-10-03
    相关资源
    最近更新 更多