Codes for the "Noisy Activation Functions" paper.
Common nonlinear activation functions used in neural networks can cause training difficulties due to the saturation behavior of the activation function, which may hide dependencies that are not visible to vanilla-SGD (using first order gradients only). Gating mechanisms that use softly saturating activation functions to emulate the discrete switching of digital logic circuits are good examples of this. We propose to exploit the injection of appropriate noise so that the gradients may flow easily, even if the noiseless application of the activation function would yield zero gradient. Large noise will dominate the noise-free gradient and allow stochastic gradient descent toexplore more. By adding noise only to the problematic parts of the activation function, we allow the optimization procedure to explore the boundary between the degenerate (saturating) and the well-behaved parts of the activation function. We also establish connections to simulated annealing, when the amount of noise is annealed down, making it easier to optimize hard objective functions. We find experimentally that replacing such saturating activation functions by noisy variants helps training in many contexts, yielding state-of-the-art or competitive results on different datasets and task, especially when training seems to be the most difficult, e.g., when curriculum learning is necessary to obtain good results.READ FULL TEXT VIEW PDF
Codes for the "Noisy Activation Functions" paper.
A list of current activation functions in deep learning.
The introduction of the piecewise-linear activation functions such as ReLU and MaxoutGoodfellow et al. (2013) units had a profound effect on deep learning, and was a major catalyst in allowing the training of much deeper networks. It is thanks to ReLU that for the first time it was shown (Glorot et al., 2011) that deep purely supervised networks can be trained, whereas using nonlinearity only allowed to train shallow networks. A plausible hypothesis about the recent surge of interest on these piecewise-linear activation functions (Glorot et al., 2011)
, is due to the fact that they are easier to optimize with SGD and backpropagation than smooth activation functions, such asand
. The recent successes of piecewise linear functions is particularly evident in computer vision, where the ReLU has become the default choice in convolutional networks.
We propose a new technique to train neural networks with activation functions which strongly saturate when their input is large. This is mainly achieved by injecting noise to the activation function in its saturated regime and learning the level of noise. Using this approach, we have found that it was possible to train neural networks with much wider family of activation functions than previously. Adding noise to the activation function has been considered for ReLU units and was explored in Bengio et al. (2013); Nair & Hinton (2010)
for feed-forward networks and Boltzmann machines to encourage units to explore more and make the optimization easier.
More recently there has been a resurgence of interest in more elaborated “gated” architectures such as LSTMs Hochreiter & Schmidhuber (1997) and GRUs Cho et al. (2014), but also encompassing neural attention mechanisms that have been used in the NTM Graves et al. (2014), Memory Networks Weston et al. (2014), automatic image captioning Xu et al. (2015), video caption generation Yao et al. (2015) and wide areas of applications LeCun et al. (2015). A common thread running through these works is the use of soft-saturating non-linearities, such as the sigmoid or softmax, to emulate the hard decisions of digital logic circuits. In spite of its success, there are two key problems with this approach.
Since the non-linearities still saturate there are problems with vanishing gradient information flowing through the gates; and
since the non-linearities only softly saturate they do not allow one to take hard decisions.
Although gates often operate in the soft-saturated regime Karpathy et al. (2015); Bahdanau et al. (2014); Hermann et al. (2015) the architecture prevents them from being fully open or closed. We follow a novel approach to address both of these problems. Our method addresses the second problem through the use of hard-saturating nonlinearities, which allow gates to make perfectly on or off decisions when they saturate. Since the gates are able to be completely open or closed, no information is lost through the leakiness of the soft-gating architecture.
By introducing hard-saturating nonlinearities, we have exacerbated the problem of gradient flow, since gradients in the saturated regime are now precisely zero instead of being negligible. However, by introducing noise into the activation function which can grow based on the magnitude of saturation, we encourage random exploration.
At test time the noise in the activation functions can be removed or replaced with the expectation, and as our experiments show, the resulting deterministic networks outperform their soft-saturating counterparts on a wide variety of tasks, and allow to reach state-of-the-art performance by simple drop-in replacement of the nonlinearities in existing training code.
The technique that we propose, addresses the difficulty of optimization and having hard-activations at the test time for gating units and we propose a way of performing simulated annealing for neural networks.
(Activation Function). An activation function is a function that is differentiable almost everywhere.
(Saturation). An activation function with derivative is said to right (resp. left) saturate if its limit as (resp. ) is zero. An activation function is said to saturate (without qualification) if it both left and right saturates.
Most common activation functions used in recurrent networks (for example, and ) are saturating. In particular they are soft saturating, meaning that they achieve saturation only in the limit.
(Hard and Soft Saturation). Let be a constant such that implies and left hard saturates when implies , . We say that hard saturates (without qualification) if it both left and right hard saturates. An activation function that saturates but achieves zero gradient only in the limit is said to soft saturate.
We can construct hard saturating versions of soft saturating activation functions by taking a first-order Taylor expansion about zero and clipping the results to an appropriate range.
For example, expanding and around , with , we obtain linearized functions and of and respectively:
Clipping the linear approximations result to,
The motivation behind this construction is to introduce linear behavior around zero to allow gradients to flow easily when the unit is not saturated, while providing a crisp decision in the saturated regime.
The ability of the hard-sigmoid and hard-tanh to make crisp decisions comes at the cost of exactly 0 gradients in the saturated regime. This can cause difficulties during training: a small but not infinitesimal change of the pre-activation (before the nonlinearity) may help to reduce the objective function, but this will not be reflected in the gradient.
In the rest of the document we will use to refer to a generic activation function and use to denote its linearization based on the first-order Taylor expansion about zero. saturates when or and saturates when or . We denote the threshold by . Absolute values of the threshold are for and for the .
Let us note that both , and are all contractive mapping. , becomes a contractive mapping only when its input is greater than the threshold. An important difference among these activation functions is their fixed points. has a fixed point at . However the fixed-point of is . Any between and can be the fixed-point of , but the fixed-point of is . and have point attractors at their fixed-points. Those mathematical differences among the saturating activation functions can make them behave differently with RNNs and deep networks.
The highly non-smooth gradient descent trajectory may bring the parameters into a state that pushes the activations of a unit towards the gradient regime for a particular example, from where it may become difficult to escape and the unit may get stuck in the gradient regime.
When units saturate and gradients vanish, an algorithm may require many training examples and a lot of computation to recover.
Consider a noisy activation function in which we have injected iid noise , to replace a saturating nonlinearity such as the and introduced in the previous section. In the next section we describe the proposed noisy activation function which has been used for our experiments, but here we want to consider a larger family of such noisy activation functions, when we use a variant of stochastic gradient descent (SGD) for training.
have varianceand mean . We want to characterize what happens as we gradually anneal the noise, going from large noise levels () to no noise at all .
Furthermore, we will assume that is such that when the noise level becomes large, so does its derivative with respect to :
In the noise limit, we recover a deterministic nonlinearity, , which in our experiments is piecewise linear and allows us to capture the kind of complex function we want to learn. As illustrated in Figure 2, in the large noise limit, large gradients are obtained because backpropagating through gives rise to large derivatives. Hence, the noise drowns the signal: the example-wise gradient on parameters is much larger than it would have been with . SGD therefore just sees noise and can move around anywhere in parameter space without “seeing” any trend.
Annealing is also related to the signal to noise ratio where can be defined as the ratio of the variance of noise and , . If , the model will do pure random exploration. As we anneal will increase, and when converges to
, the only source of exploration during the training will come from the noise of Monte Carlo estimates of stochastic gradients.
This is precisely what we need for methods such as simulated annealing (Kirkpatrick et al., 1983) and continuation methods (Allgower & Georg, 1980) to be helpful, in the context of the optimization of difficult non-convex objectives. With high noise, SGD is free to explore all parts of space. As the noise level is decreased, it will prefer some regions where the signal is strong enough to be “visible” by SGD: given a finite number of SGD steps, the noise is not averaged out, and the variance continues to dominate. Then as the noise level is reduced SGD spends more time in “globally better” regions of parameter space. As it approaches to zero we are fine-tuning the solution and converging near a minimum of the noise-free objective function. A related approach of adding noise to gradients and annealing the noise was investigated in Neelakantan et al. (2015) as well. Ge et al. (2015) showed that SGD with annealed noise will globally converge to a local-minima for non-convex objective functions in polynomial number of iterations. Recently, Mobahi (2016)
propose an optimization method that applies Gaussian smoothing on the loss function such that annealing weight noise is a Monte Carlo estimator of that.
A novel idea behind the proposed noisy activation is that the amount of noise added to the nonlinearity is proportional to the magnitude of saturation of the nonlinearity. For and , due to our parametrization of the noise, that translates into the fact that the noise is only added when the hard-nonlinearity saturates. This is different from previous proposals such as the noisy rectifier from Bengio (2013) where noise is added just before a rectifier (ReLU) unit, independently of whether the input is in the linear regime or in the saturating regime of the nonlinearity.
The motivation is to keep the training signal clean when the unit is in the non-saturating (typically linear) regime and provide some noisy signal when the unit is in the saturating regime.
refer to hard saturation activation function such as the hard-sigmoid and hard-tanh introduced in Sec. 2, we consider noisy activation functions of the following form:
and . Here
is an iid random variable drawn from some generating distribution, and the parametersand (discussed below) are used to generate a location scale family from .
Intuitively when the unit saturates we pin its output to the threshold value and add noise. The exact behavior of the method depends on the type of noise and the choice of and , which we can pick as functions of in order to let some gradients be propagated even when we are in the saturating regime.
A desirable property we would like to approximately satisfy is that, in expectation, it is equal to the hard-saturating activation function, i.e.
If the distribution has zero mean then this property can be satisfied by setting , but for biased noise it will be necessary to make other choices for . In practice, we used slightly biased with good results.
Intuitively we would like to add more noise when is far into the saturated regime, since a large change in parameters would be required desaturate . Conversely, when is close to the saturation threshold a small change in parameters would be sufficient for it to escape. To that end we make use of the difference between the original activation function and its linearization
when choosing the scale of the noise. See Eqs.1 for definitions of for the and respectively. The quantity is zero in the unsaturated regime, and when saturates it grows proportionally to the distance between and the saturation threshold . We also refer as the magnitude of the saturation.
We experimented with different ways of scaling with , and empirically found that the following formulation performs better:
In Equation 9 a free scalar parameter is learned during the course of training. By changing , the model is able to adjust the magnitude of the noise and that also effects the sign of the gradient as well. The hyper-parameter
changes the scale of the standard deviation of the noise.
In the simplest case of our method we draw from an unbiased distribution, such as a standard normal. In this case we choose to satisfy Equation 7 and therefore we will have,
Due to our parameterization of , when our stochastic activation function behaves exactly as the linear function , leading to familiar territory. Because will be
. Let us for the moment restrict our attention to the case whenand saturates. In this case the derivative of is precisely zero, however, if we condition on the sample we have
which is non-zero almost surely.
In the non-saturated regime, where the optimization can exploit the linear structure of near the origin in order to tune its output. In the saturated regime the randomness in drives exploration, and gradients still flow back to since the scale of the noise still depends on . To reiterate, we get gradient information at every point in spite of the saturation of , and the variance of the gradient information in the saturated regime depends on the variance of .
An unsatisfying aspect of the formulation with unbiased noise is that, depending on the value of occasionally the gradient of will point the wrong way. This can cause a backwards message that would push in a direction that would worsen the objective function on average over . Intuitively we would prefer these messages to “push back” the saturated unit towards a non-saturated state where the gradient of can be used safely.
A simple way to achieve this is to make sure that the noise is always positive and adjust its sign manually to match the sign of . In particular we could set
where and are as before and is the sign function, such that is if is greater than or equal to otherwise it is . We also use the absolute value of
in the reparametrization of the noise, such that the noise is being sampled from a half-Normal distribution. We ignored the sign of, such that the direction that the noise pushes the activations are determined by and it will point towards . Matching the sign of the noise to the sign of would ensure that we avoid the sign cancellation between the noise and the gradient message from backpropagation. is required to push the activations towards when the bias from is introduced.
In practice we use a hyperparameterthat influences the mean of the added term, such that near 1 approximately satisfies the above condition, as seen in Fig. 4. We can rewrite the noisy term in a way that the noise can either be added to the linearized function or . The relationship between , and is visualized Figure 3 can be expressed as in Eqn 11.
We have experimented with different types of noise. Empirically, in terms of performance we found, half-normal and normal noise to be better. In Eqn 11, we provide the formulation for the activation function where if the noise is sampled from half-normal distribution, if the noise is sampled from normal distribution.
By using Eqn 11, we arrive at the noisy activations, which we used in our experiments.
As can be seen in Eqn 12, there are three paths that gradients can flow through the neural network, the linear path (), nonlinear path () and the stochastic path (). The flow of gradients through these different pathways across different layers makes the optimization of our activation function easier.
At test time, we used the expectation of Eqn 12 in order to get deterministic units,
If , then is . Otherwise if , then is .
To illustrate the effect of and noisy activation of the , We provide plots of our stochastic activation functions in Fig 4.
The condition in Eqn 5 is satisfied only when is learned. Experimentally we found small values of to work better. When is fixed and small, as gets larger and further away from the threshold , noise will less likely be able to push the activations back to the linear regime. We also investigated the effect of injecting input noise when the activations saturate:
In our experiments, we used noise only during training: at test time we replaced the noise variable with its expected value. We performed our experiments with just a drop-in replacement of the activation functions in existing experimental setups, without changing the previously set hyper-parameters. Hence it is plausible one could obtain better results by performing a careful hyper-parameter tuning for the models with noisy activation functions. In all our experiments, we initialized uniform randomly from the range .
We provide experimental results using noisy activations with normal (NAN), half-normal noise (NAH), normal noise at the input of the function (NANI), normal noise at the input of the function with learned (NANIL) and normal noise injected to the input of the function when the unit saturates (NANIS). Codes for different types of noisy activation functions can be found at https://xx.
As a sanity-check, we performed small-scale control experiments, in order to observe the behavior of the noisy units. In Fig 5, we showed the learning curves of different types of activations with various types of noise in contrast to the and units. The models are single-layer MLPs trained on MNIST for classification and we show the average negative log-likelihood . In general, we found that models with noisy activations converge faster than those using and activation functions, and to lower NLL than the network.
We trained layer MLP on a dataset generated from a mixture of Gaussian distributions with different means and standard deviations. Each layer of the MLP contains -hidden units. Both the model with and noisy activations was able to solve this task almost perfectly. By using the learned values, in Figure 6 and 7, we showed the scatter plot of the activations of each unit at each layer and the derivative function of each unit at each layer with respect to its input.
We further investigated the performances of network with activation functions using NAN, NANI and NANIS on penntreebank (PTB) character-level language modeling. We used a GRU language model over sequences of length . We used the same model and train all the activation functions with the same hyperparameters except we ran a grid-search for for NANI and NANIS from with values. We choose the best based on the validation bit-per-character (BPC). We have not observed important difference among NAN and NANI in terms of training performance as seen on Figure 8.
The problem of predicting the output of a short program introduced in Zaremba & Sutskever (2014) 111The code is residing at https://github.com/wojciechz/learning_to_execute. We thank authors for making it publicly available. proved challenging for modern deep learning architectures. The authors had to use curriculum learning (Bengio et al., 2009) to let the model capture knowledge about the easier examples first and increase the level of difficulty of the examples further down the training.
We replaced all and
non-linearities in the reference model with their noisy counterparts. We changed the default gradient clipping tofrom in order to avoid numerical stability problems. When evaluating a network, the length (number of lines) of the executed programs was set to and nesting was set to , which are default settings in the released code for these tasks. Both the reference model and the model with noisy activations were trained with “combined” curriculum which is the most sophisticated and the best performing one.
Our results show that applying the proposed activation function leads to better performance than that of the reference model. Moreover it shows that the method is easy to combine with a non-trivial learning curriculum. The results are presented in Table 1 and in Figure 10
|Model name||Test Accuracy|
We trained a layer word-level LSTM language model on Penntreebank. We used the same model proposed by Zaremba et al. (2014). 222We used the code provided in https://github.com/wojzaremba/lstmWe simply replaced all and units with noisy and units. The reference model is a well-finetuned strong baseline from Zaremba et al. (2014). For the noisy experiments we used exactly the same setting, but decreased the gradient clipping threshold to from . We provide the results of different models in Table 2. In terms of validation and test performance we did not observe big difference between the additive noise from Normal and half-Normal distributions, but there is a substantial improvement due to noise, which makes this result the new state-of-the-art on this task, as far as we know.
|Valid ppl||Test ppl|
|Noisy LSTM + NAN||111.7||108.0|
|Noisy LSTM + NAH||112.6||108.7|
|BLEU -1||BLEU-2||BLEU-3||BLEU-4||METEOR||Test NLL|
|Soft Attention (Sigmoid and Tanh) (Reference)||67||44.8||29.9||19.5||18.9||40.33|
|Soft Attention (NAH Sigmoid & Tanh)||66||45.8||30.69||20.9||20.5||40.17|
|Soft Attention (NAH Sigmoid & Tanh wo dropout)||64.9||44.2||30.7||20.9||20.3||39.8|
|Soft Attention (NANI Sigmoid & Tanh)||66||45.0||30.6||20.7||20.5||40.0|
|Soft Attention (NANIL Sigmoid & Tanh)||66||44.6||30.1||20.0||20.5||39.9|
|Hard Attention (Sigmoid and Tanh)||67||45.7||31.4||21.3||19.5||-|
|Sigmoid and Tanh NMT (Reference)||65.26||20.18|
|Hard-Tanh and Hard-Sigmoid NMT||64.27||21.59|
|Noisy (NAH) Tanh and Sigmoid NMT||63.46||22.57|
We evaluated our noisy activation functions on a network trained on the Flickr8k dataset. We used the soft neural attention model proposed in Xu et al. (2015) as our reference model.444We used the code provided at https://github.com/kelvinxu/arctic-captions. We scaled down the weight matrices initialized to be orthogonal scaled by multiplying with . As shown in Table 3, we were able to obtain better results than the reference model and our model also outperformed the best model provided in Xu et al. (2015) in terms of Meteor score.
(Xu et al., 2015)’s model was using dropout with the ratio of on the output of the LSTM layers and the context. We have tried both with and without dropout, as in Table 3, we observed improvements with the addition of dropout to the noisy activation function. But the main improvement seems to be coming with the introduction of the noisy activation functions since the model without dropout already outperforms the reference model.
We performed experiments to validate the effect of annealing the noise to obtain a continuation method for neural networks.
We designed a new task where, given a random sequence of integers, the objective is to predict the number of unique elements in the sequence. We use an LSTM network over the input sequence, and performed a time average pooling over the hidden states of LSTM to obtain a fixed-size vector. We feed the pooled LSTM representation into a simple (one hidden-layer) ReLU MLP in order to predict the unique number of elements in the input sequence. In the experiments we fixed the length of input sequence to and the input values are between and . In order to anneal the noise, we started training with the scale hyperparameter of the standard deviation of noise with and annealed it down to with the schedule of where is being incremented at every minibatch updates. When noise annealing is combined with a curriculum strategy (starting with short sequences first and gradually increase the length of the training sequences), the best models are obtained.
|Test Error %|
|Noisy LSTM+MLP(NAN) Annealed Noise||9.53|
|Noisy LSTM+MLP(NANIL) Annealed Noise||20.94|
As a second test, we used the same annealing procedure in order to train a Neural Turing Machine (NTM) on the associative recall taskGraves et al. (2014). We trained our model with a minimum of items and a maximum of items. We show results of the NTM with noisy activations in the controller, with annealed noise, and compare with a regular NTM in terms of validation error. As can be seen in Figure 11, the network using noisy activation converges much faster and nails the task, whereas the original network failed to approach a low error.
Nonlinearities in neural networks are both a blessing and a curse. A blessing because they allow to represent more complicated functions and a curse because that makes the optimization more difficult. For example, we have found in our experiments that using a hard version (hence more nonlinear) of the sigmoid and nonlinearities often improved results. In the past, various strategies have been proposed to help deal with the difficult optimization problem involved in training some deep networks, including curriculum learning, which is an approximate form of continuation method. Earlier work also included softened versions of the nonlinearities that are gradually made harder during training. Motivated by this prior work, we introduce and formalize the concept of noisy activations as a general framework for injecting noise in nonlinear functions so that large noise allows SGD to be more exploratory. We propose to inject the noise to the activation functions either at the input of the function or at the output where unit would otherwise saturate, and allow gradients to flow even in that case. We show that our noisy activation functions are easier to optimize. It also, achieves better test errors, since the noise injected to the activations also regularizes the model as well. Even with a fixed noise level, we found the proposed noisy activations to outperform their sigmoid or counterpart on different tasks and datasets, yielding state-of-the-art or competitive results with a simple modification, for example on PennTreebank. In addition, we found that annealing the noise to obtain a continuation method could further improved performance.
International Conference on Artificial Intelligence and Statistics, pp. 315–323, 2011.
The authors would like to acknowledge the support of the following agencies for research funding and computing support: NSERC, Calcul Québec, Compute Canada, Samsung, the Canada Research Chairs and CIFAR. We would also like to thank the developers of Theano555http://deeplearning.net/software/theano/, for developing such a powerful tool for scientific computing. Caglar Gulcehre also thanks to IBM Watson Research and Statistical Knowledge Discovery Group at IBM Research for supporting this work during his internship.