# A Domain-Based, Adaptive, Multi-Scale, Inter-Subject Sleep Stage Classification Network
## 2 Materials and Methods
### Domain Adaptation  
Due to individual differences, the distribution gap among subjects leads to the poor generalization ability of the pre-training model on the test set. In this part, we combine the pre-trained feature encoder, classifiers, and domain alignment layer to improve the performance of the model during testing, as shown in Figure 4.  
For the adjustment of the feature encoder, the last two multi-scale residual blocks are fine-tuned to adjust to a new domain, which not only solves the problem of domain shift but also reduces the number of parameters for fine-tuning. For the domain alignment layer, the MMD is used to measure the distribution gap between the two domains [22]. The output of MMD is convergent as the \(L_{d}\) loss of the domain alignment layer [31; 32]:  
\[L_{d}\ =\ \mathrm{MMD}^{2}(\mathbf{F},X_{s},X_{t}) \tag{8}\]  
where \(\mathbf{F}\) is the set of mapping functions. MMD between the distribution \(u\) and the distribution \(v\) is as follows:  
\[\mathrm{MMD}[u,\ \mathrm{v}]:\ =\ sup_{f\in F}(E[g(X_{s})]\ -\ E[g(X_{t})]) \tag{9}\]  
Figure 4: The architecture of the deep adaptation network used in this paper. Different colored arrows indicate the data flow and backpropagation of different branches. where the distribution is mapped to the reproducing kernel Hilbert space (RKHS) \(\mathbf{H}\) by the mapping function \(g\). For \(g\in\mathbf{H}\),  
\[E[g(X_{s})]\ =\ \langle g,\mu_{u}\rangle_{\mathbf{H}^{\prime}}\ E[g(X_{t})]\ =\ \langle g,\mu_{v}\rangle_{\mathbf{H}}. \tag{10}\]  
\(\mu_{u}\) and \(\mu_{v}\) are unbiased estimates of the mean. The details of implementation are summarized in Algorithm 1. Firstly, we freeze the domain alignment layer and update the feature encoder and classifiers by minimizing total loss L, which is defined as follows:  
\[\text{L}\ =\ L_{1y}\ +\ \sigma L_{2y}\ +\ \beta L_{d}. \tag{11}\]  
\(\sigma,\ \beta\) are the weights among the three types of loss. Then, the domain alignment layer is updated by minimizing \(L_{d}\) while the feature encoder and classifiers are frozen. The model weights are gradually adjusted in the cross-iterative process to learn the inter-subject sleep staging task. ```
Input: Labeled training set \(S_{ii}\ =\ \{x_{s,ii^{\prime}}^{(i)}y_{s,ii}^{(i)}\}_{i\ =\ 1,...,n}\) at time \(ti\), labeled training set \(S_{ti\ -\ 1}\ =\ \{x_{s,ii^{\prime}\ -\ 1}^{(i)}y_{s,ii}^{(i)}\}_{i\ =\ 1,...,n}\) at time \(ti\ -\ 1\) to predict sleep stages at time \(ti\), unlabeled test set \(T_{ii}\ =\ \{x_{t,ii}^{(i)}\}_{i=\ 1,...,n}\), feature encoder \(G_{f}\) with parameters \(\theta_{f}\), classifier \(G_{1y}\) with parameters \(\theta_{1y}\), classifier \(G_{2y}\) with parameters \(\theta_{2y}\), domain aligning layer \(G_{d}\) with parameters \(\theta_{d}\), learning rate \(\mu\), and batch size M. for the number of training iterations do  Sample a mini-batch of training set \(s_{ii}\ =\ \{x_{s,ii^{\prime}}^{(i)}y_{s,ii}^{(i)}\}_{i\ =\ 1,...,M^{\prime}}\)\(s_{ii\ -\ 1}\ =\ \{x_{s,ii^{\prime}}^{(i)}\}_{i\ =\ 1,...,M^{\prime}}\), \(t\ =\ \{x_{t}^{(i)}\}_{j\ =\ 1,...,M}\). Concatenate \(s_{ii}\) and \(t_{ii}\) as \(B_{st,ii}\ =\ \{(x_{s,ii^{\prime}}^{(i)}y_{s,ii}^{(i)}),(x_{t,ii}^{(i)})\}_{i\ =\ 1,...,M}\), sample weights \(\omega_{st}\ =\ \{(1,1,\ldots,1)_{M},(0,0,\ldots,0)_{M}\}\). Split embedding vector \(V_{st,ii}\ =\ \{v_{s,ii^{\prime}}^{(i)}\}_{i\ =\ 1,...,M}\), \(i\ =\ 1,...,M}\) to \(V_{s,ii}\ =\ \{v_{s,ii^{\prime}}^{(i)}\}_{i\ =\ 1,...,M}\) and \(V_{t,ii}\ =\ \{v_{t,ii^{\prime}}^{(i)}\}_{j\ =\ 1,...,M}\), which is generated by \(G_{f}\) when \(B_{st,ii}\) is input into the feature encoder.