Tensorflow 2 中的 tf.contrib.layers.fully_connected()?

hki*_*ano 4 python python-3.x tensorflow tensorflow2.0

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

Pro*_*ell 6

使用:tf.compat.v1.layers.dense 例如,代替

Z = tf.contrib.layers.fully_connected(F, num_outputs, activation_fn=None)
Run Code Online (Sandbox Code Playgroud)

你可以将其替换为:

Z = tf.compat.v1.layers.dense(F, num_outputs, activation = None)
Run Code Online (Sandbox Code Playgroud)


nes*_*uno 5

在 TensorFlow 2.0 中,该包tf.contrib已被删除(这是一个不错的选择,因为整个包是放置在同一个盒子内的不同项目的巨大组合),因此您无法使用它。

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


小智 5

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