-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradint.py
87 lines (65 loc) · 1.67 KB
/
gradint.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
# -*- coding: utf-8 -*-
"""
# Author : Ming
# File : {NAME}.py
# Time : 2019/3/28 0028 上午 8:29
"""
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
mpl.rcParams['font.family'] = 'SimHei'
mpl.rcParams['axes.unicode_minus'] = False
'一维 y = x^2 - 2x +1'
def f(x):
return x ** 2-2*x + 1
def graient(x):
return 2*x -2
x0 = np.arange(-9,11,0.1)
print(x0.shape)
y0 = f(x0)
print(y0.shape)
def disply():
x_list = []
y_list = []
lr = 0.1
x = 10
for i in range(100):
x_list.append(x)
y_list.append(f(x))
x = x - lr*graient(x)
return x_list, y_list
# x_list, y_list = disply()
# plt.plot(x0, y0)
# plt.plot(x_list, y_list, 'ro--')
# plt.show()
'2维 y =0.2*(x1 + x2)^2 - 0.3x1x2 +0.4'
x1 = np.arange(-5, 5, 0.1)
x2 = np.arange(-5, 5, 0.1)
def f1(x1, x2):
return 0.2*(x1 + x2)**2 - 0.3*x1*x2 +0.4
def g_x1(x1, x2):
return 0.4*x1*(x1+x2) - 0.3*x2
def g_x2(x1, x2):
return 0.4*x2*(x1+x2) - 0.3*x1
def disply1():
x1_list = []
x2_list = []
y_list = []
lr = 0.1
x1, x2 = 4.5, 4.5
for i in range(100):
x1_list.append(x1)
x2_list.append(x2)
y_list.append(f1(x1, x2))
x1 = x1 - lr*g_x1(x1, x2)
x2 = x2 - lr * g_x2(x1, x2)
return x1_list, x2_list, y_list
x1_list, x2_list, y_list = disply1()
x11, x12 = np.meshgrid(x1, x2)
y = f1(x11, x12)
fig = plt.figure()
ax = Axes3D(fig)
ax.plot_surface(x11, x12, y, rstride=5, cstride=5, cmap='rainbow')
ax.plot(x1_list, x2_list, y_list, 'bo--')
plt.show()