Skip to content

Latest commit

 

History

History
202 lines (131 loc) · 6.59 KB

File metadata and controls

202 lines (131 loc) · 6.59 KB

Higher Order Functions

Note: Functions taking Tensor arguments can also take anything accepted by tf.convert_to_tensor.

[TOC]

Functional operations.

Higher Order Operators

TensorFlow provides several higher order operators to simplify the common map-reduce programming patterns.


tf.map_fn(fn, elems, dtype=None, parallel_iterations=10, back_prop=True, swap_memory=False, name=None) {#map_fn}

map on the list of tensors unpacked from elems on dimension 0.

This map operator repeatedly applies the callable fn to a sequence of elements from first to last. The elements are made of the tensors unpacked from elems. dtype is the data type of the return value of fn. Users must provide dtype if it is different from the data type of elems.

Suppose that elems is unpacked into values, a list of tensors. The shape of the result tensor is [len(values)] + fn(values[0]).shape.

Args:
  • fn: The callable to be performed.
  • elems: A tensor to be unpacked to apply fn.
  • dtype: (optional) The output type of fn.
  • parallel_iterations: (optional) The number of iterations allowed to run in parallel.
  • back_prop: (optional) True enables back propagation.
  • swap_memory: (optional) True enables GPU-CPU memory swapping.
  • name: (optional) Name prefix for the returned tensors.
Returns:

A tensor that packs the results of applying fn to the list of tensors unpacked from elems, from first to last.

Raises:
  • TypeError: if fn is not callable.
Example:
elems = [1, 2, 3, 4, 5, 6]
squares = map_fn(lambda x: x * x, elems)
# squares == [1, 4, 9, 16, 25, 36]

tf.foldl(fn, elems, initializer=None, parallel_iterations=10, back_prop=True, swap_memory=False, name=None) {#foldl}

foldl on the list of tensors unpacked from elems on dimension 0.

This foldl operator repeatedly applies the callable fn to a sequence of elements from first to last. The elements are made of the tensors unpacked from elems on dimension 0. The callable fn takes two tensors as arguments. The first argument is the accumulated value computed from the preceding invocation of fn. If initializer is None, elems must contain at least one element, and its first element is used as the initializer.

Suppose that elems is unpacked into values, a list of tensors. The shape of the result tensor is fn(initializer, values[0]).shape`.

Args:
  • fn: The callable to be performed.
  • elems: A tensor to be unpacked on dimension 0.
  • initializer: (optional) The initial value for the accumulator.
  • parallel_iterations: (optional) The number of iterations allowed to run in parallel.
  • back_prop: (optional) True enables back propagation.
  • swap_memory: (optional) True enables GPU-CPU memory swapping.
  • name: (optional) Name prefix for the returned tensors.
Returns:

A tensor resulting from applying fn consecutively to the list of tensors unpacked from elems, from first to last.

Raises:
  • TypeError: if fn is not callable.
Example:
elems = [1, 2, 3, 4, 5, 6]
sum = foldl(lambda a, x: a + x, elems)
# sum == 21

tf.foldr(fn, elems, initializer=None, parallel_iterations=10, back_prop=True, swap_memory=False, name=None) {#foldr}

foldr on the list of tensors unpacked from elems on dimension 0.

This foldr operator repeatedly applies the callable fn to a sequence of elements from last to first. The elements are made of the tensors unpacked from elems. The callable fn takes two tensors as arguments. The first argument is the accumulated value computed from the preceding invocation of fn. If initializer is None, elems must contain at least one element, and its first element is used as the initializer.

Suppose that elems is unpacked into values, a list of tensors. The shape of the result tensor is fn(initializer, values[0]).shape.

Args:
  • fn: The callable to be performed.
  • elems: A tensor that is unpacked into a sequence of tensors to apply fn.
  • initializer: (optional) The initial value for the accumulator.
  • parallel_iterations: (optional) The number of iterations allowed to run in parallel.
  • back_prop: (optional) True enables back propagation.
  • swap_memory: (optional) True enables GPU-CPU memory swapping.
  • name: (optional) Name prefix for the returned tensors.
Returns:

A tensor resulting from applying fn consecutively to the list of tensors unpacked from elems, from last to first.

Raises:
  • TypeError: if fn is not callable.
Example:
elems = [1, 2, 3, 4, 5, 6]
sum = foldr(lambda a, x: a + x, elems)
# sum == 21

tf.scan(fn, elems, initializer=None, parallel_iterations=10, back_prop=True, swap_memory=False, name=None) {#scan}

scan on the list of tensors unpacked from elems on dimension 0.

This scan operator repeatedly applies the callable fn to a sequence of elements from first to last. The elements are made of the tensors unpacked from elems on dimension 0. The callable fn takes two tensors as arguments. The first argument is the accumulated value computed from the preceding invocation of fn. If initializer is None, elems must contain at least one element, and its first element is used as the initializer.

Suppose that elems is unpacked into values, a list of tensors. The shape of the result tensor is [len(values)] + fn(initializer, values[0]).shape.

Args:
  • fn: The callable to be performed.
  • elems: A tensor to be unpacked on dimension 0.
  • initializer: (optional) The initial value for the accumulator.
  • parallel_iterations: (optional) The number of iterations allowed to run in parallel.
  • back_prop: (optional) True enables back propagation.
  • swap_memory: (optional) True enables GPU-CPU memory swapping.
  • name: (optional) Name prefix for the returned tensors.
Returns:

A tensor that packs the results of applying fn to the list of tensors unpacked from elems, from first to last.

Raises:
  • TypeError: if fn is not callable.
Example:
elems = [1, 2, 3, 4, 5, 6]
sum = scan(lambda a, x: a + x, elems)
# sum == [1, 3, 6, 10, 15, 21]