tf.map_fn(func, elems)
:
Отображает по оси 0. Например:
tf.map_fn(lambda x: x*2, tf.constant([1, 2, 3])) # => [2, 4, 6]
tf.map_fn(lambda x: x[0]*x[1], tf.constant([[1, 0], [2, 4], [3, 5]])) # => [0, 8, 15]
tf.nest.map_structure(func, *structure)
Когда len(structure)==1
Концептуально :
tf.map_fn(func, flatten(structure[0]))
# then reapplies the structure[0] to the return value
Например:
tf.nest.map_structure(lambda x: x*2, tf.constant((1, 2, 3))) # => (2, 4, 6)
tf.nest.map_structure(lambda x: x*2, (1, (2, 3))) # => (2, (4, 6))
Когда len(structure) > 1
, func
накладывается на застежку-молнию всех элементов конструкции
Концептуально:
tf.map_fn(func, zip(*[flatten(_) _ in structure]))
# then reapplies the structure[0] to the return value
# (all elements in structure must have the same structure,
# i.e., structure[0] structureEqualsTo structure[1], etc.,)
Например:
tf.nest.map_structure(lambda *x: sum(x), [1, [0]], [2, [4]], [3, [5]])
# => [sum([1,2,3]),[sum([0,4,5])]] == [6, [9]]