{'fraction_non_alphanumeric': 0.036043619667113065, 'fraction_numerical': 0.013927683183470442, 'mean_word_length': 4.53728813559322, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Probabilistic Inference of Binary Markov Random Fields in Spiking Neural Networks through Mean-field Approximation

Recent studies have suggested that the cognitive process of the human brain is realized as probabilistic inference and can be further modeled by probabilistic graphical models like Markov random fields. Nevertheless, it remains unclear how probabilistic inference can be implemented by a network of spiking neurons in the brain. Previous studies have tried to relate the inference equation of binary Markov random fields to the dynamic equation of spiking neural networks through belief propagation algorithm and reparameterization, but they are valid only for Markov random fields with limited network structure. In this paper, we propose a spiking neural network model that can implement inference of arbitrary binary Markov random fields. Specifically, we design a spiking recurrent neural network and prove that its neuronal dynamics are mathematically equivalent to the inference process of Markov random fields by adopting mean-field theory. Furthermore, our mean-field approach unifies previous works. Theoretical analysis and experimental results, together with the application to image denoising, demonstrate that our proposed spiking neural network can get comparable results to that of mean-field inference.


Introduction
The human brain is able to process information in the presence of sensory uncertainties [21]. For example, one can easily localize a bird in a tree via noisy visual and auditory cues. Such processes can be understood as probabilistic inference and further modeled by probabilistic graphical models [16,33], including Bayesian networks and Markov Random Fields (MRFs). With an increasing volume of behavioral and physiological evidence [15,8,20,25] that humans do actually use probabilistic rules in perception [14,30], sensorimotor control [1,17] and cognition [3,37,12], probabilistic brain is getting recognized by neuroscientists [24]. Nevertheless, it remains unclear how the brain can perform inference. Or more precisely, how a network of spiking neurons in the brain can implement inference of probabilistic graphical models? This problem is of great importance to both computer science and brain science [36]. If we known the neural algorithms of probabilistic inference, it is possible to build a machine that can perform probabilistic inference like the human brain.
In recent studies, many researchers have been devoted to developing neural circuits that can represent and imple-ment inference of undirected probabilistic graphical models, namely MRFs [16], which is widely used in computational neuroscience [10,22,26,32]. The reason for focusing on MRFs is that, for directed probabilistic graphical models, one can easily convert them to MRFs via moralization [13,16].
Here we briefly review these previous studies. Litvak and Ullman [18] designed neural circuits to implement the operations of summation and multiplication respectively, and further implemented probabilistic computation and inference of MRFs. Steimer et al. [31] proposed using a population of spiking neurons to collect messages and another population to send messages, and then implemented the Belief Propagation (BP) algorithm, a commonly used inference method in probabilistic graphical models [16,33]. All these studies require that each neuron and synapse conduct complicated computation. However, one often observes one basic principle of the neuronal system in the brain that a single neuron or a group of neurons should work in a relatively simple style, while complex functions could be achieved when they are wired together, i.e., collaborated in a network [19,2].
In order to propose biologically more plausible neural networks to implement inference, Ott and Stoop [23] established a relationship between the inference equation of binary MRFs and the dynamic equation of spiking neural networks through BP algorithm and reparameterization. However, their model relied on the specifically initialized messages and certain topological structures of MRFs. Yu et al. [35] went a further step to relax the constraints on initialized messages, but still required the special topological structure and potential function of MRFs. Another important way is based on tree-based reparameterization algorithm [27], which, however, is only limited to the case of exponential family distributions.
In this paper, we use a mean-field approximation to treat the inference process of MRFs as a time-continuous system of a recurrent spiking neural network. We analytically prove a precise equivalence between the inference equation of Markov random fields and the dynamic equation of spiking recurrent neural networks. We show that the firing rates of neurons in the network can encode the difference between the probabilities of two states. In addition, we prove that the time course of neural firing rate can implement marginal inference of arbitrary binary Markov random fields. In this way, we can obtain the state of the neuron by counting spikes from each neuron within a time window. We further show that our mean-field approximation unifies the previous approach based on BP algorithm and reparameterization. Theoretical analysis and experimental results, together with an application to the image denoising problem, show that our proposed spiking neural network can get comparable results to that of mean-field inference.
To summarize, our contributions include the following aspects: • We propose a spiking neural network model that can implement inference of arbitrary binary Markov random fields.
• We prove that there exists a precise equivalence between the dynamics of recurrent neural network and the inference equation of a Markov random field.
• We show that the previous approach based on BP algorithm and reparameterizations equals mean-field approximation.
• We show that our proposed spiking neural network can be used to solve practical computer vision problems, like image denoising.
The rest of the paper is organized as follows. In section 2 we briefly review MRFs and marginal inference, then we derive the inference equation of MRFs based on mean-field approximation and show how it is related to the dynamic equation of spiking neural networks in section 3. We show the simulation results in section 4 and conclude in section 5.

Markov Random Fields and Marginal Inference
In this section, we briefly review MRFs and marginal inference. MRFs is one typical undirected probabilistic graphical model that is widely used in computational neuroscience. Thanks to their ability to model soft contextual constraints between random variables, MRFs provide a principled probabilistic framework to model various vision problems [9,4,7] since the visual scene modeling usually involves interactions between a subset of pixels and scene components.
In a MRF, a joint distribution P ({x}) = P (x 1 , x 2 , . . . , x n ) is defined on the graph, which can be factorized into a product of potential functions according to the structure of the graph. For the MRF in Fig. 1, P ({x}) has the form: where E and V represent the set of edges and nodes in the graph respectively, Ψ ij (x i , x j ) and Ψ i (x i , y i ) denote the pairwise and unary potential functions. Z is the partition function defined as (1) can be rewritten as: Similar to the studies in [23,35], we assume that J ij (x i , x j ) = J ij x i x j and h i (x i ) = h i x i , in which J ij and h i are constants.
The inference problems of MRFs include Maximum a Posterior (MAP) estimation and marginal inference. By MAP estimation, we refer to the estimation of a maximum of posterior point estimator. Conversely, marginal inference refers to inferring the posterior or conditional distribution over the latent causes of observations. In this paper, we only consider marginal inference. Specifically, we compute the marginal distribution of each variable x i , that is:

Neural Implementation of Marginal Inference on Binary MRF
In this section, we will prove that there exists a precise equivalence between the neuronal dynamics of recurrent neural networks and mean-field inference of binary MRFs. We first derive a differential equation that has the same fixed point as the mean-field inference equation of MRFs, then we show that this differential equation can be easily implemented by the dynamic equation of recurrent neural networks. In the end, we demonstrate that the previous work based on BP algorithm and reparameterization equals the mean-field approximation.

Converting Mean-Field Inference into a Differential
Equation Similar to the studies in [23,35], we only consider inference of binary MRFs in this paper, which means the value of the variable x i can be 1 or -1 (x i = 1 or −1).
As exact inference of MRF is a NP-complete problem [16], approximate inference algorithms like variational methods are often used. The main principle of variational methods is converting the inference problem to an optimization problem: Here the target distribution p(x) is approximated by a simpler distribution q(x), which belongs to a family of tractable distribution. KL(·) represents the Kullback-Leibler divergence between two distributions. In the mean-field method, q(x) is set to be a fully factorized distribution, where α is a normalization constant to make xi b i (x i ) = 1 and N (i) denotes the set of all neighboring nodes of node i. t denotes the number of iterations, and b t i (x i ) represents the information received by node i in the t th iteration, which is a function with respect to the state of variable x i . When all the message converge to the fixed point, the marginal probability p(x i ) can be approximated by the steady- (6) In order to convert Eq. (6) to a differential equation, where n t i can be seen as the new message received by node i in the t th iteration. Note that here the message n t i is independent of the state of variable x i . When n t i converges to the fixed point, it can approximate the probability p(x i = 1) − p(x i = −1). Combining Eq. (6)-(7) and the condition b t (x i = 1) + b t (x i = −1) = 1 defined on binary MRF, one can get that: (8) Note that the third equality of Eq. (8) holds as (9) It is easy to prove that the following differential equation has the same fixed point as Eq. (8).
where τ 0 is a time constant that determines the time needed for the network to reach the fixed point.

Dynamic Equation of Spiking Recurrent Neural Networks
Recurrent neural networks are composed of a population of interconnected neurons, which have been widely used to model cortical response properties in computational neuroscience [29,28]. Here, we drive the firing-rate based equation of spiking recurrent neural network based on two steps [5]: 1) Determining how the total synaptic input to a neuron depends on the firing rate of its presynaptic afferents. 2) Modeling how the firing rate of the postsynaptic neuron depends on its total synaptic input.
First of all, considering the recurrent neural network consists of N spiking neurons z 1 , z 2 , ..., z N , the input current to the neuron z i at time t is I i (t), which includes the recurrent input of spike sequence from other neurons and can be computed as: where S j (τ ) denotes the firing spike sequence of neuron w ij denotes the synaptic weight between neuron z i and z j , κ(t) is the synaptic kernel that describes the time course of the synaptic current in response to a presynaptic spike arriving at time t. The most frequently used form of synaptic kernel is an exponential kernel, that is, κ(t) = 1 τs exp − t τs with the membrane time constant τ s .
In fact, the neural response function S j (t) could be replaced by the firing rate r j (t) of neuron z j as r j (t) = 1 ∆t t+∆t t S j (τ ) dτ with S j (t) denoting the trial-average neural response function, thus Eq. (11) can be rewritten as: By taking the derivative of I k (t) with respect to time t, one obtain: with τ s denoting the time constant that describes the decay of the synaptic conductance. So far we can determine the input current to postsynaptic neuron in terms of the firing rates of the presynaptic neurons. To obtain the firing-rate model, we also need to determine the postsynaptic firing rate with the current I i (t). For time-independent inputs, the firing rate r i (t) of the postsynaptic neuron z i can be expressed as , where F (x) denotes the neuronal activation function. As the firing rate does not follow changes of the total synaptic current instantaneously, the firing rate is often modelled by a low-pass filtered version of the synaptic current: Under the constraints of time-independent inputs, the steady state of the postsynaptic current If τ r τ s , we can make the approximation that Eq. (13) comes to equilibrium quickly compared to Eq. (14). Consequently, we can further replace I i (t) by N j=1 w ij r j (t) in Eq. (14) and obtain: In recurrent neural networks, except for the input current from recurrent neurons, there also exists an external input current. Incorporating the external input current I ext i (t) to Eq. (15), the firing rate of the recurrent neuron k is determined by:

