From Network Architecture to a Count Model
In a count VAE, the encoder and decoder are not just generic neural networks. Their output layers must correspond to the probability model for the observed counts. The encoder produces a distribution over a cell's latent vector; the decoder uses that latent vector, together with any modeled exposure, to produce parameters of a count distribution. An architecture can be expressive and still be unsuitable if its outputs do not obey the constraints of that distribution.
Here, \(x_i=(x_{i1},\ldots,x_{iG})\) denotes the observed nonnegative integer counts for cell \(i\), and \(z_i\) is its \(K\)-dimensional latent vector. Write \(\ell_i>0\) for the cell's exposure or library-size factor. The discussion uses a diagonal-Gaussian encoder and a negative-binomial decoder, with gene proportions summing to one and expected counts scaled by \(\ell_i\). These are architecture choices, not the only possible count-VAE design.
The usual likelihood and latent-variable setup is assumed: conditional on \(z_i\) and \(\ell_i\), gene counts are modeled with the chosen decoder distribution, often independently across genes; the encoder approximates the latent posterior; and the network weights are shared across cells. Any independence here is a modeling assumption, not a claim that genes are biologically independent. For negative-binomial counts, this tutorial uses mean \(\mu\) and dispersion \(\theta>0\), so the variance is \(\mu+\mu^2/\theta\).
Encoder Outputs: A Valid Latent Distribution
A standard encoder first transforms the count vector to a numerical input, for example \(u_{ig}=\log(1+x_{ig})\), and maps it through one or more hidden layers. A hidden activation such as ReLU may be used between affine layers. The final encoder layers return a mean vector \(m_i\) and a log-variance vector \(a_i\). Returning log variance rather than variance allows the network to produce any real-valued output while a deterministic transformation guarantees positive variance.
Here \(v_{ik}\) is the variance, not the standard deviation, of latent coordinate \(k\); the corresponding standard deviation is \(\sqrt{v_{ik}}\). The reparameterized expression generates a draw from the encoder distribution using noise \(\epsilon_i\) independent of the network inputs. This construction lets gradients pass through the deterministic mapping from \(m_i\) and \(v_i\) while sampling the noise separately.
The log-variance output can become extremely large or small, leading to unstable computations or nearly deterministic latent coordinates. Implementations may constrain it, use a numerically stable equivalent, or monitor its range. Such choices alter the parameterization or optimization behavior and should be documented; they do not change the requirement that the resulting variances be positive.
Worked Example: Trace Counts Through an Encoder
Consider a hypothetical cell with two observed counts \(x_i=(4,1)\) and a two-dimensional latent vector. Use \(u_{ig}=\log(1+x_{ig})\), a two-unit ReLU hidden layer, and the following illustrative network weights and biases:
Using \(\log 5\approx1.609438\) and \(\log 2\approx0.693147\), both pre-activations are positive: \(0.5(1.609438)-0.2(0.693147)=0.666090\) and \(0.1(1.609438)+0.4(0.693147)=0.438203\). Thus \(h_i\approx(0.6661,0.4382)\).
Let the mean layer be \(m_{i1}=0.2h_{i1}+0.3h_{i2}-0.05868\) and \(m_{i2}=-0.2h_{i1}+0.2h_{i2}\). Substitution gives \(m_{i1}=0.2(0.666090)+0.3(0.438203)-0.05868\approx0.2060\) and \(m_{i2}=-0.2(0.666090)+0.2(0.438203)\approx-0.04558\).
For the log-variance layer, set \(a_{i1}=0.2h_{i1}+0.1h_{i2}-0.2\) and \(a_{i2}=-0.1h_{i1}+0.3h_{i2}\). Then \(a_i\approx(-0.02296,0.06485)\), so \(v_i=\exp(a_i)\approx(0.9773,1.0670)\). The posterior standard deviations are \((\sqrt{0.9773},\sqrt{1.0670})\approx(0.9886,1.0330)\).
For a particular noise draw \(\epsilon_i=(0.5,-1)\), the latent sample is \(z_i=m_i+\sqrt{v_i}\odot\epsilon_i\approx(0.2060+0.9886(0.5),-0.04558-1.0330)\approx(0.7003,-1.0786)\). This is one draw from the cell's approximate posterior, not a fixed embedding that the encoder must always return.
Decoder Outputs: Valid Means and Dispersions
A compositional decoder can map \(z_i\) to one real-valued logit per gene, \(r_{ig}\). Real-valued logits are unconstrained; applying softmax converts them into positive proportions summing to one. Scaling those proportions by a positive exposure gives nonnegative expected counts. In particular, the resulting means are strictly positive when \(\ell_i>0\) and all logits are finite.
The negative-binomial decoder can then use \(\mu_{ig}\) and a positive dispersion \(\theta_g\) (or a cell- and gene-specific positive dispersion, if that model is intended). A common output transformation for a real-valued raw dispersion \(d_g\) is \(\theta_g=\operatorname{softplus}(d_g)+\varepsilon\), where \(\operatorname{softplus}(d)=\log(1+\exp(d))\) and \(\varepsilon>0\) is a small numerical floor. An exponential transformation is another option, though it can be more sensitive to extreme raw outputs.
Worked Example: Convert Decoder Logits to Count Means
Suppose a hypothetical decoder produces three gene logits \(r_i=(0.4,-0.2,1.0)\) for a cell with exposure \(\ell_i=120\). The logits need not be positive. Their exponentials are approximately \((1.4918,0.8187,2.7183)\), with sum \(5.0288\). Therefore:
The proportions are positive and sum to approximately one. Multiplying by the exposure gives expected counts \(\mu_i=120\rho_i\approx(35.60,19.54,64.86)\) counts. These are model means, not necessarily integer predictions or observed counts.
If gene 1 has dispersion \(\theta_1=1.3143\), its conditional variance is \(\mu_{i1}+\mu_{i1}^2/\theta_1 \approx35.5985+35.5985^2/1.3143 \approx35.5985+964.2 \approx999.8\) counts squared. The variance is much larger than the mean because this dispersion value implies substantial overdispersion under the stated parameterization.
Worked Example: Make Raw Dispersion Outputs Valid
Suppose a decoder's raw dispersion layer returns \(d=(-1,0.5)\) for two genes. These are unconstrained real numbers, so treating them directly as negative-binomial dispersions would fail: a dispersion must be positive. Use \(\theta_g=\operatorname{softplus}(d_g)+0.001\). Then \(\theta_1=\log(1+\exp(-1))+0.001\approx0.3133+0.001=0.3143\), and \(\theta_2=\log(1+\exp(0.5))+0.001\approx0.9741+0.001=0.9751\).
For illustration, if gene 1 has mean \(\mu=10\) counts, its variance under this parameterization is \(10+10^2/0.3143\approx328.1\) counts squared. If gene 2 also has mean 10 counts, its variance is \(10+10^2/0.9751\approx112.6\) counts squared. Both dispersions are valid, and the smaller dispersion corresponds to greater variance at the same mean. The floor prevents a zero dispersion but does not by itself ensure that the values are biologically or statistically well calibrated.
Justification, Diagnostics, and Alternatives
The output transformations enforce the relevant constraints directly. For finite logits, \(\exp(r_{ig})>0\), and the softmax denominator is the sum of positive terms, so every \(\rho_{ig}>0\). Summing the proportions cancels the common denominator:
Since \(\ell_i>0\), \(\mu_{ig}=\ell_i\rho_{ig}>0\) and its units are expected counts when \(\ell_i\) is a count-scale exposure. Similarly, softplus is positive for every finite input, so adding a positive floor guarantees \(\theta_g>0\). These facts ensure valid parameter values; they do not establish that the model fits the data well.
Useful checks include verifying that encoder means and log variances are finite, that transformed variances and dispersions are positive, and that each softmax vector sums to one within numerical tolerance. Inspect the distributions of \(m_i\), \(a_i\), \(\theta_g\), and predicted \(\mu_{ig}\) across cells and genes. Saturated hidden units, extreme log variances, near-zero proportions for many genes, or means systematically at odds with observed count scales can signal poor scaling, an unsuitable architecture, or an optimization problem. These are warning signs rather than formal tests of model adequacy.
The compositional decoder above allocates a specified exposure across genes. A common alternative is to have the decoder output a separate raw value per gene and transform it, for example with an exponential or softplus, to obtain a positive mean directly. That alternative does not force gene means to sum to \(\ell_i\), so it represents a different mean structure; it is not merely a different way to calculate the same proportions. Likewise, a shared dispersion, a gene-specific dispersion, and a cell-specific dispersion encode different assumptions. Dispersion output transformations should match the chosen parameterization and should not be confused with choosing which dispersion is shared.
A frequent mistake is to apply a sigmoid independently to each gene logit and call the results proportions. Sigmoids produce values between zero and one, but their sum need not equal one, so multiplying them by an exposure does not allocate that exposure as a composition. Use softmax when the model requires proportions summing to one; use independent positive means only when that is the intended model. Another mistake is to interpret a decoder mean as a denoised observed count or as a guarantee of a particular count. It is the expectation of the specified conditional distribution, with uncertainty represented by that distribution.
Check Your Understanding
Answer each question, then compare with the brief solutions below.
- Why is an encoder log-variance output often exponentiated rather than used directly as a variance?
- A decoder returns logits \((0,0)\) for two genes and exposure \(40\). What are the proportions and expected counts?
- A negative-binomial implementation uses variance \(\mu+\mu^2/\theta\). What goes wrong if a raw network output of \(-0.5\) is used directly as \(\theta\)?
- Why does replacing the decoder softmax with independent sigmoid outputs change the model rather than simply alter the computation?
Solutions. (1) The exponential maps any finite real log variance to a strictly positive variance, as required by a Gaussian distribution. (2) The softmax of equal logits is \((0.5,0.5)\), so the means are \(40(0.5,0.5)=(20,20)\) expected counts. (3) A negative dispersion is invalid and would not define the stated negative-binomial variance or distribution; transform the raw value to a positive dispersion first. (4) Sigmoid outputs are individually between zero and one but do not generally sum to one. Softmax represents a composition allocating the exposure, whereas independent positive means need not sum to that exposure.