-
Notifications
You must be signed in to change notification settings - Fork 9
/
objective_function.py
321 lines (243 loc) · 8.16 KB
/
objective_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import numpy as np
import matplotlib.pyplot as plt
# 그런데 보통 한글 글꼴에는 유니코드 마이너스(−)가 없고
# 일반 마이너스(-) 기호만 있습니다.
# 눈으로 보기에는 비슷해보이지만 다른 글자입니다.
# 따라서 유니코드 마이너스 기호를 쓰지 않도록 설정해줍니다.
def f1(x):
return (x - 2) ** 2 + 2
xx = np.linspace(-1, 4, 100)
plt.plot(xx, f1(xx))
plt.plot(2, 2, 'ro', markersize=10)
plt.ylim(0, 10)
plt.xlabel("x")
plt.ylabel("$f_1(x)$")
plt.title("Objective Function")
plt.show()
def f1d(x):
"""f1(x)의 도함수"""
return 2 * (x - 2.0)
xx = np.linspace(-1, 4, 100)
plt.plot(xx, f1(xx), 'k-')
plt.show()
#plt.hold('on')
# step size
mu = 0.4
# k = 0
x = 0
plt.plot(x, f1(x), 'go', markersize=10)
plt.text(x + 0.1, f1(x) + 0.1, "1st trial")
plt.plot(xx, f1d(x) * (xx - x) + f1(x), 'b--')
plt.show()
#plt.hold('on')
# f1(x)에서 접선 긋기
# plt.plot(xx, f1d(x)*xx, 'r--') 직선을 평행이동한 것
# x축으로 0 만큼, y축으로 6 만큼 평행이동한 직선이 접선이 됨
# f1d(x) * (xx[0] - x) + f1(x) : 10
# f1d(x) * (xx[1] - x) + f1(x) : 9.8
# f1d(x) * (xx[2] - x) + f1(x) : 9.6
# f1d(x) * (xx[99] - x) + f1(x) : -10
print("1st trial: x_1 = {:.2f}, g_1 = {:.2f}".format(x, f1d(x)))
# k = 1
x = x - mu * f1d(x)
plt.plot(x, f1(x), 'go', markersize=10)
plt.text(x - 0.2, f1(x) + 0.4, "2nd trial" )
plt.plot(xx, f1d(x) * (xx - x) + f1(x), 'b--')
# plt.plot(xx, f1d(x)*xx, 'm--') 직선을 평행이동한 것
# x축으로 1.6 만큼, y축으로 2.16 만큼 평행이동한 직선이 접선이 됨
print("2차 시도: x_2 = {:.2f}, g_2 = {:.2f}".format(x, f1d(x)))
# k = 2
x = x - mu * f1d(x)
plt.plot(x, f1(x), 'go', markersize=10)
plt.text(x - 0.2, f1(x) - 0.7, "3rd trial")
plt.plot(xx, f1d(x) * (xx - x) + f1(x), 'b--')
# plt.plot(xx, f1d(x)*xx, 'c--') 직선을 평행이동한 것
# x축으로 1.92 만큼, y축으로 2.0064 만큼 평행이동한 직선이 접선이 됨
print("3차 시도: x_3 = {:.2f}, g_3 = {:.2f}".format(x, f1d(x)))
plt.xlabel("x")
plt.ylabel("$f_1(x)$")
plt.title("optimization by gradient descent")
plt.ylim(0, 10)
plt.show()
xx = np.linspace(-3, 8, 100)
plt.figure(2)
plt.plot(xx, f1(xx), 'k-')
#plt.show()
# step size (너무 큰 값!)
mu = 1.1
# k = 0
x = 0
plt.plot(x, f1(x), 'go', markersize=10)
plt.text(x + 0.2, f1(x) + 0.1, "1st trial")
plt.plot(xx, f1d(x) * (xx - x) + f1(x), 'b--')
print("1차 시도: x_1 = {:.2f}, g_1 = {:.2f}".format(x, f1d(x)))
# k = 1
x = x - mu * f1d(x)
plt.plot(x, f1(x), 'go', markersize=10)
plt.text(x + 0.2, f1(x) + 0.4, "2nd trial")
plt.plot(xx, f1d(x) * (xx - x) + f1(x), 'b--')
print("2차 시도: x_2 = {:.2f}, g_2 = {:.2f}".format(x, f1d(x)))
# k = 2
x = x - mu * f1d(x)
plt.plot(x, f1(x), 'go', markersize=10)
plt.text(x - 1.2, f1(x) - 0.7, "3rd trial")
plt.plot(xx, f1d(x) * (xx - x) + f1(x), 'b--')
print("3차 시도: x_3 = {:.2f}, g_3 = {:.2f}".format(x, f1d(x)))
# k = 3
x = x - mu * f1d(x)
plt.plot(x, f1(x), 'go', markersize=10)
plt.text(x - 1.2, f1(x) - 0.7, "4th trial")
plt.plot(xx, f1d(x) * (xx - x) + f1(x), 'b--')
print("4차 시도: x_3 = {:.2f}, g_3 = {:.2f}".format(x, f1d(x)))
plt.ylim(0, 15)
plt.xlabel("x")
plt.ylabel("$f_1(x)$")
plt.title("optimization by gradient descent with large step size")
plt.show()
# 2차원 로젠브록 함수
def f2(x, y):
return (1 - x)**2 + 100.0 * (y - x**2)**2
xx = np.linspace(-4, 4, 800)
yy = np.linspace(-3, 3, 600)
X, Y = np.meshgrid(xx, yy)
Z = f2(X, Y)
levels=np.logspace(-1, 3, 10)
plt.contourf(X, Y, Z, alpha=0.2, levels=levels)
plt.contour(X, Y, Z, colors="gray",
levels=[0.4, 3, 15, 50, 150, 500, 1500, 5000])
plt.plot(1, 1, 'ro', markersize=10)
plt.xlim(-4, 4)
plt.ylim(-3, 3)
plt.xticks(np.linspace(-4, 4, 9))
plt.yticks(np.linspace(-3, 3, 7))
plt.xlabel("$x$")
plt.ylabel("$y$")
plt.title(" Rosenbrock Function $f(x,y)$")
plt.show()
# https://frhyme.github.io/python-lib/python_contour/
Xmesh, Ymesh = np.meshgrid(np.linspace(-3.0, 3.0, 1000),
np.linspace(-3.0, 3.0, 1000)
)
# levels = np.linspace(Z.reshape(-1, 1).min(), Z.reshape(-1, 1).max(), 50)
# np.size(levels) = 50
print("XX.shape: {}".format(Xmesh.shape))
print("YY.shape: {}".format(Ymesh.shape))
Z = np.sqrt(Xmesh**2 + Ymesh**2 )
plt.figure(figsize=(12, 5))
"""levels에 구간을 넣어줘서 등고선 표시 위치를 정할 수 있습니다.
"""
cp = plt.contourf(Xmesh, Ymesh, Z,
levels = np.linspace(Z.reshape(-1, 1).min(), Z.reshape(-1, 1).max(), 50)
)
plt.colorbar(cp)
#plt.savefig('../../assets/images/markdown_img/draw_contour_20180529_1727.svg')
plt.savefig('draw_contour.svg')
plt.show()
# https://problemsolvingwithpython.com/06-Plotting-with-Matplotlib/06.14-Contour-Plots/
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
#matplotlib inline
x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-3.0, 3.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.sin(X)*np.cos(Y)
fig, ax = plt.subplots(figsize=(6,6))
ax.contour(X,Y,Z)
plt.show()
# Matplotlib's ax.contourf() method is similar to ax.contour()
# except that ax.contourf() produces contour plots that are "filled".
import numpy as np
import matplotlib.pyplot as plt
# if using a Jupyter notebook, include:
# matplotlib inline
x = np.arange(-3.0, 3.0, 0.1)
y = np.arange(-3.0, 3.0, 0.1)
X, Y = np.meshgrid(x, y)
Z = np.sin(X)*np.cos(Y)
fig, ax = plt.subplots(figsize=(6,6))
ax.contourf(X,Y,Z)
plt.show()
##########
# 스텝 사이즈에 따른 수렴 정도 차이 그래프 그려 보기
# 스템 사이즈 :
# 아주 작은 경우 2e-4, 아직 수렴하지 못함 5000
# 적당한 경우 : 2e-3 수렴 5000
# 큰 경우 : 5e-3 발산 5000,
# figure(10), figure(11), figure(12)
import numpy as np
import matplotlib.pyplot as plt
# 2차원 로젠브록 함수
def f2(x, y):
return (1 - x)**2 + 100.0 * (y - x**2)**2
def f2g(x, y):
"""f2(x, y)의 도함수"""
# 2차원 로젠브록 함수의 도함수
# (1 - x)**2 + 100.0 * (y - x**2)**2
return np.array((2.0 * (x - 1) - 400.0 * x * (y - x**2), 200.0 * (y - x**2)))
#다음 그림에 x=−1,y−1 에서 시작하여
# 최대경사법으로 최적점을 찾아나가는 과정을 그레디언트 벡터 화살표와 함께 보였다.
xx = np.linspace(-4, 4, 800)
yy = np.linspace(-3, 3, 600)
X, Y = np.meshgrid(xx, yy)
Z = f2(X, Y)
levels = np.logspace(-1, 3, 10)
plt.figure(11)
plt.contourf(X, Y, Z, alpha=0.2, levels=levels)
plt.contour(X, Y, Z, colors="green", levels=levels, zorder=0)
plt.plot(1, 1, 'ro', markersize=10)
#plt.show() # debug 시에만 사용할 것
mu = 2e-4 # step size 8e-4
s = 0.95 # for arrowhead drawing
'''''''''
# https://matplotlib.org/gallery/text_labels_and_annotations/arrow_simple_demo.html
import matplotlib.pyplot as plt
plt.figure(10)
ax = plt.axes()
ax.arrow(0, 0, 0.5, 0.5, head_width=0.05, head_length=0.1, fc='k', ec='k')
plt.show()
'''
x, y = -1, -1
for i in range(100): # 5000
g = f2g(x, y)
plt.arrow(x, y, -s * mu * g[0], -s * mu * g[1],
head_width=0.04, head_length=0.04, fc='k', ec='k', lw=2)
x = x - mu * g[0]
y = y - mu * g[1]
plt.xlim(-3, 3)
plt.ylim(-2, 2)
plt.xticks(np.linspace(-3, 3, 7))
plt.yticks(np.linspace(-2, 2, 5))
plt.xlabel("x")
plt.ylabel("y")
plt.title("Gradient Descent" )
plt.show()
# 스텝 사이즈에 따른 수렴 정도 차이 그래프 그려 보기 끝
##########
plt.figure(100)
# 로젠브룩함수를 x범위 0, 4까지, y 범위 0,3까지 확대해서 다시 그린다
xx = np.linspace(0, 4, 800)
yy = np.linspace(0, 3, 600)
X, Y = np.meshgrid(xx, yy)
Z = f2(X, Y)
levels = np.logspace(-1, 4, 20)
plt.contourf(X, Y, Z, alpha=0.2, levels=levels)
plt.contour(X, Y, Z, colors="green", levels=levels, zorder=0)
plt.plot(1, 1, 'ro', markersize=10)
mu = 1.8e-3 # 스텝 사이즈
s = 0.95 # 화살표 크기
x, y = 1.5, 1.5
for i in range(15):
g = f2g(x, y)
plt.arrow(x, y, -s * mu * g[0], -s * mu * g[1],
head_width=0.04, head_length=0.04, fc='k', ec='k', lw=2)
x = x - mu * g[0]
y = y - mu * g[1]
plt.xlim(0, 3)
plt.ylim(0, 2)
plt.xticks(np.linspace(0, 3, 4))
plt.yticks(np.linspace(0, 2, 3))
plt.xlabel("x")
plt.ylabel("y")
plt.title("Gradient Descent Oscillation" )
plt.show()