#3层网络,4元素输入,4个第一层隐藏神经元,3个第二层隐藏神经元,1个输出,4*4*3*1 import numpy as np import math import matplotlib.pyplot as plt X = np.array([[0,0,1,1], [0,1,1,0], [1,0,1,1], [1,1,1,0]]) Y = np.array([[0,1,1,0]]) iter_times=30000 α=0.01 def sigmoid(x): return 1/(1+np.exp(-x)) def diriv(a): return a*(1-a) #loss function: L(a,y)=-(ylog(a)+(1-y)log(1-a)) W1 = np.random.random((4,4)) W2 = np.random.random((3,4)) W3 = np.random.random((1,3)) b1 =0 b2 =0 b3 =0 Cost=[] for i in range(iter_times): A1=sigmoid(np.dot(W1,X)+b1) A2=sigmoid(np.dot(W2,A1)+b2) A3=sigmoid(np.dot(W3,A2)+b3) J=Y*(np.log10(A3))+(1-Y)*(np.log10(1-A3)) J=1/4*J.sum(axis=1) Cost.append(abs(J)) dZ3=A3-Y dW3=1/4 *np.dot(dZ3,A2.T) db3=1/4*np.sum(dZ3, axis=1, keepdims=True) dZ2=np.dot(W3.T,dZ3)*diriv(A2) dW2=1/4*np.dot(dZ2,A1.T)
自己建立的3层神经网络
最新推荐文章于 2024-03-03 12:26:15 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)