Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在TensorFlow2.0中替换tf.Session.run?_Tensorflow - Fatal编程技术网

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

如何在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()而不是

嗨,我已经开始通过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()
%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教程开始。