# tfc.ops.perturb_and_apply

<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/ops/math_ops.py#L157-L216) |

Perturbs the inputs of a pointwise function.

#### View aliases


**Main aliases**

[`tfc.perturb_and_apply`](https://www.tensorflow.org/api_docs/python/tfc/ops/perturb_and_apply)

<br />

    tfc.ops.perturb_and_apply(
        f, x, *args, u=None, x_plus_u=None, expected_grads=True
    )

This function adds uniform noise in the range -0.5 to 0.5 to the first
argument of the given function.
It further replaces derivatives of the function with (analytically computed)
expected derivatives w.r.t. the noise.

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

<br />

<br />

<br />

| ## Args ||
|---|---|
| `f` | Callable. Pointwise function applied after perturbation. |
| `x` | The inputs. |
| `*args` | Other arguments to f. |
| `u` | The noise to perturb x with. If not set and x_plus_u is not provided, it will be sampled. |
| `x_plus_u` | Alternative way to provide the noise, as `x+u`. |
| `expected_grads` | If True, will compute expected gradients. |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A tuple (y, x+u) where y=f(x+u, \*args) and u is uniform noise, and the gradient of `y` w.r.t. `x` uses expected derivatives w.r.t. the distribution of u. ||

<br />