感知器算法的公式推导及代码实现
2021/10/1 20:11:43
本文主要是介绍感知器算法的公式推导及代码实现,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
公式推导
\(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)
这篇关于感知器算法的公式推导及代码实现的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南