Training Procedure {#subsec:firstalyer}
----------------------------------------------------

$z^{[1]} \leftarrow 0$ $z^{[1]} \leftarrow z^{[1]} + \operatorname*{popcount}( \operatorname{XNOR}( W^{[1]}, X_t))$ $a^{[1]} \leftarrow \operatorname{sign}(z^{[1]} - T \mu^{[1]})$ $z^{[k]} \leftarrow \operatorname*{popcount}( \operatorname{XNOR}( W^{[k]}, a^{[k-1]}))$ $a^{[k]} \leftarrow \operatorname{sign}(z^{[k]} - \mu^{[k]})$ $z^{[L]} \leftarrow \operatorname*{popcount}( \operatorname{XNOR}( W^{[L]}, a^{[L-1]}))$ $output \leftarrow \operatorname{argmax}(z^{[L]} - \mu^{[L]}) $

[ A first approach to design a stochastic computing BNN is to reuse the synaptic weights of a conventional BNN, trained with grayscale picture.]{} However, in the inference phase, we approximate the computation of the first layer by using stochastic images presentation instead of grayscale images. The full inference algorithm is presented, in vectorized form, in Algorithm \[alg:algorithm1binary\]. An input $X$ is transformed into binarized stochastic inputs $X_t$ by taking the value of each grayscale pixel (between zero and one) as the probability for the corresponding pixel in the stochastic input to be one. Then, [the networks computes]{} $\operatorname*{popcount}( XNOR( W^{[1]}, X_t)) - \mu^{[1]}$, and sums the result of this computation over a number $T$ of stochastic versions of the input $X_t$. Finally, the output of the layer is thresholded to obtain a binary value, and the rest of the neural network is computed in one pass in a fully binarized fashion.

The quality of the 