独家 | 怎么样手动优化神经网络模型(附链接)
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMn0YptVVJtnANRduO6oQSa9cyiacHBrHnT1JA0YOn2Ric2G5gVwA8CpFxpiczaJedoSxUP81ClEZwuEw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">翻译:陈丹</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">校对:车前子</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本文<strong style="color: blue;"><span style="color: black;"><strong style="color: blue;">约5400字</strong></span></strong>,<span style="color: black;">意见</span>阅读<strong style="color: blue;"><span style="color: black;">15</span></strong><strong style="color: blue;"><span style="color: black;"><strong style="color: blue;">分钟</strong></span></strong></p>本文是一个教授<span style="color: black;">怎样</span>优化神经网络模型的<span style="color: black;">基本</span>教程,<span style="color: black;">供给</span>了<span style="color: black;">详细</span>的实战代码供读者学习和实践。<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">标签:神经网络优化</span></p><span style="color: black;">深度学习的神经网络是采用随机梯度下降优化算法对训练数据进行拟合。</span><span style="color: black;">利用误差反向传播算法对模型的权值进行更新。优化和权值更新算法的组合是经过仔细挑选的,是<span style="color: black;">日前</span>已知的最有效的拟合神经网络的<span style="color: black;">办法</span>。</span><span style="color: black;">然而,<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">运用</span>交替优化算法将神经网络模型拟合到训练数据集。这是一个有用的练习,<span style="color: black;">能够</span><span style="color: black;">认识</span><span style="color: black;">更加多</span>关于神经网络的是<span style="color: black;">怎样</span>运转的,以及应用<span style="color: black;">设备</span>学习时优化的中心性。<span style="color: black;">拥有</span>非常规模型结构和不可微分传递函数的神经网络,<span style="color: black;">亦</span>可能需要它。</span><span style="color: black;">在本教程中,您将<span style="color: black;">认识</span><span style="color: black;">怎样</span>手动优化神经网络模型的权重。</span><span style="color: black;">完成本教程后,您将<span style="color: black;">晓得</span>:</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>从头<span style="color: black;">起始</span><span style="color: black;">研发</span>神经网络模型的正向推理通路。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>优化二值<span style="color: black;">归类</span>感知器模型的权值。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>利用随机爬山算法优化多层感知器模型的权值。</span></p><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">起始</span>吧。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHnRkDtnn6PW3IrmScytx4W4xyD2ucBl7icl8O4mx9b1ZgFTkbyV9ialfw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">图源土地管理局,权利归其所有</span>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">教程概述</span></strong></span></h2><span style="color: black;">本教程分为三个部分:它们是:</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">优化神经网络</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">优化感知器模型</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">优化多层感知器</span></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">优化神经网络</span></strong></span></h2><span style="color: black;">深度学习或神经网络是一种灵活的<span style="color: black;">设备</span>学习。</span><span style="color: black;">它们是受大脑结构和功能的启发而来的,由节点和层次<span style="color: black;">构成</span>的模型。神经网络模型的工作原理是将给定的输入向量传播到一个或多个层,以产生可用于<span style="color: black;">归类</span>或回归预测建模的数值输出。</span><span style="color: black;"><span style="color: black;">经过</span>反复将模型暴露在输入和输出示例中,并<span style="color: black;">调节</span>权重以使模型输出相<span style="color: black;">针对</span>期望输出的误差最小来训练模型。这<span style="color: black;">便是</span><span style="color: black;">所说</span>的随机梯度下降优化算法。模型的权值是<span style="color: black;">运用</span>微积分中的一个特定规则来<span style="color: black;">调节</span>的,即将误差按比例分配给网络中的<span style="color: black;">每一个</span>权重。这被<span style="color: black;">叫作</span>为反向传播算法。</span><span style="color: black;">利用反向传播进行权值更新的随机梯度下降优化算法是训练神经网络模型的最佳<span style="color: black;">办法</span>。然而,这并不是训练神经网络的<span style="color: black;">独一</span><span style="color: black;">办法</span>。</span><span style="color: black;"><span style="color: black;">能够</span><span style="color: black;">运用</span>任意的优化算法来训练神经网络模型。</span><span style="color: black;"><span style="color: black;">亦</span><span style="color: black;">便是</span>说,<span style="color: black;">咱们</span>可以定义一个神经网络模型结构,并<span style="color: black;">运用</span>给定的优化算法为模型找到一组权重,从而使预测误差最小或<span style="color: black;">归类</span>精度达到最大。</span><span style="color: black;">交替优化算法<span style="color: black;">一般</span><span style="color: black;">来讲</span>比反向传播的随机梯度下降算法效率更低。然而,在某些特定<span style="color: black;">状况</span>下,它可能更有效,例如非标准网络体系结构或不可微分的传递函数。</span><span style="color: black;">在训练<span style="color: black;">设备</span>学习的算法中,<span style="color: black;">尤其</span>是神经网络中,展示优化的中心性是一个有趣的练习。</span><span style="color: black;">接下来,让<span style="color: black;">咱们</span>探索<span style="color: black;">怎样</span><span style="color: black;">运用</span>随机爬山算法训练一个<span style="color: black;">叫作</span>为感知器模型的简单单节点神经网络。</span>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">优化感知器模型</span></strong></span></h2><span style="color: black;">感知器算法</span><span style="color: black;">(https://machinelearningmastery.com/implement-perceptron-algorithm-scratch-python/)</span><span style="color: black;">是最简单的人工神经网络。</span><span style="color: black;">它是一个单神经元模型,可用于两类<span style="color: black;">归类</span>问题,为以后<span style="color: black;">研发</span>更大的网络<span style="color: black;">供给</span>了<span style="color: black;">基本</span>。</span><span style="color: black;">在本节中,<span style="color: black;">咱们</span>将优化感知器神经网络模型的权重。</span><span style="color: black;"><span style="color: black;">首要</span>,让<span style="color: black;">咱们</span>定义一个综合二进制<span style="color: black;">归类</span>问题,<span style="color: black;">咱们</span><span style="color: black;">能够</span>用它<span style="color: black;">做为</span>优化模型的焦点。</span><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>make_classification()</span><span style="color: black;">(https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_classification.html)</span><span style="color: black;">函数定义一个<span style="color: black;">包括</span>1000行和5个输入变量的二<span style="color: black;">归类</span>问题。</span><span style="color: black;">下面的示例创建数据集并总结数据的形状。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHGPY1JbFxp8ql0c5uEJ8Vzhd2gpFpasAuE3b89icofALicXK8IXfMriaww/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">运行<span style="color: black;">以上</span>示例,打印出所创建数据集的形状,以确定符合<span style="color: black;">咱们</span>的预期。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHQSXcankgxGTOR6x0QF7MIr8nt1L2Bg3gDuLLUEpc5tb5tDCRWJModg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">接下来,<span style="color: black;">咱们</span>需要定义一个感知器模型。</span><span style="color: black;">感知器模型有一个节点,它对数据集中的每一列都有一个输入权重。</span><span style="color: black;"><span style="color: black;">每一个</span>输入值乘以其相应的权重得到一个加权和,<span style="color: black;">而后</span>添加一个偏差权重,就像回归模型中的截距系数<span style="color: black;">同样</span>。这个加权和<span style="color: black;">叫作</span>为活性值。最后,对活性值进行解释并用于预测类标签,1<span style="color: black;">暗示</span>正激活,0<span style="color: black;">暗示</span>负激活。</span><span style="color: black;">在优化模型权重之前,<span style="color: black;">咱们</span>必须<span style="color: black;">创立</span>模型并相信它的运作方式。</span><span style="color: black;">让<span style="color: black;">咱们</span>从定义一个用于解释模型激活的函数<span style="color: black;">起始</span>。</span><span style="color: black;">这被<span style="color: black;">叫作</span>为激活函数,或传递函数;后一个名<span style="color: black;">叫作</span>更传统,是我的首选。</span><span style="color: black;">下面的transfer()函数接受模型的激活并返回一个类标签,class=1<span style="color: black;">暗示</span>正激活或零激活,class=0<span style="color: black;">暗示</span>负激活。这<span style="color: black;">叫作</span>为阶跃函数。</span><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHxJ2svenusaxT2Nk218x7TaHG095wGYEeI3Ly6o6MdypvJfLJkrKGkg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"><span style="color: black;">接下来,<span style="color: black;">咱们</span><span style="color: black;">能够</span>开发一个函数,该函数计算来自数据集的给定输入行的模型活性值。</span><span style="color: black;">此函数将获取模型的数据行和权重,并计算输入的加权和以及偏差权重。下面的activate()函数实现了这一点。</span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">重视</span>:</span></strong><span style="color: black;"><span style="color: black;">咱们</span>特意<span style="color: black;">运用</span>简单的Python列表和命令式编程风格,而不是NumPy的数组或列表压缩,是为了让Python初学者更易读懂。您<span style="color: black;">能够</span>随意优化它,并在下面的注释中发布您的代码。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHnBDKhvG79azhsyIYE5Z2P42eiaLa2MFwCj471Drj72rHQ3rbj6mQMDg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">接下来,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">一块</span><span style="color: black;">运用</span>activate()和transfer()函数来生成给定数据行的预测。下面的predict_row()函数实现了这一点。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHiaianwD7olsmvGoiaHXw0IFNg17rn46FNC8G5iclmciaT0CXxicmwxFdIXcg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">接下来,<span style="color: black;">咱们</span><span style="color: black;">能够</span>为给定数据集中的每一行调用predict_row()函数。下面的predict_dataset()函数实现了这一点。</span><span style="color: black;"><span style="color: black;">一样</span>,<span style="color: black;">咱们</span>有意<span style="color: black;">运用</span>简单的命令式编码方式来<span style="color: black;">加强</span>可读性,而不是列表压缩。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHtRW0pjGzajv1WyZeqEiaWzuJVf0066dITyGIApzENtmbQUv7ibxMrdyQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">最后,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>该模型对<span style="color: black;">咱们</span>的合成数据集进行预测,以确认它都是正确工作的。</span><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>rand()函数生成一组随机的模型权重。</span><span style="color: black;">回想一下,<span style="color: black;">咱们</span>需要为<span style="color: black;">每一个</span>输入赋予一个权重(在这个数据集中有五个输入),再加上一个额外的权重<span style="color: black;">做为</span>偏移权重。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHwuwBexKYeXvTfg818FHKXPvhSH312es41jSRf9VbKkuljJLugmklZA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">而后</span><span style="color: black;">咱们</span><span style="color: black;">能够</span>将这些权重与数据集<span style="color: black;">一块</span><span style="color: black;">运用</span>来进行预测。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHF4B6MwQZkmHmTInibz3tfENJ6bNbyPZziag3ZgaoOvVQgyiahBBWX3Tkg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">评定</span>这些预测的<span style="color: black;">归类</span>精度。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHfWmw0R7zmPP9eruZfeMSyHb76mNxq2Micjib3kHQcJnGr87wf2QvkWIg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">就<span style="color: black;">这般</span>。</span><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">能够</span>将所有这些整理在<span style="color: black;">一块</span>,并演示用<span style="color: black;">咱们</span>的简单感知器模型进行<span style="color: black;">归类</span>。下面列出了完整的示例。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHCXAXaoyH1ES7IQvSGbd9IFKrpGaHVFTb2EwL7LsC6sXuicKwPrz8mwA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHUGyI4dL26f6WHkLHjenF9FylCiaqSrogzIARrzdpNb4pWEEkicIXEnMw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHmRrzCcfBRpGAw2RtQPfA6Saf9XtCyoHupibRqB2ibibOLvWYMt0lelsUA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">运行该示例将为训练数据集中的<span style="color: black;">每一个</span>示例生成一个预测,<span style="color: black;">而后</span>打印出<span style="color: black;">归类</span>预测的准确度。</span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">重视</span>:</span></strong><span style="color: black;">您的结果可能会<span style="color: black;">由于</span>算法或<span style="color: black;">评定</span>过程的随机性或数值精度的差异而有所<span style="color: black;">区别</span></span><span style="color: black;">(https://machinelearningmastery.com/different-results-each-time-in-machine-learning/)</span><span style="color: black;">。<span style="color: black;">思虑</span>运行该示例几次并比较平均结果。</span><span style="color: black;"><span style="color: black;">倘若</span>给定一组随机权重和一个每类中有相同数量示例的数据集,<span style="color: black;">咱们</span>期望大约50%的准确率,这与<span style="color: black;">咱们</span>在本例中看到的差不多。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHicbECGJl0ssTEibGw1DJrUdKEXRUbLbucwicTKdFJsjk6LF5amreqGNRA/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">此刻</span>,<span style="color: black;">咱们</span><span style="color: black;">能够</span>优化数据集的权重,以<span style="color: black;">得到</span>该数据集的良好准确度。</span><span style="color: black;"><span style="color: black;">首要</span>,<span style="color: black;">咱们</span>需要将数据集分成训练集和测试集。重要的是要<span style="color: black;">保存</span><span style="color: black;">有些</span>未用于优化模型的数据,以便在对新数据进行预测时,<span style="color: black;">咱们</span><span style="color: black;">能够</span>对模型的性能进行<span style="color: black;">恰当</span>的估计。</span><span style="color: black;"><span style="color: black;">咱们</span>将<span style="color: black;">运用</span>67%的数据进行培训,剩下的33%<span style="color: black;">做为</span>测试集来<span style="color: black;">评定</span>模型的性能。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHUJOiaArVINcXDEPnTIGtNM9GticiaQPCibYK1HuH7I5C5rIacnribgLAt8w/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">接下来,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">研发</span>一个随机爬山算法。</span><span style="color: black;">优化算法需要一个<span style="color: black;">目的</span>函数来优化。它必须取一组权重,并返回一个与更好的模型相对应的最小化或最大化的分数。</span><span style="color: black;">在这种<span style="color: black;">状况</span>下,<span style="color: black;">咱们</span>将<span style="color: black;">运用</span>一组给定的权重来<span style="color: black;">评定</span>模型的准确性,并返回<span style="color: black;">归类</span>精度,这必须使其最大化。</span><span style="color: black;">下面的objective()函数<span style="color: black;">经过</span>给定数据集和一组权重来实现这一点,并返回模型的精度。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHZKYaHelR1p9sp5YLibt7rqRnslz9p2MWPejpcnuJheMFydITwVdB6kw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">接下来,<span style="color: black;">咱们</span><span style="color: black;">能够</span>定义随机爬山算法</span><span style="color: black;">(https://machinelearningmastery.com/stochastic-hill-climbing-in-python-from-scratch/)</span><span style="color: black;">。</span><span style="color: black;">该算法需要一个初始解(例如随机权重),并且会<span style="color: black;">持续</span>地对解进行小的修改,并<span style="color: black;">检测</span>它<span style="color: black;">是不是</span>会产生一个性能更好的模型。对当前<span style="color: black;">处理</span><span style="color: black;">方法</span>所做的更改量由“步长”超参数<span style="color: black;">掌控</span>。此过程将<span style="color: black;">连续</span>固定次数的迭代,<span style="color: black;">亦</span><span style="color: black;">做为</span>超参数<span style="color: black;">供给</span>。</span><span style="color: black;">下面的hillclimbing()函数实现了这一点,它将数据集、<span style="color: black;">目的</span>函数、初始解和超参数<span style="color: black;">做为</span>参数,并返回找到的最佳权重集和估计的性能。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHhnuulQr7M0qOqeLsHC4UF2iaPUqRqz7cKwakXJHiaFJ7EyHXFhnhoyDw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHVcgpe9FD8wO3TqyZtvootaI0XU2hxUZ89Kw7yo0VFgiaMTTqUJlrz6A/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">而后</span><span style="color: black;">咱们</span><span style="color: black;">能够</span>调用这个函数,传入一组权重<span style="color: black;">做为</span>初始解,将训练数据集<span style="color: black;">做为</span>优化模型的数据集。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHPLMKrc5qoIRKrX3fP21RXjQiaBzMSO8yXMl6PQbw4RJia7tU1ia9NNhow/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">最后,<span style="color: black;">咱们</span><span style="color: black;">能够</span>在测试数据集上<span style="color: black;">评定</span>最佳模型并报告性能。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHPQrEQdIzTQtMicasnib1tLfS1tKtTHaVYj9Qp9ulu5uiaxlMHbsJliaSww/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">结合这一点,下面列出了在合成二进制优化数据集上优化感知器模型权重的完整示例。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHHbJF7k8ebPuFSkKjneGibYUlvmogSmlaoXtdbU6OMREDKnC05icmfvIQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/heS6wRSHVMmIONkDB3icWjlEic3V4thIUHJ94tpqekPlAiakEMTe3u4hRlqJsAoJsTHxBw6oVgca2BszyPKFicmFaw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">每次对模型进行改进时,运行示例将报告迭代次数和<span style="color: black;">归类</span>精度。</span><span style="color: black;">在搜索的最后,报告了最佳权重集在训练数据集上的性能,并计算和报告了同一模型在测试数据集上的性能。</span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">重视</span>:</span></strong><span style="color: black;">您的结果可能会<span style="color: black;">由于</span>算法或<span style="color: black;">评定</span>过程的随机性或数值精度的差异而有所<span style="color: black;">区别</span></span><span style="color: black;">(https://machinelearningmastery.com/different-results-each-time-in-machine-learning/)。</span><span style="color: black;"><span style="color: black;">思虑</span>运行该示例几次并比较平均结果。</span><span style="color: black;">在这种<span style="color: black;">状况</span>下,<span style="color: black;">咱们</span><span style="color: black;">能够</span>看到优化算法找到了一组权重,在训练数据集上达到了88.5%的准确率,在测试数据集上达到了81.8%的准确率。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">此刻</span><span style="color: black;">咱们</span><span style="color: black;">已然</span><span style="color: black;">熟练</span>了<span style="color: black;">怎样</span>手动优化感知器模型的权重,让<span style="color: black;">咱们</span><span style="color: black;">瞧瞧</span><span style="color: black;">怎样</span>扩展示例来优化多层感知器(Multilayer Perceptron,MLP)模型的权重。</span>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">优化多层感知器</span></strong></span></h2><span style="color: black;">多层感知器(MLP)模型是一个<span style="color: black;">拥有</span>一个或多个层次的神经网络,每一层都有一个或多个节点。</span><span style="color: black;">它是感知器模型的扩展,可能是应用最广泛的神经网络(深度学习)模型。</span><span style="color: black;">在本节中,<span style="color: black;">咱们</span>将在上一节所学的<span style="color: black;">基本</span>上,优化每层<span style="color: black;">拥有</span>任意数量层和节点的MLP模型的权重。</span><span style="color: black;"><span style="color: black;">首要</span>,<span style="color: black;">咱们</span>将<span style="color: black;">研发</span>模型并用随机权重进行测试,<span style="color: black;">而后</span><span style="color: black;">运用</span>随机爬山算法优化模型权重。</span><span style="color: black;">当<span style="color: black;">运用</span>MLPs进行二<span style="color: black;">归类</span>时,<span style="color: black;">一般</span><span style="color: black;">运用</span>sigmoid变换函数(<span style="color: black;">亦</span><span style="color: black;">叫作</span>为logistic函数)来代替感知器中<span style="color: black;">运用</span>的阶跃变换函数。</span><span style="color: black;">此函数输出0-1之间的实数,<span style="color: black;">暗示</span>二项式概率分布</span><span style="color: black;">(https://machinelearningmastery.com/discrete-probability-distributions-for-machine-learning/)</span><span style="color: black;">,例如一个示例属于1类的概率。下面的transfer()函数实现了这一点。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>上一节中相同的activate()函数。在<span style="color: black;">这儿</span>,<span style="color: black;">咱们</span>将<span style="color: black;">运用</span>它来计算给定层中<span style="color: black;">每一个</span>节点的激活。</span><span style="color: black;">predict_row()函数必须替换为更精细的版本。</span><span style="color: black;">函数获取一行数据和网络并返回网络的输出。</span><span style="color: black;"><span style="color: black;">咱们</span>将把<span style="color: black;">咱们</span>的网络定义为一个列表。<span style="color: black;">每一个</span>层将是一个节点列表,<span style="color: black;">每一个</span>节点将是一个权重列表或数组。</span><span style="color: black;">为了计算网络的预测,<span style="color: black;">咱们</span>简单地枚举层,<span style="color: black;">而后</span>枚举节点,<span style="color: black;">而后</span>对<span style="color: black;">每一个</span>节点的输出进行激活计算和变换。在这种<span style="color: black;">状况</span>下,<span style="color: black;">咱们</span>将对网络中的所有节点使用相同的变换函数,尽管这不是必须的。</span><span style="color: black;"><span style="color: black;">针对</span><span style="color: black;">拥有</span>多个层的网络,上一层的输出用作下一层中<span style="color: black;">每一个</span>节点的输入。<span style="color: black;">而后</span>返回网络中最后一层的输出。</span><span style="color: black;">下面的predict_row()函数实现了这一点。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">便是</span><span style="color: black;">这般</span>。</span><span style="color: black;">最后<span style="color: black;">咱们</span>需要定义一个网络。</span><span style="color: black;">例如,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>单个<span style="color: black;">隐匿</span>层和单个节点定义MLP,如下所示:</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">这<span style="color: black;">实质</span>上是一个感知器,尽管它有一个sigmoid变换函数。这很无聊。</span><span style="color: black;">让<span style="color: black;">咱们</span>定义一个<span style="color: black;">拥有</span>一个<span style="color: black;">隐匿</span>层和一个输出层的MLP。<span style="color: black;">第1</span>个<span style="color: black;">隐匿</span>层将有10个节点,<span style="color: black;">每一个</span>节点将从数据集中获取输入模式(例如5个输入)。输出层将有一个节点,从<span style="color: black;">第1</span>个<span style="color: black;">隐匿</span>层的输出接收输入,<span style="color: black;">而后</span>输出预测。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;"><span style="color: black;">而后</span><span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>模型对数据集进行预测。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">在计算<span style="color: black;">归类</span>精度之前,<span style="color: black;">咱们</span>必须将预测四舍五入到<span style="color: black;">归类</span>标签0和1。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">将这些整合在<span style="color: black;">一块</span>,在<span style="color: black;">咱们</span>合成的二进制<span style="color: black;">归类</span>数据集上<span style="color: black;">运用</span>随机初始权重来<span style="color: black;">评定</span>MLP的完整示例如下所示。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">运行该示例将为训练数据集中的<span style="color: black;">每一个</span>示例生成一个预测,<span style="color: black;">而后</span>打印预测的<span style="color: black;">归类</span>精度。</span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">重视</span>:</span></strong><span style="color: black;">您的结果可能会<span style="color: black;">由于</span>算法或<span style="color: black;">评定</span>过程的随机性或数值精度的差异而有所<span style="color: black;">区别</span></span><span style="color: black;">(https://machinelearningmastery.com/different-results-each-time-in-machine-learning/)</span><span style="color: black;">。<span style="color: black;">思虑</span>运行该示例几次并比较平均结果。</span><span style="color: black;"><span style="color: black;">一样</span>,<span style="color: black;">咱们</span><span style="color: black;">期盼</span>在给定一组随机权重和一个数据集(<span style="color: black;">每一个</span>类中有相同数量的示例)的<span style="color: black;">状况</span>下,准确率大约为50%,这与<span style="color: black;">咱们</span>在本例中看到的差不多。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">接下来,<span style="color: black;">咱们</span><span style="color: black;">能够</span>将随机爬山算法应用于数据集。</span><span style="color: black;">这与将爬山应用于感知器模型非常相同,只是在这种<span style="color: black;">状况</span>下,一个<span style="color: black;">过程</span>需要修改网络中的所有权重。</span><span style="color: black;">为此,<span style="color: black;">咱们</span>将<span style="color: black;">研发</span>一个新功能,创建一个网络副本,并在制作副本时对网络中的<span style="color: black;">每一个</span>权重进行变异。</span><span style="color: black;">下面的step()函数实现了这一点。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">修改网络中的所有权重是<span style="color: black;">拥有</span>侵略性的。</span><span style="color: black;">在搜索空间中,一个不<span style="color: black;">那样</span>激进的<span style="color: black;">过程</span>可能是对模型中的权重子集进行一个小的更改,<span style="color: black;">亦</span>许<span style="color: black;">是由于</span>一个超参数<span style="color: black;">掌控</span>的。这个<span style="color: black;">做为</span>扩展内容。</span><span style="color: black;"><span style="color: black;">而后</span><span style="color: black;">咱们</span><span style="color: black;">能够</span>从hillclimbing()函数调用这个新的step()函数。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">结合这一点,下面列出了应用随机爬山算法优化二<span style="color: black;">归类</span>MLP模型权重的完整示例。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p><span style="color: black;">每次对模型进行改进时,运行示例将报告迭代次数和<span style="color: black;">归类</span>精度。</span><span style="color: black;">在搜索的最后,报告了最佳权重集在训练数据集上的性能,并计算和报告了同一模型在测试数据集上的性能。</span><strong style="color: blue;"><span style="color: black;"><span style="color: black;">重视</span>:</span></strong><span style="color: black;">您的结果可能会<span style="color: black;">由于</span>算法或<span style="color: black;">评定</span>过程的随机性或数值精度的差异而有所<span style="color: black;">区别</span></span><span style="color: black;">(https://machinelearningmastery.com/different-results-each-time-in-machine-learning/)</span><span style="color: black;">。<span style="color: black;">思虑</span>运行该示例几次并比较平均结果。</span><span style="color: black;">在这种<span style="color: black;">状况</span>下,<span style="color: black;">咱们</span><span style="color: black;">能够</span>看到优化算法找到了一组权重,在训练数据集上达到了87.3%的准确率,在测试数据集上达到了大约85.1%的准确率。</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">进一步阅读</span></strong></span></h2><span style="color: black;"><span style="color: black;">倘若</span>您想深入<span style="color: black;">认识</span>,本节将<span style="color: black;">供给</span><span style="color: black;">相关</span>该主题的<span style="color: black;">更加多</span>资源。</span><span style="color: black;"><strong style="color: blue;"><span style="color: black;">教程</span></strong></span><span style="color: black;">用于<span style="color: black;">评定</span><span style="color: black;">设备</span>学习算法的训练测试分割:</span><span style="color: black;">https://machinelearningmastery.com/train-test-split-for-evaluating-machine-learning-algorithms</span><span style="color: black;"><span style="color: black;">怎样</span>用Python中从头<span style="color: black;">起始</span>实现感知器算法:</span><span style="color: black;">https://machinelearningmastery.com/implement-perceptron-algorithm-scratch-python/</span><span style="color: black;"><span style="color: black;">怎样</span>用Python编写反向传播的神经网络(从头<span style="color: black;">起始</span>):</span><span style="color: black;">https://machinelearningmastery.com/implement-backpropagation-algorithm-scratch-python/</span><strong style="color: blue;"><span style="color: black;">接口</span></strong><span style="color: black;">sklearn.datasets.make_classification 接口:</span><span style="color: black;">https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_classification.html</span><span style="color: black;">sklearn.metrics.accuracy_score 接口:</span><span style="color: black;">https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html</span><span style="color: black;">numpy.random.rand接口:</span><span style="color: black;">https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html</span><strong style="color: blue;"><span style="color: black;">总结</span></strong><span style="color: black;">在本教程中,您<span style="color: black;">认识</span>了<span style="color: black;">怎样</span>手动优化神经网络模型的权重。</span><span style="color: black;"><span style="color: black;">详细</span><span style="color: black;">来讲</span>,你学到了:</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>从头<span style="color: black;">起始</span><span style="color: black;">研发</span>神经网络模型的正向推理通路。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>优化二<span style="color: black;">归类</span>感知器模型的权值。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">怎样</span>利用随机爬山算法优化多层感知器模型的权值。</span></p><span style="color: black;">原文标题:</span><span style="color: black;">How to Manually Optimize Neural Network Models</span><span style="color: black;">原文链接:</span><span style="color: black;">https://machinelearningmastery.com/manually-optimize-neural-networks/</span><span style="color: black;">编辑:王菁</span><span style="color: black;">校对:林<span style="color: black;">也</span>霖</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">译者简介</strong></p><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">陈丹</strong>,复旦大学大三在读,主修预防医学,辅修数据科学。对数据分析充满兴趣,但初入这一<span style="color: black;">行业</span>,还有<span style="color: black;">非常多</span><span style="color: black;">非常多</span>需要<span style="color: black;">奋斗</span>进步的空间。<span style="color: black;">期盼</span>今后能在翻译组进行<span style="color: black;">关联</span>工作的过程中拓展文献阅读量,学习到<span style="color: black;">更加多</span>的前沿知识,<span style="color: black;">同期</span>认识<span style="color: black;">更加多</span>有<span style="color: black;">一起</span>志趣的小伙伴!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">翻译组招募信息</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">工作内容:</strong>
</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">需要一颗细致的心,将<span style="color: black;">选择</span>好的外文<span style="color: black;">文案</span>翻译成流畅的中文。<span style="color: black;">倘若</span>你是数据科学/统计学/计算机类的留学生,或在海外从事<span style="color: black;">关联</span>工作,或对自己外语水平有<span style="color: black;">自信心</span>的<span style="color: black;">伴侣</span>欢迎加入翻译小组。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">你能得到:</strong><span style="color: black;">定时</span>的翻译培训<span style="color: black;">加强</span>志愿者的翻译水平,<span style="color: black;">加强</span><span style="color: black;">针对</span>数据科学前沿的认知,海外的<span style="color: black;">伴侣</span><span style="color: black;">能够</span>和国内技术应用发展保持联系,THU数据派产学研的背景为志愿者带来好的发展机遇。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">其他福利:</strong>来自于名企的数据科学工作者,北大清华以及海外等名校学生<span style="color: black;">她们</span>都将<span style="color: black;">作为</span>你在翻译小组的伙伴。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">点击文末“<span style="color: black;">阅读原文</span>”加入数据派团队~</strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">转载须知</strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">如需转载,请在开篇<span style="color: black;">明显</span>位置注明作者和出处(<span style="color: black;">转载</span>:数据派ID:DatapiTHU),并在<span style="color: black;">文案</span>结尾<span style="color: black;">安置</span>数据派醒目二维码。有原创标识<span style="color: black;">文案</span>,请发送【<span style="color: black;">文案</span>名<span style="color: black;">叫作</span>-待授权公众号名<span style="color: black;">叫作</span>及ID】至联系邮箱,申请白名单授权并按<span style="color: black;">需求</span>编辑。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">发布后请将链接反馈至联系邮箱(见下方)。未经许可的转载以及改编者,<span style="color: black;">咱们</span>将依法追究其法律责任。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><strong style="color: blue;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></strong></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">点击<strong style="color: blue;">“阅读原文”</strong>拥抱组织</p>
你的话语如春风拂面,温暖了我的心房,真的很感谢。 我完全同意你的观点,说得太对了。 楼主节操掉了,还不快捡起来! 感谢你的精彩评论,带给我新的思考角度。 我们有着相似的经历,你的感受我深有体会。
页:
[1]