# A Domain-Based, Adaptive, Multi-Scale, Inter-Subject Sleep Stage Classification Network
## 2 Materials and Methods
### Pre-Training
A random sample of 100 sleep records from a related dataset was used to count the number of segments in each sleep stage, as shown in Table 1. The sample sizes for the deep sleep stage and REM are larger compared to the wake and light sleep stages. To avoid the model overfitting the head classes that have a large number of samples and ignoring the tail classes, the multi-class focal loss is used to balance the contributions of different classes. The loss function formula is as follows:  
\[L_{1y}\ =\ -\frac{1}{n}\sum_{\alpha}\sum_{i\ =\ 1}^{C}\alpha_{i}(1\ -\ p(x))^{ \gamma}q(x)\log p(x). \tag{5}\]  
The model is supervised by the labels \(q\) and prediction results \(p\) of samples \(x\) whose batch size is set to \(n\), and the number of classes is \(C\). The weight \(\alpha_{i}\) of class \(i\) is set based on the proportion of the corresponding class to the total samples. According to the work of [PERSON] et al. (2017) [29], the factor \(\gamma\) is set to 2 in this paper. \begin{table}
\begin{tabular}{c c c c c c c} \hline \hline  & \multicolumn{2}{c}{**SHHS2**} & \multicolumn{2}{c}{**SHHS1**} & \multicolumn{2}{c}{**MESA**} \\ \cline{2-7}  & **Number of** & **Percentage of** & **Number of** & **Percentage of** & **Number of** & **Percentage of** \\  & **Segments** & **Categories** & **Segments** & **Categories** & **Segments** & **Categories** \\ \hline Wake & 54,137 & 42.4\% & 41,490 & 42.3\% & 38,966 & 44.0\% \\ Light Sleep & 45,551 & 35.7\% & 32,099 & 32.7\% & 34,448 & 38.9\% \\ Deep Sleep & 15,472 & 12.1\% & 12,810 & 13.0\% & 9460 & 10.7\% \\ REM & 12,531 & 9.8\% & 11,753 & 12.0\% & 5706 & 6.4\% \\ \hline \hline \end{tabular}
\end{table} 
Table 1: The sequence number of each class of 100 random sleep records in each public dataset. Figure 3: The MESA’s visual feature map of the first convolutional layer for the different residual modules whose kernel size is 5 samples, 11 samples, and 21 samples, respectively. (**a**) A random ECG sequence for MESA, (**b**) feature maps extracted by residual modules whose kernel size is 5, (**c**) feature maps extracted by residual modules whose kernel size is 11, and (**d**) feature maps extracted by residual modules whose kernel size is 21. The transition of sleep states is correlated with timing [30]. For example, the wake stage can enter the REM stage or the NREM light sleep stage but cannot transfer to the NREM deep sleep stage directly. Inspired by this, in the process of supervised sleep stage classification, time-series historical information is added as additional information. We used historical 30 s ECG data to predict the sleep stage of the current period via LSTM. The output is then activated through the fully connected layer, the value of which is optimized by the cross-entropy loss:  
\[L_{2y}\ =\ -\frac{1}{n}\sum_{x}\sum_{i\ =\ 1}^{C}q(x)\log p(x). \tag{6}\]  
Finally, the total loss of the pre-trained model is the combination of \(L_{1y}\) and \(L_{2y}\) with a weight \(\rho\), which is optimized by the supervised information:  
\[\mathrm{L}_{pt}\ =\ L_{1y}\ +\ \rho L_{2y}. \tag{7}\]