Does tensorflow map_fn support taking more than one tensor?

匿名 (未验证) 提交于 2019-12-03 02:03:01

问题:

Does tf.map_fn support taking more than one tensors as is supported by python's native map function (example provided below)?

a = [1,2,3,4] b = [17,12,11,10] print(map(lambda x,y:x+y, a,b)) # ==> [18, 14, 14, 14] 

回答1:

Not natively, but here's a quick function that achieves it:

def map(fn, arrays, dtype=tf.float32):     # assumes all arrays have same leading dim     indices = tf.range(tf.shape(arrays[0])[0])     out = tf.map_fn(lambda ii: fn(*[array[ii] for array in arrays]), indices, dtype=dtype)     return out  # example: batch affine tranformation x = tf.random_normal([4,5,6]) M = tf.random_normal([4,6,10]) b = tf.random_normal([4,10])  f = lambda x0,M0,b0: tf.matmul(x0,M0) + b0 batch_y = map(f, [x,M,b]) 


回答2:

As on today, I see that map_fn is enhanced to take two tensors as the documentation says that - "elems: A tensor or (possibly nested) sequence of tensors, each of which will be unpacked along their first dimension. The nested sequence of the resulting slices will be applied to fn." The example (though given in numpy form) also shows that it can take two tensors. I'm copying it here.

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] 

Source:



回答3:

The source code shows that this function takes only one elems tensor:

def map_fn(fn, elems, dtype=None, parallel_iterations=10, back_prop=True,        swap_memory=False, name=None): 

I don't see any * and ** parameters.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!