<jupyter_start><jupyter_text>最急降下法とニュートン法機械学習を意識すると，データが given，モデルを構築し，損失関数およびコスト関数を定義するというようにすべきだが，今回は artificial な関数に対して勾配法を用いるというようにする．また，勾配を求める処理も自動微分を用いて行っている．ここでは，最急降下法とニュートン法を実装した．<jupyter_code>########## Packages ##########
import autograd.numpy as np
from lib.lib import Test_function
from lib.plot import Plot_func
from lib.steepest_descent import Steepest
from lib.newton import Newton
##############################
##### instance of the class #####
test = Test_function()
pf = Plot_func()
stp = Steepest(maxiter=250)
nt = Newton(maxiter=250)
#################################<jupyter_output><empty_output><jupyter_text>最急降下法<jupyter_code>##### execute steepest descent #####
xinit = np.array([2.0, 1.5])
distort_steepest_x, itr_distort_steepest = stp.steepest_descent(
    func=test.distorted, xinit=xinit)
####################################
x_range = np.linspace(-2.5, 2.5, 51); y_range = np.linspace(-2.5, 2.5, 51)
pf.plot_contour(
    func=test.distorted, x_range=x_range, y_range=y_range, 
    x_val=distort_steepest_x.T[0], y_val=distort_steepest_x.T[1], 
    itr=itr_distort_steepest, func_name="distorted", method_name="steepest")<jupyter_output><empty_output><jupyter_text><jupyter_code>##### execute steepest descent #####
xinit = np.array([2.0, 2.5])
ackley_steepest_x, itr_ackley_steepest = stp.steepest_descent(
    func=test.ackley, xinit=xinit)
####################################
x_range = np.linspace(-5, 5, 51); y_range = np.linspace(-5, 5, 51)
pf.plot_contour(
    func=test.ackley, x_range=x_range, y_range=y_range, 
    x_val=ackley_steepest_x.T[0], y_val=ackley_steepest_x.T[1], 
    itr=itr_ackley_steepest, func_name="ackley", method_name="steepest")<jupyter_output><empty_output><jupyter_text><jupyter_code>##### execute steepest descent #####
xinit = np.array([2.0, 1.0])
matyas_steepest_x, itr_matyas_steepest = stp.steepest_descent(
    func=test.matyas, xinit=xinit)
####################################
x_range = np.linspace(-0.5, 2.25, 51); y_range = np.linspace(-0.5, 2.25, 51)
pf.plot_contour(
    func=test.matyas, x_range=x_range, y_range=y_range, 
    x_val=matyas_steepest_x.T[0], y_val=matyas_steepest_x.T[1], 
    itr=itr_matyas_steepest, func_name="matyas", method_name="steepest")<jupyter_output><empty_output><jupyter_text>ニュートン法<jupyter_code>##### execute Newton #####
xinit = np.array([2.0, 1.5])
distort_newton_x, itr_distort_newton = nt.newton(
    func=test.distorted, xinit=xinit)
##########################
x_range = np.linspace(-2.5, 2.5, 51); y_range = np.linspace(-2.5, 2.5, 51)
pf.plot_contour(
    func=test.distorted, x_range=x_range, y_range=y_range, 
    x_val=distort_newton_x.T[0], y_val=distort_newton_x.T[1], 
    itr=itr_distort_newton, func_name="distorted", method_name="newton")<jupyter_output><empty_output><jupyter_text><jupyter_code>##### execute Newton #####
xinit = np.array([2.0, 2.5])
ackley_newton_x, itr_ackley_newton = nt.newton(
    func=test.ackley, xinit=xinit)
##########################
x_range = np.linspace(-5, 5, 51); y_range = np.linspace(-5, 5, 51)
pf.plot_contour(
    func=test.ackley, x_range=x_range, y_range=y_range, 
    x_val=ackley_newton_x.T[0], y_val=ackley_newton_x.T[1], 
    itr=itr_ackley_newton, func_name="ackley", method_name="newton")<jupyter_output><empty_output><jupyter_text><jupyter_code>##### execute Newton #####
xinit = np.array([2.0, 1.0])
matyas_newton_x, itr_matyas_newton = nt.newton(
    func=test.matyas, xinit=xinit)
##########################
x_range = np.linspace(-0.5, 2.25, 51); y_range = np.linspace(-0.5, 2.25, 51)
pf.plot_contour(
    func=test.matyas, x_range=x_range, y_range=y_range, 
    x_val=matyas_newton_x.T[0], y_val=matyas_newton_x.T[1], 
    itr=itr_matyas_newton, func_name="matyas", method_name="newton")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21473851030110935, 'fraction_numerical': 0.029054410987849975, 'mean_word_length': 7.341409691629956, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '110721', 'license': 'MIT', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1692, 'n_words': 203, 'path': 'note/1.steepest_newton.ipynb', 'repo_name': 'SeeKT/ML_optimization', 'size': '3786'}