简体   繁体   English

如何在 TensorFlow2.0 中替换 tf.Session.run?

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

Hi I've started learning machine learning by TensorFlow.嗨,我已经开始通过 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由于我不是英语母语,这是我关于堆栈溢出的第一个问题,如果您感到任何不适,我很抱歉

In Tensorflow 2.0, You can use tf.compat.v1.Session() instead of tf.session()在 Tensorflow 2.0 中,您可以使用tf.compat.v1.Session()而不是tf.session()

Please refer code in TF 1.X in below请参考以下 TF 1.X 中的代码

%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: Output:

1.15.2
Hello, World

Please refer code in TF 2.X in below请参考以下 TF 2.X 中的代码

%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: Output:

2.2.0-rc3
Hello, World

I probably believe that you might be using Tensorflow version2.我可能相信您可能正在使用 Tensorflow version2。 Let me help you with your query:让我帮你查询:

x = tf.Variable("Hello")
y = tf.Variable(" World")

add = tf.add(x, y)
tf.print(add)

output: Hello World output: Hello World

Note: Make sure that you use the same data types, I have made use of string while initiating x , y .注意:请确保您使用相同的数据类型,我在启动xy时使用了字符串。

In your case, I guess you might have used在你的情况下,我想你可能已经使用过

x = tf.Variable("Hello")
y =tf.Variable([a,b,c])

your y should be y = tf.Variable(["a", "b", "c"])你的y应该是y = tf.Variable(["a", "b", "c"])

This would give you the output:这将为您提供 output:

"Hello" ["a" "b" "c"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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