View source on GitHub
|
Performs a scan with an associative binary operation, in parallel.
tf.keras.ops.associative_scan(
f, elems, reverse=False, axis=0
)
This operation his similar to scan, with the key difference that
associative_scan is a parallel implementation with
potentially significant performance benefits, especially when jit compiled.
The catch is that it can only be used when f is a binary associative
operation (i.e. it must verify f(a, f(b, c)) == f(f(a, b), c)).
For an introduction to associative scans, refer to this paper: Blelloch, Guy E. 1990. Prefix Sums and Their Applications.
Returns | |
|---|---|
A (possibly nested Python tree structure of) array(s) of the same shape
and structure as elems, in which the k'th element of axis is
the result of recursively applying f to combine the first k
elements of elems along axis. For example, given
elems = [a, b, c, ...], the result would be
[a, f(a, b), f(f(a, b), c), ...].
|
Examples:
sum_fn = lambda x, y: x + yxs = keras.ops.arange(5)ys = keras.ops.associative_scan(sum_fn, xs, axis=0)ys[0, 1, 3, 6, 10]
sum_fn = lambda x, y: [x[0] + y[0], x[1] + y[1], x[2] + y[2]]xs = [keras.ops.array([1, 2]) for _ in range(3)]ys = keras.ops.associative_scan(sum_fn, xs, axis=0)ys[[1, 3], [1, 3], [1, 3]]
View source on GitHub