def get_samples_from_trace(trace, with_intermediates=False):
    """ Extracts all sample values from a numpyro trace.

    :param trace: trace object obtained from `numpyro.handlers.trace().get_trace()`
    :param with_intermediates: If True, intermediate(/latent) samples from
        sample site distributions are included in the result.
    :return: Dictionary of sampled values associated with the names given
        via `sample()` in the model. If with_intermediates is True,
        dictionary values are tuples where the first element is the final
        sample values and the second element is a list of intermediate values.
    """
    samples = {
        k: (v['value'], v['intermediates']) if with_intermediates else v['value']
        for k, v in trace.items() if v['type'] == 'sample'
    }
    return samples