【问题标题】:tf.contrib.layers.fully_connected() in Tensorflow 2?Tensorflow 2 中的 tf.contrib.layers.fully_connected()?
【发布时间】:2019-10-13 01:25:17
【问题描述】:

我正在尝试在我的一个项目中使用 tf.contrib.layers.fully_connected(),但它已在 tensorflow 2.0 中被弃用。是否有等效的功能,或者我应该在我的虚拟环境中为这个项目保留 tensorflow v1.x?

【问题讨论】:

  • 你考虑过 tf.keras.layers.Dense 吗?

标签: python python-3.x tensorflow tensorflow2.0


【解决方案1】:

tf-slim,作为一个独立的包,已经包含了tf.contrib.layers。你可以通过pip install tf-slim安装,通过from tf_slim.layers import layers as _layers; _layers.fully_conntected(..)调用它。和原来一样,容易替换

【讨论】:

    【解决方案2】:

    在 TensorFlow 2.0 中,包 tf.contrib 已被删除(这是一个不错的选择,因为整个包是不同项目的巨大组合,都放在同一个盒子中),所以你不能使用它。

    在 TensorFlow 2.0 中,我们需要使用 tf.keras.layers.Dense 来创建全连接层,但更重要的是,您必须将代码库迁移到 Keras。事实上,如果不创建使用它的tf.keras.Model 对象,就无法定义层并使用它。

    【讨论】:

      【解决方案3】:

      使用:tf.compat.v1.layers.dense 例如,而不是

      Z = tf.contrib.layers.fully_connected(F, num_outputs, activation_fn=None)
      

      您可以将其替换为:

      Z = tf.compat.v1.layers.dense(F, num_outputs, activation = None)
      

      【讨论】:

        【解决方案4】:

        tf.contrib.layers.fully_connected() 是一团糟。这是一个非常古老的历史标记(或史前 DNN 遗产)。谷歌已经完全弃用了这个功能,因为谷歌讨厌它。 TensoFlow 2.x 中没有任何直接函数可以替换 tf.contrib.layers.fully_connected()。因此,不值得探究和了解该功能。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-08
          • 1970-01-01
          • 2021-06-25
          • 1970-01-01
          • 1970-01-01
          • 2020-01-30
          • 2019-06-23
          • 2020-04-12
          相关资源
          最近更新 更多