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反向传播神经网络的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门