# 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 edge-oriented hierarchical federated learning algorithm optimizes the conventional FedAvg algorithm by designing a hierarchical algorithm process with an added edge aggregation step. To address the heterogeneity of client data, a dynamic weighting method is used to adjust the contribution of each local model to the global model. The shared dataset is selected under each branch, and the Earth mover's distance (EMD) is used to represent the difference between the local data distribution and the shared data distribution. The weight of each local model parameter is adjusted based on the EMD value during edge aggregation. The next section will detail the hierarchical federated learning algorithm based on dynamic weighting. 1. Local training. Assume that there is a set of client devices \(\mathcal{N}=\{n|n=1,2,\ldots,N\}\), and each client device \(n\) has a local data collection \(\{D_{n}|n=1,2,\ldots,N\}\). When the \(t\)-th round of global training begins, the central server distributes the global model parameters to each edge server \(\mathcal{M}=\{m|m=1,2,\ldots,M\}\). The edge server \(m\) then randomly selects the client devices \(\mathcal{C}=\{i|i=1,2,\ldots,C;C\leq N\}\) that participate in this round of global training. Let \(P_{i}\) denote the local data distribution of participating client \(i\), and let \(P_{m}\) denote the shared data distribution of edge server \(m\). The learning task of a participating client device \(i\) is to iteratively optimize its parameter through \(E\) rounds of random gradient descent. The optimization process is the same as FedAvg's local calculation process. The iterative loss function is selected as cross-entropy, and the loss value of a single node in each round can be expressed as \(F_{i}(\omega_{i},P_{i})\). The local model parameter \(\omega_{i}^{(l)}\) of the \(l\)-th local iteration is then computed as follows:  
\[\omega_{i}^{(l)}=\omega_{i}^{(l-1)}-\eta\
abla F_{i}\Big{(}\omega_{i}^{(l-1)}, P_{i}\Big{)}. \tag{8}\]  
Accordingly, the time consumed by a client device for local training is affected by the device's computing power (determined by the CPU) and the size of the training dataset. The total time delay generated by device \(i\) through \(E\) rounds of iterative local computations is defined as follows:  
\[T_{(cmp,i)}=\frac{c_{i}|D_{i}|}{f_{i}}\cdot E, \tag{9}\]  
where \(c_{i}\) is the number of CPU cycles required for device \(i\) to process a data sample. Since each data sample \(\left(x_{j},y_{j}\right)\) has the same size, the total number of CPU cycles for a local iteration can be expressed as \(c_{i}|D_{i}|\). \(f_{i}\) denotes the CPU frequency allocated by the client device \(i\). For a \(K\)-classification training task, the differences between the local data distribution \(P_{i}\) and shared data distribution \(P_{m}\) can be measured by calculating the EMD, expressed as:  
\[EMD_{(i,m)}=EMD(P_{i}.P_{m})=\left\|\sum_{k=1}^{K}P_{i}(y=k)-P_{m}(y=k)\right\| \tag{10}\]  
According to the analysis in Section 3.2, a larger value of \(EMD_{(i,m)}\) indicates a greater gap between the local data distribution of the client and the shared data distribution of the edge server, leading to greater divergence of the trained local model. Therefore, to limit this deviation to some extent, it is necessary to reduce the weight assignment of the model during the aggregation process. (2) Edge aggregation. After completing local training, each participating client device \(i\) uploads its local model parameter \(\omega_{i}^{t}\) and data distribution deviation \(EMD_{(i,m)}\) to the corresponding edge server \(m\), which generates a communication delay \(T_{com}\).