5.4 Activation Functions | Choosing activation functions--[Machine Learning | Andrew Ng]
 Let's take a look at how you can choose the activation function for different neurons in your neural network. We'll start with some guidance for how to choose it for the output layer. It turns out that depending on what the target label or the ground truth label Y is, there will be one fairly natural choice for the activation function for the output layer. And we'll then go and look at the choice of the activation function also for the hidden layers of your neural network. Let's take a look. You can choose different activation functions for different neurons in your neural network. And when considering the activation function for the output layer, it turns out that there will often be one fairly natural choice depending on what is the target or the ground truth label Y. Specifically, if you are working on a classification problem where Y is either 0 or 1, so a binary classification problem, then the sigmoid activation function will almost always be the most natural choice because then the neural network learns to predict the probability that Y is equal to 1, just like we had for logistic regression. So my recommendation is if you're working on a binary classification problem, use sigmoid at the output layer. Alternatively, if you're solving a regression problem, then you might choose a different activation function. For example, if you're trying to predict how tomorrow's stock price will change compared to today's stock price, well, it can go up or down. And so in this case, Y would be a number that can be either positive or negative. And in that case, I would recommend you use the linear activation function. Why is that? Well, that's because then the output of your neural network, f of x, which is equal to a3 in the example above, would be g applied to z3. And with the linear activation function, g of z can take on either positive or negative values. So Y can be positive or negative, use the linear activation function. And finally, if Y can only take on non-negative values, such as if you're predicting the price of a house, that can never be negative. Then the most natural choice would be the ReLU activation function. Because as you see here, this activation function only takes on non-negative values, either zero or positive values. So when choosing the activation function to use for your output layer, usually depending on what is the label, why you're trying to predict, there'll be one fairly natural choice. And in fact, the guidance on this slide is how I pretty much always choose my activation function as well for the output layer of a neural network. How about the hidden layers of a neural network? It turns out that the ReLU activation function is by far the most common choice in how neural networks are trained by many, many practitioners today. Even though we had initially described neural networks using the sigmoid activation function, and in fact, in the early history of the development of neural networks, people use sigmoid activation functions in many places, the field has evolved to use ReLU much more often and sigmoids hardly ever. With the one exception that you do use a sigmoid activation function in the output layer if you have a binary classification problem. So why is that? Well, there are a few reasons. First, if you compare the ReLU and the sigmoid activation functions, the ReLU is a bit faster to compute because it just requires computing max of 0, z, whereas the sigmoid requires taking an exponentiation and an inverse and so on, and so it's a little bit less efficient. But the second reason, which turns out to be even more important, is that the ReLU function kind of goes flat only in one part of the graph, here on the left, it's completely flat. Whereas the sigmoid activation function, it kind of goes flat in two places. It goes flat to the left of the graph and it goes flat to the right of the graph. And if you're using gradient descent to train a neural network, then when you have a function that is flat in a lot of places, gradient descent will be really slow. I know that gradient descent optimizes the cost function J of WB rather than optimizes the activation function, but the activation function is a piece of what goes into computing, and that results in more places in the cost function J of WB that are flat as well and with a smaller gradient and it slows down learning. I know that that was just an intuitive explanation, but researchers have found that using the ReLU activation function can cause your neural network to learn a bit faster as well, which is why for most practitioners, if you're trying to decide what activation function to use for the hidden layer, the ReLU activation function has become now by far the most common choice. In fact, if I'm building a neural network, this is how I choose activation functions for the hidden layers as well. So to summarize, here's what I recommend in terms of how you choose the activation functions for your neural network. For the output layer, use a sigmoid if you have a binary classification problem, linear if Y is a number that can take on positive or negative values, or use ReLU if Y can take on only positive values or zero positive values or non-negative values. Then for the hidden layers, I would recommend just using ReLU as a default activation function. And in TensorFlow, this is how you would implement it. Rather than saying activation equals sigmoid as we had previously, you can then for the hidden layers, that's the first hidden layer, the second hidden layer, ask TensorFlow to use the ReLU activation function. And then for the output layer, in this example, I've asked it to use the sigmoid activation function. But if you want it to use the linear activation function instead, that's the syntax for it. Or if you wanted to use the ReLU activation function, that shows the syntax for it. With this richer set of activation functions, you'd be well positioned to build much more powerful neural networks than just once using only the sigmoid activation function. By the way, if you look at the research literature, you sometimes hear of authors using even other activation functions, such as the tanh activation function or the leaky ReLU activation function or the swish activation function. Every few years, researchers sometimes come up with another interesting activation function, and sometimes they do work a little bit better. For example, I've used the leaky ReLU activation function a few times in my work, and sometimes it works a little bit better than the ReLU activation function you learned about in this video. But I think for the most part, and for the vast majority of applications, what you learned about in this video would be good enough. Of course, if you want to learn more about other activation functions, feel free to look on the internet, and there are just a small handful of cases where these other activation functions could be even more powerful as well. With that, I hope you also enjoy practicing these ideas, these activation functions, in the optional labs and in the practice labs. But this raises yet another question. Why do we even need activation functions at all? Why don't we just use the linear activation function, or use no activation function anywhere? It turns out this does not work at all. And in the next video, let's take a look at why that's the case, and why activation functions are so important for getting your neural networks to work.
