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
提交
取消