人工智能实战_第五次作业_陈泽寅
16071070 _ 陈泽寅 _ 第五次作业:
一、简要概述
| 项目 | 内容 |
|---|---|
| 课程 | 人工智能实战2019 |
| 作业要求 | 作业要求 |
| 我在这个课程的目标是 | 了解人工智能理论,提升coding能力 |
| 这个作业在哪个具体方面帮助我实现目标 | 了解单层神经工作原理,掌握几种梯度下降法的优缺点,自己实现简单算法. |
二、题目要求以及代码逻辑
- 训练一个逻辑与门和逻辑或门,结果及代码形成博客
1、与门
import numpy as np
X = np.array([[0, 0],[0, 1],[1, 0],[1, 1]])
y = np.array([0, 0, 0, 1])
y_final = np.zeros((4, 2))
y_final[np.arange(4),y]=1
weights = np.random.randn(2, 2)
bias = np.ones((1, 2))
loss = np.infty
while loss > 0.0001:
output = 1/(1 + np.exp(-np.dot(X, weights)-bias))
weights -= np.dot(X.T ,(output - y_final))
bias -= np.mean(output-y_final, axis=0, keepdims=True)
loss = -np.mean(np.log(output)*y_final)
print output
print weights
print bias
print loss
代码结果
*************
outputs:
[[ 9.99980721e-01 2.00393691e-05]
[ 9.75025709e-01 2.52898406e-02]
[ 9.75025709e-01 2.52898406e-02]
[ 2.85468143e-02 9.71092396e-01]]
*************
weights:
[[-7.19543715 7.16967209]
[-7.19543715 7.16967209]]
*************
loss:
0.00999197747583
可视化:
for i in range(X.shape[0]):
if y[i] == 0:
plt.plot(X[i,0], X[i,1], '.', c='b')
elif y[i] == 1:
plt.plot(X[i,0], X[i,1], '^', c='r')
plt.axis([-0.2, 1.2, -0.2, 1.2])
w = -weights[0,0]/weights[1,0]
b = -bias[0,0]/weights[1,0]
x = np.linspace(0,1,10)
y = w*x + b
plt.plot(x,y)
plt.show()

二、或门
import numpy as np
import matplotlib.pyplot as plt
X = np.array([[0, 0],[0, 1],[1, 0],[1, 1]])
y = np.array([0, 1, 1, 1])
y_final = np.zeros((4, 2))
y_final[np.arange(4),y]=1
weights = np.random.randn(2, 2)
bias = np.ones((1, 2))
loss = 100
while loss > 0.001:
output = 1/(1 + np.exp(-np.dot(X, weights)-bias))
weights -= np.dot(X.T ,(output - y_final))
bias -= np.mean(output-y_final, axis=0, keepdims=True)
loss = -np.mean(np.log(output[np.arange(4), y]))
print output
print weights
print bias
print loss
代码结果
*************
outputs:
[[ 9.97338067e-01 2.66987992e-03]
[ 6.65036579e-04 9.99332973e-01]
[ 6.65039192e-04 9.99332983e-01]
[ 1.18202142e-09 9.99999999e-01]]
*************
weights:
[[-13.24170158 13.2357432 ]
[-13.24170551 13.23572832]]
*************
loss:
0.000999993120121
可视化:


浙公网安备 33010602011771号