There are many times that you will want to take the derivative of an expression. Taking a derivative is a very common thing to do in Machine Learning. One example is the activation function of a neural network. If you are going to train the neural network using any of the backpropagation techniques, you will need the derivative of the activation function. In this article I will show you how to take the derivative of a function using R.

For this article we will obtain the derivative of the Sigmoid Activation Function. You might be tempted to create a function for the sigmoid. This can be done as follows.

1 | sigmoid <- function(x) { |

Once the sigmoid function has been created it can easily be queried as follows.

1 | sigmoid(0) |

The above code would produce 0.5. Functions are useful when you need to graph the function. However, to take the derivative you must produce an expression. This is done as follows.

1 | library(Ryacas); |

This produces the following.

1 | e^-x * log(e)/(1 + e^-x)^2 |

If you would like to simplify the above, use the following.

1 | Simplify(deriv(s,x)); |