基于Numpy的反向传播算法

下载mlp.ipynb
import numpy as np
from numpy.random import randn
import matplotlib.pyplot as plt

#N,D_in,H,D_out=64,1000,100,10 # 64条数据,1000维度,隐层神经元100,输出10类
N,D_in,H,D_out=150,4,20,3 # 150条数据,4维度,隐层神经元6,输出2类
# Iris鸢尾花数据集 https://cuc.yingshinet.com/V7/CH10/irisKmeans.htm
x,y=randn(N,D_in),randn(N,D_out)
print(x.shape)
print(y.shape)
w1,w2=randn(D_in,H),randn(H,D_out)
print(w1.shape)
print(w2.shape)
plt.scatter(x[:,0],x[:,1],alpha=0.5)
plt.show()
lsloss=[]
for t in range(50):
----h=1/(1+np.exp(-x.dot(w1)))
----#print("h=",h.shape)
----y_pred=h.dot(w2)
----#print("y_pred",y_pred.shape)
----#print("y",y.shape)
----loss=np.square(y_pred-y).sum()
----#print("Iteration=",t,loss)
----lsloss.append(loss)
----grad_y_pred=2.0*(y_pred-y)
----grad_w2=h.T.dot(grad_y_pred)
----grad_h=grad_y_pred.dot(w2.T)
----grad_w1=x.T.dot(grad_h*h*(1-h))
----w1-=1e-3*grad_w1
----w2-=1e-3*grad_w2
plt.plot(lsloss)
plt.show()

基于Numpy的MLP(从50分钟开始)【CS231N中文版:神经网络与反向传播】
矩阵乘法的几何解释【LLM张老师】
全连接神经网络【LLM张老师】