title: 人工智能入门原理

1.一元一次函数感知器

一元一次函数感知器

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
import dataset
from matplotlib import pyplot as plt
xs,ys=dataset.get_beans (100)
print(xs)
print(ys)
#配置图像
plt.title("Size-Toxicity Function" ,fontsize=12)#设置图像名称
plt.xlabel( "Bean size")#设置横坐标的名字
plt.ylabel("Toxicity")#设置纵坐标的名字

plt.scatter(xs,ys)
#y=0.5*×
w=0.5
for m in range(100):

for i in range(100):
x = xs[i]
y = ys[i]
y_pre = w*x
e = y - y_pre
alpha = 0.05
w = w+ alpha*e* x
y_pre =w*xs
print(y_pre)
plt.plot(xs,y_pre)
plt.show()

2.方差代价函数

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
import dataset
import matplotlib.pyplot as plt
import numpy as np
xs,ys = dataset.get_beans(100)
#配置图像
plt.title("size-Toxicity Function",fontsize=12)#设置图像名称
plt.xlabel( "Bean size")#设置横坐标的名字
plt.ylabel("Toxicity")#设置纵坐标的名字
plt.scatter(xs,ys)
w = 0.1
y_pre = w*xs

plt.plot(xs,y_pre)
plt.show()
es = (ys-y_pre)**2
sum_e = np.sum(es)
sum_e = (1/100)*sum_e
print(sum_e)

ws = np.arange(0,3,0.1)
es =[]
for w in ws:
y_pre = w*xs
e=(1/100)*np.sum((ys-y_pre)**2)
es.append(e)
#配置图像
plt.title("cost function" ,fontsize=12)#设置图像名称
plt.xlabel("w")#设置横坐标的名字
plt.ylabel("e")#设置纵坐标的名字
plt.plot(ws,es)
plt.show()
w_min = np.sum( xs*ys)/np.sum( xs*xs)
print("e最小点w: "+str(w_min))

3.梯度下降和反向传播

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
import dataset
import matplotlib.pyplot as plt
import numpy as np
xs,ys = dataset.get_beans ( 100)
#配置图像
plt.title( "size-Toxicity Function",fontsize=12)#设置图像名称
plt.xlabel( "Bean size")#设置横坐标的名字
plt.ylabel( "Toxicity")#设置纵坐标的名字
plt.scatter(xs,ys)
w= 0.1
y_pre = w*xs
plt.plot(xs,y_pre)
plt.show()
for m in range(100):
for i in range(100):
x = xs[i]
y = ys[i]#a=x个2#b=-2*x*y#cC=y^2
#斜率k=2aw+b
k = 2*(x**2)*w +(-2*x*y)
alpha = 0.1
w=w- alpha*k
plt.clf() # 清空窗口
plt.scatter(xs,ys)
y_pre = w*xs
plt.xlim(0, 1)
plt.ylim(0, 1.2)
plt.plot(xs, y_pre)
plt.pause(0.01) # 暂停o.o1s

# plt.scatter(xs,ys)
# y_pre = w*xs
# plt.plot(xs,y_pre)
# plt.show()

4.Y= kx + b

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
import matplotlib.pyplot as plt
import dataset
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

#从数据中获取随机豆豆
m=100
xs,ys = dataset.get_beans(m)

#配置图像
plt.title("Size-Toxicity Function", fontsize=12)
plt.xlabel('Bean Size')
plt.ylabel('Toxicity')

# 豆豆毒性散点图
plt.scatter(xs, ys)

#预测函数
w=0.1
b=0.1
y_pre = w*xs+b

#预测函数图像
plt.plot(xs,y_pre)

#显示图像
plt.show()

#代价函数
ws = np.arange(-1,2,0.1)
bs = np.arange(-2,2,0.1)

fig = plt.figure()
ax = Axes3D(fig)

ax.set_zlim(0,2)

for w in ws:#每次取不同的w
es = []
for b in bs:
y_pre = w*xs+b
#得到w和b的关系
e = (1/m)*np.sum((ys-y_pre)**2)
es.append(e)
#plt.plot(ws,es)
figure = ax.plot(bs, es, w, zdir='y')


#显示图像
plt.show()
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
import matplotlib.pyplot as plt
import dataset
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

#从数据中获取随机豆豆
m=100
xs,ys = dataset.get_beans(m)


#配置图像
plt.title("Size-Toxicity Function", fontsize=12)
plt.xlabel('Bean Size')
plt.ylabel('Toxicity')

# 豆豆毒性散点图
plt.scatter(xs, ys)

#预测函数
w=0.1
b=0.1
y_pre = w*xs+b

#预测函数图像
plt.plot(xs,y_pre)

#显示图像
plt.show()

for _ in range(500):
for i in range(100):
x = xs[i]
y = ys[i]#a=x个2#b=-2*x*y#c=y^2
#斜率k=2aw+b
dw= 2*x**2*w +2*x*b - 2*x*y
db =2*b + 2*x*w -2*y
alpha = 0.01
w=w - alpha*dw
b =b - alpha*db
plt.clf()#清空窗口
plt.scatter(xs,ys)
y_pre = w*xs +b
plt.xlim(0,1)
plt.ylim(0,1.2)
plt.plot(xs,y_pre)
plt.pause(0.01)#暂停0.01s

5.激活函数


文章作者: RickyLove
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 RickyLove !
  目录