gpt4 book ai didi

python - Tensorflow - 迭代两个张量

转载 作者:行者123 更新时间:2023-12-01 08:45:51 29 4
gpt4 key购买 nike

我有两个相似的张量;一个包含所有找到的有效框,另一个包含它们所属的所有索引。

张量("valid_boxes:0", shape=(?, 9), dtype=float32)

张量("valid_boxes_indexes:0", shape=(?, 4), dtype=int64)

我需要一个可以访问这两个变量的map_fun。我试过这个:

operation = tf.map_fn(lambda x: generate_bounding_box(x[0], x[1][1], x[1][0], x[1][2], grid_h, grid_w, anchors), (valid_boxes, valid_boxes_indexes))

Tensorflow 给了我以下内容:

ValueError: The two structures don't have the same nested structure.

First structure: type=tuple str=(tf.float32, tf.int64)

Second structure: type=Tensor str=Tensor("map_14/while/stack:0", shape=(5,), dtype=float32)

More specifically: Substructure "type=tuple str=(tf.float32, tf.int64)" is a sequence, while substructure "type=Tensor str=Tensor("map_14/while/stack:0", shape=(5,), dtype=float32)" is not

有什么办法可以正确地做到这一点吗?

谢谢!

最佳答案

当输入和输出值不具有相同的结构时,您需要指定dtype。来自tf.map_fn的文档:

Furthermore, fn may emit a different structure than its input. For example, fn may look like: fn = lambda t1: return (t1 + 1, t1 - 1). In this case, the dtype parameter is not optional: dtype must be a type or (possibly nested) tuple of types matching the output of fn.

试试这个:

operation = tf.map_fn(
lambda x: generate_bounding_box(x[0], x[1][1], x[1][0], x[1][2],
grid_h, grid_w, anchors),
(valid_boxes, valid_boxes_indexes)
dtype=tf.float32)

关于python - Tensorflow - 迭代两个张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53304490/

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