Search

Search papers and lessons

Symlog Transformation

Level 4 · 10 min read

Symlog squashes large positive and negative numbers with a logarithm while keeping their sign and leaving small numbers almost unchanged, so a network can predict targets of any size.

On this page

  • Colour = topic mix
  • Discrete math
  • Losses & regularization
  • Sign in to see your progress

TL;DR

Symlog is a function that takes any real number and returns a smaller one with the same sign. It is the natural logarithm of one plus the size of the number, multiplied by the sign: symlog⁡(x)=sign⁡(x)ln⁡(∣x∣+1)\operatorname{symlog}(x) = \operatorname{sign}(x)\ln(|x|+1). Large values, positive or negative, get compressed. Values near zero pass through almost unchanged. Its inverse, symexp, turns predictions back into the original units.

Training a network on symlog-transformed targets lets one fixed loss work whether the targets are around 1 or around a million.

Where you'll see it: DreamerV3 uses symlog to scale observations, reward predictions and value targets for reinforcement learning across many domains with one set of hyperparameters.

Intuition

A ruler that stretches near zero and squeezes the ends

Picture a ruler where the marks near zero are wide apart and the marks far away are packed tightly. Ordinary numbers sit on a straight ruler, so 1000 is a thousand times further from zero than 1. Symlog bends that ruler. The distance from 0 to 1 becomes ln⁡2≈0.69\ln 2 \approx 0.69. The distance from 0 to 1000 becomes ln⁡1001≈6.91\ln 1001 \approx 6.91. A thousandfold change in input is only about a tenfold change in output.

The sign is kept, so negative numbers stay on the negative side of the ruler. That is what separates symlog from a plain logarithm. The logarithm is undefined for zero and negative inputs, so it cannot represent a reward of −5-5 or a temperature below zero.

Why does a network need this? Suppose it must predict a reward that is sometimes 0.1 and sometimes 10000. A squared loss on the raw reward is dominated by the rare huge values, and its gradients swing wildly. Squeezing the targets first means the network works on numbers of similar size, and the loss stays well behaved.

−100−50050100−4−2024xsymlog(x)
  • symlog(x)
  • identity scaled by 1/10
Symlog follows the identity near zero and grows only logarithmically away from it. The dashed line shows how a linear map would grow instead.

Why not just use a logarithm?

Three things go wrong with a plain logarithm for regression targets:

  1. It only accepts positive inputs. Targets that can be zero or negative cannot be transformed at all.
  2. Its derivative at small inputs is huge. ln⁡x\ln x near x=0x = 0 blows up, so small targets get unstable treatment.
  3. It has no symmetric version for negative numbers. Symlog is odd: symlog⁡(−x)=−symlog⁡(x)\operatorname{symlog}(-x) = -\operatorname{symlog}(x), so a positive target and its negative mirror image are treated the same way.

Symlog fixes all three by adding one inside the logarithm. The +1+1 means the argument is at least 1 for any input, so the logarithm is always defined and never negative in its argument.

The math

Definition

Definition: Symlog and symexp

For a real number xx, the symmetric logarithm is

symlog⁡(x)=sign⁡(x) ln⁡ ⁣(∣x∣+1),\operatorname{symlog}(x) = \operatorname{sign}(x)\,\ln\!\left(|x| + 1\right),

where sign⁡(x)\operatorname{sign}(x) is 11 for x>0x \gt 0, −1-1 for x<0x \lt 0, and 00 for x=0x = 0. Its inverse, the symmetric exponential, is

symexp⁡(y)=sign⁡(y)(exp⁡(∣y∣)−1).\operatorname{symexp}(y) = \operatorname{sign}(y)\left(\exp(|y|) - 1\right).

Here ∣x∣|x| is the absolute value of xx, and ln⁡\ln is the natural logarithm, which is the logarithm base ee. The +1+1 inside makes the argument of ln⁡\ln at least 1, so the function is defined on the whole real line. The function is a member of the bi-symmetric logarithmic family, which Webber (2012) introduced for wide-range data.

Checking that the two functions are inverses

Take x=100x = 100. Then symlog⁡(100)=ln⁡101≈4.6151\operatorname{symlog}(100) = \ln 101 \approx 4.6151. Applying symexp:

symexp⁡(4.6151)=exp⁡(4.6151)−1≈101−1=100.\operatorname{symexp}(4.6151) = \exp(4.6151) - 1 \approx 101 - 1 = 100.

For a negative input, x=−100x = -100 gives symlog⁡(−100)=−ln⁡101≈−4.6151\operatorname{symlog}(-100) = -\ln 101 \approx -4.6151, and symexp of that returns −100-100. The sign is carried through both directions, so the two maps invert each other on all of R\mathbb{R}.

Behaviour near zero and far from zero

Differentiate for x≥0x \ge 0. The derivative is

ddxsymlog⁡(x)=1∣x∣+1.\frac{d}{dx}\operatorname{symlog}(x) = \frac{1}{|x| + 1}.

