map_fn當(dāng)我在元組上使用它時(shí),我很難理解它的作用。為了測(cè)試我做了以下事情:a = tf.constant([[1,2,3,4],[5,6,7,8],[9,10,11,12]])b = tf.constant([[1,2,3],[4,5,6],[7,8,9]])func = lambda x: x[0]y = tf.map_fn(func,(a,b))我在這里期望的是,map_fn 將 a 和 b 分別分成三個(gè)向量,并將它們賦予 func。func 然后只返回第一個(gè)輸入,而 map_fn 將它們重新堆疊在一起。所以我想我應(yīng)該回來(lái)。相反發(fā)生的是一個(gè)可怕的錯(cuò)誤:ValueError: The two structures don't have the same nested structure.First structure: type=tuple str=(<tf.Tensor: shape=(3, 4), dtype=int32, numpy=array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]], dtype=int32)>, <tf.Tensor: shape=(3, 3), dtype=int32, numpy=array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=int32)>)Second structure: type=EagerTensor str=tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)More specifically: Substructure "type=tuple str=(<tf.Tensor: shape=(3, 4), dtype=int32, numpy=array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]], dtype=int32)>, <tf.Tensor: shape=(3, 3), dtype=int32, numpy=array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=int32)>)" is a sequence, while substructure "type=EagerTensor str=tf.Tensor([1 2 3 4], shape=(4,), dtype=int32)" is notDuring handling of the above exception, another exception occurred:ValueError Traceback (most recent call last)9 frames/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/nest.py in assert_same_structure(nest1, nest2, check_types, expand_composites) 400 "Entire first structure:\n%s\n" 401 "Entire second structure:\n%s"--> 402 % (str(e), str1, str2)) 403 404 ValueError: The two structures don't have the same nested structure.First structure: type=tuple str=(<tf.Tensor: shape=(3, 4), dtype=int32, numpy=array([[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12]], dtype=int32)>, <tf.Tensor: shape=(3, 3), dtype=int32, numpy=array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=int32)>)在我看來(lái),map_fn試圖以某種方式將整個(gè)元組與其組件之一結(jié)合起來(lái)。有人可以解釋一下這是怎么回事嗎?
1 回答

斯蒂芬大帝
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超8個(gè)贊
來(lái)自文檔:
如果 fn 的輸入和輸出簽名不同,則必須使用 fn_output_signature 指定輸出簽名。
您的函數(shù)接受兩個(gè)張量的元組并返回一個(gè)張量,因此您需要指定fn_output_signature
:
y?=?tf.map_fn(func,(a,b),?fn_output_signature=tf.int32)
添加回答
舉報(bào)
0/150
提交
取消