## 3 CSCL-WVE-UDI: Method for Identifying Unknown IoT Devices
### Algorithm for Generating Permutation Positive-Negative Sample Pairs (PNSPs)
#### 3.4.2 CSCL Function  
To accurately measure the similarity between IoT devices, a contrastive loss function is typically employed to guide the training process of the Siamese network. This function trains the Siamese network by minimizing the contrastive loss, thereby generating compact and dispersed interclass embedding vectors [15]. However, the PNSP algorithm proposed in Section 3.3.3 causes a significant imbalance: the number of negative sample pairs will significantly exceed that of positive ones. If we continue using the contrastive loss function as the learning objective for the Siamese network, then the loss for positive and negative sample pairs will retain the same weighting factors, thus causing the identification boundary of the Siamese network to skew toward negative sample pairs and degrade the recognition performance for the same IoT device. In IoT device identification, distinguishing between identical and different devices is important. Therefore, the classical contrastive loss function is inadequate for processing PNSPs. To mitigate the bias in the decision boundary toward negative sample pairs due to the insufficient number of positive sample pairs, we propose a CSCL function that combines cost-sensitive weighting factors [16] with the contrastive loss function [15]. The CSCL function comprises two components, as presented in Equation (3). The left section represents the loss for positive sample pairs, whereas the right section denotes the loss for negative sample pairs. Here, \(W\) represents the network weights; \(e_{a}\) and \(e_{b}\) denote the embedding vectors for samples \(a\) and \(b\), respectively. The indicator \(y_{ab}\) indicates whether \(e_{a}\) and \(e_{b}\) belong to the same class; if \(e_{a}\) and \(e_{b}\) belong to the same class, then \(y=1\); otherwise, \(y=0\). The threshold \(m\) denotes the minimum acceptable distance among negative sample pairs. First, the model weights \(W\) are obtained by training the Siamese network. Let \(d_{w}\) represent the Manhattan distance between \(e_{a}\) and \(e_{b}\) to assess the similarity between the two embedding vectors. It can be calculated using Equation (4), where \(i\) is the feature dimensions of the embedding vectors. Next, the disparity in the number of positive and negative sample pairs is utilized to compute the cost-sensitive weighting factors. The loss of positive sample pairs \(\lambda_{po}\) and that of negative sample pairs \(\lambda_{ne}\) are calculated using Equations (5) and (6), respectively. Here, \(n_{po}\) and \(n_{ne}\) represent the counts of positive and negative sample pairs, respectively. Since \(n_{po}\ll n_{ne}\), then \(\lambda_{po}\gg\lambda_{ne}\). This condition  
Figure 6: Structure of a single subnetwork of the Siamese network. enhances the sensitivity of the Siamese network to the loss of the positive sample pairs to that of the negative sample pairs, i.e., \(\lambda_{po}y_{ab}d_{w}^{2}>>\lambda_{ne}(1-y_{ab})\big{\{}\max(0,m-d_{w}^{2}) \big{\}}\), which consequently increases the focus on the positive sample pairs. \[L(W,(e_{a},e_{b},y_{ab}))=\lambda_{po}y_{ab}d_{w}^{2} \tag{3}\] \[+\lambda_{ne}(1-y_{ab})\{\max(0,m-d_{w}^{2})\}\]  
\[d_{w}=\sum_{i=0}^{255}|e_{ai}-e_{bi}| \tag{4}\] \[\lambda_{po}=\frac{n_{ne}}{n_{po}+n_{ne}}\] (5) \[\lambda_{ne}=1-\lambda_{po}=\frac{n_{po}}{n_{po}+n_{ne}} \tag{6}\]  
Based on Equations (3)-(6), when \(y_{ab}=1\), i.e., the sample pairs are similar, the loss is \(L=\lambda_{po}d_{w}^{2}\). In this case, we are only required to minimize \(d_{w}\). When \(y_{ab}=0\), i.e., the sample pairs are different, and minimizing the loss is equivalent to maximizing \(\lambda_{ne}\big{\{}\max\big{(}0,m-d_{w}^{2}\big{)}\big{\}}\). In this case, an extremely small value of \(d_{w}\) suggests that the embedding vectors of negative sample pairs are closely spaced, thus warranting an increased penalty \(\lambda_{ne}\big{(}m-d_{w}^{2}\big{)}\) to reduce the loss.