return f def df(x): #fの勾配 const = -2 * (1 - pmas.ups(x)) df = const * pmas.dups(x) # df = np.array(df) # print(df) return df ######## initial = np.array([5.0, -14.48, 8.6, 0.28, -280000.0, 34.9, 17.0]) #初期値 ######## algo = gd.GradientDescent(f, df) # 勾配降下法のインスタンスを生成 algo.solve(initial) # 勾配降下法を計算 print(algo.x__) # 計算結果で得られた xx を出力 print(algo.opt__) # 最小化された目的関数 f を出力 plt.scatter(initial[0], initial[1]) plt.plot(algo.path__[:, 0], algo.path__[:, 1], linewidth=1.5) xs = np.linspace(-2, 2, 300) ys = np.linspace(-2, 2, 300) xmesh, ymesh = np.meshgrid(xs, ys) xx = np.r_[xmesh.reshape(1, -1), ymesh.reshape(1, -1)] levels = [-3.0, -2.5, -2.0, 0, 1, 2] # plt.contour(xs,ys,f(xx).reshape(xmesh,ymesh),levels = levels, colors = "k", linestyles = "dotted")
import gd def f(xx): x = xx[0] y = xx[1] return 5 * x**2 - 6 * x * y + 3 * y**2 + 6 * x - 6 * y def df(xx): x = xx[0] y = xx[1] return np.array([10 * x - 6 * y + 6, -6 * x + 6 * y - 6]) algo = gd.GradientDescent(f, df) initial = np.array([1, 1]) algo.solve(initial) print(algo.x_) print(algo.opt_) plt.scatter(initial[0], initial[1], color="k", marker="o") plt.plot(algo.path_[:, 0], algo.path_[:, 1], color="k", linewidth=1.5) xs = np.linspace(-2, 2, 300) ys = np.linspace(-2, 2, 300) xmesh, ymesh = np.meshgrid(xs, ys) xx = np.r_[xmesh.reshape(1, -1), ymesh.reshape(1, -1)] levels = [-3, -2.9, -2.8, -2.6, -2.4, -2.2, -2, -1, 0, 1, 2, 3, 4]
z = fxy(x_vec).reshape(xmesh.shape) return xmesh, ymesh, z if __name__ == '__main__': # constant alphas = [0.01, 0.05, 0.1, 0.2] x_range, y_range = [-3, 3], [-3, 3] levels = [-3, -2.9, -2.8, -2.6, -2.4, -2.2, -2, -1, 0, 1, 2, 3, 4] # contour levels fig = plt.figure(figsize=(15, 5)) axes = fig.subplots(1, len(alphas)) # calculation for i, alpha in enumerate(alphas): # solve algo = gd.GradientDescent(f, df, alpha=alpha) initial = np.array([1, 1]) algo.solve(initial) # plot xmesh, ymesh, z = mesh(x_range, y_range, fxy=f) axes[i].scatter(initial[0], initial[1], marker="o", s=20) axes[i].plot(algo.path[:, 0], algo.path[:, 1], ls="-", color="k") map_ax = axes[i].contour(xmesh, ymesh, z, linestyles="dotted", colors="b", levels=levels) # cmap="jet" # config