# CANPerFL: Improve In-Vehicle Intrusion Detection Performance by Sharing Knowledge
## 4 Personalized Federated Learning  
### Federated Learning  
Federated Learning (FL) [35] is an idea of training a model from distributed data across clients while preserving data privacy. The FL setting includes a central server and multiple \(M\) clients. Each client \(k\in[M]\) owns a dataset \(\mathcal{D}_{k}\) having a size of \(n_{k}\) samples and following an underlying distribution over \(\mathcal{X}\times\mathcal{Y}\), where \(\mathcal{X}\) is the input space and \(\mathcal{Y}\) is the label space. The final outcome of the learning process is a set of parameters of a mapping model from input space to label space, e.g., \(h_{\mathbf{\theta}}:\mathcal{X}\rightarrow\mathcal{Y}\). In general, the loss of model parameters \(\mathbf{\theta}\) on the \(k\)-th client is defined as:  
\[f_{k}(\mathbf{\theta})=\frac{1}{n_{k}}\sum_{i=1}^{n_{k}}l(h_{\mathbf{\theta}}(\mathbf{ x}_{k,i}),\mathbf{y}_{k,i}), \tag{1}\]  
where \((\mathbf{x}_{k,i},\,\mathbf{y}_{k,i})\) is the \(i\)-th sample in \(\mathcal{D}_{k}\). To ensure the communication and privacy constraints, the central server exploits the data across clients by learning a global model that minimize the loss \(f_{k}\) for each client. Concretely, the global loss can be defined as the average of the client losses weighted by number of samples:  
\[\min_{\mathbf{\theta}}\frac{1}{N}\sum_{k=1}^{M}n_{k}f_{k}(\mathbf{\theta})=\frac{1}{N }\sum_{k=1}^{M}\sum_{i\in\mathcal{D}_{k}}l(h_{\mathbf{\theta}}(\mathbf{x}_{k,i}, \mathbf{y}_{k,i}), \tag{2}\]  
where \(N=\sum_{k=1}^{M}n_{k}\) is the total number of samples across clients. As illustrated in Figure 2, the clients send only their models' weights to the server at each step. As a result, the data of each client will be kept privately.  
Federated Averaging (FedAvg) [35] is the most popular algorithm to solve (2). On each round \(t\), the server picks a set of clients randomly to perform federated training. Each selected client takes the global parameters \(\mathbf{\theta}_{t-1}\) from the server, which are used for optimizing (1) by \(E\) stochastic gradient descent (SGD) steps. The client \(k\) then sends \(\mathbf{\theta}_{t}^{k}\) back to the server which averages all received clients's parameters to obtain the global parameters. The details of FedAvg are summarized in Algorithm 1. Compared to FedSGD corresponding to \(E=1\), FedAvg converges faster and requires fewer communication overhead.  
```
0: number of clients \(M\), local batch size \(B\), local epochs \(E\), learning rate \(\alpha\)
0: global parameters \(\mathbf{\theta}\) Server executes:
1: Initialize \(\theta_{0}\)
2:for each round \(t=1,2,3\ldots\)do
3:for each client \(k=1,2,\ldots,M\) in parallel do
4:\(\theta_{t}^{k}=\textbf{ClientUpdate}(k,\mathbf{\theta}_{t-1})\)
5:endfor
6:\(\mathbf{\theta}_{t}=\sum_{k=1}^{M}\frac{n_{t}}{N}\theta_{t}^{k}\)
7:endfor ClientUpdate\((k,\mathbf{\theta})\):
8:\(\mathcal{B}=\) split data into batches of size \(B\)
9:for each local epoch \(=1,2,\ldots,E\)do
10:for batch \(b\in\mathcal{B}\)do
11:\(\mathbf{\theta}=\theta-\alpha\
abla l(h_{\mathbf{\theta}}(b))\)
12:endfor
13:endfor
```  
**Algorithm 1** FedAvg algorithm.