我可以将 tf.map_fn(...) 应用于多个输入/输出吗? [英] Can I apply tf.map_fn(...) to multiple inputs/outputs?

查看:51
本文介绍了我可以将 tf.map_fn(...) 应用于多个输入/输出吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a = tf.constant([[1,2,3],[4,5,6]])
b = tf.constant([True, False], dtype=tf.bool)

a.eval()
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)
b.eval()
array([ True, False], dtype=bool)

我想使用 tf.map_fn 将函数应用于上述输入、ab.它将同时输入 [1,2,3]True 并输出相似的值.

I want to apply a functions to the inputs above, a, and b using tf.map_fn. It will input both [1,2,3], and True and output similar values.

假设 out 函数只是身份:lambda(x,y): x,y 所以,给定输入 [1,2,3], True,它将输出那些相同的张量.

Let's say out function is simply the identity: lambda(x,y): x,y so, given an input of [1,2,3], True, it will output those identical tensors.

我知道如何将 tf.map_fn(...) 与一个变量一起使用,但不知道如何与两个变量一起使用.在这种情况下,我有混合数据类型(int32 和 bool),所以我不能简单地连接张量并在调用后拆分它们.

I know how to use tf.map_fn(...) with one variable, but not with two. And in this case I have mixed data types (int32 and bool) so I can't simply concatenate the tensors and split them after the call.

我可以将 tf.map_fn(...) 用于不同数据类型的多个输入/输出吗?

Can I use tf.map_fn(...) with multiple inputs/outputs of different data types?

推荐答案

想通了.您必须在 dtype 中为每个不同的张量定义每个张量的数据类型,然后您可以将张量作为元组传递,您的 map 函数接收一个输入元组,并且 map_fn 返回一个元组.

Figured it out. You have to define the data types for each tensor in dtype for each of the different tensors, then you can pass the tensors as a tuple, your map function receives a tuple of inputs, and map_fn returns back back a tuple.

有效示例:

a = tf.constant([[1,2,3],[4,5,6]])
b = tf.constant([True, False], dtype=tf.bool)

c = tf.map_fn(lambda x: (x[0], x[1]), (a,b), dtype=(tf.int32, tf.bool))

c[0].eval()
array([[1, 2, 3],
       [4, 5, 6]], dtype=int32)
c[1].eval()
array([ True, False], dtype=bool)

这篇关于我可以将 tf.map_fn(...) 应用于多个输入/输出吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