划重点:从0维度的 elems 中解压的张量列表上的映射。
map_fn(
fn,
elems,
dtype=None,
parallel_iterations=10,
back_prop=True,
swap_memory=False,
infer_shape=True,
name=None
)
1. map_fn 的最简单版本是反复地将可调用的 fn 应用于从第一个到最后一个的元素序列。这些元素由 elems 解压缩的张量构成。如:
elems = np.array([1, 2, 3, 4, 5, 6])
squares = tf.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 = tf.map_fn(lambda x: x[0] * x[1], elems, dtype=tf.int64)
# alternate == [-1, 2, -3]
elems = np.array([1, 2, 3])
alternates = tf.map_fn(lambda x: (x, -x), elems, dtype=(tf.int64, tf.int64))
# alternates[0] == [1, 2, 3]
# alternates[1] == [-1, -2, -3]
2.
def integrate(integrate_input):
Xy = integrate_input[0]
Yx = integrate_input[1]
assert dimensions_equal(Xy.shape,(max_len, dim,))
assert dimensions_equal(Xy.shape, (max_len, dim,))
outputs = tf.concat([Xy,Yx],1)
return outputs
#若inputs1,inputs2的shape为[batch_size,max_len,dim]
#map_fn将inputs1、inputs2按第0维展开
#每次执行integrate时,Xy的shape为[max_len, dim]
outputs = tf.map_fn(integrate, (inputs1, inputs2))