神经网络-基本逻辑-代码实现

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chuan403082010/article/details/86370551

nn_data.txt数据

6.5 4   1
7.5 4.5 1
2   0.5 0
4   1   0
1   2   0
7   6   1
5   6   1
5.5 4.5 1
1   1   0
3.5 2   0
5   2   0
7   2   0
8.5 3   0
9   4   0
1.5 3.5 0
4   3   0
1   5   0
3   5   0
2   6.5 0
3.5 6.5 0
import  numpy as np
import  matplotlib.pyplot as plt

#loaddata
def loaddata(filename):
    fr = open(filename)
    x = []
    y = []
    for line in fr.readlines():
        line = line.strip().split()
        x.append([float(line[0]),float(line[1])])
        y.append(float(line[-1]))
    return  np.mat(x),np.mat(y).T

#data scaling
def scaling(data):
    max = np.max(data)
    min = np.min(data)
    return (data - min)/(max - min),max,min

#sigmoid
def sigmoid(data):
    return 1/(1 + np.exp(-data))

# w b calc
def wb_calc(X,ymat,alpha = 0.1,n_hidden_dim = 3,reg_lamda=0,maxIter = 10001):
    #init w b
    w1 = np.mat(np.random.randn(2,n_hidden_dim))
    b1 = np.mat(np.random.randn(1,n_hidden_dim))
    w2 = np.mat(np.random.randn(n_hidden_dim, 1))
    b2 = np.mat(np.random.randn(1, 1))
    w1_save = []
    w2_save = []
    b1_save = []
    b2_save = []
    ss = []

    for step in range(maxIter):
        #FP
        z1 = X * w1 + b1#(20,2) (2,3) + (1,3) = (20,3)
        a1 = sigmoid(z1) #(20,3)
        z2 = a1 * w2 + b2#(20,3) (3,1) + (1,1) = (20,1)
        a2 = sigmoid(z2)#(20,1)
        #BP
        a0 = X.copy()
        detal2 =a2 - ymat#(20,1)
        delta1 =np.mat((detal2 * w2.T).A * (a1.A * (1- a1).A))
        #              (20,1)(1,3) .* (20,3) = (20,3)
        dw1 = a0.T * delta1 + reg_lamda * w1#(2,20)(20,3)+ (2,3) = (2,3)
        db1 = np.sum(delta1,0) #
        #db1 = np.mat(np.ones()) * datal
        dw2 = a1.T * detal2 + reg_lamda * w2
        db2 = np.sum(detal2,0) #?
        #update w b
        w1 -= alpha * dw1
        b1 -= alpha * db1
        w2 -= alpha * dw2
        b2 -= alpha * db2
        if step % 100== 0:
            w1_save.append(w1.copy())
            w2_save.append(w2.copy())
            b1_save.append(b1.copy())
            b2_save.append(b2.copy())
            ss.append(step)
    return w1,b1,w2,b2,w1_save,b1_save,w2_save,b2_save,ss


#implement
xmat,ymat = loaddata('nn_data.txt')
# print('x:',xmat,xmat.shape)
# print('y:',ymat,ymat.shape)


xmat_s,xmat_max,xmat_min = scaling(xmat)
# print('x_s:',xmat_s)
w1,b1,w2,b2,w1_save,b1_save,w2_save,b2_save,ss = wb_calc(xmat_s,ymat,alpha=0.05,maxIter=10000,n_hidden_dim = 10)

#普通展示
# plotx1 = np.arange(0,10,0.01) #array
# plotx2 = np.arange(0,10,0.01)
# plotX1,plotX2 = np.meshgrid(plotx1,plotx2)
# plotx_new = np.c_[plotX1.ravel(),plotX2.ravel()]
# ploty_new2 = (plotx_new - xmat_min)/(xmat_max - xmat_min)
#
# plot_z1 = ploty_new2 * w1 + b1
# plot_a1 = sigmoid(plot_z1)
# plot_z2 = plot_a1 * w2 + b2
# plot_a2 = sigmoid(plot_z2)
# ploty_new = np.reshape(plot_a2,plotX1.shape)
#
# plt.contourf(plotX1,plotX2,ploty_new,1,alpha = 0.5)
# plt.scatter(xmat[:,0][ymat ==0].A,xmat[:,1][ymat==0].A,label = '0',marker='o',s = 100)
# plt.scatter(xmat[:,0][ymat ==1].A,xmat[:,1][ymat==1].A,label = '1',marker='^',s = 150)
# plt.grid()
# plt.legend()
# plt.show()

# 动画展示
plotx1 = np.arange(0,10,0.01) #array
plotx2 = np.arange(0,10,0.01)
plotX1,plotX2 = np.meshgrid(plotx1,plotx2)
plotx_new = np.c_[plotX1.ravel(),plotX2.ravel()]
ploty_new2 = (plotx_new - xmat_min)/(xmat_max - xmat_min)

for i in range(len(w1_save)):
    plt.clf()
    plot_z1 = ploty_new2 * w1_save[i] + b1_save[i]
    plot_a1 = sigmoid(plot_z1)
    plot_z2 = plot_a1 * w2_save[i] + b2_save[i]
    plot_a2 = sigmoid(plot_z2)
    ploty_new = np.reshape(plot_a2,plotX1.shape)

    plt.contourf(plotX1,plotX2,ploty_new,1,alpha = 0.5)
    plt.scatter(xmat[:,0][ymat ==0].A,xmat[:,1][ymat==0].A,label = '0',marker='o',s = 100)
    plt.scatter(xmat[:,0][ymat ==1].A,xmat[:,1][ymat==1].A,label = '1',marker='^',s = 150)
    plt.grid()
    plt.legend()
    plt.title('iter:%s'%np.str(ss[i]))
    plt.pause(0.001)
plt.show()


猜你喜欢

转载自blog.csdn.net/chuan403082010/article/details/86370551