公式推导
\(L_{i}=(\sigma(\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j})-t_{i})^{2}\).
\(\nabla_{i}=\frac{dL}{d\omega_{i}}=2(\sigma(\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j})-t_{i})\sigma(\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j})(1-\sigma(\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j}))x_{i}^{j}\).
其中这里取激活函数\(\sigma(x)=\frac{1}{1+e^{-x}}\),\(\frac{\partial\sigma(x)}{\partial x}=\frac{-1}{(1-e^{-x})^{2}}=\sigma(x)(1-\sigma(x))\)
将\(\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j}\)带入激活函数\(\sigma(x)\)可以得到\(\sigma(\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j})=\frac{1}{1+e^{-\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j}}}\)
所以\(\nabla_{i}=\frac{dL}{d\omega_{i}}=2(\frac{1}{1+e^{-\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j}}})-t_{i})(\frac{1}{1+e^{-\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j}}})(1-\frac{1}{1+e^{-\Sigma_{j=0}^{2}x^{i}_{j}\omega_{j}}})x_{i}^{j}\)
代码实现
import math
import random
class Perceptron:
def __init__(self) -> None:
self.theta = 0.5
self.eps = 1e-4
self.tot = 0
self.delta = [10.0, 10.0, 10.0]
self.W = [random.random(), random.random(), random.random()]
self.X = [[1, 0, 0], [1, 0, 1], [1, 1, 0], [1, 1, 1]]
self.T = [0, 0, 0, 1]
def __calSigmod(self, x:float):
return 1.0 / (1.0 + math.pow(math.e, -x))
def __calSegma(self, x:list):
res = 0.0
for i in range(3):
res = res + x[i] * self.W[i]
# res = (res + 1.0) / 2.0
return res
def check(self):
for p in range(4):
for i in range(3):
s = self.__calSigmod(self.__calSegma(self.X[p]))
t = (s - self.T[p]) * s * (1.0 - s) * self.X[p][i]
if t > self.eps:
return True
return False
def iterate(self):
self.tot = self.tot + 1
p = random.randint(0, 3)
for i in range(3):
s = self.__calSigmod(self.__calSegma(self.X[p]))
self.delta[i] = (s - self.T[p]) * s * (1 - s) * self.X[p][i]
for i in range(3):
self.W[i] = self.W[i] - self.delta[i] * self.theta
def printResult(self):
print('Iteration time: ', self.tot)
print('Coe: ', self.W)
print('Result:')
for i in range(4):
x = self.X[i]
print(x[1:], ': ', end='')
res = 0.0
for j in range(3):
res = res + x[j] * self.W[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()
运行结果
Iteration time: 201751
Coe: [-13.56814252989061, 8.978083107458904, 8.976992168326456]
Result:
[0, 0] : 0(0.000001)
[0, 1] : 0(0.010039)
[1, 0] : 0(0.010050)
[1, 1] : 1(0.987714)