学习目标:
深度学习感知器的实现
实现代码:
import numpy as np import random import math
class Ganzhi(): def init(self): self.w1 = 0 self.w2 = 0 self.y = 0 self.delta1 = 0 self.delta2 = 0
def shuru(self):
x1 = np.array([1,3,5,7])
x2 = np.array([2,4,6,8])
t = np.array([3,5,7,9])
sigmod = 0
a = 0
for i in range(3):
i=0
a = x1[i]*self.w1 + x2[i]*self.w2
self.y += a
sigmod = 1 / (1 + math.exp(-self.y))
self.delta1 = 2 * (sigmod-t[i])*(sigmod*(1-sigmod))*x1[i]
self.delta2 = 2 * (sigmod-t[i])*(sigmod*(1-sigmod))*x2[i]
return self.y,self.delta1,self.delta2
def xuexi(self):
g = 0.01
while self.delta1 <= math.exp(-5) and self.delta2 <= math.exp(-5):
i = 0
self.w1 = self.w1 + g * self.delta1
self.w2 = self.w2 + g * self.delta2
i += 1
return self.w1,self.w2
if name == ‘main’: a = Ganzhi() a.shuru() a.xuexi() print(a.w1) print(a.w2)
编译结果:
-0.0125 -0.025
|