Implementation of Inference with Neural Network
Now one can relate the inference equation of MRFs (Eq. (10)) to the dynamics of recurrent neural networks (Eq. (16)). Obviously, Eq. (10) is equivalent to Eq. (16) if the following equations hold: Eq. (17)- (21) mean that if the synaptic weights w ij and input current I ext i (t) of a recurrent neural network encode the potential functions J ij and h i of a binary MRF respectively, the firing rate r i (t) of neuron z i encodes the probability p(x i = 1) − p(x i = −1). Moreover, the time course of neural firing rate in the recurrent neural network can implement marginal inference of the MRF. Thus, we can read out the inference result by counting spikes from each neuron within a time window. Note that as the value of n i (t) varies from −1 to 1, the firing rate r i (t) in Eq. (18) could be negative, which is biological implausible. As discussed in [29], we can assume that the actual firing ratê r i (t) is linearly related to the "firing rate" r i (t) obtained from Eq. (16), that is,r i (t) = ar i (t)+b. Here a is a positive factor and b is a rectification value that ensurer i (t) to be positive. In conclusion, we implement mean-field inference of binary MRFs with spiking recurrent neural networks.

Relating Mean-field Inference to Belief Propagation
Here we will build the relationship between mean-field inference and BP, and show that the previous work based on BP and reparameterization equals the mean-field inference.
Previous studies have tried to relate BP algorithm of binary MRF to the dynamics of Hopfield Networks by deriving a new formulation of belief propagation based on reparameterization [23,35]: where µ t+1 i represents the new message after reparameteration of node i at the t + 1 th iteration, and µ ∞ s→j is a function of the message m t s→j (x j ) in BP that is sent from node s to node j in the t th iteration. To be specific, . With the assumptions that the number of neighboring nodes of each node is large enough (N (j) >> 1) and the potential function is small (J ij << 1 and h i << 1), Ott et al. [23] and Yu et al. [35] proved that Eq. (22) can be simplified to: As J ij << 1, tanh(J ij ) ≈ J ij . Thus one can further simplify Eq. (23) to: One can find that there exists a precise equivalence between Eq. (24) and Eq. (8), which implies that the previous work based on BP and reparameteration equals the mean-field approximation. These results suggest that the Hopfield networks used in the previous work actually implement mean-field inference, instead of the BP algorithm.
In addition, our current results explain the experiments in [35] where the inference result based on Hopfield networks is not as accurate as that of BP when the potential function is large (J ij > 1 and h i > 1). These errors come from the difference between mean-field inference and the BP algorithm.

