亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在張量流中找到張量和/或運算的輸出?

如何在張量流中找到張量和/或運算的輸出?

慕斯709654 2021-08-24 15:20:08
首先,這可能是一個非?;竞陀薮赖膯栴},但我已經嘗試了很多事情并四處搜索無濟于事,所以我來了。問題如下:我有一個張量,出于各種原因,我想找到“它通向何處”。理論上這樣做的方法是根據文檔等查看 my_tensor.op.outputs,但這似乎總是指向 my_tensor本身!我以前很容易走另一條路,這意味著我可以通過使用 my_tensor.op.inputs 來獲得輸入張量,但由于某種原因,“輸出”沒有達到預期的效果。這是一個簡單的例子:import tensorflow as tfa = tf.placeholder(tf.uint8, name='a')b = tf.placeholder(tf.uint8, name='b')my_sum = tf.identity(a + b, name='my_sum')graph = tf.get_default_graph()# I should have 4 ops at this point, as validated by:print(graph.get_operations())>> [<tf.Operation 'a' type=Placeholder>, <tf.Operation 'b' type=Placeholder>, <tf.Operation 'add' type=Add>, <tf.Operation 'my_sum' type=Identity>]# So let's try get the output of 'a':print(list(a.op.outputs))>> [<tf.Tensor 'a:0' shape=<unknown> dtype=uint8>]如果您嘗試了上述操作,您會看到您回到了 'a'...再次,運行 my_sum.op.inputs 給出了 'add' 操作,并且運行得更遠讓我們回到 'a' 和 'b'正如預期的那樣:input_to_my_sum = list(my_sum.op.inputs)[0]print(input_to_my_sum)>> Tensor("add:0", dtype=uint8)print(list(input_to_my_sum.op.inputs))>> [<tf.Tensor 'a:0' shape=<unknown> dtype=uint8>, <tf.Tensor 'b:0' shape=<unknown> dtype=uint8>]但反過來呢?沒有這樣的運氣:print(list(input_to_my_sum.op.outputs))>> [<tf.Tensor 'add:0' shape=<unknown> dtype=uint8>]print('This is no fun at all')>> This is no fun at all那么我做錯了什么?我也試過使用(不推薦使用的)op.values() 沒有成功,我很困惑,因為文檔明確指出這應該給我操作的輸出(來自https://www.tensorflow.org /api_docs/python/tf/Operation):輸出表示此操作輸出的張量對象列表。(我檢查了 a.op.__class__ 是正確的類,并且我正在閱讀正確的文檔)。(總結一下,操作的 node_def 也沒有顯示輸出字段的跡象......)。提前感謝您的任何建議!編輯(由于 Yuxin 的回答):只是為了澄清,將等的輸出的輸出保持在相同的張量上。我正在嘗試達到下一個張量/操作。
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

我很困惑,因為文檔明確指出這應該給我操作的輸出

它確實為您提供了操作的輸出:

print(list(input_to_my_sum.op.outputs))
>> [<tf.Tensor 'add:0' shape=<unknown> dtype=uint8>]

所以我不確定你對什么感到困惑或你在期待什么。

一定要理解“張量”和“運算”的概念。在張量流圖中,操作采用輸入張量列表并輸出張量列表。在 Python 中,您可以print(x)查看x是 Tensor 還是 Operation。

操作有.inputs(它們是張量)和.outputs(它們是張量)。張量沒有輸入和輸出。張量有.op,這是產生它作為輸出的操作。張量也有.consumers(),這是一個以張量作為輸入的操作列表。

input_to_my_sum是一個張量,是加法的結果。 input_to_my_sum.op是一個操作,即產生結果的添加操作input_to_my_sum。因此input_to_my_sum.op.outputs包含input_to_my_sum.


查看完整回答
反對 回復 2021-08-24
  • 1 回答
  • 0 關注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號