map_fn

code example:

Example for Tensorflow code for python’s native map for print(map(lambda x,y:x+y, a,b)) # ==> [18, 14, 14, 14]

# 
# declare variables 
a = tf.constant([1, 2, 3, 4])
b = tf.constant([17, 12, 11, 10])

# NOTE: use stack because map_tf only takes one input tensor
ab = tf.stack([a, b], 1)


def map_operation(value_ab):
    # iterates each value_ab
    value_a = value_ab[0]
    value_b = value_ab[1]
    return value_a+value_b
    
# iterates each value_ab at map_operation()
map_result = tf.map_fn(map_operation, ab, dtype=tf.int32)

with tf.Session() as sess:
    tf.initialize_all_variables().run()
    print(sess.run(map_result))         # [18 14 14 14]
    
''' output
[18 14 14 14]
'''

In reference to this question LINK

Manuel Cuevas

Manuel Cuevas

Hello, I'm Manuel Cuevas a Software Engineer with background in machine learning and artificial intelligence.

Recent post