O N THE ADVANTAGES OF STOCHASTIC ENCODERS
Lucas Theis ∗
Google Research theis@google.com Eirikur Agustsson ∗ Google Research eirikur@google.com
A BSTRACT
Stochastic encoders have been used in rate-distortion theory and neural compression because they can be easier to handle.
However, in performance comparisons with deterministic encoders they often do worse, suggesting that noise in the encoding process may generally be a bad idea.
It is poorly understood if and when stochastic encoders can do better than deterministic encoders.
In this paper we provide one illustrative example which shows that stochastic encoders can signiﬁcantly outperform the best deterministic encoders.
Our toy example suggests that stochastic encoders may be particularly useful in the regime of “perfect perceptual quality”.
I NTRODUCTION
In lossy compression, information is typically deterministically encoded through the following procedure.
First, the input is transformed using a ﬁxed analysis transform or a learned neural network.
This is followed by a quantization step mapping continuous to discrete values.
Finally, the remaining information is further compressed and encoded into bits using entropy encoding.
Taken together, these steps implement a deterministic function from the input to the integers.
In this paper we discuss whether it is ever advantageous to stochastically encode data by introducing noise into this process.
To make this discussion more precise, let
[Equation]
be sets of stochastic encoders and decoders .
The encoder expects n -dimensional data as well as an additional source of randomness as input.
This source of randomness will be represented by a random variable distributed uniformly between zero and one, which we can think of as an inﬁnite number of random bits when expressed in binary form ( U = 0 .B
1 B 2 B 3 ... ).
The decoder in turn expects the output of an encoder and a source of randomness as input.
This could either be the same random input as for the encoder or a different source (Fig. 1).
By deterministic encoders we mean
[Equation]
that is, encoders which ignore the random inputs.
Deﬁned in this way, it is clear that stochastic encoders can perform no worse than deterministic encoders since ˜ F ⊂ F .
However, for a given loss and data distribution we can ask whether an optimal encoder can be found in ˜ F or whether we should hope to do better by considering the larger set F .
Rate-distortion theory considers stochastic encoders because they can be more amenable to a theoretical analysis.
For example, universal quantization is a randomized quantization procedure whose error distribution is independent of the source (Ziv, 1985), making it easier to analyze than deterministic quantization.
In machine learning, stochastic encoders recently have been considered because they allow for the transmission of continuous signals and thus enable differentiable losses (Havasi et al., 2019).
While this can make it easier to optimize stochastic encoders, the introduction of noise can also hurt performance (Agustsson & Theis, 2020).
For the Laplace and exponential distributions, optimal quantizers exist which are deterministic (Sullivan, 1996).
Universal quantization is known to introduce redundancy compared to the best deterministic quantizer at the same squared error distortion level (Ziv, 1985).
More generally we can
[Figure]
ˆx g (
[Figure]
, u
[Figure]
[Figure]
[Figure]
u
[Figure]
k
[Figure]
uu
C g ( k, u ) ˆx f ( x , u ) k x u (A single uniform random variable can be split into two independent variables. For example, the encoder can use the odd numbered bits and the decoder the even numbered bits.
say that the Lagrangian of a rate-distortion problem is always minimized by a deterministic encoder (Appendix A).
This begs the question whether stochastic encoders are ever better than deterministic encoders.
Wagner & Ball ´ e (2021) provide one example where deterministic encoders are only optimal at certain levels of distortion.
For levels in between, a better rate can be achieved by randomly choosing one of two deterministic codecs, proving that stochastic encoders are indeed sometimes needed for optimal performance.
Here we consider another setting where stochastic encoders appear to provide further advantages.
P ERFECT PERCEPTUAL QUALITY
The setting we consider here is the setting of “perfect perceptual quality” (Blau & Michaeli, 2018).
We still wish to optimize a rate-distortion trade-off but under the constraint that reconstructions follow the data distribution, that is, the reconstructions are guaranteed to be free of any artefacts.
More formally, let ˆX =
g ( f ( X , U ) , U ) .
We seek to minimize E [ d ( X , ˆX
[Equation]
For simplicity, we will only consider ﬁxed-rate encoders
[Equation]
Tschannen et al.
(2018) also called this problem “distribution-preserving lossy compression”.
Their approach combined deterministic encoders with stochastic decoders.
In the next section we demonstrate how also making the encoder stochastic can be advantageous in this setting.
C IRCULAR UNIFORM DISTRIBUTION
Consider a uniform distribution over a unit circle.
It will be convenient to use polar coordinates ( θ, r ) in addition to Cartesian coordinates ( x 1 , x 2 ) .
In polar coordinates, we can write down the marginal density
[Equation]
By our requirement of perfect perceptual quality, any reconstruction ˆx will have to live on the unit circle as well, that is, ˆ r = r = 1 .
Any distortion can therefore be expressed as a function of θ and ˆ θ
and we choose
[Equation]
which is the cosine distance or equivalently the mean squared error.
We further target a bit rate of 1, that is, f ( x , u ) ∈ { 0 , 1 } .
We will now compare two approaches for this example, vector quantization (which uses a deterministic encoder) and universal quantization (Ziv, 1985) (which uses a stochastic encoder).
[Figure]
3. V ECTOR QUANTIZATION
Choose θ 0 =
and θ 1 = π .
Let f ∈ ˜ F 1 be the encoder with
[Equation]
for any input x with polar coordinates ( θ, 1) .
To satisfy the perceptual quality constraint, we decode by uniformly sampling from the Voronoi section corresponding to k , [Equation] ˆ θ
[Equation]
where
[Equation] [Equation]
For a ﬁxed θ ∈ [ − π/ 2 , π/ 2) , the error is
[Equation]
and similarly for other angles.
The expected distortion is
[Equation]
[Equation]
Appendix C shows that this is the best performance we can hope for with a deterministic encoder.
3. U NIVERSAL QUANTIZATION
Universal quantization (Ziv, 1985) is a simple trick which allows us to communicate a uniform sample using bits.
Given y ∈ R and a random variable U uniformly distributed over any unit interval, the encoder computes
[Equation]
The decoder receives K and computes the reconstruction
[Equation]
It can be shown that ˆ Y ∼ y
+ U (cid:48) where U (cid:48) is uniformly distributed between − 0 .
5 and 0 .
Note that universal quantization requires that the encoder and decoder both have access to U .
We will adapt this trick to communicate a noisy version of the input angle θ .
For the encoder, we choose
[Equation]
[Equation]
The decoder receives k ∈ { 0 , 1 } and computes
[Equation]
where
[Equation]
The modulo operation is only changing the decoder output by a multiple of 2.
Hence, for a ﬁxed input with angle θ , the reconstruction is distributed as follows:
[Equation]
for some integer valued random variable J which depends on θ and U .
Note that we only care about θ up to multiples of 2 π since d ( θ, ˆ θ +2 π ) =
d ( θ, ˆ θ ) .
Figure 2C illustrates the conditional distribution of reconstructions when using universal quantization.
Note that the distribution is centered around the data point, unlike for the deterministic encoder.
The expected distortion is independent of θ
and we have
[Equation]
This distortion is about 38.9% smaller than with a deterministic encoder.
Appendix D compares the two approaches further at other bit rates.
D ISCUSSION
Stochastic encoders can be much more convenient to optimize than deterministic encoders.
It is thus important to understand the impact of noise on performance.
When only optimizing for distortion, the potential for stochastic encoders to outperform deterministic encoders is limited (Appendix A).
In practice, stochasticity can even degrade the performance of neural encoders (Agustsson & Theis, 2020).
On the other hand, here we showed that stochasticity can help signiﬁcantly when taking into account perceptual quality (measured in terms of the reconstructions’ distribution).
This justiﬁes using stochastic encoders beyond reasons of convenience and differentiability.
In Appendix B we further show that stochasticity is not enough but that shared randomness is needed to achieve an improvement over deterministic encoders in our example.
Our stochastic encoder relied on universal quantization.
Universal quantization is of particular interest because it is easy to implement and uniform noise is commonly used in neural compression.
The setting of perfect perceptual quality is also practically relevant since it corresponds to artefact free codecs.
However, here we only considered a simple example in a one-shot setting (that is, encoding a single data point).
From rate-distortion theory we know that a deterministic encoder (vector quantization) asymptotically achieves minimal distortion if we encode many data points at once.
An important question for future research is whether a similar achievability result holds in the setting of perfect perceptual quality or whether we should also expect noisy encoders to perform signiﬁcantly better in a wide variety of realistic settings. A CKNOWLEDGMENTS
The authors would like to thank Johannes Ball´e for helpful discussions.
A PPENDIX A
Here we show that under mild assumptions deterministic encoders are optimal for a given ratedistortion trade-off.
This generalizes a result by Ball ´ e et al.
(2021) showing that a random offset used in dithering can always be replaced by a ﬁxed offset which performs at least as well.
Theorem 1.
For every stochastic codec ( f, g ) ∈ F ×G and trade-off parameter λ ∈ [ ∞ ) we can derive a deterministic codec which performs at least as well in terms of the expected rate-distortion trade-off.
Proof.
Let f ∈ F be a stochastic encoder and g ∈ G be a stochastic decoder.
Let U ∼ U ([0 , 1)) be a uniform random variable representing a shared source of randomness.
Further, let
[Equation]
[Equation]
17) represent a number of bits, rate, or other coding cost which in general may depend on the state of the noise.
Now consider the rate-distortion trade-off for a given trade-off parameter λ ∈ [0 , ∞ ) , (cid:96)
[Equation]
x
[Equation]
[Equation]
Under mild conditions on (cid:96) λ (see Tonelli’s theorem), we can write for the expected trade-off
[Equation]
[Equation]
We know there must exist a u ∗ ∈ [0 , 1]
such that E X
[ (cid:96) λ
( X , u ∗ )] ≤ l λ , otherwise the expected trade-off would be larger.
We therefore have
[Equation]
where
[Equation]
is a deterministic encoder and ˜ g and ˜ r are deﬁned analogously.
That is, for every stochastic codec we can derive a deterministic codec which performs at least as well in terms of the expected ratedistortion trade-off.
Interestingly, the same cannot be said if we consider the constrained optimization problem to minimize f,g
[Equation]
Here, stochastic encoders may already perform better as Wagner & Ball´e (2021) showed in an example.
This can happen when the Pareto front of deterministic encoders is non-convex
such that its
Lagrangian (that is, the trade-off in Eq. 21) can only address a subset of the constrained optimization problems.
However, Wagner & Ball´e (2021) also showed that in these cases a simple randomized interpolation of two deterministic codecs can achieve optimal performance.
A PPENDIX B
The following theorem shows that if the encoder and decoder do not have a access to a shared source of randomness (Fig.
1C), then requiring perfect perceptual quality necessarily leads to a twofold increase in squared error relative to a codec which only minimizes distortion.
Theorem 2.
Let H R
⊆
F R ×G be the subset of encoders f
and decoders g such that ˆX ∼ X where
[Equation]
[Equation]
[Equation]
26) and
U 1 and U 2 are assumed to be independent.
Then
[Equation]
Proof.
We can divide the error into two components,
[Equation] [Equation]
[Equation] [Equation]
31) since
[Equation]
The second equality is true because X and ˆX are independent conditioned on K .
It follows that
[Equation]
Here, the second inequality is true because the conditional expectation minimizes the squared error.
To understand the last equality, note that both terms in Eq.
40 represent the same minimization problem over the same set of joint distributions over X and K (or ˆX and K ).
In the ﬁrst minimization problem we ﬁx p ( x ) and chose p ( k | x ) .
In the second minimization problem we equivalently choose both p ( k ) and p ( ˆx | k ) subject to a constraint on the marginal distribution of p ( ˆx ) .
A result of Blau & Michaeli (2019, Theorem 2) could be adapted to prove the achievability of this bound (we assume a ﬁxed rate here where they constrain the mutual information).
While the minimum squared error increases at most by a factor of two by adding a perfect perceptual quality constraint (Blau & Michaeli, 2019), our result shows that we need a shared source of randomness if we want the error to be any lower.
A PPENDIX C
Here we show that the deterministic encoder described in Section 3.1 is optimal among deterministic encoders.
Let f ∈ F 1 be any stochastic 1-bit encoder.
Futher, let K = f
( X , U ) .
Given K , a decoder g ∈ G which minimizes the squared error is given by
[Equation]
where c k = E [ X | K ] .
[Equation]
Then the following deterministic encoder f ∗ ∈ F 1 performs at least as well,
[Equation]
In other words, when searching for an encoder which minimizes squared error we only need to consider deterministic encoders of the form of Equation 43.
The codebook vectors c 0 and c 1 create two Voronoi regions in R 2 which split the circle into two sections.
The squared error corresponding to a uniform distribution over a section of length t can be calculated to be
[Equation]
44) where
[Equation]
[Equation]
Assume the lengths of the Voronoi sections corresponding to c 0 and c 1 are t and 2 π − t , respectively.
Then the average error is given by
[Equation]
This function is minimized at t =
π where
[Equation]
Using Theorem 2 (Appendix B), we conclude
[Equation]
That is, there is no deterministic encoder which performs better than the one described in Section 3.1.
A PPENDIX D
A      0 .0 .0 .0 .
Rate (R) [bit]
D i s t o r t
i on ( M
SE )
VQ
UQ
B
Rate (R) [bit]
S NR [ d B ]
VQ
UQ
We showed that the optimal deterministic encoder at target bit rate of 1 is a vector quantizer (VQ) followed by stochastic decoding (Appendix C).
Without proof, here we illustrate the performance of this approach and compare it to universal quantization (UQ) at other rates.
If R = log 2 N is the target bit rate, then we have for the distortion:
[Equation]
[Equation] Figure 3 visualizes both the average distortion D and the signal-to-noise ratio − 10 log 10 D (SNR).
As we increase the bit rate, the distortion converges to zero in both cases.
On the other hand, the gap between deterministic and stochastic encoders even increases if measured as a difference in SNR.