勾配法のテスト

import numpy as np
import matplotlib.pyplot as plt

Lx = lambda x : 2*x
Ly = lambda y : 2*y
Lx2 = lambda x : 2*x/20.0
Ly2 = lambda y : 2*y

def gradient_descent(eta, Lx, Ly):
    # eta = 0.01
    xinit = 3
    yinit = 4
    X = np.array([xinit,yinit])
    epsilon = 1e-6
    count = 0
    xs = [xinit]
    ys = [yinit]
    while True:
        x = X[0]
        y = X[1]
        LX = np.array([Lx(x), Ly(y)])
        X = X - eta*LX
        grad = max(abs(Lx(x)),abs(Ly(y))) 
        if grad < epsilon:
            break
        count += 1
        xs.append(x)
        ys.append(y)
    
    print("###Gradient descent###")
    print("eta=",eta)
    print("minimum value",x,y)
    print("convergence period",count)
    # plt.plot(xs,ys,'o-')
    # plt.show()

gradient_descent(0.01, Lx, Ly)
gradient_descent(0.1, Lx, Ly)
gradient_descent(0.5, Lx, Ly)

gradient_descent(0.01, Lx2, Ly2)
gradient_descent(0.1, Lx2, Ly2)
gradient_descent(0.5, Lx2, Ly2)