如何在TensorFlow2.0中替换tf.Session.run?
嗨,我已经开始通过TensorFlow学习机器学习。 我已经学习了下面的代码,并意识到这些代码已经不起作用了如何在TensorFlow2.0中替换tf.Session.run?,tensorflow,Tensorflow,嗨,我已经开始通过TensorFlow学习机器学习。 我已经学习了下面的代码,并意识到这些代码已经不起作用了 sess = tf.Session() print(sess.run(hello)) print(sess.run([a, b, c])) sess.close 如果有人能帮助我如何更改这些代码,我将不胜感激。 由于我不是英语母语,这是我关于堆栈溢出的第一个问题,如果您在Tensorflow 2.0中感到不舒服,我很抱歉,您可以使用tf.compat.v1.Session()而不是
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()
%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()
输出:
1.15.2
Hello, World
2.2.0-rc3
Hello, World
请参考下面TF 2.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()
%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()
输出:
1.15.2
Hello, World
2.2.0-rc3
Hello, World
我可能认为您可能正在使用Tensorflow版本2。 让我帮你回答你的问题:
x = tf.Variable("Hello")
y = tf.Variable(" World")
add = tf.add(x, y)
tf.print(add)
输出:
Hello World
注意:请确保使用相同的数据类型,我在启动x
,y
时使用了字符串
在你的情况下,我想你可能用了
x = tf.Variable("Hello")
y =tf.Variable([a,b,c])
您的y
应该是y=tf.变量([“a”、“b”、“c”)
这将为您提供以下输出:
"Hello" ["a" "b" "c"]
从Tensorflow.org上的Tensorflow 2.0教程开始。