# A GNN-Based False Data Detection Scheme for Smart Grids
## 4 The GNN-Based False Data Detection Scheme
### Cross-Domain Anomaly Detection  
As shown in Figure 2, after obtaining the next attribute values related to the power network, we assigned the attribute values to the power network, as well as the nodes of the physical transmission network to represent the values of the relevant factors that affected the nodes at the future time based on geographical proximity. As shown in Algorithm 2, to use anomaly information of the communication network and the the physical transmission network for the power network anomaly detection. First, we should perform domain adaptation for different domain graphs, which involved learning a domain-invariant representation from the combined samples of the Source and Target domains. Figure 3: Strutural diagram of the STGNN. ```
1:Layer \(l\), node \(v_{i}\)
2:function of cross-domain anomaly detection
3:for\(r^{(t)},u^{(t)},C^{(t)}\)do
4:\(h_{i}^{(l+1)}=\sigma\Big{(}\sum_{j\in\mathcal{N}_{i}}W\cdot\text{MEAN}(h_{i}^{(l )}\cup h_{j}^{(l)})\Big{)}\)
5:for the latent representation \(z_{i}\) of the node \(i\)do
6:\(p_{i}=tanh(W_{d}z_{i}+b_{d}),\hat{y}_{i}=sigmod(u_{c}^{T}q_{i})\)
7:for\(\hat{y}\),\(u\)do
8:\(\mathcal{L}_{C}=\frac{1}{N_{S}}\sum_{i=1}^{N_{S}}[y_{i}\hat{y}_{i}+(1-y_{i}) \log(1-\hat{y}_{i})]\)
9:endfor
10:endfor
11:return\(\mathcal{L}_{C}\)
```  
**Algorithm 2** Cross-Domain Anomaly Detection  
We used GraphSage [25] to construct a shared encoder that extracts a potential representation of each node in the source and target graphs. In a formal manner, in the layer \(l\), the node \(v_{i}\) obtained a representation of layer \(l+1\) by integrating the features of neighboring nodes through the following equation:  
\[h_{i}^{(l+1)}=\sigma\Bigg{(}\sum_{j\in\mathcal{N}_{i}}W\cdot\text{MEAN}(h_{i}^ {(l)}\cup h_{j}^{(l)})\Bigg{)} \tag{5}\]  
where \(\mathcal{N}_{i}\) stands for the set of neighbors for \(v_{i}\). By stacking multiple GraphSage layers, an encoder that maps the learned nodes from different graphs into a shared embedding space was built. This encoder also facilitated the transfer of knowledge between graphs from different domains. To enhance the encoder's domain adaptation capacity, we employed the approach from [28]; here, a domain discriminator was employed to determine whether the node embedding originate in the source or target graph. Then, the encoder was trained adversarially to fool the domain discriminator, effectively performing adversarial domain adaptation as described by [29] in a two-player minimax framework. The domain discriminator _Dis_ was constructed by an MLP with tanh nonlinearity:  
\[ \begin{split} p_{i}&=tanh(W_{d}z_{i}+b_{d})\\ \tilde{d}_{i}&=sigmoid(u_{d}^{T}p_{i})\end{split}  \tag{6}\]  
where \(p_{i}\) is the output of the MLP, \(\hat{y}\) denotes the predicted domain label, and \(u\) denotes a trainable weight vector. The discriminator loss could be formulated as follows:  
\[\mathcal{L}_{D}=-\frac{1}{N}\sum_{i=1}^{N}[d_{i}\log\tilde{d}_{i}+(1-d_{i}) \log(1-\tilde{d}_{i})] \tag{7}\]  
Here, \(N\) represents the total number of nodes in different graphs. \(d_{i}\) denotes the domain label of node \(i\). We closed the differences between discrepant domains via maximizing the above loss function. In other words, the domain labels of nodes in different graphs were not accurately identified by the domain discriminator. Adapting to learning tasks in other domain, we developed an anomaly classifier following a shared encoder to determine whether a node in the source graph was anomalous or not. The anomaly classifier \(Cls\) was formulated through an MLP with tanh nonlinearity and then a sigmoid function:  
\[ \begin{split} q_{i}&=tanh(W_{c}z_{i}+b_{c})\\ \tilde{y}_{i}&=sigmoid(u_{c}^{T}q_{i})\end{split}  \tag{8}\]  
Anomaly classification loss is formulated as follows:  
\[\mathcal{L}_{C}=-\frac{1}{N_{S}}\sum_{i=1}^{N_{S}}[y_{i}\log\tilde{y}_{i}+(1- y_{i})\log(1-\tilde{y}_{i})] \tag{9}\]  
Here, \(N_{S}\) represents the number of nodes selected from the labeled source graph.