【问题标题】:Any new version of tf.placeholder?任何新版本的 tf.placeholder?
【发布时间】:2019-09-22 15:50:31
【问题描述】:

我在使用 tf.placeholder 时遇到问题,因为它已在新版本的 TensorFlow 2.0 中被删除。

我现在应该怎么做才能使用这个功能?

【问题讨论】:

    标签: tensorflow placeholder tensorflow2.0


    【解决方案1】:

    您只需将数据直接应用为图层的输入。例如:

    import tensorflow as tf
    import numpy as np
    
    x_train = np.random.normal(size=(3, 2))
    astensor = tf.convert_to_tensor(x_train)
    logits = tf.keras.layers.Dense(2)(astensor)
    print(logits.numpy())
    # [[ 0.21247671  1.97068912]
    #  [-0.17184766 -1.61471399]
    #  [-0.03291694 -0.71419362]]
    

    上面代码的TF1.x 等效项是:

    import tensorflow as tf
    import numpy as np
    
    input_ = np.random.normal(size=(3, 2))
    x = tf.placeholder(tf.float32, shape=(None, 2))
    logits = tf.keras.layers.Dense(2)(x)
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print(sess.run(logits, feed_dict={x:input_}))
    # [[-0.17604277  1.8991518 ]
    #  [-1.5802367  -0.7124136 ]
    #  [-0.5170298   3.2034855 ]]
    

    【讨论】:

      猜你喜欢
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 1970-01-01
      • 2018-09-21
      • 2020-06-03
      • 1970-01-01
      相关资源
      最近更新 更多