# Hierarchical Federated Learning for Edge-Aided Unmanned Aerial Vehicle Networks
## 3 Hierarchical FL Algorithm  
The key idea of the proposed hierarchical FL algorithm is that the edge servers are used as intermediate aggregators with commonly shared data to improve the performance of learning, even with non-i.i.d data. For this purpose, in practice, one can collect exemplary data samples from the UAVs and employ them as the commonly shared data. In the hierarchical FL, the commonly shared data are used to train the local models at the edge servers. In addition, we suggest aggregating the local models of both the UAVs and edge servers hierarchically. Detailed explanations are given in the following. The proposed hierarchical FL algorithm for the edge-aided UAV network is presented in Algorithm 1, where \(T\) is the overall aggregation step. In addition, \(C\) denotes the fraction of UAVs participating in the hierarchical FL, which are selected from the total \(N\) UAVs. ```
1:\(\mathcal{P}_{i}\), \(\mathcal{P}



models can be reduced in the case of attack, which ensures that the single backdoor has less effect on the overall update procedure. ```
1:Initialize \(w_{0}\) and \(\mathcal{Q}\)
2:for\(t=0,1,\ldots T-1\)do
3:for each edge \(l=1,\ldots,L\)do // in parallel
4:\(\{w_{i}^{l}\}_{i\in\mathcal{C}_{l}}\gets w_{t}\)
5:for\(k=1,2,\ldots k_{2}\)do
6:for each UAV \(i\in C_{l}\)do // in parallel
7:for\(j=1,2,\ldots k_{1}\)do
8:\(w_{i}^{l}\leftarrow\) LocalUpdate\((i,w_{i}^{l},\mathcal{P}_{i})\) in Algorithm 2
9:endfor
10:endfor
11:\(w^{l}\leftarrow\) EdgeAggregation\((l,\{w_{i}^{l}\}_{i\in\mathcal{C}_{l}})\) in Algorithm 3
12:endfor
13:endfor
14:\(w_{t+1}\leftarrow\) GlobalAggregation\((\{w_{t}^{l}\}_{l=1}^{L})\) in Algorithm 4
15:endfor
```
**Algorithm 1** Proposed hierarchical FL algorithm. After \(k_{1}\) local iterations, each UAV sends its local model \(w_{i}^{l}\) trained with private dataset \(\mathcal{P}_{i}\) to the edge servers. Upon receiving the local models from the corresponding UAVs, the edge servers perform the _EdgeAggregation_ procedure in Algorithm 3, wherein the local models \(w_{i}^{e}\) of the edge servers are trained with the shared dataset \(\mathcal{Q}\) and those are then aggregated together with the local models of the UAVs. ```
1:functionLocalUpdate\((w,\mathcal{P})\)
2:\(\mathcal{B}\leftarrow\) (split \(\mathcal{P}\) into batches of size \(B\))
3:for each local epoch \(i\) from \(1\) to \(k_{1}\)do
4:for batch \(b\in\mathcal{B}\)do
5:\(w\gets w-\eta\
abla(w;b)\)
6:endfor
7:endfor
8:return\(w\)
9:endfunction
```
**Algorithm 2** Local update procedure. After \(k_{2}\) iterations of the _EdgeAggregation_ procedure, the edge servers send their aggregated models \(\{w_{t}^{l}\}_{l=1}^{L}\) to the cloud server, where the global model \(w_{t+1}\) is obtained according to the _GlobalAggregation_ procedure in Algorithm 4. Overall, the local update at the \(i\)th UAV assigned to the \(l\)th edge server takes the following form:\[\mathbf{w}_{i}^{l}(t+1)= \begin{cases}w_{i}^{l}(t)-\eta_{t}\
abla F_{i}^{l}(w_{i}^{l}(t),\mathcal{P}_{i}),&rem(t,k_{1})\
eq 0\\ \frac{1}{2}\bigg{[}\sum_{i\in\mathcal{C}_{l}}\frac{n_{i}}{n_{l}}w_{i}^{l}(t)+w ^{l}(t)\bigg{]}&rem(t,k_{1})=0\\ \sum_{l=1}^{L}\frac{n_{l}}{n}w^{l}(t),&rem(t,k_{1}k_{2})=0\end{cases} , \tag{5}\]

which is clearly different from that of the traditional FL in Equation (3). We note that when the intermediate aggregator is unable to perform the _EdgeAggregation_ procedure due to the low system resources, which is the case when \(k_{1}=1\) and \(w^{l}(t)=\sum_{i\in\mathcal{C}_{l}}\frac{n_{i}}{n_{l}}w_{i}^{l}(t)\), the overall process reduces to FedAvg Equation (3). ```
1:functionGlobalAggregation(\(\{\mathbf{w}_{t}^{l}\}_{l=1}^{L}\))
2:\(w_{t+1}\leftarrow\sum_{l=1}^{L}\frac{n_{l}}{n}w_{t}^{l}\)
3:return\(w_{t+1}\)
4:endfunction
```

**Algorithm 4** Global aggregation procedure.