Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The name tf.Session is deprecated. Please use tf.compat.v1.Session instead

Tags:

I got the following deprecation warning in my tensorflow code:

The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

  • Why I got this warning
  • What will happen in tensorflow 2.0. instead of tf.session
  • Is it okay to use tf.compat.v1.Session
like image 455
Hadi Rasekh Avatar asked Jun 29 '19 19:06

Hadi Rasekh


People also ask

What can I use instead of TF session?

tensorflow - The name tf. Session is deprecated. Please use tf. compat.

What is TF compat v1?

tf. compat allows you to write code that works both in TensorFlow 1.

What is TF session ()?

tf.Session() initiates a TensorFlow Graph object in which tensors are processed through operations (or ops). The with block terminates the session as soon as the operations are completed. Hence, there is no need for calling Session.close . Also, a session contains variables, global variables, placeholders, and ops.


1 Answers

To make TensorFlow be more "Pythonic" in version 2.0, by design TF 2.0 does not have tf.Session.

TensorFlow 1.X requires users to manually stitch together an abstract syntax tree (the graph) by making tf.* API calls. It then requires users to manually compile the abstract syntax tree by passing a set of output tensors and input tensors to a session.run() call.

TensorFlow 2.0 executes eagerly (like Python normally does) and in 2.0, graphs and sessions should feel like implementation details.

You could use:

import tensorflow.compat.v1 as tf tf.disable_v2_behavior() 

However, this does not let you take advantage of many of the improvements made in TensorFlow 2.0.

The better solution is:

  • Replace tf.Session.run calls: Every tf.Session.run call should be replaced by a Python function.
    • The feed_dict and tf.placeholders become function arguments.
    • The fetches become the function's return value.
like image 179
Hadi Rasekh Avatar answered Sep 17 '22 18:09

Hadi Rasekh