tf.map_fn 如何工作? [英] How does tf.map_fn work?

查看:33
本文介绍了tf.map_fn 如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看演示:

elems = np.array([1, 2, 3, 4, 5, 6])
squares = map_fn(lambda x: x * x, elems)
# squares == [1, 4, 9, 16, 25, 36]

elems = (np.array([1, 2, 3]), np.array([-1, 1, -1]))
alternate = map_fn(lambda x: x[0] * x[1], elems, dtype=tf.int64)
# alternate == [-1, 2, -3]

elems = np.array([1, 2, 3])
alternates = map_fn(lambda x: (x, -x), elems, dtype=(tf.int64, tf.int64))
# alternates[0] == [1, 2, 3]
# alternates[1] == [-1, -2, -3]

我无法理解第二个和第三个.

I can't understand the second and third.

第二个:我认为结果是 [2, -1],因为第一次 x=np.array([1, 2, 3]) 并返回 1*2,第二次 x=np.array([-1, 1, -1]) 并返回 1*(-1)

For the second: I think the result is [2, -1], because the first time x=np.array([1, 2, 3]) and return 1*2, the second time x=np.array([-1, 1, -1]) and return 1*(-1)

对于第三个:我认为结果的形状是 (3,2),因为第一次 x=1 并返回 (1,-1),第二次 x=2 并返回 (2,-2),第三次 x=3并返回 (3,-3).

For the third: I think the shape of result is (3,2), because the first time x=1 and return (1,-1), the second time x=2 and return (2,-2), the third time x=3 and return (3,-3).

那么 map_fn 是如何工作的?

So how does map_fn work?

推荐答案

对于第二次:我认为结果是[2, -1],因为第一次x=np.array([1, 2, 3]) 并返回 1*2,第二次 x=np.array([-1,1, -1]) 并返回 1*(-1)

For the second: I think the result is [2, -1], because the first time x=np.array([1, 2, 3]) and return 1*2, the second time x=np.array([-1, 1, -1]) and return 1*(-1)

In [26]: a = np.array([[1, 2, 3], [2, 4, 1], [5, 1, 7]])
In [27]: b = np.array([[1, -1, -1], [1, 1, 1], [-1, 1, -1]])
In [28]: elems = (a, b)    
In [29]: alternate = map_fn(lambda x: x[0] * x[1], elems, dtype=tf.int64)
In [30]: alternate.eval()
Out[30]: 
array([[ 1, -2, -3],
       [ 2,  4,  1],
       [-5,  1, -7]])

您将看到应用于函数的 elems 中每个元素的 0 维张量.

You will see that it is the tensor in 0 dimension of each element in the elems that is applied to the function.

对于第三个:我认为结果的形状是 (3,2),因为第一个时间 x=1 并返回 (1,-1),第二次 x=2 并返回 (2,-2),第三次 x=3 并返回 (3,-3).

For the third: I think the shape of result is (3,2), because the first time x=1 and return (1,-1), the second time x=2 and return (2,-2), the third time x=3 and return (3,-3).

In [36]: elems = np.array([[1, 2, 3], [4, 5, 1], [1, 6, 1]])
In [37]: alternates = map_fn(lambda x: (x, -x), elems, dtype=(tf.int64, tf.int64))
In [38]: alternates
Out[38]: 
(<tf.Tensor 'map_6/TensorArrayStack/TensorArrayGatherV3:0' shape=(3, 3) dtype=int64>,
 <tf.Tensor 'map_6/TensorArrayStack_1/TensorArrayGatherV3:0' shape=(3, 3) dtype=int64>)
In [39]: alternates[0].eval()
Out[39]: 
array([[1, 2, 3],
       [4, 5, 1],
       [1, 6, 1]])
In [40]: alternates[1].eval()
Out[40]: 
array([[-1, -2, -3],
       [-4, -5, -1],
       [-1, -6, -1]])

要获得您预期的结果:

In [8]: elems = np.array([[1], [2], [3]])                                                          
In [9]: alternates = map_fn(lambda x: (x, -x), elems, dtype=(tf.int64, tf.int64))
In [10]: sess = tf.InteractiveSession()                                                            
In [11]: alternates[0].eval()
Out[11]: 
array([[1],
       [2],
       [3]])

In [12]: alternates[1].eval()                                                                      
Out[12]: 
array([[-1],
       [-2],
       [-3]])

希望这可以帮助您更好地理解 map_fn.

May this can help you in understanding the map_fn better.

这篇关于tf.map_fn 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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