【问题标题】:What are the new versions of tf.placeholder() and tf.get_variable()?tf.placeholder() 和 tf.get_variable() 的新版本是什么?
【发布时间】:2021-05-13 10:51:51
【问题描述】:

我是 tensorflow 的新手,我想知道为什么某些重要功能在最新版本中被弃用,特别是占位符和 get_variable。例如,我无法在 TF 2.0 中执行此操作:

# tf.placeholder()
X = tf.placeholder(tf.float32, shape=(2,2))
Y = tf.placeholder(tf.float32, shape=(2,2))
Z = X + Y
x, y = [[1,1], [1,1]], [[2,2], [2,2]]
with tf.Session() as sess:
    sess.run(Z, feed_dict={X: x, Y: y})

# tf.get_variable()
W1 = tf.get_variable("W1", [4, 4, 3, 8], initializer = tf.contrib.layers.xavier_initializer(seed=0))
init = tf.global_variables_initializer()
with tf.Session() as sess:
        sess.run(init)
        # etc ...

从我目前了解到的情况来看,这些似乎是非常重要的功能,所以我认为它们的功能已被 TF 2.0 中的其他功能取代,但我似乎无法弄清楚?我知道您可以改用 tf.compat.v1.placeholder,但我正在尝试弄清楚如何在 TF 2.0 中做到这一点。

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    TF 2.0 已更改为急切执行方法,因此您无需使用会话并将数据传递到其中,而是像使用 numpy 一样使用 tf。

    Z = tf.constant([[1,1], [1,1]]) + tf.constant([[2,2], [2,2]])
    

    Tensorflow 也有 tf.Variable,它定义了你可能认为的占位符。基本上是一个张量,其内部允许更改。大多数 keras ops 使用 Variable internal 作为其计算图。

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 2019-09-22
      • 2016-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      • 2016-12-26
      相关资源
      最近更新 更多