知行编程网知行编程网  2022-06-08 12:00 知行编程网 隐藏边栏 |   抢沙发  15 
文章评分 0 次,平均分 0.0

机器学习入门贴,神经网络原来这么简单

白交 发自 凹非寺 
转自 | 量子位

你想学机器学习吗?这里有一个入门贴适合你。
什么神经网络、随机森林、计算机视觉通通一网打尽。
这个Facebook软件工程师做了一个入门贴。
专为基础为零的初学者打造。
有基础的同学,也可以来看看加深一下理解。

机器学习入门贴,神经网络原来这么简单
我们就以神经网络为例先来一睹为快吧!


   神经网络概论

作者说,神经网络并不复杂!
“神经网络”一词很流行,人们通常认为它很难,但其实要简单得多。
是不是这样呢?先看再说。
神经网络的理解主要分为三个部分,神经元、神经网络的构建、训练神经网络。

神经元——神经网络的基本单元

机器学习入门贴,神经网络原来这么简单

这是2-input神经元的样子。
首先神经元接受输入x1、x2,进行一些数学运算以后,然后产生一个输出y。
在神经元里,通常会发生三件事:
1、每个输入乘以相应的权重;
机器学习入门贴,神经网络原来这么简单
2、将所有加权输入加在一起,在加上一个偏差b;
机器学习入门贴,神经网络原来这么简单
3、导入一个激活函数,得到输出y。

机器学习入门贴,神经网络原来这么简单

通常来说,激活函数使用Sigmoid函数,也就是常说的S型函数,输入任意值(-∞,+∞),最后输出都能停留在0-1之间。
机器学习入门贴,神经网络原来这么简单
对此,他还举了一个简单的例子。
以激活函数是S型函数、2输入神经元为例,设置参数 w=[0,1] (w1=0,w2=1),b=4。
input:x=[2,3]
output:y=0.999

