# tfc.entropy_models.UniversalBatchedEntropyModel

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/compression/blob/v2.14.1/tensorflow_compression/python/entropy_models/universal.py#L64-L304) |

Batched entropy model model which implements Universal Quantization.

#### View aliases


**Main aliases**

[`tfc.UniversalBatchedEntropyModel`](https://www.tensorflow.org/api_docs/python/tfc/entropy_models/UniversalBatchedEntropyModel)

<br />

    tfc.entropy_models.UniversalBatchedEntropyModel(
        prior,
        coding_rank,
        compression=False,
        laplace_tail_mass=0.0,
        expected_grads=False,
        tail_mass=(2 ** -8),
        range_coder_precision=12,
        bottleneck_dtype=None,
        num_noise_levels=15,
        stateless=False,
        decode_sanity_check=True
    )

In contrast to the base class, which uses rounding for quantization, here
"quantization" is performed additive uniform noise, which is implemented with
Universal Quantization.

This is described in Sec. 3.2. in the paper
> "Universally Quantized Neural Compression"  
>
> Eirikur Agustsson \& Lucas Theis  
>
> <https://arxiv.org/abs/2006.09952>

<br />

<br />

<br />

| ## Args ||
|---|---|
| `prior` | A [`tfp.distributions.Distribution`](https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/Distribution) object. A density model fitting the marginal distribution of the bottleneck data with additive uniform noise, which is shared a priori between the sender and the receiver. For best results, the distribution should be flexible enough to have a unit-width uniform distribution as a special case, since this is the marginal distribution for bottleneck dimensions that are constant. The distribution parameters may not depend on data (they must be either variables or constants). |
| `coding_rank` | Integer. Number of innermost dimensions considered a coding unit. Each coding unit is compressed to its own bit string, and the `bits()` method sums over each coding unit. |
| `compression` | Boolean. If set to `True`, the range coding tables used by `compress()` and `decompress()` will be built on instantiation. This assumes eager mode (throws an error if in graph mode or inside a [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) call). If set to `False`, these two methods will not be accessible. |
| `laplace_tail_mass` | Float. If positive, will augment the prior with a laplace mixture for training stability. |
| `expected_grads` | If True, will use analytical expected gradients during backpropagation w.r.t. additive uniform noise. |
| `tail_mass` | Float. Approximate probability mass which is encoded using an Elias gamma code embedded into the range coder. |
| `range_coder_precision` | Integer. Precision passed to the range coding op. |
| `bottleneck_dtype` | [`tf.dtypes.DType`](https://www.tensorflow.org/api_docs/python/tf/dtypes/DType). Data type of bottleneck tensor. Defaults to [`tf.keras.mixed_precision.global_policy().compute_dtype`](https://www.tensorflow.org/api_docs/python/tf/keras/config/dtype_policy). |
| `num_noise_levels` | Integer. The number of levels used to quantize the uniform noise. |
| `stateless` | Boolean. If `True`, creates range coding tables as `Tensor`s rather than `Variable`s. This makes the entropy model stateless and allows it to be constructed within a [`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) body. If `compression=False`, then `stateless=True` is implied and the provided value is ignored. |
| `decode_sanity_check` | Boolean. If `True`, an raises an error if the binary strings passed into `decompress` are not completely decoded. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `bottleneck_dtype` | Data type of the bottleneck tensor. |
| `cdf` | The CDFs used by range coding. |
| `cdf_offset` | The CDF offsets used by range coding. |
| `coding_rank` | Number of innermost dimensions considered a coding unit. |
| `compression` | Whether this entropy model is prepared for compression. |
| `expected_grads` | Whether to use analytical expected gradients during backpropagation. |
| `laplace_tail_mass` | Whether to augment the prior with a `NoisyLaplace` mixture. |
| `name` | Returns the name of this module as passed or determined in the ctor. <br /> > [!NOTE] > **Note:** This is not the same as the `self.name_scope.name` which includes parent module names. |
| `name_scope` | Returns a [`tf.name_scope`](https://www.tensorflow.org/api_docs/python/tf/name_scope) instance for this class. |
| `non_trainable_variables` | Sequence of non-trainable variables owned by this module and its submodules. > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `prior` | Prior distribution, used for deriving range coding tables. |
| `prior_shape` | Batch shape of `prior` (dimensions which are not assumed i.i.d.). |
| `prior_shape_tensor` | Batch shape of `prior` as a `Tensor`. |
| `range_coder_precision` | Precision used in range coding op. |
| `stateless` | Whether range coding tables are created as `Tensor`s or `Variable`s. |
| `submodules` | Sequence of all sub-modules. Submodules are modules which are properties of this module, or found as properties of modules which are properties of this module (and so on). a = tf.Module() b = tf.Module() c = tf.Module() a.b = b b.c = c list(a.submodules) == [b, c] True list(b.submodules) == [c] True list(c.submodules) == [] True <br /> |
| `tail_mass` | Approximate probability mass which is range encoded with overflow. |
| `trainable_variables` | Sequence of trainable variables owned by this module and its submodules. <br /> > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `variables` | Sequence of variables owned by this module and its submodules. > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |

## Methods

### `compress`

[View source](https://github.com/tensorflow/compression/blob/v2.14.1/tensorflow_compression/python/entropy_models/universal.py#L228-L265)

    compress(
        bottleneck
    )

Compresses a floating-point tensor.

Compresses the tensor to bit strings. `bottleneck` is first quantized
as in `quantize()`, and then compressed using the probability tables in
`self.cdf` derived from `self.prior`. The quantized tensor can later be
recovered by calling `decompress()`.

The innermost `self.coding_rank` dimensions are treated as one coding unit,
i.e. are compressed into one string each. Any additional dimensions to the
left are treated as batch dimensions.

<br />

<br />

<br />

| Args ||
|---|---|
| `bottleneck` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) containing the data to be compressed. Must have at least `self.coding_rank` dimensions, and the innermost dimensions must be broadcastable to `self.prior_shape`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) having the same shape as `bottleneck` without the `self.coding_rank` innermost dimensions, containing a string for each coding unit. ||

<br />

### `decompress`

[View source](https://github.com/tensorflow/compression/blob/v2.14.1/tensorflow_compression/python/entropy_models/universal.py#L267-L300)

    decompress(
        strings, broadcast_shape
    )

Decompresses a tensor.

Reconstructs the quantized tensor from bit strings produced by `compress()`.
It is necessary to provide a part of the output shape in `broadcast_shape`.

<br />

<br />

<br />

| Args ||
|---|---|
| `strings` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) containing the compressed bit strings. |
| `broadcast_shape` | Iterable of ints. The part of the output tensor shape between the shape of `strings` on the left and `self.prior_shape` on the right. This must match the shape of the input to `compress()`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) of shape `strings.shape + broadcast_shape + self.prior_shape`. ||

<br />

### `get_config`

[View source](https://github.com/tensorflow/compression/blob/v2.14.1/tensorflow_compression/python/entropy_models/universal.py#L302-L304)

    get_config()

Returns the configuration of the entropy model.

<br />

<br />

<br />

| Returns ||
|---|---|
| A JSON-serializable Python dict. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `RuntimeError` | on attempting to call this method on an entropy model with `compression=False` or with `stateless=True`. |

<br />

### `get_weights`

[View source](https://github.com/tensorflow/compression/blob/v2.14.1/tensorflow_compression/python/entropy_models/continuous_base.py#L362-L363)

    get_weights()

### `set_weights`

[View source](https://github.com/tensorflow/compression/blob/v2.14.1/tensorflow_compression/python/entropy_models/continuous_base.py#L365-L370)

    set_weights(
        weights
    )

### `with_name_scope`

    @classmethod
    with_name_scope(
        method
    )

Decorator to automatically enter the module name scope.

    class MyModule(tf.Module):
      @tf.Module.with_name_scope
      def __call__(self, x):
        if not hasattr(self, 'w'):
          self.w = tf.Variable(tf.random.normal([x.shape[1], 3]))
        return tf.matmul(x, self.w)

Using the above module would produce [`tf.Variable`](https://www.tensorflow.org/api_docs/python/tf/Variable)s and [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor)s whose
names included the module name:

    mod = MyModule()
    mod(tf.ones([1, 2]))
    <tf.Tensor: shape=(1, 3), dtype=float32, numpy=..., dtype=float32)>
    mod.w
    <tf.Variable 'my_module/Variable:0' shape=(2, 3) dtype=float32,
    numpy=..., dtype=float32)>

<br />

<br />

<br />

| Args ||
|---|---|
| `method` | The method to wrap. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The original method wrapped such that it enters the module's name scope. ||

<br />

### `__call__`

[View source](https://github.com/tensorflow/compression/blob/v2.14.1/tensorflow_compression/python/entropy_models/universal.py#L188-L226)

    __call__(
        bottleneck, training=True
    )

Perturbs a tensor with additive uniform noise and estimates bitcost.

<br />

<br />

<br />

| Args ||
|---|---|
| `bottleneck` | [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor) containing a non-perturbed bottleneck. Must have at least `self.coding_rank` dimensions. |
| `training` | Boolean. If `False`, computes the bitcost using discretized uniform noise. If `True`, estimates the differential entropy with uniform noise. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A tuple (bottleneck_perturbed, bits) where `bottleneck_perturbed` is `bottleneck` perturbed with nosie and `bits` is the bitcost of transmitting such a sample having the same shape as `bottleneck` without the `self.coding_rank` innermost dimensions. ||

<br />