Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of tf.compat?

Tags:

tensorflow

What's the purpose of tf.compat module? It looks like just the entire Tensorflow API is replicated inside this module. The documentation states

Functions for Python 2 vs. 3 compatibility.

So why there is a "v1" and a "v2" submodule? What are the compatibility problems address by tf.compat specifically?

like image 791
robertspierre Avatar asked Oct 30 '19 18:10

robertspierre


1 Answers

tf.compat allows you to write code that works both in TensorFlow 1.x and 2.x. For example, the following piece of code:

import tensorflow as tf

tf.compat.v1.disable_v2_behavior()
with tf.compat.v1.Session() as sess:
    x = tf.compat.v1.placeholder(tf.float32, [2])
    x2 = tf.square(x)
    print(sess.run(x2, feed_dict={x: [2, 3]}))
    # [4. 9.]

Runs the same on TensorFlow 1.15.0 and 2.0.0, even though session and placeholders were deprecated in 2.x. Likewise, tf.compat.v2 allows you to use things introduced in 2.x from 1.x. Also, these APIs provide also backwards compatibility for the future too, so if at some point a 3.x version is released, the mechanism to write version-independent code will already be there since the first version of 2.x.

EDIT: The documentation for the module about Python should actually be changed. Originally, tf.compat only held functions for that purpose (and it was like that until 1.13, see all module documentation). However, it was later repurposed for TensorFlow version compatibility.

like image 144
jdehesa Avatar answered Sep 27 '22 18:09

jdehesa