# Detecting Social Media Bots with Variational AutoEncoder and k-Nearest Neighbor
## 3 Social Media Bot Detection
### Anomaly Detection  
VAE, as a form of a deep generation model, is a generative network structure based on Variable Bayes (VB) inference. The structure is shown in Figure 3. VAE uses two neural networks to establish two probability density distribution models: one is used for variational inference of original input data to generate the variational probability distribution of hidden variables, which is called an inferential network; the other is to restore the approximate probability distribution of the original data according to the generated variational probability distribution of hidden variables, which is called a generative network. The sample set is \(X=\{x_{i}\}_{i=1}^{N}\), where each sample \(x_{i}\) is a randomly generated independent,  
Figure 2: Feature analysis. (**a**) The cumulative distribution of the average mentions of tweets (**b**) The cumulative distribution of the average number of emojs used in tweets (**c**) The cumulative distribution of stop words (**d**) The cumulative distribution of topic participation (**e**) The cumulative distribution of link usage (**f**) The cumulative distribution corresponding to the proportion of tweet forwarding (**g**) The cumulative distribution function of the average similarity of tweets (**h**) Histogram of average length of original and forwarded tweets (**i**) Histogram of average number of the punctuations (”,”,\",\",\",\",\",\",\",\",\",\",\",\",\",\",\",\",\",\",\", and ”). continuous, or discrete distribution variable. The observable variable X is a random vector in a high-dimensional space, which is used as the input visible layer variable, and then the hidden layer unobservable variable Z is generated. Z is a random vector in a relatively low-dimensional space, and the dataset \(X^{*}=\left\{x_{i}^{*}\right\}_{i=1}^{N}\) is generated by a generative network. The VAE generation model includes two processes:  
1. [label=(0)]
2. Approximate the inference process of hidden variable Z posterior distribution: the recognition model \(q_{\phi}(z|x)\), an inferential network, represents the process of inferring z from a known value of \(x\). 3. Generate the conditional distribution of variables \(X^{*}\): conditional distribution \(p_{\theta}(z)p_{\theta}\) (\(x^{*}|z\)), namely a generative network. VAE uses KL divergence to measure the similarity between \(q_{\phi}(z|x)\) and true posterior distribution \(p_{\theta}(z|x)\) and minimizes KL divergence by optimizing constraint parameters \(\theta\) (generative network parameters) and \(\phi\) (inferential network parameters). \[ \begin{array}{rcl}\phi,\ \theta&=&\underset{\phi,\theta}{argmin}D_{KL}\big{(}q_{ \phi}(z|x)\big{|}\big{|}p_{\theta}(z|x)\big{)}\\ &=&E_{q_{\phi}(z|x)}\big{[}\log q_{\phi}(z|x)-\log p_{\theta}(z,x)\big{]}+\log p _{\theta}(x),\end{array}  \tag{7}\]  
where argmin is used to minimize KL divergence. \(\mathrm{L}(\theta,\phi;\mathrm{X})\) is the variational lower bound function of the logarithmic marginal likelihood of the set X, which means we calculate the parameters \(\theta\) and \(\phi\) according to the known sample set X.  
\[\mathrm{L}(\theta,\phi;\mathrm{X})=E_{q_{\phi}(z|x)}\big{[}-\log q_{\phi}(z|x )+\log p_{\theta}(z,x)\big{]} \tag{8}\]  
The optimization objective of inferential network and generative network is to maximize the variational lower bound function. \[\phi,\ \theta=\underset{\phi,\theta}{argmax}\ \mathrm{L}(\theta,\phi; \mathrm{X}) \tag{9}\]  
In the above equation, argmax is the function L that maximizes the lower bound of variation. The sampling of Z is as follows:  
\[z^{i}=\mu^{i}+\epsilon^{i}\cdot\delta^{i} \tag{10}\]  
The latent variable corresponding to sample \(i\) is represented as \(z^{i}\) in Equation (10). \(\mu^{i}\) means the average value of sample \(i\) in the inferential network, and \(\delta^{i}\) means the variance of sample \(i\) in the inferential network. The mean and variance of the network are derived from the neural network and can be calculated directly.