Brief Guide
项目 | 内容 |
---|---|
这个作业属于哪个课程 | 北航人工智能实战课 |
这个作业的要求在哪里 | 第三次作业要求 |
我在这个课程的目标是 | 获得机器学习相关的完整项目与学习经验;通过与人工智能行业的大牛们聊天了解行业不同方向的发展以便进行职业规划;为转CS积累基础知识并获得相关课程的成绩 |
这个作业在哪个具体方面帮助我实现目标 | 自己动手创建、训练了一个能够区分“&”门与“ |
作业正文… | 【王俊杰de人工智能实战课】第5次作业 |
其他参考文献… | 微软AI教育GitHub |
Main Homework
数学公式
sigmoid函数
\[f(z) = \frac{1}{1 + e^{-z}}\]
\[f^{'}(z) = f(z)(1 - f(z))\]
交叉熵损失函数
\[J(w,b) = -{1 \over m} \sum^m_{i=1}y_i \ln (a_i)+(1-y_i) \ln (1-a_i)\]
反向传播公式
\[{\partial{J} \over \partial{w}}={\partial{J} \over \partial{z_i}}{\partial{z_i} \over \partial{w}}={1 \over m} \sum^m_{i=1}(a_i-y_i)x_i\]
\[{\partial{J} \over \partial{b}}={\partial{J} \over \partial{z_i}}{\partial{z_i} \over \partial{b}}={1 \over m} \sum^m_{i=1}(a_i-y_i)\]
代码
导包
# /* Author: Wang Junjie */
# /* Date: 2019/04/08 */
# /* Coding: UTF-8 */
# /* Using Python3 */
# Discription: 实现逻辑与门、或门
import numpy as np
import matplotlib.pyplot as plt
初始化
# 初始化
def Initialize():
W = np.zeros((2,1))
B = np.ones(1)
eta = 20
return W,B,eta
输入训练数据
# 输入训练数据
def Input(gate):
if gate=='and':
X = np.array([[0,0],[0,1],[1,0],[1,1]])
Y = np.array([0,0,0,1]).reshape(4,1)
elif gate=='or':
X = np.array([[0,0],[0,1],[1,0],[1,1]])
Y = np.array([0,1,1,1]).reshape(4,1)
else:
print('Input Error!')
return None
return X,Y
前向计算
# 前向计算
def Forward(W,X,B):
# 线性公式
Z = np.dot(X,W)+B
# 激活函数:sigmoid函数
A = 1/(1+np.exp(-Z))
return A
计算损失函数
# 计算损失函数
def CalLoss(A,Y):
m = Y.shape[0]
# 交叉熵函数
loss = ( -(Y*np.log(A)+(1-Y)*np.log(1-A)) ).sum() /m
return loss
反向传播
# 反向传播
def Backward(X,A,Y,eta,W,B):
m = Y.shape[0]
dW = np.dot(X.T,(A-Y)) /m
dB = (A-Y).sum() /m
W = W-eta*dW
B = B-eta*dB
return W,B
作图
# 作图
def Plot(X, Y, W, B, gate):
m = Y.shape[0]
# 0为圆点,1为三角点
for i in range(m):
if Y[i] == 0:
plt.plot(X[i][0], X[i][1], '.', c='r')
elif Y[i] == 1:
plt.plot(X[i][0], X[i][1], '^', c='g')
# 线性公式
a = - (W[0][0] / W[1][0])
b = - (B[0] / W[1][0])
x = np.linspace(-0.1,1.1,100)
y = a * x + b
# 画图
plt.plot(x,y)
plt.axis([-0.1,1.1,-0.1,1.1])
plt.title(gate)
plt.xlabel("x1")
plt.ylabel("x2")
plt.show()
分类器训练、计算与输出结果
# 分类器训练、计算与输出结果
def Classification(gate,diff):
print('-'*20,gate,'gate','-'*20)
# 初始化
epoch = 0
X,Y = Input(gate)
W,B,eta = Initialize()
# 前向计算
A = Forward(W,X,B)
# 损失函数计算
loss = CalLoss(A,Y)
# 判定
while loss>diff:
# 反向传播
W,B = Backward(X,A,Y,eta,W,B)
A = Forward(W,X,B)
loss = CalLoss(A,Y)
epoch+=1
# 返回结果
print('W结果为:[',W[0][0],',',W[1][0],']')
print('B结果为:',B[0])
print('损失值为:',loss)
print('运算次数为:',epoch)
print('计算公式为: Y= (',- (W[0][0] / W[1][0]),') X + (',- (B[0] / W[1][0]),')')
# 画图
Plot(X, Y, W, B, gate)
主程序入口
gates = ['and','or']
diff = 0.00001
for gate in gates:
Classification(gate,diff)
运算结果展示
与门
-------------------- and gate --------------------
W结果为:[ 22.36432787024086 , 22.36432787024086 ]
B结果为: -33.714730345863195
损失值为: 9.999992034393631e-06
运算次数为: 84848
计算公式为: Y= ( -1.0 ) X + ( 1.507522628959745 )
或门
-------------------- or gate --------------------
W结果为:[ 22.345154781905602 , 22.345154781905602 ]
B结果为: -10.714424617636544
损失值为: 9.999784190615524e-06
运算次数为: 44998
计算公式为: Y= ( -1.0 ) X + ( 0.47949654957470894 )