簡體   English   中英

在Tensorflow中,您是否需要提供與所需內容無關的值?

[英]In Tensorflow, do you need to feed values that aren't relevant to what you need?

我是否正確,在Tensorflow中,當我run任何內容時,我的feed_dict需要為所有占位符提供值,即使那些值與我正在運行的內容無關?

特別是我正在考慮進行預測,在這種情況下,我的targets占位符是不相關的。

好吧,這取決於計算圖的外觀以及如何運行由張量(此處為placeholders )饋送的操作。 如果您將在會話中執行的計算圖的任何部分都不依賴於占位符,則不需要為其輸入值。 這是一個小例子:

In [90]: a = tf.constant([5, 5, 5], tf.float32, name='A')
    ...: b = tf.placeholder(tf.float32, shape=[3], name='B')
    ...: c = tf.constant([3, 3, 3], tf.float32, name='C')
    ...: d = tf.add(a, c, name="Add")
    ...: 
    ...: with tf.Session() as sess:
    ...:       print(sess.run(d))
    ...:

# result       
[8. 8. 8.]

另一方面,如果執行計算圖的一部分,該部分依賴於占位符,則必須將其饋入一個值,否則它將引發InvalidArgumentError 這是一個演示此示例:

In [89]: a = tf.constant([5, 5, 5], tf.float32, name='A')
    ...: b = tf.placeholder(tf.float32, shape=[3], name='B')
    ...: c = tf.add(a, b, name="Add")
    ...: 
    ...: with tf.Session() as sess:
    ...:       print(sess.run(c))
    ...:       

執行上面的代碼,引發以下InvalidArgumentError

InvalidArgumentError:您必須使用dtype float和shape [3]輸入占位符張量“ B”的值

[[節點:B = Placeholderdtype = DT_FLOAT,形狀= [3],_ device =“ / job:localhost /副本:0 /任務:0 /設備:CPU:0”]]


因此,要使其正常工作,您必須使用feed_dictfeed_dict占位符,如下所示:

In [91]: a = tf.constant([5, 5, 5], tf.float32, name='A')
    ...: b = tf.placeholder(tf.float32, shape=[3], name='B')
    ...: c = tf.add(a, b, name="Add")
    ...: 
    ...: with tf.Session() as sess:
    ...:       print(sess.run(c, feed_dict={b: [3, 3, 3]}))
    ...:       
    ...:       
[8. 8. 8.]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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