## 2 Materials and Methods
### Models
The amalgamation of the outputs from both layers results in the vector \(x_{T}\) and is computed as follows:  
\[h_{t}^{f} =\tanh\left(W_{xh}^{f}x_{t}+W_{hh}^{f}h_{t-1}+b_{h}^{f}\right) \tag{4}\] \[h_{t}^{b} =\tanh\left(W_{xh}^{b}x_{t}+W_{hh}^{b}h_{t+1}+b_{h}^{b}\right)\] \[y_{t} =\left(W_{hy}^{f}h_{t}^{f}+W_{hy}^{b}h_{t}^{b}+b_{y}\right)\]  
In Equation (4), \(W_{x}\) values are the input-to-hidden-layer weights, \(W_{h}\) values are the consecutive hidden-to-hidden-state weights, and \(b\) is the bias vector of the hidden state. To emphasize any patterns found across the features, we employ the Luong-style soft-attention mechanism of the dot product proposed in [21]. Let the outputs of the BiLSTM block be \(\hat{X}_{i}=[\hat{x_{1}},\hat{x_{2}}\dots\hat{x_{T}}]\) combined into a matrix \(A\) of size \(N\times T\), where \(N\) is the size of the output vector and \(T\) is the number of time steps. Then, the weighted output vector \(h_{attn}\) of the attention mechanism is formulated as  
\[\beta =\text{softmax}\left(w_{attn}^{T}A\right) \tag{5}\] \[h_{attn} =A\beta^{T}\]  
In Equation (5), \(\beta\) is a weight vector calculated from the matrix \(A\), and the output \(h_{attn}\) is calculated as the weighted sum of all outputs leaving the BiLSTM block. The proposed model is shown in Figure 3 and consists of a single dilated convolutional layer with rectified linear unit (ReLU) on a time-distributed wrapper, a BiLSTM layer, and an attention mechanism followed by two fully connected layers with ReLU and sigmoid activations, respectively. In terms of regularization, batch normalization was added after the convolutional layer reduced covariance shift and a dropout layer preceded and succeeded the BiLSTMAttn block to mitigate overfitting. The model accepts the first input, with a dimensionality of [2552, 128, 128, 3, 1] indicating timesteps of 10 (3 s), with features of per-image signal sampling points. The CNN output is a \(X\)-dimensional feature vector that summarizes the spatial irregularities and patterns found in the raw input signal. This output is propagated through the BiLSTM network, where the temporal patterns are captured and fed into the attention mechanism to highlight the integral aspects of the input. The model then accepts the second auxiliary input of sleep features and additional EHR measurements with a dimensionality of [37; 1] and concatenates this into the output of the attention mechanism to help improve the representation of the input. Finally, this concatenated vector passes through a sigmoid activation function for binary classification of the different classes. The CNN-BiLSTM architecture, combined with an attention mechanism, is hypothesized to be more applicable to hypnodensity and hypnogram data because it effectively utilizes spatial and temporal characteristics. CNNs are adept at capturing local patterns in the data, such as sharp transitions or microarousal, which are key to identifying sleep stages. By convolving over the input, CNNs extract hierarchical features while maintaining robustness to noise or slight temporal misalignments. This is especially important for hypnodensity images, where the transitions between stages or subtle changes in probabilities require detailed local feature extraction. The BiLSTM component complements CNNs by capturing temporal dependencies and contextual trends within sleep data. Sleep staging has a strong temporal nature, as current stages are influenced by prior and subsequent epochs. BiLSTM processes sequences in both forward and backward directions, allowing the model to understand the broader context, such as recurring sleep cycles or transitions between sleep stages. The addition of an attention mechanism enhances this process by dynamically focusing on contextually important features, such as critical stage transitions or ambiguous epochs, making the model more robust to intra- and inter-rater disagreements. The attention mechanism also improves interpretability by highlighting which regions of the data were the most influential in the predictions.