相关公式推导与思考
首先是Li与Δi:
再是有激活函数σ(x)(注意偏导啥的切勿搞混)
搞清楚各类函数之间推导关系(实际上我这个公式推了多次才正确推导成功),接下来据此构思代码。
关联代码实现:
import random
import math
class Perceptron:
def __init__(self) -> None:
self.h = 0.5
self.e = 1e-4
self.t = 0
self.d = [10.0, 10.0, 10.0]
self.A = [random.random(), random.random(), random.random()]
self.B = [[1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]
self.C = [0, 0, 0, 1]
def check(self):
for p in range(4):
for i in range(3):
s = self.__calSigmod(self.__calSegma(self.B[p]))
t = (s - self.C[p]) * s * (1.0 - s) * self.B[p][i]
if t > self.e:
return True
return False
def iterate(self):
self.t = self.t + 1
p = random.randint(0, 3)
for i in range(3):
s = self.__calSigmod(self.__calSegma(self.B[p]))
self.d[i] = (s - self.C[p]) * s * (1 - s) * self.B[p][i]
for i in range(3):
self.A[i] = self.A[i] - self.d[i] * self.h
def __calSegma(self, x:list):
res = 0.0
for i in range(3):
res = res + x[i] * self.A[i]
return res
def __calSigmod(self, x:float):
return 1.0 / (1.0 + math.pow(math.e, -x))
def printResult(self):
print( self.t)
print( self.A)
for i in range(4):
x = self.B[i]
print(x[1:], ': ', end='')
res = 0.0
for j in range(3):
res = res + x[j] * self.A[j]
res = self.__calSigmod(res)
print('%.0f(%f)' % (res, res))
def main():
perceptron = Perceptron()
while perceptron.check():
perceptron.iterate()
perceptron.printResult()
if __name__ == '__main__':
main()
相应运行结果:
207745
[-13.608992226708809, 9.01890974370569, 9.017339611730574]
[0, 0] : 0(0.000001)
[0, 1] : 0(0.010034)
[1, 0] : 0(0.010050)
[1, 1] : 1(0.988194)