For xx between −1-1 and 11 the slope is between 12\tfrac{1}{2} and 11, and at x=0x = 0 it is exactly 1. So near zero, symlog is the identity. For large ∣x∣|x| the slope falls off like 1/∣x∣1/|x|, which gives the logarithmic compression. Writing ln⁡(1+x)=x−x22+⋯\ln(1+x) = x - \tfrac{x^2}{2} + \cdots for small positive xx makes the identity behaviour explicit: the first-order term is just xx.

The symlog squared loss

DreamerV3 trains a network f(x,θ)f(x, \theta) to predict a transformed target and reads out predictions with the inverse:

L(θ)=12(f(x,θ)−symlog⁡(y))2,y^=symexp⁡ ⁣(f(x,θ)).\mathcal{L}(\theta) = \frac{1}{2}\left(f(x,\theta) - \operatorname{symlog}(y)\right)^2, \qquad \hat{y} = \operatorname{symexp}\!\left(f(x,\theta)\right).

The gradient with respect to the network output is f(x,θ)−symlog⁡(y)f(x,\theta) - \operatorname{symlog}(y). Because symlog⁡(y)\operatorname{symlog}(y) grows only logarithmically in yy, this gradient grows only logarithmically in the size of the target. That is the point of the construction.

Twohot classification for rewards and values

For stochastic targets such as rewards or returns, DreamerV3 goes one step further. The network outputs logits over a set of exponentially spaced bins bib_i, defined as B=symexp⁡([−20,…,+20])B = \operatorname{symexp}([-20, \dots, +20]) in the paper's notation. The prediction is the probability-weighted average of the bin positions:

y^=softmax⁡(f(x))⊤B.\hat{y} = \operatorname{softmax}(f(x))^\top B.

The target yy is "twohot" encoded: the two bins nearest to yy itself (the bins are already spaced exponentially, so no extra symlog is applied) get weights that sum to 1 and split in proportion to closeness. The loss is then the cross-entropy between the predicted Softmax distribution and the twohot target:

L(θ)=−twohot⁡(y)⊤log⁡softmax⁡(f(x,θ)).\mathcal{L}(\theta) = -\operatorname{twohot}(y)^\top \log \operatorname{softmax}(f(x,\theta)).

This loss depends on the probabilities the network assigns to bins, not on the bin positions themselves. So the gradient size does not depend on the size of the target, which is the same robustness goal reached in a different way.

Worked examples

Example
Compressing targets of different sizes

Compute symlog⁡\operatorname{symlog} for four targets, each step on its own line.

  1. symlog⁡(0)=sign⁡(0)ln⁡(0+1)=0⋅0=0\operatorname{symlog}(0) = \operatorname{sign}(0)\ln(0 + 1) = 0 \cdot 0 = 0.
  2. symlog⁡(1)=1⋅ln⁡(1+1)=ln⁡2≈0.6931\operatorname{symlog}(1) = 1 \cdot \ln(1 + 1) = \ln 2 \approx 0.6931.
  3. symlog⁡(1000)=1⋅ln⁡(1000+1)=ln⁡1001≈6.9088\operatorname{symlog}(1000) = 1 \cdot \ln(1000 + 1) = \ln 1001 \approx 6.9088.
  4. symlog⁡(−1000)=−1⋅ln⁡(1000+1)≈−6.9088\operatorname{symlog}(-1000) = -1 \cdot \ln(1000 + 1) \approx -6.9088.

Interpretation: the inputs 11 and 10001000 differ by a factor of 1000, but their symlog values differ only by a factor of about 10. The target −1000-1000 maps to the mirror image of 10001000, so the sign is preserved and the magnitude is compressed the same way.

Example
One squared-loss step on a large reward

A reward target is y=1000y = 1000 and the network currently outputs f=0f = 0. Compare the raw and symlog squared losses.

  1. Raw squared loss: 12(0−1000)2=12⋅106=500 000\tfrac{1}{2}(0 - 1000)^2 = \tfrac{1}{2} \cdot 10^6 = 500\,000.
  2. Symlog target: symlog⁡(1000)≈6.9088\operatorname{symlog}(1000) \approx 6.9088.
  3. Symlog squared loss: 12(0−6.9088)2=12⋅47.731≈23.87\tfrac{1}{2}(0 - 6.9088)^2 = \tfrac{1}{2} \cdot 47.731 \approx 23.87.
  4. Gradient with respect to ff (symlog case): f−symlog⁡(y)=−6.9088f - \operatorname{symlog}(y) = -6.9088, a magnitude of about 6.9.
  5. Gradient with respect to ff (raw case): −1000-1000, a magnitude of 1000.

Interpretation: the raw loss is about 21 000 times larger and its gradient is about 145 times larger. Symlog keeps the update large enough to move the prediction, yet it does not explode. The network can then recover from a wrong guess without the loss scale dominating training.

Example
A round trip through symexp

