def gradient_method(fn, x_init, lr=0.01, step=100): x = x_init # 점진적으로 변화시킬 변수 x_history = [] # x가 변화되는 과정을 저장할 배열 for i in range(step): # step 회수만큼 반복하면서 x_history.append(x.copy()) # x의 복사본을 x 변화 과정에 기록 grad = numerical_gradient(fn, x) # x에서의 gradient를 계산 x -= lr * grad # x_new = x_init - lr * grad: x를 변경 return x, np.array(x_history)
def gradient_method(fn, x_init, lr = 0.01, step = 100): """ :param fn: :param x_init: :param lr: learning rate :param step: 몇번 반복할 것인지 :return: """ x = x_init #점진적으로 변호시킬 변수 x_history = [] #x가 변화되는 과정을 저장할 배열 #나중에 로그를 보기 위함 -> 과정의 기록 for i in range(step): #step 횟수만큼 반복 x_history.append(x.copy()) #x의 복사본을 x 변화 과정에 기록 # if we dont use copy(): only shows the same number -> the address of array x # because x is an array -> if we only append x [x_history.append(x)], the array returns the address of array x -> @123 @123 @123 -> 이렇게 append 되는 것 # -> but what we need is the "values" of array x -> we have to use the function copy() -> @123 @124 @125 ... grad = numerical_gradient(fn, x) #점 x에서의 gradient 계산 (gradient가 있어야 새로운 점을 계산할 수 있다) x -= lr * grad #x_new = x_init - lr * grad: x를 변경 return x, np.array(x_history)
def gradient_method(fn, x_init, lr=0.01, step=100): """ 경사 하강법 : 현 위치에서 기울어진 방향으로 일정 거리만큼 이동, 그런 다음 이동한 곳에서도 마찬가지로 기울기를 구하고, 또 그 기울어진 방향으로 나아가기를 반복해서 함수의 값을 점차 줄이는 것 :param fn: 그래프를 그릴 함수 :param x_init: 초기 x값 :param lr: 학습율(변화율) :param step: 반복횟수 :return: 최종 x값 """ x = x_init # 점진적으로 변화시킬 변수(초기 x값) x_history = [] # x가 변화되는 과정을 저장할 배열 for i in range(step): x_history.append(x.copy()) # x의 복사본을 x 변화 과정에 기록 gradient = numerical_gradient(fn, x) # 점 x에서의 gradient 계산 x -= lr * gradient # x_new = x_init - lr * gradient -> x를 변경 return x, np.array(x_history)
# Error: # gradients = numerical_gradient(fn, np.array([X,Y])) # print(gradients) # 안되는 이유: X(2차원 배열) + Y(2차원 배열)을 다시 배열로 묶었다 -> 고려 대상 밖 # X와 Y의 2차원 배열을 1차원으로 풀어준다 X = X.flatten() Y = Y.flatten() print('X = ', X) print('Y = ', Y) # (X,Y) 를 좌표로 묶으려고 하는 것 XY = np.array([X,Y]) print('XY =', XY) gradients = numerical_gradient(fn, XY) print('gradients = ', gradients) x0 = np.arange(-2, 2.5, 0.25) # 구간을 더 잘게 쪼개기 # print('x0 =',x0) x1 = np.arange(-2, 2.5, 0.25) # print('x1 =', x1) X,Y = np.meshgrid(x0, x1) X = X.flatten() Y = Y.flatten() XY = np.array([X,Y]) gradients = numerical_gradient(fn, XY) plt.quiver(X, Y, -gradients[0], -gradients[1], angles = 'xy') # quiver: https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.quiver.html
def gradient(self, x, t): """ W 행렬에 대한 손실함수의 기울기 """ fn = lambda W: self.loss(x, t) return numerical_gradient(fn, self.W)
def gradient(self, x, t): """ x: 입력, t: 출력 실제 값 (정답 레이블) """ fn = lambda W: self.loss(x, t) #오차의 최솟값 구하기 -> 손실함수 사용 (loss) # [0. , 0., 1. ] return numerical_gradient(fn, self.W)