机器学习入门贴,神经网络原来这么简单
这也就是最为朴素的神经网络——前馈神经网络。
对此,作者还用Python实现了整个过程。
<section style="padding: 0.5em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;background: rgb(29, 31, 33);color: rgb(197, 200, 198);margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;">import numpy as np<br  /><br  /><span style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">def</span> <span style="line-height: inherit;color: rgb(129, 162, 190);overflow-wrap: inherit !important;word-break: inherit !important;">sigmoid</span><span style="line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">(x)</span></span>:<br  />  <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># Our activation function: f(x) = 1 / (1 + e^(-x))</span><br  />  <span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">return</span> <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">1</span> / (<span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">1</span> + np.exp(-x))<br  /><br  /><span style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">class</span> <span style="line-height: inherit;color: rgb(129, 162, 190);overflow-wrap: inherit !important;word-break: inherit !important;">Neuron</span>:</span><br  />  <span style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">def</span> <span style="line-height: inherit;color: rgb(129, 162, 190);overflow-wrap: inherit !important;word-break: inherit !important;">__init__</span><span style="line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">(<span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">self</span>, weights, bias)</span></span>:<br  />    <span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">self</span>.weights = weights<br  />    <span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">self</span>.bias = bias<br  /><br  />  <span style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">def</span> <span style="line-height: inherit;color: rgb(129, 162, 190);overflow-wrap: inherit !important;word-break: inherit !important;">feedforward</span><span style="line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">(<span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">self</span>, inputs)</span></span>:<br  />    <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># Weight inputs, add bias, then use the activation function</span><br  />    total = np.dot(<span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">self</span>.weights, inputs) + <span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">self</span>.bias<br  />    <span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">return</span> sigmoid(total)<br  /><br  />weights = np.array([<span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>]) <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># w1 = 0, w2 = 1</span><br  />bias = <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">4</span>                   <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># b = 4</span><br  />n = Neuron(weights, bias)<br  /><br  />x = np.array([<span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">2</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">3</span>])       <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># x1 = 2, x2 = 3</span><br  />print(n.feedforward(x))    <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># 0.9990889488055994</span><br  /></section>


构建神经网络

神经元连接在一起就是神经网络。
机器学习入门贴,神经网络原来这么简单
两个输入,一个含有两个神经元的隐藏层,一个含有1个神经元的输出层就构建了一个神经网络。
需要注意的是,可以用多层隐藏层。就比如,像这样:       机器学习入门贴,神经网络原来这么简单
我们仍以上个示例的条件为例。
机器学习入门贴,神经网络原来这么简单
一个神经网络可以包含任意数量的层和任意数量的神经元。
以Python代码示例如下:
<section style="padding: 0.5em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;background: rgb(29, 31, 33);color: rgb(197, 200, 198);margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> numpy <span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">as</span> np<br  /><br  /><span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># ... code from previous section here</span><br  /><br  /><span style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">class</span> <span style="line-height: inherit;color: rgb(129, 162, 190);overflow-wrap: inherit !important;word-break: inherit !important;">OurNeuralNetwork</span>:</span><br  />  <span style="font-size: inherit;line-height: inherit;color: rgb(181, 189, 104);overflow-wrap: inherit !important;word-break: inherit !important;">'''<br  />  A neural network with:<br  />    - 2 inputs<br  />    - a hidden layer with 2 neurons (h1, h2)<br  />    - an output layer with 1 neuron (o1)<br  />  Each neuron has the same weights and bias:<br  />    - w = [0, 1]<br  />    - b = 0<br  />  '''</span><br  />  <span style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">def</span> <span style="line-height: inherit;color: rgb(129, 162, 190);overflow-wrap: inherit !important;word-break: inherit !important;">__init__</span><span style="line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">(self)</span>:</span><br  />    weights = np.array([<span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>])<br  />    bias = <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">0</span><br  /><br  />    <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># The Neuron class here is from the previous section</span><br  />    self.h1 = Neuron(weights, bias)<br  />    self.h2 = Neuron(weights, bias)<br  />    self.o1 = Neuron(weights, bias)<br  /><br  />  <span style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">def</span> <span style="line-height: inherit;color: rgb(129, 162, 190);overflow-wrap: inherit !important;word-break: inherit !important;">feedforward</span><span style="line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">(self, x)</span>:</span><br  />    out_h1 = self.h1.feedforward(x)<br  />    out_h2 = self.h2.feedforward(x)<br  /><br  />    <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># The inputs for o1 are the outputs from h1 and h2</span><br  />    out_o1 = self.o1.feedforward(np.array([out_h1, out_h2]))<br  /><br  />    <span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">return</span> out_o1<br  /><br  />network = OurNeuralNetwork()<br  />x = np.array([<span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">2</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">3</span>])<br  />print(network.feedforward(x)) <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># 0.7216325609518421</span></section>


训练神经网路——计算损失函数

假设,我们正在处理以下这个项目。通过人员的体重和身高来判断性别。
机器学习入门贴,神经网络原来这么简单
以weight、height作为输入,以gender作为输出。
机器学习入门贴,神经网络原来这么简单
Male设置为0,Female设置为1,还对其余数据进行了简化。
机器学习入门贴,神经网络原来这么简单
在训练神经网络之前,首先需要一个方法来量化它做得有多“好”,是否能够做得“更好”,那就是损失函数(loss)。
这里,我们将使用损失函数的一种——均方误差来计算。
机器学习入门贴,神经网络原来这么简单
预测结果越好,说明损失也就会越低。而训练神经网络的目的,就在于尽可能的减少损失。
如果我们确信所有的人都是Male,也就是说预测值为0,会出现什么样的结果?机器学习入门贴,神经网络原来这么简单
Python示例:
<section style="padding: 0.5em;line-height: 18px;font-size: 14px;letter-spacing: 0px;font-family: Consolas, Inconsolata, Courier, monospace;border-radius: 0px;background: rgb(29, 31, 33);color: rgb(197, 200, 198);margin-left: 8px;margin-right: 8px;overflow-wrap: normal !important;word-break: normal !important;overflow: auto !important;display: -webkit-box !important;"><span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">import</span> numpy <span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">as</span> np<br  /><br  /><span style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">def</span> <span style="line-height: inherit;color: rgb(129, 162, 190);overflow-wrap: inherit !important;word-break: inherit !important;">mse_loss</span><span style="line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">(y_true, y_pred)</span>:</span><br  />  <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># y_true and y_pred are numpy arrays of the same length.</span><br  />  <span style="font-size: inherit;line-height: inherit;color: rgb(178, 148, 187);overflow-wrap: inherit !important;word-break: inherit !important;">return</span> ((y_true - y_pred) ** <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">2</span>).mean()<br  /><br  />y_true = np.array([<span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">1</span>])<br  />y_pred = np.array([<span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">0</span>, <span style="font-size: inherit;line-height: inherit;color: rgb(222, 147, 95);overflow-wrap: inherit !important;word-break: inherit !important;">0</span>])<br  /><br  />print(mse_loss(y_true, y_pred)) <span style="font-size: inherit;line-height: inherit;color: rgb(150, 152, 150);overflow-wrap: inherit !important;word-break: inherit !important;"># 0.5</span><br  /></section>

