[英]How can I alternate tf.Session.run in TensorFlow2.0?
嗨,我已经开始通过 TensorFlow 学习机器学习。 我在下面学习了代码,并意识到这些不再起作用了。
sess = tf.Session()
print(sess.run(hello))
print(sess.run([a, b, c]))
sess.close
有人可以帮助我如何更改这些代码的工作方式将不胜感激。 由于我不是英语母语,这是我关于堆栈溢出的第一个问题,如果您感到任何不适,我很抱歉
在 Tensorflow 2.0 中,您可以使用tf.compat.v1.Session()
而不是tf.session()
请参考以下 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:
1.15.2
Hello, World
请参考以下 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:
2.2.0-rc3
Hello, World
我可能相信您可能正在使用 Tensorflow version2。 让我帮你查询:
x = tf.Variable("Hello")
y = tf.Variable(" World")
add = tf.add(x, y)
tf.print(add)
output: Hello World
注意:请确保您使用相同的数据类型,我在启动x
、 y
时使用了字符串。
在你的情况下,我想你可能已经使用过
x = tf.Variable("Hello")
y =tf.Variable([a,b,c])
你的y
应该是y = tf.Variable(["a", "b", "c"])
这将为您提供 output:
"Hello" ["a" "b" "c"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.