## 2 Materials and Methods
### Methods
#### 2.2.1 Disentangled Autoencoder
The latent loss,  
\[\mathcal{L}_{S}(\Phi;x)=\frac{1}{2}\Big{(}\text{tr}\Big{(}\Sigma^{-1} \Sigma_{x}\Big{)}+(\mu-\mu_{x})^{\prime}\Sigma^{-1}(\mu-\mu_{x})-|\chi|+\text{ ln}\Big{(}\frac{\text{det}\Sigma}{\text{det}\Sigma_{x}}\Big{)}\Big{)} \tag{3}\]  
for a sample \(x\) is computed as the Kullback-Leibler divergence between the encoder output \(N_{x}\) (see Equation (2)) and the standard multivariate Gaussian prior with zero mean and diagonal covariance matrix with unit variances,  
\[N(\mathbf{0},\,\mathbf{I}),\,\text{with}\,\,\mathbf{0}=(0,\ldots,\,0)\,\in \mathbb{R}^{l}\,\,\text{and}\,\,\mathbf{I}=\left( \begin{array}{ccc}1&\cdots& 0\\ \vdots&\ddots&\vdots\\ 0&\cdots&1\end{array} \right) \tag{4}\]  
following [20] but with the additional weighting factor \(\beta\) from [24] (see also Equation (8)). The reconstruction loss,  
\[\mathcal{L}_{R}(\Phi,\theta;x)=\sum_{i=1}^{h}\sum_{j=1}^{w}\sum_{k=1}^{c} \abs{x_{i,j,k}-q_{\theta}(p_{\Phi}(x))_{i,j,k}}, \tag{5}\]  
for an image (region) \(x\in\mathbb{R}^{h\times w\times c}\) of width \(w\), height \(h\) and number of channels \(c\), is based on the sum of absolute differences over the image domain. The supervised softmax classification loss functions are  
\[\mathcal{L}_{C}(\psi;z_{s},y)=-\sum_{\chi}q_{\psi}(\overline{y}_{s}|z_{s})\log p (y), \tag{6}\]  
and  
\[\mathcal{L}_{D}(\phi;z_{m},y)=-\sum_{\chi}q_{\phi}(\overline{y}_{s}|z_{m}) \log p(y), \tag{7}\]  
respectively, and are minimized by updating the classifier and discriminator weights \(\psi\) and \(\phi\). The joint encoder/decoder loss function combines the losses defined in Equations (3) and (5)-(7) into a single grand loss,  
\[\mathcal{F}(\Phi,\theta,\,\psi,\phi;x,y)=\mathcal{L}_{R}(\Phi,\theta;x)+\beta \mathcal{L}_{S}(\phi;x)+\alpha\mathcal{L}_{C}(\psi;z_{s},y)-\delta\mathcal{L} _{D}(\phi;z_{m},y) \tag{8}\]  
as a weighted sum of reconstruction loss (\(\mathcal{L}_{R}\)), latent loss (\(\mathcal{L}_{S}\)), classification loss (\(\mathcal{L}_{C}\)), and discriminator loss (\(\mathcal{L}_{D}\)). Note that the discriminator loss is used in an adversarial manner such that \(\Phi\) is optimized to maximize \(\mathcal{L}_{D}\), but minimize \(\mathcal{L}_{C}\). The respective terminology of an adversarial and a discriminator was initially introduced by [PERSON] in [25] and is by now heavily associated with generative adversarial networks (GANs). Despite not being a GAN-based approach, we decided to use the terms as the components have similar properties and purpose. The discriminator acts as a measurement of model performance, as low accuracy after training marks low content of stain-related information in \(z_{m}\), and the loss is used to update the weights of the encoder \(\Phi\), similar to how the generative model is trained in a GAN approach. By restricting the label-related information to a specific part of the latent code \(z_{s}\), the remaining part \(z_{m}\) becomes domain-invariant as it contains structural information which is persistent across domains. A similar approach is also pursued in [26; 27]. Disentangling the latent space allows further applications to decisively choose which features to include for the task at hand. This is already implicitly done by the classification network.