# A Hierarchical Federated Learning Algorithm Based on Time Aggregation in Edge Computing Environment
## 3 Dynamic Weighted Hierarchical Federated Learning Algorithm
### Hierarchical Federated Learning Algorithm
#### 3.3.2 Hierarchical Federated Learning Training Process Based on Dynamic Weighting
The transmission delay for device \(i\) is defined as follows:  
\[T_{(com,i,m)}=\frac{|\omega_{i}^{t}|}{r_{i}}, \tag{11}\]  
where \(|\omega_{i}^{t}|\) is the size of the local model parameter \(\omega_{i}^{t}\) for client device \(i\), and \(r_{i}\) represents the transmission rate in the channel, which is defined based on the orthogonal frequency division multiple access (OFDMA) protocol:  
\[r_{i}=\beta_{i:m}W_{m}\ln\left(1+\frac{h_{i}P_{i:m}}{N_{0}}\right), \tag{12}\]  
where \(W_{m}\) is the total bandwidth provided by edge server \(m\), \(\beta_{i:m}\) is the bandwidth allocation ratio for client device \(i\), \(P_{i:m}\) indicates the transmission power of client device \(i\), \(h_{i}\) is the channel gain of client device \(i\), and \(N_{0}\) is the Gaussian noise. Assuming that the local model parameters received by the edge server \(m\) are from the participating client devices \(\mathcal{S}_{m}=\{s|s=1,\dots,S;S\leq C\}\), the edge server must perform edge aggregation on these local model parameters \(\omega_{s}^{t}\), and the dynamic weighted aggregation method is used to obtain the edge-aggregated model parameters \(\omega_{m}^{t}\):  
\[\omega_{m}^{t}=\sum_{s=1}^{S}\alpha_{s}\omega_{s}^{t-1}, \tag{13}\]  
where the weight \(\alpha_{s}\) of the local model parameters of participating client \(s\) is based on its \(EMD_{(s,m)}\). According to the above analysis, \(\alpha_{s}\) and \(EMD_{(s,m)}\) are negatively correlated. In other words, the larger the \(EMD_{(s,m)}\) is, the greater the divergence of the local model of client \(s\), and the smaller aggregate weight values need to be allocated. The aggregate weight value must be normalized to the sum of all values. The specific calculation formula is given by Equation (14). \[\alpha_{s}=\frac{\left(1-EMD_{(s,m)}\right)/\left(1+EMD_{(s,m)}\right)}{\sum _{s=1}^{S}\left[\left(1-EMD_{(s,m)}\right)/\left(1+EMD_{(s,m)}\right)\right]} \tag{14}\]When each client's local q is IID, the value of \(EMD_{(s,m)}\) is 0, and \(\alpha_{s}\) is equal to \(\frac{1}{s}\), which can be considered the average weighted aggregation. When the local data of a client \(s\) are non-IID, the higher the degree of non-independent and non-identical distribution of the local data, the greater the \(EMD_{(s,m)}\) value, and the smaller the corresponding \(\alpha_{s}\) value, i.e., the smaller the aggregation weight. After all edge aggregation model parameters \(\omega_{m}^{t}\) are received, the central server initiates the global aggregation. The average weighted aggregation method is used to obtain the global model parameters \(\omega^{t}\):  
\[\omega^{t}=\sum_{m=1}^{M}\frac{|D_{m}|}{|D|}\omega_{m}^{t}, \tag{15}\]  
where \(|D|\) represents the total amount of local data from all participating clients, and \(|D_{m}|\) indicates the amount of local data from the clients participating in the aggregation of edge server \(m\). In the global aggregation, the algorithm is the same as FedAvg, which aggregates the local model parameters of participating clients using a weighted average. However, in contrast to FedAvg, the central server in the hierarchical federated learning architecture aggregates the edge aggregation model parameters.