Simulation Experiments
To validate the proposed computational framework, we evaluate the performance of recurrent neural networks through simulation experiments. We firstly test the accuracy of the propose method, and then prove that it is robust to different parameters. At last, we scale up the proposed spiking neural network to solve practical computer vision problems.

Testing on the Accuracy of Our Method
In order to test the accuracy of the proposed method, we generated several MRFs with different graph topologies (chain, single loop, grid and fully connected graph, see Fig. 2), and perform inference of these MRFs with spiking recurrent neural network and mean-field method respectively.
For a MRF with M nodes, we calculated marginal probabilities for all these M nodes with mean-field method and the corresponding recurrent neural network respectively. The mean relative error δ is defined as follows: where P M F (x i = 1) represents the marginal probabilities computed with mean-field method, and P RN N (x i = 1) represents the result obtained by the corresponding recurrent neural network.   Fig. 2 illustrates how the relative errors rapidly convergence with a few iterations. For each MRF, the potential functions J ij and h i are drawn from two uniform distributions on [0, λ 1 ] and [−λ 2 , λ 2 ] respectively. One can find that even for MRFs with different topologies, the error decreases in a fast way with only a few iterations. These results imply that the simulation of spiking recurrent neural networks can get comparable results as an analytical mean-field method.
To illustrate the inference mechanism of the spiking recurrent neural network, Fig. 3A shows the spiking activity of all 9 neurons in the recurrent neural network when performing inference of a 9-node MRF with chain structure ( Fig. 2A). Here the mapping between actual firing rater i (t) and the "firing rate" r i (t) isr i (t) = 50r i (t) + 50. Thus the maximum firing rate of each neuron is 100 Hz. Fig. 3B shows the time course of the firing rate of each neuron. One can see that the firing rate of each neuron converges to a fixed value and then fluctuates around it.

