作业五:训练一个逻辑与门和逻辑或门_刘强

项目 内容
这个作业属于哪个课程 人工智能实战2019
这个作业的要求在哪里 作业要求
我在这个课程的目标是 将机器学习理论与实践相结合,获得一定的项目经验,提高编程能力
这个作业在哪个具体方面帮助我实现目标 使用sigmoid激活函数和二分类交叉熵函损失数
我的GitHub链接 https://github.com/QiangLiu404

正文

一、样本与特征:

  1. 逻辑与门的样本和特征

Example| 1| 2| 3| 4

  • | :-: | :-: | :-: | -:
    x1| 0| 0| 1| 1|
    x2| 0| 1| 0| 1|
    y| 0| 0| 0| 1|
  1. 逻辑或门的样本和特征

Example| 1| 2| 3| 4

  • | :-: | :-: | :-: | -:
    x1| 0| 0| 1| 1|
    x2| 0| 1| 0| 1|
    y| 0| 1| 1| 1|

二、代码实现:

1.逻辑或门 OR

import numpy as np
from matplotlib import pyplot as plt

def sigmod(x):
   S=1/(1+np.exp(-x))
   return S

def Forward(w,b,x):
   z = np.dot(w, x) + b
   N=sigmod(z)
   return N

def Back(x,y,N,Data_size):
   dz=N-y
   db=dz.sum(axis=1,keepdims=True)/Data_size
   dw=np.dot(dz,x.T)/Data_size
   return dw,db

def UpWeight(w,b,dw,db,eta):
   w=w-eta*dw
   b=b-eta*db
   return w,b

def Loss(w,b,X,Y,Data_size):
   size=Data_size
   A=Forward(w,b,X)
   n1 = 1 - Y
   n2 = np.log(1-A)
   n3 = np.log(A)
   n4 = np.multiply(n1,n2)
   n5 = np.multiply(Y,n3)
   LOSS = np.sum(-(n4 + n5))
   loss = LOSS/size
   return loss

def Show(w,b,X,Y,Data_size):
   w1 = w[0,0]
   w2 = w[0,1]
   w = -w1/w2
   b = -b[0,0]/w1
   x = np.array([0,1])
   y = w * x + b
   plt.plot(x,y,color='r',label='y='+str(round(w,3))+'x+'+str(round(b,3)))
   # plot data
   for i in range (Data_size):
       if Y[0,i] == 0:
           plt.scatter(X[0,i],X[1,i],marker='o',c='r',s=40)
       else:
            plt.scatter(X[0,i],X[1,i],marker='*',c='b',s=40)
   plt.axis([-0.2,1.3,-0.2,1.3])
   plt.title('OR')
   plt.xlabel("X1")
   plt.ylabel("X2")
   plt.show()

eta = 0.6
w = np.array([0,0]).reshape(1,2)
b = np.array([0]).reshape(1,1)
eps = 1e-2
max_epoch = 10000
loss = 1
X = np.array([0,0,1,1,0,1,0,1]).reshape(2,4)
Y = np.array([0,1,1,1]).reshape(1,4)
Data_features = X.shape[0]
Data_size = X.shape[1]

for epoch in range(max_epoch):
   for i in range(Data_size):
       x = X[:,i].reshape(2,1)
       y = Y[:,i].reshape(1,1)
       z = Forward(w,b,x)
       dW, dB = Back(x,y,z,Data_size)
       w, b = UpWeight(w,b,dW,dB,eta)
   loss = Loss(w,b,X,Y,Data_size)
   print(epoch,i,loss,w,b)

   if loss < eps:
       break

print("epoch=%d,loss=%f,w1=%f,w2=%f,b=%f" %(epoch,loss,w[0,0],w[0,1],b))
Show(w,b,X,Y,Data_size)


结果:

epoch=1546,loss=0.009995,w1=8.515211,w2=8.517734,b=-3.791060

图片:

2.逻辑与门 AND

import numpy as np
from matplotlib import pyplot as plt

def sigmod(x):
   S=1/(1+np.exp(-x))
   return S

def Forward(w,b,x):
   z = np.dot(w, x) + b
   N=sigmod(z)
   return N

def Back(x,y,N,Data_size):
   dz=N-y
   db=dz.sum(axis=1,keepdims=True)/Data_size
   dw=np.dot(dz,x.T)/Data_size
   return dw,db

def UpWeight(w,b,dw,db,eta):
   w=w-eta*dw
   b=b-eta*db
   return w,b

def Loss(w,b,X,Y,Data_size):
   size=Data_size
   A=Forward(w,b,X)
   n1 = 1 - Y
   n2 = np.log(1-A)
   n3 = np.log(A)
   n4 = np.multiply(n1,n2)
   n5 = np.multiply(Y,n3)
   LOSS = np.sum(-(n4 + n5))
   loss = LOSS/size
   return loss

def Show(w,b,X,Y,Data_size):
   w1 = w[0,0]
   w2 = w[0,1]
   w = -w1/w2
   b = -b[0,0]/w1
   x = np.array([0,1])
   y = w * x + b
   plt.plot(x,y,color='r',label='y='+str(round(w,3))+'x+'+str(round(b,3)))
   # plot data
   for i in range (Data_size):
       if Y[0,i] == 0:
           plt.scatter(X[0,i],X[1,i],marker='o',c='r',s=40)
       else:
            plt.scatter(X[0,i],X[1,i],marker='*',c='b',s=40)
   plt.axis([-0.2,1.3,-0.2,1.3])
   plt.title('AND')
   plt.xlabel("X1")
   plt.ylabel("X2")
   plt.show()

eta = 0.6
w = np.array([0,0]).reshape(1,2)
b = np.array([0]).reshape(1,1)
eps = 1e-2
max_epoch = 10000
loss = 1
X = np.array([0,0,1,1,0,1,0,1]).reshape(2,4)
Y = np.array([0,0,0,1]).reshape(1,4)
Data_features = X.shape[0]
Data_size = X.shape[1]

for epoch in range(max_epoch):
   for i in range(Data_size):
       x = X[:,i].reshape(2,1)
       y = Y[:,i].reshape(1,1)
       z = Forward(w,b,x)
       dW, dB = Back(x,y,z,Data_size)
       w, b = UpWeight(w,b,dW,dB,eta)
   loss = Loss(w,b,X,Y,Data_size)
   print(epoch,i,loss,w,b)

   if loss < eps:
       break

print("epoch=%d,loss=%f,w1=%f,w2=%f,b=%f" %(epoch,loss,w[0,0],w[0,1],b))
Show(w,b,X,Y,Data_size)

结果:

epoch=2872,loss=0.009998,w1=8.537218,w2=8.535023,b=-12.970825

图片:

posted @ 2019-04-07 22:09  LQiang-  阅读(263)  评论(0编辑  收藏  举报