# A Hierarchical Federated Learning Algorithm Based on Time Aggregation in Edge Computing Environment
## 3 Dynamic Weighted Hierarchical Federated Learning Algorithm
### Data Heterogeneity  
Data heterogeneity in federated learning is characterized by differences in data distribution and quantity, which can vary significantly due to environmental preferences and usage patterns [22; 23]. However, current research mainly focuses on the non-IID problem in single-mode datasets, where each participant's dataset only contains one type of data mode, such as pictures, text, audio, or video [24]. This paper aims to address the non-IID distribution of image data for a classification task, focusing on label distribution offsets,where the sample labels in each participant's local dataset are unevenly distributed. The impact of non-IID data on the training effectiveness of the federated learning architecture is examined to provide insights into its performance in such scenarios. In this section, the FedAvg algorithm is contrasted with the centralized training method to illustrate the impact of non-IID data on federated learning. Suppose that the problem is defined as a \(K\)-classification task with a feature space \(\mathcal{X}=\{x|1,2,\dots,X\}\), label space \(Y=\{y|1,2,\dots,K\}\), and the sample data \(\{x,y\}\) that obey the data distribution \(P\). The classification task can be described as obtaining the probability distribution \(Q=\{q_{k}|k=1,2,\dots,K,q_{k}\geq 0,\sum_{k=0}^{K}q_{k}=1\}\) of the classification results corresponding to sample feature \(x\) through function \(f\), where \(\omega\) is the weight of the neural network. The cross-entropy function is typically used as the loss function \(\ell(\omega)\). Therefore, the classification problem becomes a minimization problem as follows:  
\[\min_{\omega}\sum_{k=1}^{K}P(y=k)[-\log f_{k}(x,\omega)], \tag{5}\]  
where \(P(y=k)\) is the true probability of the \(k\)-th classification and \(f_{k}(x,\omega)\) is the predicted probability value of the \(k\)-th class. To optimize the parameter \(\omega\), SGD is used for iterative solutions. Under the centralized training method of the \(t\)-th round, the parameter \(\omega_{ccn}^{t}\) is updated as follows:  
\[\omega_{ccn}^{(t)}=\omega_{ccn}^{(t-1)}-\eta\
abla\ell\Big{(}\omega_{ccn}^{(t- 1)}\Big{)}=\omega_{ccn}^{(t-1)}-\eta\
abla\sum_{k=1}^{K}P(k)\cdot\Big{[}-\log f _{k}\Big{(}x,\omega_{ccn,k}^{(t-1)}\Big{)}\Big{]}. \tag{6}\]  
Assuming that \(N\) clients are participating in coordinated training, the local training of each participating client \(n\) can be regarded as concentrated training locally. Furthermore, if the model parameter \(\omega_{n}^{(t)}\) can be obtained after \(t\) local iterations, the local SGD update given in Equation (3) for federated learning can be rewritten as:  
\[\omega_{n}^{(t)}=\omega_{n}^{(t-1)}-\eta\
abla\ell\Big{(}\omega_{n}^{(t-1)} \Big{)}=\omega_{n}^{(t-1)}-\eta\
abla\sum_{k=1}^{K}P_{n}(k)\cdot\Big{[}-\log f _{k}\Big{(}x,\omega_{n,k}^{(t-1)}\Big{)}\Big{]}, \tag{7}\]  
where \(P_{n}\) represents the data distribution of client \(n\). After \(T\) rounds of global aggregation as shown in Equation (4), the federated learning global model \(\omega_{fedavg}^{(T)}\) can be obtained. There are differences between the model parameter results obtained after updating the same initialized model parameters by FedAvg and centralized training methods, respectively. This discrepancy is also known as the model weight divergence and is defined as \(\Big{\|}\omega_{fedavg}-\omega_{ccn}\Big{\|}/\|\omega_{ccn}\|\). As the non-IID degree of data increases, the weight divergence will cumulatively increase during the training process. When the client's data are independent and identically distributed (IID), the divergence between \(\omega_{fedavg}^{(t)}\) and \(\omega_{ccn}^{(t)}\) will be small, and the two will be comparable. When the q is non-IID, the divergence between \(omega_{fedavg}^{(t)}\) and \(\omega_{ccn}^{(t)}\) will be significant. Figure 1 illustrates the cumulative process of divergence between \(\omega_{fedavg}^{(T)}\) and \(\omega_{ccn}^{(T)}\).