AI | Gradient Descent Method

송치헌·2021년 6월 11일
0
post-thumbnail

import numpy as np
#define the fuction and its gradient
def f(x,y):
    return x**2+y**2+2*(np.sin(1.5*(x**2+y**2)))**2+2

def fx(x,y): #df/dx
    return 2*x*(3*np.sin(3*(x**2+y**2))+1)

def fy(x,y): #df/dy
    return 2*y*(3*np.sin(3*(x**2+y**2))+1)
#Gradient Descent
x0=1
y0=1
xk=x0
yk=y0
t0=0.0001 #step size
tk=t0
eps=10**(-10)
iter=0
max_iter=100000
while True:
    dxk=-fx(xk,yk)
    dyk=-fy(xk,yk)
    xkp1=xk+tk*dxk
    ykp1=yk+tk*dyk
    iter = iter+1

    norm_dist=np.sqrt((xkp1-xk)**2+(ykp1-yk)**2)
    if norm_dist<eps:
        print("Converges at : ",iter)
        print("Min f(x) is attained at (", xkp1, ",", ykp1,")")
        break
 
    if iter>max_iter:
        print("Not converge!")

    xk=xkp1
    yk=ykp1
profile
https://oraange.tistory.com/ 여기에도 많이 놀러와 주세요

0개의 댓글

관련 채용 정보