Estimating or Propagating Gradients Through Stochastic Neurons
Stochastic neurons can be useful for a number of reasons in deep learning models, but in many cases they pose a challenging problem: how to estimate the gradient of a loss function with respect to the input of such stochastic neurons, i.e., can we “back-propagate” through these stochastic neurons? We examine this question, existing approaches, and present two novel families of solutions, applicable in different settings. In particular, it is demonstrated that a simple biologically plausible formula gives rise to an an unbiased (but noisy) estimator of the gradient with respect to a binary stochastic neuron firing probability. Unlike other estimators which view the noise as a small perturbation in order to estimate gradients by finite differences, this estimator is unbiased even without assuming that the stochastic perturbation is small. This estimator is also interesting because it can be applied in very general settings which do not allow gradient back-propagation, including the estimation of the gradient with respect to future rewards, as required in reinforcement learning setups. We also propose an approach to approximating this unbiased but high-variance estimator by learning to predict it using a biased estimator. The second approach we propose assumes that an estimator of the gradient can be back-propagated and it provides an unbiased estimator of the gradient, but can only work with non-linearities unlike the hard threshold, but like the rectifier, that are not flat for all of their range. This is similar to traditional sigmoidal units but has the advantage that for many inputs, a hard decision (e.g., a 0 output) can be produced, which would be convenient for conditional computation and achieving sparse representations and sparse gradients.
1 Introduction and Background
Many learning algorithms and in particular those based on neural networks or deep learning rely on gradient-based learning. To compute exact gradients, it is better if the relationship between parameters and the training objective is continuous and generally smooth. If it is only constant by parts, i.e., mostly flat, then gradient-based learning is impractical. This was what motivated the move from neural networks made of so-called formal neurons, with a hard threshold output, to neural networks whose units are based on a sigmoidal non-linearity, and the well-known back-propagation algorithm to compute the gradients (Rumelhart et al., 1986).
We call computational graph or flow graph the graph that relates inputs and parameters to outputs and training criterion. Although it had been taken for granted by most researchers that smoothness of this graph was a necessary condition for exact gradient-based training methods to work well, recent successes of deep networks with rectifiers and other “semi-hard” non-linearities (Glorot et al., 2011; Krizhevsky et al., 2012a; Goodfellow et al., 2013) clearly question that belief: see Section 2 for a deeper discussion.
In principle, even if there are hard decisions (such as the treshold function typically found in formal neurons) in the computational graph, it is possible to obtain estimated gradients by introducing perturbations in the system and observing the effects. Although finite-difference approximations of the gradient appear hopelessly inefficient (because independently perturbing each of parameters to estimate its gradient would be times more expensive than ordinary back-propagation), another option is to introduce random perturbations, and this idea has been pushed far (and experimented on neural networks for control) by Spall (1992) with the Simultaneous Perturbation Stochastic Approximation (SPSA) algorithm.
As discussed here (Section 2), semi-hard non-linearities and stochastic perturbations can be combined to obtain reasonably low-variance estimators of the gradient, and a good example of that success is with the recent advances with dropout (Hinton et al., 2012; Krizhevsky et al., 2012b; Goodfellow et al., 2013). The idea is to multiply the output of a non-linear unit by independent binomial noise. This noise injection is useful as a regularizer and it does slow down training a bit, but not apparently by a lot (maybe 2-fold), which is very encouraging. The symmetry-breaking and induced sparsity may also compensate for the extra variance and possibly help to reduce ill-conditioning, as hypothesized by Bengio (2013).
However, it is appealing to consider noise whose amplitude can be modulated by the signals computed in the computational graph, such as with stochastic binary neurons, which output a 1 or a 0 according to a sigmoid probability. Short of computing an average over an exponential number of configurations, it would seem that computing the exact gradient (with respect to the average of the loss over all possible binary samplings of all the stochastic neurons in the neural network) is impossible in such neural networks. The question is whether good estimators (which might have bias and variance) can be computed. We show in Section 3 that one can indeed produce an unbiased estimator of the gradient, and that this gradient is even cheaper to compute than with the back-propagation algorithm, since it does not require a backwards pass. To compensate its possibly high variance, we also propose in Section 3.2 a biased but lower-variance estimator that is based on learning a correction factor to deterministically transform a biased but low-variance estimator into a less biased one, with the same variance.
In a first attempt to study the question of the efficiency of such estimators, in Section 4.1, we first show that the proposed unbiased estimator can be seen as basically estimating the correlation between the perturbation and the training loss. We then show that the Boltzmann machine gradient can also be interpreted as a form of correlation-based estimator, but missing an additive normalization. This is encouraging, since various Boltzmann machines (in particular the restricted Boltzmann machine) have been quite successful in recent years (Hinton et al., 2006; Bengio, 2009).
1.1 More Motivations
One motivation for studying stochastic neurons is that stochastic behavior may be a required ingredient in modeling biological neurons. The apparent noise in neuronal spike trains could come from an actual noise source or simply from the hard to reproduce changes in the set of input spikes entering a neuron’s dendrites. Until this question is resolved by biological observations, it is interesting to study how such noise, which has motivated the Boltzmann machine (Hinton et al., 1984), may impact computation and learning in neural networks.
Stochastic neurons with binary outputs are also interesting because they can easily give rise to sparse representations (that have many zeros), a form of regularization that has been used in many representation learning algorithms (Bengio, 2009; Bengio et al., 2013). Sparsity of the representation corresponds to the prior that, for a given input scene , most of the explanatory factors are irrelevant (and that would be represented by many zeros in the representation).
Semi-hard stochastic neurons such as those studied in Section 2 also give rise to sparse gradients, i.e., such that for most examples, the gradient vector (with respect to parameters) has many zeros. Indeed, for weights into units that are shut off or are in a flat saturation region (e.g., at 0 or 1), the derivative will be zero. As argued in Bengio (2013), sparse gradients may be useful to reduce the optimization difficulty due to ill-conditioning, by reducing the number of interactions between parameters to those parameters that are simultaneously “active” (with a non-zero gradient) for a particular example.
As argued by Bengio (2013), sparse representations may be a useful ingredient of conditional computation, by which only a small subset of the model parameters are “activated” (and need to be visited) for any particular example, thereby greatly reducing the number of computations needed per example. Sparse gating units may be trained to select which part of the model actually need to be computed for a given example.
Binary representations are also useful as keys for a hash table, as in the semantic hashing algorithm (Salakhutdinov and Hinton, 2009). In the latter, zero-mean Gaussian noise is added to the pre-sigmoid activation of the code neuron (whose output will be used as hash keys). As the amount of noise is increased, this forces the weights and biases to also increase to avoid losing too much information in the sigmoid output, thereby gradually forcing these units into saturation, i.e., producing a nearly 0 or nearly 1 output. Hence one option would be to train hard-decision neurons by gradually annealing the magnitude of the parameters, turning soft-output easy to train units into hard-decision binary-output units. However, it means that during training, we would not be able to take advantage of the hard zeros (only available at the end of training). Since both conditional computatation and sparse gradients are motivated by speeding up training, this would not be an interesting solution.
Trainable stochastic neurons would also be useful inside recurrent networks to take hard stochastic decisions about temporal events at different time scales. This would be useful to train multi-scale temporal hierarchies 111Daan Wierstra, personal communication such that back-propagated gradients could quickly flow through the slower time scales. Multi-scale temporal hierarchies for recurrent nets have already been proposed and involved exponential moving averages of different time constants (El Hihi and Bengio, 1996), where each unit is still updated after each time step. Instead, identifying key events at a high level of abstraction would allow these high-level units to only be updated when needed (asynchronously), creating a fast track (short path) for gradient propagation through time.
1.2 Prior Work
The idea of having stochastic neuron models is of course very old, with one of the major family of algorithms relying on such neurons being the Boltzmann machine (Hinton et al., 1984). In Section 4.2 we study a connection between Boltzmann machine log-likelihood gradients and perturbation-based estimators of the gradient discussed here.
Another biologically motivated proposal for synaptic strength learning was proposed by Fiete and Seung (2006). It is based on small zero-mean i.i.d. perturbations applied at each stochastic neuron potential (prior to a non-linearity) and a Taylor expansion of the expected reward as a function of these variations. Fiete and Seung (2006) end up proposing a gradient estimator that looks like a correlation between the reward and the perturbation, just like what we obtain in Section 3. However, their estimator is only unbiased in the limit of small perturbations.
Gradient estimators based on stochastic perturbations have been shown for long (Spall, 1992) to be much more efficient than standard finite-difference approximations. Consider quantities to be adjusted in order to minimize an expected loss . A finite difference approximation is based on measuring separately the effect of changing each one of the parameters, e.g., through , or even better, through , where where the 1 is at position . With quantities (and typically computations to calculate ), the computational cost of the gradient estimator is . Instead, a perturbation-based estimator such as found in Simultaneous Perturbation Stochastic Approximation (SPSA) (Spall, 1992) chooses a random perturbation vector (e.g., isotropic Gaussian noise of variance ) and estimates the gradient of the expected loss with respect to through . So long as the perturbation does not put too much probability around 0, this estimator is as efficient as the finite-difference estimator but requires less computation. However, like the algorithm proposed by Fiete and Seung (2006) this estimator becomes unbiased only as the perturbations go towards 0. When we want to consider all-or-none perturbations (like a neuron sending a spike or not), it is not clear if these assumptions are appropriate. The advantage of the unbiased estimator proposed here is that it does not require that the perturbations be small.
Another estimator of the expected gradient through stochastic neurons was proposed by Hinton (2012) in his lecture 15b. The idea is simply to back-propagate through the hard threshold function (1 if the argument is positive, 0 otherwise) as if it had been the identity function. It is clearly a biased estimator, but when considering a single layer of neurons, it has the right sign (this is not guaranteed anymore when back-propagating through more hidden layers).
2 Semi-Hard Stochastic Neurons
One way to achieve gradient-based learning in networks of stochastic neurons is to build an architecture in which noise is injected so that gradients can sometimes flow into the neuron and can then adjust it (and its predecessors in the computational graph) appropriately.
In general, we can consider the output of a stochastic neuron as the application of a determinstic function that depends on its inputs (typically, a vector containing the outputs of other neurons), its internal parameters (typically the bias and incoming weights of the neuron) and on a noise source :
So long as in the above equation has a non-zero gradient with respect to and , gradient-based learning (with back-propagation to compute gradients) can proceed.
For example, if the noise is added or multiplied somewhere in the computation of , gradients can be computed as usual. Dropout noise (Hinton et al., 2012) and masking noise (in denoising auto-encoders (Vincent et al., 2008)) is multiplied (just after the neuron non-linearity), while in semantic hashing (Salakhutdinov and Hinton, 2009) noise is added (just before the non-linearity). For example, that noise can be binomial (dropout, masking noise) or Gaussian (semantic hashing).
However, if we want to be binary (like in stochastic binary neurons), then will have derivatives that are 0 almost everywhere (and infinite at the threshold), so that gradients can never flow.
There is an intermediat option, that we put forward here: choose so that it has two main kinds of behavior, with zero derivatives in some regions, and with significantly non-zero derivatives in other regions. We call these two states of the neuron respectively the insensitive state and the sensitive state.
A special case is when the insensitive state corresponds to and we have sparsely activated neurons. The prototypical example of that situation is the rectifier unit (Nair and Hinton, 2010; Glorot et al., 2011), whose non-linearity is simply . For example,
where is 0-mean Gaussian noise, and is the -th input of unit , either a raw input (visible unit) or the output of some other unit in the computational graph (hidden unit).
Let us consider two cases:
If , the basic state is active, the unit is generally sensitive and non-zero, but sometimes it is shut off (e.g., when is sufficiently negative to push the argument of the rectifier below 0). In that case gradients will flow in most cases (samples of ). If the rest of the system sends the signal that should have been smaller, then gradients will push it towards being more often in the insensitive state.
If , the basic state is inactive, the unit is generally insensitive and zero, but sometimes turned on (e.g., when is sufficiently positive to push the argument of the rectifier above 0). In that case gradients will not flow in most cases, but when they do, the signal will either push the weighted sum lower (if being active was not actually a good thing for that unit in that context) and reduce the chances of being active again, or it will push the weight sum higher (if being active was actually a good thing for that unit in that context) and increase the chances of being active again.
So it appears that even though the gradient does not always flow (as it would with sigmoid or tanh units), it might flow sufficiently often to provide the appropriate training information. The important thing to notice is that even when the basic state (second case, above) is for the unit to be insensitive and zero, there will be an occasional gradient signal that can draw it out of there.
One concern with this approach is that one can see an asymmetry between the number of times that a unit with an active basic state can get a chance to receive a signal telling it to become inactive, versus the number of times that a unit with an inactive basic state can get a signal telling it to become active.
Another potential and related concern is that some of these units will “die” (become useless) if their basic state is inactive in the vast majority of cases (for example, because their weights pushed them into that regime due to random fluctations). Because of the above asymmetry, dead units would stay dead for very long before getting a chance of being born again, while live units would sometimes get into the death zone by chance and then get stuck there. What we propose here is a simple mechanism to adjust the bias of each unit so that in average its “firing rate” (fraction of the time spent in the active state) reaches some pre-defined target. For example, if the moving average of being non-zero falls below a threshold, the bias is pushed up until that average comes back above the threshold.
3 Unbiased Estimator of Gradient for Stochastic Binary Neurons
Let us consider the case where we want some component of our model to take a hard decision but allow this decision to be stochastic, with a probability that is a continuous function of some quantities, through parameters that we wish to learn. We will also assume that many such decisions can be taken in parallel with independent noise sources driving the stochastic samples. Without loss of generality, we consider here a set of binary decisions, i.e., the setup corresponds to having a set of stochastic binary neurons, whose output influences an observed future loss . In the framework of Eq. 1, we could have for example
where is uniform and is the sigmoid function. In the case of the traditional artificial neuron, we would have
and is the set of parameters for unit (scalar bias and weight vector ). We would ideally like to estimate how a change in would impact in average over the noise sources, so as to be able to propagate this estimated gradients into and possibly into .
3.1 Derivation of Unbiased Estimator
Let be defined as in Eq. 2, then is an unbiased estimator of where the expectation is over and over all the noise sources , besides , that do not influence but may influence , i.e., conditioned on the set of noise sources that influence .
We will compute the expected value of the estimator and verify that it equals the desired derivative. The set of all noise sources in the system is . We can consider to be an implicit deterministic function of all the noise sources, i.e., , where contains everything about that is needed to predict . denotes the expectation over variable , while denotes the expectation over all the other random variables besides , i.e., conditioned on .
Since does not influence , differentiating with respect to gives
First consider that since ,
and , so
Now let us consider the expected value of the estimator .
which is the same as Eq. 4, i.e., the expected value of the estimator equals the gradient of the expected loss, . ∎
Under the same conditions as Theorem 1, and for any (possibly unit-specific) constant the centered estimator
is also an unbiased estimator of . Furthermore, among all possible values of , the minimum variance choice is
which we note is a weighted average of the loss values , whose weights are specific to unit .
The centered estimator can be decomposed into the sum of the uncentered estimator and the term . Since , , so that the expected value of the centered estimator equals the expected value of the uncentered estimator. By Theorem 1 (the uncentered estimator is unbiased), the centered estimator is therefore also unbiased, which completes the proof of the first statement.
Regarding the optimal choice of , first note that the variance of the uncentered estimator is
Now let us compute the variance of the centered estimator:
where . Let us rewrite :
is maximized (to minimize variance of the estimator) when is minimized. Taking the derivative of that expression with respect to , we obtain
which is achieved for
as claimed. ∎
Practically, we could get the lowest variance estimator (among all choices of the ) by keeping track of two numbers (running or moving averages) for each stochastic neuron, one for the numerator and one for the denominator of the unit-specific in Eq. 7. This would lead the lowest-variance estimator
Note how the unbiased estimator only requires broadcasting throughout the network, no back-propagation and only local computation. Note also how this could be applied even with an estimate of future rewards or losses , as would be useful in the context of reinforcement learning (where the actual loss or reward will be measured farther into the future, much after has been sampled).
3.2 Training a Lower-Variance Biased Estimator
One potential problem with the above unbiased estimators is that their variance could be large enough to considerably slow training, when compared to using stochastic gradient descent with back-propagated gradients.
We propose here a general class of solutions to address that challenge, but for this purpose we need to have a biased but low-variance estimator.
3.2.1 Biased Low-Variance Estimator
A plausible unbiased estimator is the developed below.
Let us be an estimator of the gradient of the expected loss with respect to the activation (pre-nonlinearity) of unit , and let unit compute its activation as a deterministic smooth function of the output of unit (for example, ). Then we can clearly get an estimator of the gradient with respect to by . The problem is to back-propagate through the binary threshold function which produced from the noise and the activation (Eq. 2). The biased estimator we propose here222already explored by Goeff Hinton (Hinton, 2012), lecture 15b is simply
as the estimator of the gradient of the expected loss with respect to , i.e., we ignore the derivative of the threshold function .
3.2.2 Combining a Low-Variance High-Bias Estimator with a High-Variance Low-Bias Estimator
Let us assume someone hands us two estimators and , with the first having low variance but high bias, while the second has high variance and low bias. How could we take advantage of them to obtain a better estimator?
What we propose is the following: train a function which takes as input the high-bias low-variance estimator and predicts the low-bias high-variance estimator .
By construction, since is a deterministic function of a low-variance quantity (we could add other inputs to help it in its prediction, but they should not be too noisy), it should also have low variance. Also by construction, and to the extent that the learning task is feasible, the prediction will strive to be as close as possible to the expected value of the unbiased estimator, i.e., . It is therefore a way to unbias to the extent that it is possible. Note that adding appropriate auxiliary inputs to could be helpful in this respect.
4 Efficiency of Reward Correlation Estimators
One of the questions that future work should address is the efficiency of estimators such as those introduced above.
4.1 The Unbiased Estimator as Reward Correlator
In this respect, it is interesting to note how the proposed unbiased estimator (in particular the centered one) is very similar in form to the just estimating the correlation between the stochastic decision and the ensuing loss :
Note how this is the correlation between and in the context of the other noise sources that influence . Note that a particular “noise source” is just the input of the model.
4.2 The Boltzmann Machine Gradient as Unnormalized Reward Correlation
The log-likelihood gradient over a bias (offset) parameter associated with a unit (visible or hidden) of a Boltzmann machine with distribution and a training example (associated with visible units ) is
where the expectation is over the model’s distribution, which defines a joint distribution between all the units of the model, including the visible ones. The conditional distribution of binary unit given the other units is given by
where is the unit activation (prior to applying the sigmoid). An unbiased estimator of the above gradient is
where is a configuration obtained while was clamped to a training example , and is a configuration obtained without this constraint. Similarly, the log-likelihood gradient over weight is estimated unbiasedly by
We call these estimators the Boltzmann machine log-likelihood gradient estimators.
We now show that this log-likelihood gradient can also be interpreted as an unnormalized reward correlator in a particular setup, where the objective is to discriminate between examples coming from the data generating distribution and examples from the model distribution .
The setup is the following. Let the model generate samples , where are the visible units and the hidden units. Let the reward for generating a “negative example” in this way be . However, let us toss a coin to select from this stream some samples such that we can declare as coming from the training distribution of interest , for example using rejection sampling. Then we let the reward be because the model has generated a “good example” . Although this might be a rare event let us randomly choose among the negative examples so that the average number of negative examples equals the average number of positive examples . Clearly the samples follow the training distribution while the samples follow the model distribution . In our setup, let us imagine that ’s were obtained by Gibbs sampling. The Gibbs chain corresponds to constructing a computational graph that deterministically or stochastically computes various quantities (one per node of the graph), here the activation and the binomially sampled bit for each stochastic unit at each step of the chain.
Using Theorem 1, is an unbiased estimator of
Now we consider an unnormalized variant of that estimator,
where (rather than its centered version) is multiplied by . This means in particular that is a (possibly biased) estimator of the gradient with respect to , while, by the chain rule is an estimator of the gradient with respect to .
If we separate the training examples into those with and those with , we obtain the estimator associated with a pair,
for biases , and
for weights . The above two estimators of the gradients and correspond exactly to the Boltzmann machine log-likelihood gradient estimators.
In this paper, we have motivated estimators of the gradient through highly non-linear non-differentiable functions (such as those corresponding to an indicator function), especially in networks involving noise sources, such as neural networks with stochastic neurons. They can be useful as biologically motivated models and they might be useful for engineering (computational efficiency) reasons when trying to reduce computation via conditional computation or to reduce interactions between parameters via sparse updates (Bengio, 2013). We have discussed a general class of stochastic neurons for which the gradient is exact for given fixed noise sources, but where the non-linearity is not saturating on all of its range (semi-hard stochastic neurons), and for which ordinary back-prop can be used. We have also discussed the case of completely saturating non-linearities, for which we have demonstrated the existence of an unbiased estimator based on correlating the perturbation with the observed reward, which is related to but different from the SPSA (Spall, 1992) estimator. Indeed the latter divides the change in reward by the perturbation, instead of multiplying them.
We have shown that it was possible in principle to obtain lower variance estimators, in particular by training a function to turn a biased estimator but low variance estimator into one that is trained to be unbiased but has lower variance. We have also shown that the Boltzmann machine gradient could be interpreted as a particular form of reward correlation (without the additive normalization). Since training of Restricted Boltzmann Machines has been rather successful, this suggests that correlation-based estimators might actually work well in practice. Clearly, future work should investigate the relative practical merits of these estimators, and in particular how their variance scale with respect to the number of independent noise sources (e.g., stochastic neurons) present in the system.
The author would like to thank NSERC, the Canada Research Chairs, and CIFAR for support.
- Bengio (2009) Bengio, Y. (2009). Learning deep architectures for AI. Now Publishers.
- Bengio (2013) Bengio, Y. (2013). Deep learning of representations: Looking forward. Technical Report arXiv:1305.0445, Universite de Montreal.
- Bengio et al. (2013) Bengio, Y., Courville, A., and Vincent, P. (2013). Unsupervised feature learning and deep learning: A review and new perspectives. IEEE Trans. Pattern Analysis and Machine Intelligence (PAMI).
- El Hihi and Bengio (1996) El Hihi, S. and Bengio, Y. (1996). Hierarchical recurrent neural networks for long-term dependencies. In NIPS 8. MIT Press.
- Fiete and Seung (2006) Fiete, I. R. and Seung, H. S. (2006). Gradient learning in spiking neural networks by dynamic perturbations of conductances. Physical Review Letters, 97(4).
- Glorot et al. (2011) Glorot, X., Bordes, A., and Bengio, Y. (2011). Deep sparse rectifier neural networks. In AISTATS.
- Goodfellow et al. (2013) Goodfellow, I. J., Warde-Farley, D., Mirza, M., Courville, A., and Bengio, Y. (2013). Maxout networks. In ICML’2013.
- Hinton (2012) Hinton, G. (2012). Neural networks for machine learning. Coursera, video lectures.
- Hinton et al. (1984) Hinton, G. E., Sejnowski, T. J., and Ackley, D. H. (1984). Boltzmann machines: Constraint satisfaction networks that learn. Technical Report TR-CMU-CS-84-119, Carnegie-Mellon University, Dept. of Computer Science.
- Hinton et al. (2006) Hinton, G. E., Osindero, S., and Teh, Y.-W. (2006). A fast learning algorithm for deep belief nets. Neural Computation, 18, 1527–1554.
- Hinton et al. (2012) Hinton, G. E., Srivastava, N., Krizhevsky, A., Sutskever, I., and Salakhutdinov, R. (2012). Improving neural networks by preventing co-adaptation of feature detectors. Technical report, arXiv:1207.0580.
- Krizhevsky et al. (2012a) Krizhevsky, A., Sutskever, I., and Hinton, G. (2012a). ImageNet classification with deep convolutional neural networks. In NIPS’2012.
- Krizhevsky et al. (2012b) Krizhevsky, A., Sutskever, I., and Hinton, G. (2012b). ImageNet classification with deep convolutional neural networks. In Advances in Neural Information Processing Systems 25 (NIPS’2012).
- Nair and Hinton (2010) Nair, V. and Hinton, G. E. (2010). Rectified linear units improve restricted Boltzmann machines. In ICML’10.
- Rumelhart et al. (1986) Rumelhart, D. E., Hinton, G. E., and Williams, R. J. (1986). Learning representations by back-propagating errors. Nature, 323, 533–536.
- Salakhutdinov and Hinton (2009) Salakhutdinov, R. and Hinton, G. (2009). Semantic hashing. In International Journal of Approximate Reasoning.
- Spall (1992) Spall, J. C. (1992). Multivariate stochastic approximation using a simultaneous perturbation gradient approximation. IEEE Transactions on Automatic Control, 37, 332–341.
- Vincent et al. (2008) Vincent, P., Larochelle, H., Bengio, Y., and Manzagol, P.-A. (2008). Extracting and composing robust features with denoising autoencoders. In ICML 2008.