[{"start": 0.0, "end": 6.3, "text": " Let's take a look at how you can choose the activation function for different neurons"}, {"start": 6.3, "end": 7.84, "text": " in your neural network."}, {"start": 7.84, "end": 12.42, "text": " We'll start with some guidance for how to choose it for the output layer."}, {"start": 12.42, "end": 17.88, "text": " It turns out that depending on what the target label or the ground truth label Y is, there"}, {"start": 17.88, "end": 24.14, "text": " will be one fairly natural choice for the activation function for the output layer."}, {"start": 24.14, "end": 28.48, "text": " And we'll then go and look at the choice of the activation function also for the hidden"}, {"start": 28.48, "end": 30.28, "text": " layers of your neural network."}, {"start": 30.28, "end": 31.72, "text": " Let's take a look."}, {"start": 31.72, "end": 37.96, "text": " You can choose different activation functions for different neurons in your neural network."}, {"start": 37.96, "end": 43.92, "text": " And when considering the activation function for the output layer, it turns out that there"}, {"start": 43.92, "end": 50.84, "text": " will often be one fairly natural choice depending on what is the target or the ground truth"}, {"start": 50.84, "end": 51.84, "text": " label Y."}, {"start": 51.84, "end": 59.64, "text": " Specifically, if you are working on a classification problem where Y is either 0 or 1, so a binary"}, {"start": 59.64, "end": 66.16, "text": " classification problem, then the sigmoid activation function will almost always be the most natural"}, {"start": 66.16, "end": 72.88, "text": " choice because then the neural network learns to predict the probability that Y is equal"}, {"start": 72.88, "end": 76.28, "text": " to 1, just like we had for logistic regression."}, {"start": 76.28, "end": 81.60000000000001, "text": " So my recommendation is if you're working on a binary classification problem, use sigmoid"}, {"start": 81.6, "end": 83.36, "text": " at the output layer."}, {"start": 83.36, "end": 88.44, "text": " Alternatively, if you're solving a regression problem, then you might choose a different"}, {"start": 88.44, "end": 90.28, "text": " activation function."}, {"start": 90.28, "end": 96.83999999999999, "text": " For example, if you're trying to predict how tomorrow's stock price will change compared"}, {"start": 96.83999999999999, "end": 100.0, "text": " to today's stock price, well, it can go up or down."}, {"start": 100.0, "end": 105.0, "text": " And so in this case, Y would be a number that can be either positive or negative."}, {"start": 105.0, "end": 110.32, "text": " And in that case, I would recommend you use the linear activation function."}, {"start": 110.32, "end": 111.32, "text": " Why is that?"}, {"start": 111.32, "end": 115.55999999999999, "text": " Well, that's because then the output of your neural network, f of x, which is equal to"}, {"start": 115.55999999999999, "end": 121.24, "text": " a3 in the example above, would be g applied to z3."}, {"start": 121.24, "end": 126.88, "text": " And with the linear activation function, g of z can take on either positive or negative"}, {"start": 126.88, "end": 127.88, "text": " values."}, {"start": 127.88, "end": 132.0, "text": " So Y can be positive or negative, use the linear activation function."}, {"start": 132.0, "end": 139.04, "text": " And finally, if Y can only take on non-negative values, such as if you're predicting the price"}, {"start": 139.04, "end": 142.16, "text": " of a house, that can never be negative."}, {"start": 142.16, "end": 146.28, "text": " Then the most natural choice would be the ReLU activation function."}, {"start": 146.28, "end": 151.6, "text": " Because as you see here, this activation function only takes on non-negative values, either"}, {"start": 151.6, "end": 153.7, "text": " zero or positive values."}, {"start": 153.7, "end": 159.12, "text": " So when choosing the activation function to use for your output layer, usually depending"}, {"start": 159.12, "end": 164.84, "text": " on what is the label, why you're trying to predict, there'll be one fairly natural choice."}, {"start": 164.84, "end": 171.48000000000002, "text": " And in fact, the guidance on this slide is how I pretty much always choose my activation"}, {"start": 171.48000000000002, "end": 174.76, "text": " function as well for the output layer of a neural network."}, {"start": 174.76, "end": 178.36, "text": " How about the hidden layers of a neural network?"}, {"start": 178.36, "end": 185.88, "text": " It turns out that the ReLU activation function is by far the most common choice in how neural"}, {"start": 185.88, "end": 190.66, "text": " networks are trained by many, many practitioners today."}, {"start": 190.66, "end": 196.96, "text": " Even though we had initially described neural networks using the sigmoid activation function,"}, {"start": 196.96, "end": 202.4, "text": " and in fact, in the early history of the development of neural networks, people use sigmoid activation"}, {"start": 202.4, "end": 210.72, "text": " functions in many places, the field has evolved to use ReLU much more often and sigmoids hardly"}, {"start": 210.72, "end": 211.72, "text": " ever."}, {"start": 211.72, "end": 216.22, "text": " With the one exception that you do use a sigmoid activation function in the output layer if"}, {"start": 216.22, "end": 218.88, "text": " you have a binary classification problem."}, {"start": 218.88, "end": 219.88, "text": " So why is that?"}, {"start": 219.88, "end": 221.84, "text": " Well, there are a few reasons."}, {"start": 221.84, "end": 228.88, "text": " First, if you compare the ReLU and the sigmoid activation functions, the ReLU is a bit faster"}, {"start": 228.88, "end": 234.79999999999998, "text": " to compute because it just requires computing max of 0, z, whereas the sigmoid requires"}, {"start": 234.79999999999998, "end": 240.76, "text": " taking an exponentiation and an inverse and so on, and so it's a little bit less efficient."}, {"start": 240.76, "end": 246.64, "text": " But the second reason, which turns out to be even more important, is that the ReLU function"}, {"start": 246.64, "end": 254.0, "text": " kind of goes flat only in one part of the graph, here on the left, it's completely flat."}, {"start": 254.0, "end": 258.32, "text": " Whereas the sigmoid activation function, it kind of goes flat in two places."}, {"start": 258.32, "end": 266.32, "text": " It goes flat to the left of the graph and it goes flat to the right of the graph."}, {"start": 266.32, "end": 272.03999999999996, "text": " And if you're using gradient descent to train a neural network, then when you have a function"}, {"start": 272.04, "end": 278.24, "text": " that is flat in a lot of places, gradient descent will be really slow."}, {"start": 278.24, "end": 285.14000000000004, "text": " I know that gradient descent optimizes the cost function J of WB rather than optimizes"}, {"start": 285.14000000000004, "end": 291.36, "text": " the activation function, but the activation function is a piece of what goes into computing,"}, {"start": 291.36, "end": 297.6, "text": " and that results in more places in the cost function J of WB that are flat as well and"}, {"start": 297.6, "end": 301.64000000000004, "text": " with a smaller gradient and it slows down learning."}, {"start": 301.64, "end": 306.56, "text": " I know that that was just an intuitive explanation, but researchers have found that using the"}, {"start": 306.56, "end": 312.24, "text": " ReLU activation function can cause your neural network to learn a bit faster as well, which"}, {"start": 312.24, "end": 316.32, "text": " is why for most practitioners, if you're trying to decide what activation function to use"}, {"start": 316.32, "end": 322.03999999999996, "text": " for the hidden layer, the ReLU activation function has become now by far the most common"}, {"start": 322.03999999999996, "end": 323.03999999999996, "text": " choice."}, {"start": 323.03999999999996, "end": 328.08, "text": " In fact, if I'm building a neural network, this is how I choose activation functions"}, {"start": 328.08, "end": 330.44, "text": " for the hidden layers as well."}, {"start": 330.44, "end": 337.04, "text": " So to summarize, here's what I recommend in terms of how you choose the activation functions"}, {"start": 337.04, "end": 338.84, "text": " for your neural network."}, {"start": 338.84, "end": 345.04, "text": " For the output layer, use a sigmoid if you have a binary classification problem, linear"}, {"start": 345.04, "end": 351.52, "text": " if Y is a number that can take on positive or negative values, or use ReLU if Y can take"}, {"start": 351.52, "end": 356.56, "text": " on only positive values or zero positive values or non-negative values."}, {"start": 356.56, "end": 365.12, "text": " Then for the hidden layers, I would recommend just using ReLU as a default activation function."}, {"start": 365.12, "end": 368.84000000000003, "text": " And in TensorFlow, this is how you would implement it."}, {"start": 368.84000000000003, "end": 374.76, "text": " Rather than saying activation equals sigmoid as we had previously, you can then for the"}, {"start": 374.76, "end": 380.0, "text": " hidden layers, that's the first hidden layer, the second hidden layer, ask TensorFlow to"}, {"start": 380.0, "end": 383.08, "text": " use the ReLU activation function."}, {"start": 383.08, "end": 389.35999999999996, "text": " And then for the output layer, in this example, I've asked it to use the sigmoid activation"}, {"start": 389.35999999999996, "end": 390.35999999999996, "text": " function."}, {"start": 390.35999999999996, "end": 396.12, "text": " But if you want it to use the linear activation function instead, that's the syntax for it."}, {"start": 396.12, "end": 402.26, "text": " Or if you wanted to use the ReLU activation function, that shows the syntax for it."}, {"start": 402.26, "end": 407.36, "text": " With this richer set of activation functions, you'd be well positioned to build much more"}, {"start": 407.36, "end": 413.72, "text": " powerful neural networks than just once using only the sigmoid activation function."}, {"start": 413.72, "end": 419.56, "text": " By the way, if you look at the research literature, you sometimes hear of authors using even other"}, {"start": 419.56, "end": 426.56, "text": " activation functions, such as the tanh activation function or the leaky ReLU activation function"}, {"start": 426.56, "end": 429.68, "text": " or the swish activation function."}, {"start": 429.68, "end": 434.64, "text": " Every few years, researchers sometimes come up with another interesting activation function,"}, {"start": 434.64, "end": 437.56, "text": " and sometimes they do work a little bit better."}, {"start": 437.56, "end": 443.24, "text": " For example, I've used the leaky ReLU activation function a few times in my work, and sometimes"}, {"start": 443.24, "end": 448.12, "text": " it works a little bit better than the ReLU activation function you learned about in this"}, {"start": 448.12, "end": 449.12, "text": " video."}, {"start": 449.12, "end": 453.8, "text": " But I think for the most part, and for the vast majority of applications, what you learned"}, {"start": 453.8, "end": 456.76, "text": " about in this video would be good enough."}, {"start": 456.76, "end": 461.84, "text": " Of course, if you want to learn more about other activation functions, feel free to look"}, {"start": 461.84, "end": 467.4, "text": " on the internet, and there are just a small handful of cases where these other activation"}, {"start": 467.4, "end": 471.56, "text": " functions could be even more powerful as well."}, {"start": 471.56, "end": 477.59999999999997, "text": " With that, I hope you also enjoy practicing these ideas, these activation functions, in"}, {"start": 477.59999999999997, "end": 480.96, "text": " the optional labs and in the practice labs."}, {"start": 480.96, "end": 483.76, "text": " But this raises yet another question."}, {"start": 483.76, "end": 486.84, "text": " Why do we even need activation functions at all?"}, {"start": 486.84, "end": 492.71999999999997, "text": " Why don't we just use the linear activation function, or use no activation function anywhere?"}, {"start": 492.71999999999997, "end": 495.76, "text": " It turns out this does not work at all."}, {"start": 495.76, "end": 500.46, "text": " And in the next video, let's take a look at why that's the case, and why activation functions"}, {"start": 500.46, "end": 517.16, "text": " are so important for getting your neural networks to work."}]