map_fn當我在元組上使用它時,我很難理解它的作用。為了測試我做了以下事情: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 分別分成三個向量,并將它們賦予 func。func 然后只返回第一個輸入,而 map_fn 將它們重新堆疊在一起。所以我想我應該回來。相反發生的是一個可怕的錯誤: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)>)在我看來,map_fn試圖以某種方式將整個元組與其組件之一結合起來。有人可以解釋一下這是怎么回事嗎?
1 回答

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