<jupyter_start><jupyter_text>Table of Contents1&nbsp;&nbsp;Descente de gradient: choix de pas, démonstration interactive  Descente de gradient: choix de pas, démonstration interactive***> __Auteur__: <NAME>, <NAME>  We're interested in minimizing$f(x_1, x_2) = (x_1^2 + x_2 - 11)^2 + (x_1 + x_2^2 - 7)^2$**Question** : is it a convex function?<jupyter_code>import numpy as np


def f(x):
    x1, x2 = x
    return (x1**2 + x2 - 11)**2 + (x1 + x2**2 - 7)**2
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib
cmap_reversed = matplotlib.cm.get_cmap('RdBu_r')
from mpl_toolkits import mplot3d
X1, X2 = np.meshgrid(np.linspace(-5.5, 5.5, 50),
                     np.linspace(-5.5, 5.5, 50))
Z = f([X1, X2])  # Altitude
fig = plt.figure(figsize=(6, 6))
ax = plt.axes(projection='3d')
ax.plot_surface(X1, X2, Z, rstride=1, cstride=1,
                cmap=cmap_reversed, edgecolor='none')
ax.set_xlim(-5, 5)
ax.set_ylim(-5, 5)
ax.set_zlim(0, 500)
plt.show()
def plot(xs=None):
    levels = list(1.7 ** np.linspace(0, 10, 30) - 1.) + [300]
    plt.figure(figsize=(8, 8))
    plt.contourf(X1, X2, np.sqrt(Z), levels=np.sqrt(
        levels), cmap=cmap_reversed)
    plt.colorbar(extend='both')
    if xs is not None:
        x1, x2 = np.array(xs).T
        plt.plot(x1, x2, 'k')
        plt.plot(x1, x2, 'o', color='purple')
    plt.show()


plot()
def f_grad(x):
    x1, x2 = x
    df_x1 = 2 * (-7 + x1 + x2**2 + 2 * x1 * (-11 + x1**2 + x2))
    df_x2 = 2 * (-11 + x1**2 + x2 + 2 * x2 * (-7 + x1 + x2**2))
    return np.array([df_x1, df_x2])


x0 = [-0.2, 0.72]


def grad_descent(step_size=0.01, max_iter=0):
    """Gradient descent with constant step size"""
    x = x0
    xs = [x]
    for k in range(max_iter):
        d_k = - f_grad(x)  # direction of descent
        t_k = step_size   # does not depend on k
        x = x + step_size * d_k
        xs.append(x)
    plot(xs)
    plt.show()


grad_descent(0.01, max_iter=20)
from ipywidgets import interact

interact(grad_descent, step_size=(0., .05, 0.005), max_iter=(0, 50, 1));
from scipy import optimize

def grad_descent_line_search(step_size=0.01, max_iter=2, line_search=False):
    """Gradient descent with constant step size or line search"""
    x = x0
    xs = [x]
    for k in range(max_iter):
        d_k = -f_grad(x)
        if line_search:
            c1, c2 = 0.1, 0.7
            t_k = optimize.line_search(f, f_grad, x, d_k, -d_k, c1=c1, c2=c2)[0]
        else:
            t_k = step_size
        x = x + t_k * d_k
        xs.append(x)
    plot(xs)


interact(grad_descent_line_search, step_size=(0., .05, 0.005), max_iter=(0, 50, 1));<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16258015843078083, 'fraction_numerical': 0.06186344775556394, 'mean_word_length': 2.8658892128279883, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '333161', 'license': 'MIT', 'n_tokens_mistral': 1202, 'n_tokens_neox': 1127, 'n_words': 269, 'path': 'Descente de gradient JS.ipynb', 'repo_name': 'fradav/Cours-Deep-Learning', 'size': '2670'}