<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/mcmc/tracing_reducer.py#L43-L201) |

`Reducer` that accumulates trace results at each sample.

Inherits From: [`Reducer`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/mcmc/Reducer)

    tfp.experimental.mcmc.TracingReducer(
        trace_fn=_trace_state_and_kernel_results, size=None, name=None
    )

Trace results are defined by an appropriate `trace_fn`, which accepts the
current chain state and kernel results, and returns the desired result.
At each sample, the traced values are added to a `TensorArray` which
accumulates all results. By default, all kernel results are traced but in the
future the default will be changed to no results being traced, so plan
accordingly.

If wrapped in a [`tfp.experimental.mcmc.WithReductions`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/mcmc/WithReductions) Transition Kernel,
TracingReducer will not accumulate the kernel results of `WithReductions`.
Rather, the top level kernel results will be that of `WithReductions`' inner
kernel.

As with all reducers, `TracingReducer` does not hold state information;
rather, it stores supplied metadata. Intermediate calculations are held in
a `TracingState` named tuple, which is returned via `initialize` and
`one_step` method calls.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `trace_fn` | A callable that takes in the current chain state and the previous kernel results and return a `Tensor` or a nested collection of `Tensor`s that is accumulated across samples. |
| `size` | Integer or scalar `Tensor` denoting the size of the accumulated `TensorArray`. If this is `None` (which is the default), a dynamic-shaped `TensorArray` will be used. |
| `name` | Python `str` name prefixed to Ops created by this function. Default value: `None` (i.e., 'tracing_reducer'). |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `name` | <br /> <br /> |
| `parameters` | <br /> <br /> |
| `size` | <br /> <br /> |
| `trace_fn` | <br /> <br /> |

<br />

## Methods

### `finalize`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/mcmc/tracing_reducer.py#L172-L185)

    finalize(
        final_reducer_state
    )

Finalizes tracing by stacking the accumulated `TensorArray`.

<br />

<br />

<br />

| Args ||
|---|---|
| `final_reducer_state` | `TracingState` that holds all desired traced results. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `trace` | `Tensor` that represents stacked tracing results. |

<br />

### `initialize`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/mcmc/tracing_reducer.py#L92-L136)

    initialize(
        initial_chain_state, initial_kernel_results=None
    )

Initializes a `TracingState` using previously defined metadata.

Both the `initial_chain_state` and `initial_kernel_results` do not count
as a sample, and hence, will not be traced. This is a deliberate decision
that ensures consistency across sampling procedures.

<br />

<br />

<br />

| Args ||
|---|---|
| `initial_chain_state` | A (possibly nested) structure of `Tensor`s or Python `list`s of `Tensor`s representing the current state(s) of the Markov chain(s). It is used to infer the structure of future trace results. |
| `initial_kernel_results` | A (possibly nested) structure of `Tensor`s representing internal calculations made in a related `TransitionKernel`. It is used to infer the structure of future trace results. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `state` | `TracingState` with an empty `TensorArray` in its `trace_state` field. |

<br />

### `one_step`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/mcmc/tracing_reducer.py#L138-L170)

    one_step(
        new_chain_state, current_reducer_state, previous_kernel_results
    )

Update the `current_reducer_state` with a new trace result.

The trace result will be computed by evaluating the `trace_fn` provided
at instantiation with the `new_chain_state` and `previous_kernel_results`.

<br />

<br />

<br />

| Args ||
|---|---|
| `new_chain_state` | A (possibly nested) structure of incoming chain state(s) with shape and dtype compatible with those used to initialize the `TracingState`. |
| `current_reducer_state` | `TracingState`s representing all previously traced results. |
| `previous_kernel_results` | A (possibly nested) structure of `Tensor`s representing internal calculations made in a related `TransitionKernel`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `new_reducer_state` | `TracingState` with updated trace. Its `trace_state` field holds a `TensorArray` that includes the newly computed trace result. |

<br />