训练神经网络——最小化损失

计算了损失函数之后,就需要将损失最小化,这也是训练神经网络的最终目的所在。
接下来帖子有一段多变量演算,涉及微积分。
作者表示,
如果对微积分不满意,可随时跳过。
简单起见,我们就假设这个数据集中只有Alice。
那么,它的损失函数就是这样。
机器学习入门贴,神经网络原来这么简单
那么它的权重w跟偏差b,在图上标示,那么就有6个权重变量,3个偏差变量。
机器学习入门贴,神经网络原来这么简单
于是,便将损失函数写为多变量函数。
       机器学习入门贴,神经网络原来这么简单
想象一下,我们只要调整w1,就可能导致L的变化。那具体是如何变化的呢?这就需要计算偏导数了。
       机器学习入门贴,神经网络原来这么简单
利用链式求导法则进行反向求导,而这一过程就叫做反向传播
详细计算过程就不放在这里了,大家去他个人网站去看哦~(链接已附文末)
作者温馨提示,看这个过程的时候不要着急,拿出手中的笔和纸,能够帮助你理解。
接下来,使用随机梯度下降的优化算法,公式表示如下(以w1为例):
机器学习入门贴,神经网络原来这么简单
其中的“学习速率”控制着训练速度,过大或者过小都不合适。
如果我们将所有的变量都进行这样的优化,那么损失函数将逐渐减少,神经网络就能够得到改善。
机器学习入门贴,神经网络原来这么简单
简单来说,整个训练过程是这样的:
1、数据集中选择一个样本,就如Alice。
2、利用反向传播计算所有变量的偏导数。
3、使用随机梯度下降来训练神经网络,更新变量。
4、返回步骤1。
神经网络的部分就介绍到这里,怎么样?看完之后,有什么感想?
是不是觉得神经网络也还好了。还有其他概念等着你来学习呢!


入门贴传送门

https://victorzhou.com/tag/machine-learning/
作者系网易新闻·网易号“各有态度”签约作者

<pre style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;white-space: normal;font-family: -apple-system-font, system-ui, "Helvetica Neue", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;widows: 1;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="max-width: 100%;letter-spacing: 0.544px;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section powered-by="xiumi.us" style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section style="margin-top: 15px;margin-bottom: 25px;max-width: 100%;opacity: 0.8;box-sizing: border-box !important;overflow-wrap: break-word !important;"><section><p style="margin-bottom: 15px;padding-right: 0em;padding-left: 0em;max-width: 100%;color: rgb(127, 127, 127);font-size: 12px;font-family: sans-serif;line-height: 25.5938px;letter-spacing: 3px;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;color: rgb(0, 0, 0);box-sizing: border-box !important;overflow-wrap: break-word !important;"><strong style="max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="max-width: 100%;font-size: 16px;font-family: 微软雅黑;caret-color: red;box-sizing: border-box !important;overflow-wrap: break-word !important;">为您推荐</span></strong></span></p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;">MIT校长评中美科技竞赛:胜利不是期盼对手的失利</p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">GitHub重大更新:在线开发上线,是时候卸载IDE了</span></p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;"><span style="font-size: 14px;">美国官宣117000名 IT 人失业,真是史无前例!</span><br  /></p><p style="margin-top: 5px;margin-bottom: 5px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;">数据分析入门常用的23个牛逼Pandas代码</p><section style="margin: 5px 8px;padding-right: 0em;padding-left: 0em;max-width: 100%;min-height: 1em;font-family: sans-serif;letter-spacing: 0px;opacity: 0.8;line-height: normal;text-align: center;box-sizing: border-box !important;overflow-wrap: break-word !important;">特朗普拿H1B签证开刀,LeCun吴恩达等实名谴责!<br  /></section></section></section></section></section></section></section></section></section>
机器学习入门贴,神经网络原来这么简单

本篇文章来源于: 深度学习这件小事

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享