Testing on the Robustness of Our Method
The experimental results above indicate that the inference model of recurrent neural networks can get accurate results as mean-field inference for a given set of parameters of λ 1 and λ 2 as 0.1. Here we make a concrete analysis of the robustness of our model with different parameters. Fig. 4 shows the results where λ 1 and λ 2 are set to different combinations of 1 and 0.1, except the setting that λ 1 = λ 2 = 0.1 as shown in Fig. 2. We can see that, in all cases, the errors converge to almost zero in a fast manner. These results indicate that, different from the previous works [23,35] that only apply to MRFs with special potential function (J ij << 1 and h i << 1), our method is robust to different parameters and could implement inference for arbitary MRFs.
Then we investigate whether our framework can be scaled up to large-scale MRFs with more nodes. Two examples are included here: a MRF with 25 nodes and 300 edges and a MRF with 100 nodes and 4950 edges. As shown in Fig. 5, the same conclusion is obtained that the spiking recurrent neural networks can get comparable results as the mean-field method.

Binary Images Denoising by Recurrent Neural Networks
Here we investigate whether our spiking neural network can be scaled up to solve more realistic tasks. We consider the task of image denoising, that is, correcting an image that has been corrupted. In the field of image processing, the researchers often model image denoising problem by MRFs with grid-like structures (shown in Fig. 1) and then convert the denoising problem to MAP estimation or marginal inference problem. Based on this, we can also tackle this problem with recurrent neural networks by computing the marginal probabilities of each pixel and then infer whether this pixel is white or black in a binary setting.
The image denoising experiments are performed on the NIST Special Database 19 (SD 19), which contains NIST's entire corpus of training materials for handwritten document and character recognition. This dataset includes samples from 3600 writers, consisting of 10 digits 0 − 9, 26 lower letters a-z and 26 upper letters A-Z. Therefore we have totally 62 categories. During the experiment, 100 images of each class are randomly selected as dataset. All images used here are 128 × 128 pixels. In this experiment, each image is modeled by a square lattice pairwise MRF (shown in Fig. 1), where the hidden variables {x} = {x 1 , x 2 , ..., x n } represent the denoise image and observed variables {y} = {y 1 , y 2 , ..., y n } represent the observed noise image. As observed pixel value is usually the same as the true pixel value, so the unary potential h(x i ) is set to 0.1 if the variable x i is the same as the observation variable y i and −0.1 otherwise (h i = 0.1). Besides, as nearby pixel values are usually the same in an image, the pairwise potential function J ij (x i , x j ) is set to 0.8 if x i = x j and −0.8 otherwise (J ij = 0.8). All the other settings in this experiment are the same as in experiment 4.1 above. Fig. 6 shows some examples of image denoising with mean-field inference and the corresponding recurrent neural network. Here the noise images are generated by randomly flipping the pixel value with a probability of 5%.
We also quantitatively analyze these results by computing the structural similarity index (SSIM) and the peak signal-to-noise ratio (PSNR). As shown in Fig. 7, the SSIM of the original image, denoised image by mean-field inference, and denoised image by recurrent neural networks   are 13.01 ± 0.14, 29.19 ± 1.97 and 29.19 ± 1.97, respectively. The PSNR of the original image, denoised image by mean-field inference, and denoised image by recurrent neural networks are 0.1322 ± 0.0207, 0.9905 ± 0.0047 and 0.9905 ± 0.0047 respectively. All these results demonstrate that recurrent neural networks can get the same denois-ing results as mean-field inference. Fig. 8 illustrates how the mean relative error between recurrent neural networks and mean-field inference varies over time. We can find the error converges to 0 with a few iterations.

