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] 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] 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]) 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] 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.