Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I alternate tf.Session.run in TensorFlow2.0?

Tags:

tensorflow

Hi I've started learning machine learning by TensorFlow. I've learned codes below and realized that these doesn't work anymore.

sess = tf.Session()

print(sess.run(hello))
print(sess.run([a, b, c]))

sess.close

It would be grateful someone can help me how to change these codes work. Since I'm not English native and this is my first question on Stack overflow, I'm sorry for if you feel any uncomfort

like image 931
1220auto Avatar asked Nov 18 '25 09:11

1220auto


1 Answers

In Tensorflow 2.0, You can use tf.compat.v1.Session() instead of tf.session()

Please refer code in TF 1.X in below

%tensorflow_version 1.x

import tensorflow as tf
print(tf.__version__)

with tf.Session() as sess:
  output = tf.constant(""Hello, World"")

  print(sess.run(output).decode())

  sess.close()

Output:

1.15.2
Hello, World

Please refer code in TF 2.X in below

%tensorflow_version 2.x

import tensorflow as tf
print(tf.__version__)

with tf.compat.v1.Session() as sess:
  output = tf.constant(""Hello, World"")

  print(sess.run(output).decode())

  sess.close()

Output:

2.2.0-rc3
Hello, World
like image 196
bsquare Avatar answered Nov 21 '25 10:11

bsquare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!