The network outputs f=2.3979f = 2.3979 and we want the predicted target.

  1. Apply symexp: symexp⁡(2.3979)=sign⁡(2.3979) (exp⁡(2.3979)−1)\operatorname{symexp}(2.3979) = \operatorname{sign}(2.3979)\,(\exp(2.3979) - 1).
  2. Compute exp⁡(2.3979)≈11.0\exp(2.3979) \approx 11.0, so the bracket is 11.0−1=10.011.0 - 1 = 10.0.
  3. The sign is +1+1, so y^≈10.0\hat{y} \approx 10.0.

Interpretation: 2.3979=ln⁡112.3979 = \ln 11 is exactly the symlog of 10, so the round trip recovers the target. The network works in the compressed space and the readout returns the original units.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Targets are all positive and of similar sizeSymlog adds complexity for little gainPlain targets with MSE, or a log transform on positive data
Targets span many orders of magnitude and can be negativePlain squared loss diverges on large valuesSymlog squared loss
Need a calibrated full distribution over a noisy rewardA single symlog regression gives only a point estimateTwohot classification over bins
Target scale changes over trainingA fixed symlog still works, but the bins may cover the wrong rangePick bin range to cover the expected targets
Small errors in the compressed space matter a lotA small error at a large target can correspond to a large absolute errorCheck the readout error in original units, not just the loss

The main weakness is the readout. An error of 0.1 in symlog space is small in loss terms, but near a target of 10000 it corresponds to an error of about 0.1⋅(10001−1)≈10000.1 \cdot (10001 - 1) \approx 1000 in the original units, because the slope of symexp there is about e9.21≈10000e^{9.21} \approx 10000. Keep track of errors after symexp when reporting results.

Symlog also does not truncate or clip, unlike Huber losses, which stop learning from very large targets. It is one of several ways to make regression robust, alongside target normalisation and clipping.

History

Symlog belongs to the family of bi-symmetric logarithmic transforms. Webber (2012) introduced that family for wide-range measurement data, in the journal Measurement Science and Technology. Hafner, Pasukonis, Ba and Lillicrap (2023) used it in DreamerV3 to make a single reinforcement learning configuration work across many domains. The paper argues that symlog avoids both the truncation of large targets from Huber losses and the non-stationarity that comes from normalising targets with running statistics.

Check yourself

QuestionWhat is symlog⁡(0)\operatorname{symlog}(0), and why is that value useful?Show answer

symlog⁡(0)=0\operatorname{symlog}(0) = 0. Because the sign of 0 is 0, and ln⁡(0+1)=0\ln(0 + 1) = 0, the function passes through the origin. This matters because it means a target of zero stays zero after the transform and does not shift the network's bias.

QuestionCompute symlog⁡(99)\operatorname{symlog}(99) to three decimal places.Show answer

symlog⁡(99)=ln⁡(99+1)=ln⁡100≈4.605\operatorname{symlog}(99) = \ln(99 + 1) = \ln 100 \approx 4.605. Note that ln⁡100=2ln⁡10≈4.6052\ln 100 = 2 \ln 10 \approx 4.6052.

QuestionWhy can symlog represent negative targets while a plain logarithm cannot?Show answer

A plain logarithm is defined only for positive arguments, so it cannot take a negative target at all. Symlog takes the logarithm of ∣x∣+1|x| + 1, which is always at least 1, so the logarithm is defined. It then multiplies by the sign of xx, so negative targets map to negative values symmetric to their positive mirror images.

QuestionA network predicts f=3.0f = 3.0 for a target y=−400y = -400. What is the symlog squared loss?Show answer

The symlog target is symlog⁡(−400)=−ln⁡401≈−5.9940\operatorname{symlog}(-400) = -\ln 401 \approx -5.9940. The difference is 3.0−(−5.9940)=8.99403.0 - (-5.9940) = 8.9940. The loss is 12⋅8.99402≈12⋅80.89≈40.4\tfrac{1}{2} \cdot 8.9940^2 \approx \tfrac{1}{2} \cdot 80.89 \approx 40.4.

QuestionApplied scenario: a reward model is trained with symlog targets, and one training batch has a reward of 10000. Should you worry about the gradient for that sample?Show answer

The symlog gradient for that sample has magnitude ∣f−symlog⁡(10000)∣≈∣f−9.21∣|f - \operatorname{symlog}(10000)| \approx |f - 9.21|, so for a near-zero prediction it is about 9.2. That is large compared with a reward of 0.1, but it is a logarithmic rather than linear dependence on the target. The network sees a strong update, not an explosive one. You should still check that the readout after symexp is in a plausible range, since small errors in the transformed space are amplified on the way back.

Sources

  • Hafner, D., Pasukonis, J., Ba, J., Lillicrap, T. (2023). Mastering Diverse Domains through World Models. arXiv:2301.04104.
  • Webber, J. B. W. (2012). A bi-symmetric log transformation for wide-range data. Measurement Science and Technology, 24(2), 027001. Cited in the DreamerV3 reference list.
  • Logarithm (Wikipedia)