```python
# Notebook imports and packages
import numpy as np
from sympy import symbols, diff, lambdify
```

# Please lambdify your derivatives

$$f(x, y)=\frac{1}{3^{-x^2-y^2}+1}$$
<hr color="lightblue">
$$\frac{\partial f(x, y)}{\partial x}=\frac{2x\ln \left(3\right)\cdot \:3^{-x^2-y^2}}{\left(3^{-x^2-y^2}+1\right)^2}$$
<hr color="lightblue">
$$\frac{\partial f(x, y)}{\partial y}=\frac{2y\ln \left(3\right)\cdot \:3^{-y^2-x^2}}{\left(3^{-x^2-y^2}+1\right)^2}$$


```python
def f(x, y):
    return 1/(3**(-(x**2)-(y**2)) + 1)
```


```python
a, b = symbols('x, y')
```

Make sure to lambdify your functions:

If you do GD with `diff(f(a,b),a).evalf(subs={a:params[0],b:params[1]})` differentiating every time, it will be slow.

10k iterations with `dfx(x,y)` is way faster than 1k iterations differentiating every time.


```python
dfx=lambdify([a,b], diff(f(a,b), a)) 
dfy=lambdify([a,b], diff(f(a,b), b)) 
```


```python
params = np.array([1.8, 1.0])
```


```python
dfx(x=1.8,y=1.0), dfx(1.8,1.0), dfx(*params) # All three result in the same thing
```




    (0.036808971619750504, 0.036808971619750504, 0.036808971619750504)



# Bath Gradient Descent with SymPy


```python
multiplier = .1
max_iter = 10000
params = np.array([1.8, 1.0])

for n in range(max_iter):
    gradient_x, gradient_y = dfx(*params), dfy(*params)
    gradients = np.array([gradient_x,gradient_y]) # These two first steps could be combined into one;
    params = params - multiplier * gradients

print("Values in gradient array", gradients)
print("Minimum occurs at (x,y):", tuple(params))
print("The cost is:\t\t", f(*params))
```

    Values in gradient array [1.61557085e-244 8.97539364e-245]
    Minimum occurs at (x,y): (2.7795548456563392e-244, 1.544197136475744e-244)
    The cost is:		 0.5


667150e54731e46049ef7d4906c6e19ad9fb93c0
ipynb
Jupyter Notebook
Section_04/Example_04_(05-08)/07-GD_and_Lambdify.ipynb
ArielMAJ/Data-Science-and-Machine-Learning_Bootcamp
afae685c96d9fc8af0b2ee1be4d817df505c6c8d
MIT
Section_04/Example_04_(05-08)/07-GD_and_Lambdify.ipynb
ArielMAJ/Data-Science-and-Machine-Learning_Bootcamp
afae685c96d9fc8af0b2ee1be4d817df505c6c8d
MIT
Section_04/Example_04_(05-08)/07-GD_and_Lambdify.ipynb
ArielMAJ/Data-Science-and-Machine-Learning_Bootcamp
afae685c96d9fc8af0b2ee1be4d817df505c6c8d
MIT
Qwen/Qwen-72B
1. YES
2. YES