簡體   English   中英

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

[英]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

注意:請確保您使用相同的數據類型,我在啟動xy時使用了字符串。

在你的情況下,我想你可能已經使用過

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM