激活函数 sigmoid
# author: Roy.G
import dataset
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D as a3d
xs,ys=dataset.get_beans(100)
# plt.scatter(xs,ys)
b = 0.1
w = 0.1
# z = w*xs+b
# a = 1/(1+np.exp(-z))
# plt.plot(xs,a)
# plt.show()
for k in range(50000):
for i in range(100):
x = xs[i]
y = ys[i]
#对e=(y-sigmoid(z)**2 的函数进行分解:
z = w*x+b
a = 1 / (1 + np.exp(-z))
e = (y-a)**2
#分别对每个分解函数进行求导,求de_dw
de_da = -2*(y-a)
da_dz = a*(1-a)
dz_dw = x
dz_db = 1
# 求w,b 偏导数
de_dw = de_da*da_dz*dz_dw
de_db = de_da*da_dz*dz_db
alpha=0.05
w = w - alpha * de_dw
b = b - alpha * de_db
z = w * xs + b
a = 1 / (1 + np.exp(-z))
if k%100 == 0:
plt.clf()
plt.scatter(xs, ys)
plt.title("size-toxicity function")
plt.xlabel("beans-size")
plt.ylabel("toxicity")
plt.xlim(0,1.5)
plt.ylim(0,1.5)
plt.plot(xs,a)
plt.pause(0.1)