?
import?numpy?as?np import?matplotlib.pyplot?as?plt from?mpl_toolkits.mplot3d?import?Axes3D import?math import?random
def?sigma(x): ????return?1?/?(1?+?math.exp(-x)) e0?=?1 e1?=?1 e2?=?1
alpha?=?0.1 omga0?=?np.random.uniform(-1,0) omga1?=?np.random.uniform(0,1) omga2?=?np.random.uniform(0,1) omga?=?np.array([omga1,omga1])
eps?=?0.0000000001
x?=?np.array([[0,0],[1,1],[1,0],[0,1]]) t?=?np.array([0,1,0,0])
i?=?0
cnt?=?0 while(abs(e0)?>=?eps?or?abs(e1)?>=?eps?or?abs(e2)?>=?eps): ????cnt?+=?1 ????z1?=?np.sum((x[i]?*?oga)) ????e0?=?(1-sigma(z1?+?oga0))?*?sigma(z1?+?oga0)?*?((sigma(z1?+?oga0)?-?t[i])) ????e1?=?(1-sigma(z1?+?oga0))?*?sigma(z1?+?oga0)?*?((sigma(z1?+?oga0)?-?t[i]))?*?x[i][0] ????e2?=?(1-sigma(z1?+?oga0))?*?sigma(z1?+?oga0)?*?((sigma(z1?+?oga0)?-?t[i]))?*?x[i][1] ????i?+=?1 ????if(i?>?3): ????????i?=?0
? ? oga0?=?oga0?-?(alpha?*?e0) ????oga1?=?oga1?-?(alpha?*?e1) ????oga2?=?oga2?-?(alpha?*?e2) ????oga?=?np.array([oga1,?oga2]) ????print(e0,e1,e2) print(cnt) print(oga0,oga1,oga2) print(sigma(oga0?+?oga1?*?0?+?oga2?*?0)) print(sigma(oga0?+?oga1?*?1?+?oga2?*?0)) print(sigma(oga0?+?oga1?*?0?+?oga2?*?1)) print(sigma(oga0?+?oga1?*?1?+?oga2?*?1))
?
|