简体   繁体   中英

tf.contrib.layers.fully_connected() in Tensorflow 2?

I'm trying to use tf.contrib.layers.fully_connected() in one of my projects, and it's been deprecated in tensorflow 2.0. Is there an equivalent function, or should I just keep tensorflow v1.x in my virtual environment for this projcet?

tf-slim, as a standalone package, already included tf.contrib.layers.you can install by pip install tf-slim ,call it by from tf_slim.layers import layers as _layers; _layers.fully_conntected(..) from tf_slim.layers import layers as _layers; _layers.fully_conntected(..) .The same as the original, easy to replace

In TensorFlow 2.0 the package tf.contrib has been removed (and this was a good choice since the whole package was a huge mix of different projects all placed inside the same box), so you can't use it.

In TensorFlow 2.0 we need to use tf.keras.layers.Dense to create a fully connected layer, but more importantly, you have to migrate your codebase to Keras. In fact, you can't define a layer and use it, without creating a tf.keras.Model object that uses it.

tf.contrib.layers.fully_connected() is a perfect mess. It is a very old historical mark(or a prehistory DNN legacy). Google has completely deprecated the function since Google hated it. There is no any direct function in TensoFlow 2.x to replace tf.contrib.layers.fully_connected(). Therefore, it is not worth inquiring and getting to know the function.

use: tf.compat.v1.layers.dense for example, instead of

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

you can replace it with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM