gpt4 book ai didi

python - 我不明白有两个输入的 map_fn

转载 作者:行者123 更新时间:2023-12-05 04:57:17 24 4
gpt4 key购买 nike

当我在元组上使用 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 not

During 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)>)

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 not
Entire first structure:
(., .)
Entire second structure:
.

在我看来,map_fn 以某种方式试图将 whióle 元组与其组件之一结合起来。有人可以解释一下这里发生了什么吗?

最佳答案

来自docs :

If fn's input and output signatures are different, then the output signature must be specified using fn_output_signature.

您的函数采用两个张量的元组并返回一个张量,因此您需要指定 fn_output_signature:

y = tf.map_fn(func,(a,b), fn_output_signature=tf.int32)

关于python - 我不明白有两个输入的 map_fn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64508203/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com