```python
#derivative (slope at given point)

from sympy import *
import numpy as np
```


```python
x,y,z = sym.symbols('x y z')
```


```python
# single variable ftns
sym.diff(x**3)
```




$\displaystyle 3 x^{2}$




```python
# product rule (use cos from sympy lib)
sym.diff((x**3 + 18) * sym.cos(x))
```




$\displaystyle 3 x^{2} \cos{\left(x \right)} - \left(x^{3} + 18\right) \sin{\left(x \right)}$




```python
#chain rule (der = der of an outer ftn * inner ftn, then multipled by der of inner ftn.)
sym.diff((x**2 - 3*x +5)**3)
```




$\displaystyle \left(6 x - 9\right) \left(x^{2} - 3 x + 5\right)^{2}$




```python
# multi-variable partial derivative for x
sym.diff(x**3 * y, x)
```




$\displaystyle 3 x^{2} y$




```python
# multi-variable partial derivative for y
sym.diff(x**3 * y, y)
```




$\displaystyle x^{3}$




```python
# set ftn to variable
f = x**3 * y * z**2
```


```python
sym.diff(f, z)
```




$\displaystyle 2 x^{3} y z$




```python
# der of sigmoid ftn
sym.diff(1/1 + sym.exp(-x))
```




$\displaystyle - e^{- x}$




```python
sym.integrate(sym.exp(-x), (x, 0, sym.oo))
```




$\displaystyle 1$




```python
integrate(exp(-x), (x,0, oo))
```




$\displaystyle 1$




```python

#sigmoid for binary classification
import matplotlib.pyplot as plt
import numpy as np

def sigmoid(x):
    s=1/(1+np.exp(-x))
    ds=s*(1-s)  
    return s,ds
x=np.arange(-6,6,0.01)
sigmoid(x)
# Setup centered axes
fig, ax = plt.subplots(figsize=(9, 5))
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Create and show plot
ax.plot(x,sigmoid(x)[0], color="#307EC7", linewidth=3, label="sigmoid")
ax.plot(x,sigmoid(x)[1], color="#9621E2", linewidth=1, label="derivative")
ax.legend(loc="upper right", frameon=False)
fig.show()
```


```python
#> sigmoid, easier optmization, values -1 to 1, centers to zero?, suffers from vanishing gradient problem

import matplotlib.pyplot as plt
import numpy as np

def tanh(x):
    t=(np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))
    dt=1-t**2
    return t,dt
z=np.arange(-4,4,0.01)
tanh(z)[0].size,tanh(z)[1].size
# Setup centered axes
fig, ax = plt.subplots(figsize=(9, 5))
ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
# Create and show plot
ax.plot(z,tanh(z)[0], color="#307EC7", linewidth=3, label="tanh")
ax.plot(z,tanh(z)[1], color="#9621E2", linewidth=1, label="derivative")
ax.legend(loc="upper right", frameon=False)
fig.show()
```


```python
# ReLu, less comp espensive than tanh and sigmoid, avoid vanishing gradient, should only be used in hidden layers of NN model, can result in dead neurons.
```

845648024ab8173fd70711bb276b844b432e0ce7
ipynb
Jupyter Notebook
derivatives.ipynb
masonkadem/mathfunctions
7733880da524be712ba8bac0632b2ca00557f387
MIT
derivatives.ipynb
masonkadem/mathfunctions
7733880da524be712ba8bac0632b2ca00557f387
MIT
derivatives.ipynb
masonkadem/mathfunctions
7733880da524be712ba8bac0632b2ca00557f387
MIT
Qwen/Qwen-72B
1. YES
2. YES