def gradient_descent(f, init_x, lr=0.01, step_num=100): x = init_x for i in range(step_num): grad = numerical_gradient(f, x) x -= lr * grad return x
def numerical_gradient(self, x, t): """重みパラメーターに対する勾配の算出 Args: x (numpy.ndarray): ニューラルネットワークへの入力 t (numpy.ndarray): 正解のラベル Returns: dictionary: 勾配を格納した辞書 """ loss_W = lambda W: self.loss(x, t) grads = {} grads['W1'] = numerical_gradient(loss_W, self.params['W1']) grads['b1'] = numerical_gradient(loss_W, self.params['b1']) grads['W2'] = numerical_gradient(loss_W, self.params['W2']) grads['b2'] = numerical_gradient(loss_W, self.params['b2']) return grads
def gradient_descent(f, init_x, lr=0.01, step_num=100): x = init_x x_history = [] for i in range(step_num): x_history.append(x.copy()) grad = numerical_gradient(f, x) x -= lr * grad return x, np.array(x_history)
def gradient_descent(f, init_x, lr=0.01, step_num=100): x = init_x # 초깃값 x_history = [] for i in range(step_num): x_history.append( x.copy() ) # x가 갱신되기 전 값을 저장 grad = numerical_gradient(f, x) # 함수의 기울기를 구함 x -= lr * grad # 기울기에 학습률을 곱한 값으로 x를 갱신 return x, np.array(x_history)
def gradient_descent(f, init_x, lr=0.01, step_num=100): x = init_x x_history = [] # numerical_gradient 这个函数会求f函数的梯度,用该梯度乘以学习率得到的值进行更新操作 for i in range(step_num): x_history.append(x.copy()) grad = numerical_gradient(f, x) x -= lr * grad return x, np.array(x_history)
def gradient_descent(f, init_x, lr=0.01, step_num=100): x = init_x x_history = [] for i in range(step_num): x_history.append( x.copy() ) grad = numerical_gradient(f, x) x -= lr * grad return x, np.array(x_history)
def gradient_descent(f, init_x, lr=0.01, step_num=100): x = init_x x_history = [] for i in range(step_num): x_history.append(x.copy()) grad = numerical_gradient(f, x) plt.quiver(x[0], x[1], -grad[0], -grad[1], angles="xy", color="#666666") x -= lr * grad return x, np.array(x_history)
sys.path.append(os.pardir) # 親ディレクトリのファイルをインポートするための設定 import numpy as np from 2_softmax import softmax from cross_entropy_function import cross_entropy_error from gradient_2d import numerical_gradient class simpleNet: def __init__(self): self.W = np.random.randn(2,3) def predict(self, x): return np.dot(x, self.W) def loss(self, x, t): z = self.predict(x) y = softmax(z) loss = cross_entropy_error(y, t) return loss x = np.array([0.6, 0.9]) t = np.array([0, 0, 1]) net = simpleNet() f = lambda w: net.loss(x, t) dW = numerical_gradient(f, net.W) print(dW)