Tensorflow 2.0中Placeholder的替换是什么

Rah*_*mar 0 python tensorflow2.0

我是 Tensorflow 的新手,我在 tensorflow 1.0 中使用过 tensorflow.placeholder()。但是是否有任何替代占位符。

Dan*_*van 6

粗略地说,TF 2 中与占位符最相似的语法元素是 aa 函数的参数@tf.function。因此,在 TF 1 中,您有这样的事情:

x = tf.placeholder(...)
y = 2 * x
Run Code Online (Sandbox Code Playgroud)

在 TF 2 你写:

@tf.function
def my_function(x):
  y = 2 * x
  return y
Run Code Online (Sandbox Code Playgroud)

同样,在 TF 1 中,您有会话:

y_val = sess.run(y, feed_dict={x: tf.constant(1)})
Run Code Online (Sandbox Code Playgroud)

但是在 TF 2 中,您只有函数调用(关于它们的参数类型有一些警告 - 您必须明确地使它们成为张量):

y_val = my_function(tf.constant(1))
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,TF 2 稍微改变了思维模型,但希望您最终编写的代码更加直观。

您可以在此 RFC 中阅读有关它的更多信息。