#!/usr/bin/env python # coding: utf-8 # In[1]: import numpy as np # # $sigmoid(x) = \frac{1}{1+e^{(-x)}}$ # In[2]: def sigmoid(x): """ Compute the sigmoid function for the input here. Arguments: x -- A scalar or numpy array. Return: s -- sigmoid(x) """ ### YOUR CODE HERE s = 1 / (1 + np.exp(-x)) ### END YOUR CODE return s # # sigmoid_grad # - $\sigma(-x) = \frac{1}{1 + e^{x}} = \frac{e^x+1}{e^x + 1} - \frac{e^x}{e^x + 1}=1-\sigma(x)$ # # - $\sigma' = \sigma(x) \times (1 - \sigma(x))$ # In[3]: def sigmoid_grad(s): """ Compute the gradient for the sigmoid function here. Note that for this implementation, the input s should be the sigmoid function value of your original input x. Arguments: s -- A scalar or numpy array. Return: ds -- Your computed gradient. """ ### YOUR CODE HERE ds = s * (1-s) ### END YOUR CODE return ds # In[4]: def test_sigmoid_basic(): """ Some simple tests to get you started. Warning: these are not exhaustive. """ print("Running basic tests...") x = np.array([[1, 2], [-1, -2]]) f = sigmoid(x) g = sigmoid_grad(f) print(f) f_ans = np.array([ [0.73105858, 0.88079708], [0.26894142, 0.11920292]]) assert np.allclose(f, f_ans, rtol=1e-05, atol=1e-06) print(g) g_ans = np.array([ [0.19661193, 0.10499359], [0.19661193, 0.10499359]]) assert np.allclose(g, g_ans, rtol=1e-05, atol=1e-06) print("You should verify these results by hand!\n") if __name__ == "__main__": test_sigmoid_basic();