Python BP反向传播神经网络
2021/11/1 9:40:31
本文主要是介绍Python BP反向传播神经网络,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Code:
import numpy as np """Sigmod激活函数""" def sigmod(x): return 1.0/(1.0 + np.exp(-x)) def BackpropCE(W1,W2,X,D): alpha = 0.9 #学习率 N=4 #4层网络 for k in range(N): x = X[k,:].T #对数据每行转置 d = D[k] #每行对应结果(标签) v1 = np.matmul(W1, x) y1 = sigmod(v1) v = np.matmul(W2,y1) y = sigmod(v) e = d - y delta = e*y*(1-y) e1 = np.matmul(W2.T,delta) delta1 = y1*(1-y1)*e1 dW1 = (alpha*delta1).reshape(4,1) W1 += dW1 dW2 = alpha * delta *y1 W2 += dW2 return W1,W2 def TestBackpropCE(): X = np.array([[0,0,1], [0,1,1], [1,0,1], [1,1,1]]) D = np.array([[0],[1],[1],[0]]) W1 = 2*np.random.random((4,3))-1 W2 = 2*np.random.random((1,4))-1 for epoch in range(10000): W1,W2 = BackpropCE(W1, W2, X, D) N = 4 for k in range(N): x = X[k,:].T v1 = np.matmul(W1,x) y1 = sigmod(v1) v = np.matmul(W2,y1) y = sigmod(v) print(y) if __name__ == "__main__": TestBackpropCE()
这篇关于Python BP反向传播神经网络的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用FastAPI掌握Python异步IO:轻松实现高并发网络请求处理
- 2025-01-02封装学习:Python面向对象编程基础教程
- 2024-12-28Python编程基础教程
- 2024-12-27Python编程入门指南
- 2024-12-27Python编程基础
- 2024-12-27Python编程基础教程
- 2024-12-27Python编程基础指南
- 2024-12-24Python编程入门指南
- 2024-12-24Python编程基础入门
- 2024-12-24Python编程基础:变量与数据类型