<p>接着吴老师的思路进行实验,开始搭建一个具有一个隐藏层的神经网络。</p>
<h3>一、导入数据集和画图</h3>
<p>导入数据集之前需要两个文件,具体请参考<a href="https://blog.csdn.net/u013733326/article/details/79702148">【数据】</a></p>
<pre class="blockcode"><code class="language-python">import numpy as np
import pandas as pd
from matplotlib import pyplot as plot
from testCases import *
import sklearn
from sklearn import datasets
from sklearn import linear_model
from planar_utils import *
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
np.set_printoptions(suppress=True)#防止出现科学计数法
np.random.seed(1)
X,Y = load_planar_dataset()
plt.figure(figsize=(12,8))
plt.scatter(X[0,:],X[1,:],c = Y.flatten(),label = '散点')
plt.show()</code></pre>
<p><img alt="" height="268" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-5675002d264e744690f84c1c62cb32a1.png" width="500"></p>
<p> 在plt.scatter()中参数c就是color,赋值为可迭代参数对象,长度与x,y相同,根据值的不同使得(x,y)参数对表现为不同的颜色。简单地说,按x,y值其中某一个值来区分颜色就好,比如上边想按照y值来区分,所以直接c=y就可以了。</p>
<h3>二、logistic回归测试</h3>
<p>没有自己向之前学习机器学习那样造轮子,直接调用sklearn的函数,用logistic回归进行测试。</p>
<pre class="blockcode"><code class="language-python"># X:所有的样本 维度是2*400
# Y:所有的标签 维度是1*400 (0|1)
lr = linear_model.LogisticRegression()
lr.fit(X.T,Y.flatten())
#查看其对应的w
print(lr.coef_)
#查看其对应的w0
print(lr.intercept_)
w1 = lr.coef_[0,0]
w2 = lr.coef_[0,1]
w0 = lr.intercept_
c = np.linspace(min(X[0,:]),max(X[0,:]),100)
f = [(-w1*i-w0)/w2 for i in c]
plt.plot(c,f,label = '分类曲线',color = 'r')
plt.legend()
plt.show()
#由预测结果组成的数组
res = lr.predict(X.T)
print('逻辑回归的准确定%f%%'%((float(np.dot(Y,res))+float(np.dot(1-Y,1-res)))/Y.size*100))</code></pre>
<p> <img alt="" height="359" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-82dae00c14839d586b35ac53a66559ba.png" width="500"></p>
<p><img alt="" height="108" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-894e1a94ea0afbe4291259daa73268fb.png" width="300"></p>
<p>我才用的画图方式和吴老师的不一样,他还专门写了一个画出分类线的函数,我是直接利用w0,w1,w2画的,因为w0+w1*x1+w2*x2 = 0,正好是一次函数,也能大致画出吴老师的效果。 </p>
<p>我之前还想利用这个线性函数来画出一个封闭的圆形,我自己尝试了好多次,还是没有画出来。后来一想,这本身就是一个一次函数,怎么能画出二次函数的图像呢?</p>
<h3>三、搭建神经网络模型</h3>
<p><strong>构建神经网络的一般方法:</strong></p>
<p><strong>1、定义神经网络的结构</strong></p>
<p><strong>2、分析每一层结构对应参数的维度</strong></p>
<p><strong>3、定义激活函数</strong></p>
<p><strong>4、初始化模型参数</strong></p>
<p><strong>5、定义正向传播函数</strong></p>
<p><strong>6、计算损失</strong></p>
<p><span style="color:#f33b45;"><strong>7、定义反向传播函数</strong></span></p>
<p><strong>8、迭代整合</strong></p>
<p><strong>9、预测</strong></p>
<p><strong>10、图像分析</strong></p>
<p>搭建的网络模型如下所示:</p>
<p><img alt="" height="329" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-a2de0c80a2fe494827557224463f1266.png" width="500"></p>
<p>根据这个模型图片,计算一下参数的维度(自己一点点推测,便于后面利用np进行向量乘法):</p>
<p><strong>X : 2*400 数据集X是共有400个,每个2个特征</strong></p>
<p><strong>Y:1*400 每个样本对应的分类(0|1)</strong></p>
<p><strong>第一层的W:维度是4*2 因为本层有4个节点 上一层有两个节点</strong></p>
<p><strong>第一层的B:因为这一层有4个节点,所以为4*1</strong></p>
<p><strong>所以第一层的Z和A为4*400 因为有400个样本 激活函数为tanh</strong></p>
<p><strong>第二层的W:维度是1*4 本层具有一个节点 上一层具有4个节点 和logistic回归相似</strong></p>
<p><strong>第二层的B:维度是1*1 就是单值</strong></p>
<p><strong>第二层的Z和A:1*400 激活函数为sigmoid</strong></p>
<h3>四、定义模型结构</h3>
<p>定义模型的结构主要就是定义每一层的含有的数量,还要定义sigmoid函数,对于tanh函数,可以用np.tanh()来计算</p>
<pre class="blockcode"><code class="language-python">def sigmoid(x):
return 1/(1+np.exp(-x))
def layer_size(X,Y):
#输入层
n_x = X.shape[0]
#隐藏层
n_h = 4
#输出层
n_y = Y.shape[0]
retur |
|