A federated learning method and medium
By adaptively adjusting sample weights by combining sample loss and uncertainty in federated learning, the problem of model instability caused by data noise is solved, and a more stable federated learning process and performance improvement are achieved.
Patent Information
- Application Number
- CN202311512264.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-14
- Publication Date
- 2024-09-24
- Estimated Expiration
- 2043-11-14
AI Technical Summary
In existing federated learning methods, when data noise exists, model parameter changes are unstable, resulting in slow convergence speed, unstable convergence process, and reduced model performance.
By combining the sample loss and sample uncertainty output by the global model, the sample weight is adaptively adjusted to determine the sample set participating in training, and the local model and global model are updated in each iteration until the preset iteration stop conditions are met.
It achieves the stabilization of the federated learning process when the training data contains label noise, and improves the performance of the federated learning global model.
Smart Images

Figure CN117556253B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of machine learning, and in particular to a federated learning method and a computer-readable storage medium. Background Art
[0002] Machine learning (ML) tends to be trained on large-scale data and models to obtain better generalization performance. Individuals or organizations collect their own training data but are usually reluctant to share it due to privacy issues. Therefore, federated learning (FL) is proposed to collaboratively train global models on isolated datasets from different clients without transmitting client data over the network.
[0003] Typical federated learning methods, such as FedAvg, allow local updates to reduce communication frequency and protect data privacy. They learn the global model based on the following three steps: (i) In each iteration, a portion of clients are selected and obtain the current global model from the server; (ii) these clients use local data to update their local models for several steps; (iii) the server selects the latest local model and updates the global model according to certain rules. This process is repeated until convergence.
[0004] However, the inventors of this application found in their research that each client receives the global model of the server and updates the model using its own training data. This causes significant changes in model parameters and the training process exhibits unstable convergence. This situation is more serious when there is noise in the data, because each client has different standards for collecting data, and the generalization performance of the local model trained on noisy data will be impaired, especially for neural networks, which are prone to overfitting to the training data, which slows down the convergence of federated learning, makes the convergence process unstable, and greatly reduces the model performance. Summary of the invention
[0005] In response to the above problems, the purpose of this application is to provide a federated learning method and a computer-readable storage medium, which combines the sample loss and sample uncertainty output by the global model, adaptively adjusts the weight of the sample, and achieves performance improvement of the global model of federated learning; at the same time, the federated learning process can be stabilized when there is label noise in the training data.
[0006] To achieve the above objectives, this application adopts the following technical solutions:
[0007] In a first aspect, the present application provides a federated learning method, the method comprising:
[0008] S1, initialize the global model on the server side, and initialize the local model and sample weights of each client;
[0009] S2, starting from the first iteration, each client obtains the initial global model in the current iteration number, and calculates the sample uncertainty and loss value of each sample based on the global model;
[0010] S3, each client adjusts the weight of the sample according to the calculated sample uncertainty and loss value of each sample, and determines the sample set participating in the training under the current number of iterations based on the adjusted samples;
[0011] S4, each client updates the local model according to the sample set participating in the training at the current iteration number, and uploads the updated local model to the server, so that the server performs global optimization according to the updated local model of each client to update the global model;
[0012] S5, updates the number of iterations and returns to S2 to execute until the preset iteration stop condition is met.
[0013] In one implementation of the present application, each client optimizes the loss function based on the sample weight, and the optimized mathematical formula is:
[0014]
[0015] Among them, (x j ,y j ) is the jth sample on client i, and the number of samples on client i is n i ω is the model parameter of the global model in the current iteration; f(ω,·) represents the global model in the current iteration according to the model parameters and input x j The model output obtained by calculation, l(y j , f(ω, x j )) is the loss value of the jth sample; Indicates n i The weight value of each sample is a set; ||·||1 represents the operation of finding the 1-norm of the vector.
[0016] In one implementation of the present application, the mathematical formula for global optimization on the server side is:
[0017]
[0018] Where M is the number of clients and N is the total number of training samples on all clients.
[0019] In one implementation of the present application, in S1, the global model and the local model of each client are randomly initialized;
[0020] Initialize the weight of each sample of each client to 1.
[0021] In one implementation of the present application, in S2, the calculation formula for the uncertainty of each sample x is:
[0022]
[0023] Among them, the two prediction results with the top two prediction probability confidences of the global model used in the current iteration batch are and probability difference.
[0024] In one implementation of the present application, in S3, each client adjusts the weight of the sample according to the calculated sample uncertainty and loss value of each sample, including:
[0025] Each client i selects the loss value L of the sample with the largest sample uncertainty i,un For reference, client i sample D i Divide into two sets:
[0026]
[0027]
[0028] Iteratively calculate the sample proportion of weighted question 1 according to the number of elements in the above two sets t is the serial number of the current iteration;
[0029] Sort the samples according to their loss values from small to large. The weights of some samples are reset to 1, and the weights of the remaining samples are reset to 0.
[0030] In one implementation of the present application, in S3, determining a sample set participating in training at the current number of iterations includes:
[0031] The proportion of samples participating in training is calculated according to the following formula:
[0032]
[0033] in, |·| represents the number of data in the dataset; β is a hyperparameter, β≤0.1;
[0034] Arrange the samples from small to large according to their loss values, and select the first samples as the sample set participating in training.
[0035] In one implementation of the present application, in S4, each client updates the local model according to the sample set participating in the training at the current iteration number, including:
[0036] Each client updates the local model using stochastic gradient descent based on the sample set participating in the training.
[0037] In one implementation of the present application, in S4, the server averages the local models uploaded by all clients as the updated global model.
[0038] In a second aspect, the present application provides a computer-readable storage medium, characterized in that a computer program is stored in the computer-readable storage medium, and when the computer program is executed, the device where the computer-readable storage medium is located is controlled to execute the federated learning method described in the first aspect.
[0039] Due to the adoption of the above technical scheme, the present invention has the following advantages: In the application scheme of the present invention, the global model of the server side is first initialized, and the local model and sample weights of each client are initialized, and then the iterative update process of the model is entered, that is, from the first iteration, each client obtains the initial global model in the current iteration number, and calculates the sample uncertainty and loss value of each sample based on the global model, and further adjusts the sample weight according to the calculated sample uncertainty and loss value of each sample, and determines the sample set participating in the training under the current iteration number according to the adjusted sample, and then each client updates the local model according to the sample set participating in the training under the current iteration number, and uploads the updated local model to the server side, so that the server side performs global optimization according to the updated local model of each client to update the global model, and then updates the iteration number, and re-executes the iteration until the preset iteration stop condition is met, so that compared with the prior art, the sample loss and sample uncertainty output by the global model are combined to adaptively adjust the sample weight, so as to achieve the performance improvement of the global model of federated learning. At the same time, the federated learning process can be stabilized when there is label noise in the training data. . BRIEF DESCRIPTION OF THE DRAWINGS
[0040] Figure 1 The topological relationship between the server and the client and the parameter transfer process in the federated average method;
[0041] Figure 2 A schematic diagram of a network structure of a global model or a local model used in an embodiment of the present invention;
[0042] Figure 3 This is a diagram showing the convergence and performance stability of the federated average algorithm during training in an embodiment of the present invention when label noise is added to client data;
[0043] Figure 4 This is a curve diagram showing how the adaptive data selection ratio changes during the training process in an embodiment of the present invention. DETAILED DESCRIPTION
[0044] In order to make the purpose, technical solution and advantages of the embodiment of the present invention clearer, the technical solution of the embodiment of the present invention will be clearly and completely described below in conjunction with the drawings of the embodiment of the present invention. Obviously, the described embodiment is a part of the embodiment of the present invention, not all of the embodiments. Based on the described embodiment of the present invention, all other embodiments obtained by ordinary technicians in this field belong to the scope of protection of the present invention.
[0045] Aiming at the problem that the average class federated learning model in the prior art has slow convergence speed and poor stability under noisy training data. The present application provides a federated learning method and a computer-readable storage medium accordingly. Among them, the method described includes: S1, initializing the global model of the server side, and initializing the local model and sample weights of each client; S2, from the first iteration, each client obtains the initial global model in the current iteration number, and calculates the sample uncertainty and loss value of each sample based on the global model; S3, each client adjusts the sample weight according to the calculated sample uncertainty and loss value of each sample, and determines the sample set participating in the training under the current iteration number according to the adjusted sample; S4, each client updates the local model according to the sample set participating in the training under the current iteration number, and uploads the updated local model to the server side, so that the server side can perform global optimization according to the updated local model of each client to update the global model; S5, update the number of iterations, and return to S2 to execute until the preset iteration stop condition is met. The technical solution of the present application realizes the performance improvement of the global model.
[0046] Please further illustrate the above method in more detailed embodiments of the present application in conjunction with more drawings of the present application.
[0047] In the embodiments of the present application, Figure 1 and Figure 2 To set up a client-server connection and a training network structure according to an embodiment of the present invention, Figure 2 The network structure shown is used for training on the EMNIST dataset. Under the network structure setting of this method, the global optimization problem solved by the server is expressed as:
[0048]
[0049] Among them, n i is the number of samples on client i, N is the total number of training samples on all clients; M is the number of clients.
[0050] The mathematical form of the optimization on client i is:
[0051]
[0052] (x j ,yj ) is the jth sample on client i, and the number of samples on client i is n i ω is the model parameter of the global model in the current iteration; f(ω,·) represents the global model in the current iteration according to the model parameters and input x j The model output obtained by calculation, l(y j , f(ω, x j )) is the loss value of the jth sample; Indicates n i The set of weight values of samples; ||·||1 represents the operation of finding the 1-norm of the vector. It can be seen from the formula that when ω is fixed, the optimal sample weight v i Can be Obtained, I is a unit step function, that is, when l (i,j) When ≤λi holds, otherwise l (i,j) is l(y j , f(ω, x j )) is an abbreviation of the formula, which shows that for client i, the loss is less than λ i Samples with Figure 1 In a specific embodiment of the present application, one communication round is an iterative process, and in each iteration, the client and the server communicate bidirectionally once) a local iteration.
[0053] The specific training process of this method includes the following steps:
[0054] Step A): The network model is randomly initialized and the network parameters are initialized.
[0055] Specifically, the initial model on all clients at this time is ω 0 , and set the weight of each sample to 1, denoted as v 0 , the proportion of samples with weight 1 on the client is set to
[0056] Step B): For the tth communication round, the client receives the global model ω from the server t , and based on ω t Calculate the uncertainty for each sample.
[0057] Specifically, for the tth communication round, the iterative initial model of each client i is recorded as
[0058] For each sample (x j ,y j ), using the model Predict the output results of the sample and calculate the sample uncertainty. The calculation formula is:
[0059]
[0060] in, is the probability difference between the two models’ most confident predictions. For example, the model can be a deep neural network model, and the input is x, which can predict different results (such as semantic classification) and their probabilities. In this example, the result with the highest prediction probability (confidence) is The probability value is recorded as The result with the second highest prediction probability is The probability value is recorded as
[0061] Step C): Each client independently calculates: Select the loss value L corresponding to the sample with the largest sample uncertainty un For reference, the samples are divided into two sets, which are considered as deviation sample sets and sample sets suitable for model training. The sample quantity ratio of the two sample sets is calculated, and the sample ratio r with weight 1 is calculated iteratively. i .
[0062] Specifically, in the tth communication round, the global model is ω t , let the loss corresponding to the sample with the largest uncertainty be L i,un , according to each sample and L i,un The size relationship is that the samples on each client are divided into two sets:
[0063]
[0064]
[0065] Calculate the proportion of samples with smaller loss:
[0066] Iteratively calculate the proportion of samples with a weight of 1 in the tth communication iteration, and the calculation formula is:
[0067]
[0068] Among them, β is a hyperparameter, which is set to β = 0.05 to ensure that there are enough samples to participate in the training when the model performance is unstable in the initial model training stage.
[0069] Step D): In the tth communication round, the calculation result of step C) Identify samples with weight 1.
[0070] Specifically, the samples are sorted from small to large according to their loss values, and the top samples, as the sample set participating in the training in this communication round, denoted as D i,r ;
[0071] Step E): In the tth communication round, perform R times of stochastic gradient descent on the sample weighted result in step D) to obtain an updated local model, which is uploaded to the server to calculate an updated global model.
[0072] Specifically, during the local iteration, from D i,r Select a batch of data with a size of B, denoted as The formula for performing stochastic gradient descent is:
[0073]
[0074] Among them, α t is the learning rate, set to 0.01.
[0075] After R iterations, the local model is updated to Upload to the server;
[0076] Average the local models uploaded by all clients as the new global model:
[0077]
[0078] Step F): Return to step B) and continue until the algorithm converges or reaches the stopping condition.
[0079] Specifically, since the training model based on the neural network of the present invention is used, a fixed communication round is set in advance. The iteration stop condition can also be set in combination with the specific application.
[0080] The federated learning method of the present invention is described in detail below with reference to examples, but it should not be construed as limiting the scope of protection of the present invention.
[0081] Calculation example 1:
[0082] In this example, consider using Figure 2 The ResNet-10 network is trained on the EMNIST dataset. 20 clients are set. The EMNIST data is divided into 20 clients using a sharding method. The data is sorted by label and divided into data slices of equal size, with each client receiving two data slices. This division method makes the client data heterogeneously distributed. Data label noise is added, and the original category label of each sample is flipped to any incorrect category label with a probability of 0.3.
[0083] In the training of the model, we used a momentum SGD optimizer with an initial learning rate of 0.01 and a momentum of 0.9. The learning rate was decreased by 0.99 per round, and a weight decay of 1e-5 was applied. In each round of communication, we selected 10% of the clients to participate in the training of the global model.
[0084] The optimization formula on the client is:
[0085]
[0086] Among them, x j For each image in EMNIST, y j For the corresponding label.
[0087] Step A1): Randomly initialize the global model of federated learning, denoted as ω 0 .
[0088] Step A2): Initialize the sample weights on each client. The initial weight value of all samples is 1, denoted as v 0 , and set the sample ratio with weight 1 on each client to
[0089] Step B1): In the tth communication round, the client receives the global model ω from the server t .
[0090] Step B2): For each sample (x j ,y j ), using the model Predict the output of the sample and calculate the sample uncertainty. The output is converted into probability form using Softmax, and the calculation formula for uncertainty is:
[0091]
[0092] in, is the probability difference between the two models' most confident predictions.
[0093] Step C1): The selected clients search for the samples with the largest uncertainty and obtain the corresponding sample output loss under the global model prediction:
[0094] Step C2): According to each sample and L i,un The size relationship is that the samples on each client are divided into two sets:
[0095]
[0096]
[0097] Calculate the proportion of samples with smaller loss:
[0098] Step C3): Iteratively calculate the proportion of samples with a weight of 1 in the tth communication iteration, and the calculation formula is:
[0099]
[0100] Among them, β is a hyperparameter and is set to 0.05.
[0101] Step D): Sort the samples from small to large according to their loss values and select the top samples, as the sample set participating in the training in this communication round, denoted as D i,r .
[0102] Step E1): For the sample weighted result in step D), i,r Select a batch of data with a size of B, denoted as The formula for performing stochastic gradient descent is:
[0103]
[0104] Here, R is set to ni / B, where B=128.
[0105] After R iterations, the local model is updated to Upload to the server to calculate the updated global model.
[0106] Step E2): Aggregation of the global model
[0107] Step F): Return to step B) and continue until the communication round reaches 1000 times.
[0108] Figure 3 This is a diagram showing the stabilization effect of the sample weighting method of the present invention on the convergence and performance of the federated average class algorithm during training when label noise is added to the client data. Figure 3 It is shown in Figure 2 that our method significantly improves the federated average. It is also better than RFL and RHFL, two methods for dealing with noise problems in federated learning scenarios. RFL needs to transmit the embedding vector and model parameters of each category, while RHFL requires a clean reference dataset. Our method does not require additional communication or a clean reference dataset.
[0109] Figure 4 The adaptive data selection ratio is shown as the training process changes. In the early stages of training, the model cannot effectively extract features and its predictions are inaccurate. iThe value speeds up the convergence process. As training progresses, the proportions of clients stabilize. When adding different proportions of noise, these proportions eventually stabilize at values that match the noise proportions.
[0110] In the several embodiments provided by the present invention, it should be understood that the disclosed method can be implemented in other ways. For example, the embodiments described above are merely illustrative.
[0111] The above-mentioned integrated unit implemented in the form of a software functional unit can be stored in a computer-readable storage medium. The above-mentioned software functional unit is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor (Processor) to perform some steps of the above-mentioned method of each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), disk or optical disk and other media that can store program codes.
[0112] The above are only preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A federated learning method, characterized in that: The method comprises: S1: Initialize the global model on the server side, as well as the weights of the local model and samples on each client. The EMNIST dataset is trained using a ResNet-10 network. The global and local models use the ResNet-10 network, and the samples are data from the EMNIST dataset. The EMNIST dataset data is distributed to the clients using a sharding method. The data is sorted by label and divided into equal-sized slices, with each client receiving data from two slices. S2, starting from the first iteration, each client obtains the initial global model in the current iteration and calculates the sample uncertainty and loss value of each sample based on the global model, where Each sample The calculation formula for the uncertainty of in, The two prediction results with the top two probability confidences of the global model used in the current iteration batch and The probability difference; S3, each client adjusts the sample weight according to the calculated sample uncertainty and loss value of each sample, and determines the sample set participating in training under the current number of iterations based on the adjusted samples. Each client optimizes the loss function based on the sample weights. The mathematical formula for optimization is: in, For the client On the Samples, client The number of samples is ; ; Indicates that the global model in the current iteration is based on the model parameters and input The model output obtained by calculation is It is The loss value of samples; express A set of weight values for samples; Indicates the operation of finding the 1-norm of a vector; is a picture in the EMNIST dataset, is the corresponding label; In S3, each client adjusts the weight of each sample according to the calculated sample uncertainty and loss value of each sample, including: Each client Select the loss value of the sample with the largest sample uncertainty For reference, the client sample Divided into two sets: , , Iteratively calculate the proportion of samples with a weight of 1 based on the number of elements in the above two sets , t is the sequence number of the current iteration; Sort the samples according to their loss values from small to large. The weights of some samples are reset to 1, and the weights of the remaining samples are reset to 0; in, is a sample in the EMNIST dataset, To correspond to 's label; S4, each client updates the local model based on the sample set participating in the training at the current iteration number, and uploads the updated local model to the server, so that the server can perform global optimization based on the updated local model of each client to update the global model; S5, updates the number of iterations and returns to S2 to execute until the preset iteration stop condition is met.
2. The federated learning method according to claim 1, characterized in that: The mathematical formula for global optimization on the server side is: , in, M is the number of clients, is the total number of training samples on all clients.
3. The federated learning method according to claim 2, characterized in that: In S1, the global model and the local model of each client are randomly initialized; Initialize the weight of each sample of each client to 1.
4. The federated learning method according to claim 1, wherein: In S3, determining the sample set participating in training at the current number of iterations includes: The proportion of samples participating in training is calculated according to the following formula: in, , Indicates the number of data in the dataset; is a hyperparameter, ; Arrange the samples from small to large according to their loss values, and select the first samples as the sample set participating in training.
5. The federated learning method according to claim 4, characterized in that: In S4, each client updates the local model according to the sample set participating in the training at the current iteration number, including: Each client updates the local model using stochastic gradient descent based on the sample set participating in the training.
6. The federated learning method according to claim 5, characterized in that: In S4, the server averages the local models uploaded by all clients as the updated global model.
7. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, which, when executed, controls the device where the computer-readable storage medium is located to execute the federated learning method according to any one of claims 1 to 6.
