<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/expectations_reducer.py#L43-L190) |

`Reducer` that computes a running expectation.

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

    tfp.experimental.mcmc.ExpectationsReducer(
        transform_fn=_get_sample, name=None
    )

`ExpectationsReducer` calculates expectations over some arbitrary structure
of `transform_fn`s. A `transform_fn` is a function that accepts a Markov
chain sample and kernel results, and outputs the relevant value for
expectation calculation. In other words, if we denote a `transform_fn`
as f(x,y), `ExpectationsReducer` approximates E\[f(x,y)\] for all provided
functions. The finalized expectation will also identically mimic the
structure of `transform_fn`.

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

<br />

<br />

<br />

| ## Args ||
|---|---|
| `transform_fn` | A (possibly nested) structure of functions that accept a chain state and kernel results. Defaults to simply returning the incoming chain state. |
| `name` | Python `str` name prefixed to Ops created by this function. Default value: `None` (i.e., 'expectations_reducer'). |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `name` | <br /> <br /> |
| `parameters` | <br /> <br /> |
| `transform_fn` | <br /> <br /> |

<br />

## Methods

### `finalize`

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

    finalize(
        final_reducer_state
    )

Finalizes expectation calculation from the `final_reducer_state`.

If the finalized method is invoked on a stream of no inputs, a corresponding
structure of [`tf.ones`](https://www.tensorflow.org/api_docs/python/tf/ones) will be returned.

<br />

<br />

<br />

| Args ||
|---|---|
| `final_reducer_state` | `ExpectationsReducerState` that represents the final reducer state. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `expectation` | an estimate of the expectation with identical structure to `self.transform_fn`. |

<br />

### `initialize`

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

    initialize(
        initial_chain_state, initial_kernel_results=None
    )

Initializes an empty `ExpectationsReducerState`.

<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). |
| `initial_kernel_results` | A (possibly nested) structure of `Tensor`s representing internal calculations made in a related `TransitionKernel`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `state` | `ExpectationsReducerState` representing a stream of no inputs. |

<br />

### `one_step`

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

    one_step(
        new_chain_state,
        current_reducer_state,
        previous_kernel_results=None,
        axis=None
    )

Update the `current_reducer_state` with a new chain state.

Chunking semantics are specified by the `axis` parameter. If chunking is
enabled (axis is not `None`), all elements along the specified `axis` will
be treated as separate samples. If a single scalar value is provided for a
non-scalar sample structure, that value will be used for all elements in the
structure. If not, an identical structure must be provided.

<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 `current_reducer_state`. |
| `current_reducer_state` | `ExpectationsReducerState` representing the current reducer state. |
| `previous_kernel_results` | A (possibly nested) structure of `Tensor`s representing internal calculations made in a related `TransitionKernel`. |
| `axis` | If chunking is desired, this is a (possibly nested) structure of integers that specifies the axis with chunked samples. For individual samples, set this to `None`. By default, samples are not chunked (`axis` is None). |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `new_reducer_state` | `ExpectationsReducerState` with updated running statistics. It tracks a running total and the number of processed samples. |

<br />