Comparison among Different Neural Network Based Image Denoising Methods
In section 3.4, we have proved that the previous approaches based on BP and reparameterization (BP-based neural networks) can be unified in our framework. In order to test this, we compare our method with the BP algorithm and the BP-based neural network model for the task of image denoising. In order to increase the difficulty of inference, here we created a dataset of 100 images with 128 × 128 pixels by making randomly noisy images and then smooth them to get true output values. Fig. 9 shows one example of the randomly generated binary images. One can find that there exists more separated space in these images compared with the images in NIST SD 19. Thus it's more difficult to be denoised.
To compare the performance of these algorithms, we add the different levels of salt and pepper noise on the binary images, and characterize the quality of the denoised images with the criterion of the SSIM and PSNR. Fig. 9 illustrates one example of image denoising with mean-field inference, recurrent neural networks, BP algorithm [16], and BP-based neural networks [35]. Fig. 10 compares the SSIM and PSNR of different methods and noise levels. One can see that the performance of recurrent neu-   The relative error decays to 0 rapidly. The red, green and blue curves represent the results for digit, upper letter and lower letter respectively. All the results are averaged over 5 independent trials, shaded area indicates standard deviation (STD).
ral networks (purple curve) is the same as mean-field inference (red curve). Besides, one can also find that the performance of BP-based neural networks (green curve) is nearly the same as that of recurrent neural networks (purple curve) and mean-field inference (red curve), which demonstrates that the previous work equals mean-field inference and can be unified in our framework. Note that there exists a gap between BP-based neural networks (green curve) and recurrent neural networks (purple curve) when the noise level is larger than 0.3, which comes from the approximation between Eq. (23) and Eq. (24).

Conclusion
In this paper, we prove that there exists a precise equivalence between the dynamics of recurrent neural network and mean-field inference of binary Markov random fields. We show that if the synaptic weights and input current encode the potential function of MRFs, the firing rates of neuron in recurrent neural networks encode the difference between the probabilities for two states. The time course of neuronal firing rate can implement marginal inference. Theoretical analysis and experiments on MRFs with different topologies show that our neural network can get the same performance as the mean-field method. Besides, we also apply our proposed spiking framework to practical computer vision problem, i.e., binary images denoising.
Differ from previous works based on BP algorithm and reparameterization, where the potential functions of MRF should meet some strict conditions, we design a spiking network that can implement mean-field inference for arbitrary MRFs. What's more, we have demonstrated that our work unifies previous works.
The previous work of neural implementation of Bayesian inference [29,6,34] with recurrent neural networks focused on inference of hidden Markov models. There also exist some studies [28,11] that extended the networks to a multilayer structure to perform hierarchical Bayesian inference. Different from these works, we are focusing on how spiking neural networks are able to implement probabilistic inference of MRF. In future work, we will try to extend our proposed framework to tackle more advanced realistic problems, like recognition and stereo matching.