lbk60ox 发表于 2024-7-29 10:48:14

9行Python代码创立一个简单神经网络

<strong style="color: blue;"><span style="color: black;"><span style="color: black;">海量</span>Python学习资料</span></strong><span style="color: black;"><strong style="color: blue;">领取方式:</strong></span><span style="color: black;"><strong style="color: blue;">添加<span style="color: black;">微X</span>huanglaoshiJS或扫码,备注"</strong><strong style="color: blue;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">学习资料</span></strong></span></strong>领取</span></strong><strong style="color: blue;">"<span style="color: black;">就可</span>领取。</strong></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/I0gZcoibfPicuXZnyCSIoka2F2IhOeiadHZ6eNK53gKNyVM2DiaKAXDkKKwsnVib6pZbzUDazWFkz6S0nBWQgIbSCQw/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;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><span style="color: black;">仅有</span>9行代码,完<span style="color: black;">成为了</span>一个简单的神经网络:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">from&nbsp;numpy&nbsp;import&nbsp;exp,&nbsp;array,&nbsp;random,&nbsp;dot</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">training_set_inputs&nbsp;=&nbsp;array([,&nbsp;,&nbsp;,&nbsp;])</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">training_set_outputs&nbsp;=&nbsp;array([]).T</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">random.seed(1)</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">synaptic_weights&nbsp;=&nbsp;2&nbsp;*&nbsp;random.random((3,&nbsp;1))&nbsp;-&nbsp;1</p><span style="color: black;">for</span>&nbsp;iteration&nbsp;<span style="color: black;">in</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;range(10000):</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;output&nbsp;=&nbsp;1&nbsp;/&nbsp;(1&nbsp;+&nbsp;exp(-(dot(training_set_inputs,&nbsp;synaptic_weights))))</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">synaptic_weights&nbsp;+=&nbsp;dot(training_set_inputs.T,&nbsp;(training_set_outputs&nbsp;-&nbsp;output)&nbsp;*&nbsp;output&nbsp;*&nbsp;(1&nbsp;-&nbsp;output))</p><span style="color: black;">print</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(1&nbsp;/&nbsp;(1&nbsp;+&nbsp;exp(-(dot(array(),&nbsp;synaptic_weights)))))</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>更长,但更<span style="color: black;">美丽</span>的源代码版本。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">目录</strong></span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">1. 什么是神经网络?</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">2. 训练过程</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">3. 计算神经元输出的方式</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">4. <span style="color: black;">调节</span>权重的公式</strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">5. 构建Python代码</strong></p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">Part1</strong>什么是神经网络?</span></h2>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>什么是神经网络?人类大脑由1000亿神经元<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><img src="https://mmbiz.qpic.cn/mmbiz_png/3iau662zwicSddcSGyYhpPWS3qJFwJ9JBegXWeSDX4oCibia8NuWZKJwOfjzEEGoLYWPUEYBbe6Xnlpwsxic3sLH9IA/640?wx_fmt=other&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1&amp;tp=webp" style="width: 50%; margin-bottom: 20px;">
    <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>数学技术——矩阵,一种网格数据。为了让它更简单<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;"><span style="color: black;">咱们</span>将训练神经网络<span style="color: black;">处理</span>下面的问题,前四个例子被<span style="color: black;">叫作</span>为训练集。你能<span style="color: black;">处理</span>这个问题吗??处是0还是1?</p><img src="https://mmbiz.qpic.cn/mmbiz_png/3iau662zwicSddcSGyYhpPWS3qJFwJ9JBe2RjjyVGtTnPLxJEibVPicoN71WFjpKwMuicdTqNgxibCrcPHdicpiatMia9xw/640?wx_fmt=other&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1&amp;tp=webp" style="width: 50%; margin-bottom: 20px;">
    <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>,答案是“?”应为1。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">Part2</strong>训练过程</span></h2>
    <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><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;">从训练集示例中获取输入,<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;">计算误差,即训练集示例中神经元输出与期望输出之间的差异。</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>权重。</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">重复这个过程10000次。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/3iau662zwicSddcSGyYhpPWS3qJFwJ9JBe5CCsBiam6NBJsSaDUhrpop33sVIibkibU1bJmDXE4I2SssP9pn9Q3zJcg/640?wx_fmt=other&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1&amp;tp=webp" style="width: 50%; margin-bottom: 20px;">
    <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>,遵循相同的模式,它应该做出一个很好的预测。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">Part3</strong>计算神经元输出的公式</span></h2>
    <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>取神经元输入的加权和,即:</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>结果在0和1之间。为此,<span style="color: black;">咱们</span><span style="color: black;">运用</span>一个数学方便的函数,<span style="color: black;">叫作</span>为Sigmoid函数:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>绘制在图上,则Sigmoid函数绘制一个S形曲线。</p><img src="https://mmbiz.qpic.cn/mmbiz_png/3iau662zwicSd4Yfg8ogWM6icUNgxfr8d1uoDyPfoQHfMGyCUCvkXmQHZG3IlrlG7YS1y9AQuuF1l2top4ELhyNRQ/640?wx_fmt=other&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1&amp;tp=webp" style="width: 50%; margin-bottom: 20px;">
    <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;">第1</span>个方程代入第二个方程,神经元输出的<span style="color: black;">最后</span>公式为:</p>Output of neuron<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>最小阈值,以保持简单。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">Part4</strong><span style="color: black;">调节</span>权重的公式</span></h2>
    <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>权重?<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>“错误加权导数”公式:</p>Adjust weights by<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>的优点是非常简单。</p>
    <h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><strong style="color: blue;">Part5</strong>构建Python代码</span></h2>
    <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>将从Python的NumPy数学库中导入四种<span style="color: black;">办法</span>。这些是:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">exp — 自然指数</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">array —构建矩阵</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">dot — <span style="color: black;">繁杂</span>矩阵</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">random — <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><span style="color: black;">能够</span><span style="color: black;">运用</span>array()<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;">training_set_inputs&nbsp;=&nbsp;array([,&nbsp;,&nbsp;,&nbsp;])</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">training_set_outputs&nbsp;=&nbsp;array([]).T</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.T函数将矩阵从水平转换为垂直, <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;"><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><span style="color: black;">这是一个用Python编写的完整工作示例:</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">from&nbsp;numpy&nbsp;import&nbsp;exp,&nbsp;array,&nbsp;random,&nbsp;dot</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">class&nbsp;NeuralNetwork():</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self):</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;设定随机数<span style="color: black;">出现</span>器,生成相同的数字</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;random.seed(1)</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;<span style="color: black;">咱们</span>构造一个三个输入和一个输出相连接的神经网络</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;将随机权重设置为3*1的矩阵,值保持在-1到1之间平均值为0</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.synaptic_weights&nbsp;=&nbsp;2&nbsp;*&nbsp;random.random((3,&nbsp;1))&nbsp;-&nbsp;1</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;Sigmoid&nbsp;函数描绘的是S形曲线</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;将<span style="color: black;">她们</span>规范化在0-1之间.</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__sigmoid(self,&nbsp;x):</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;1&nbsp;/&nbsp;(1&nbsp;+&nbsp;exp(-x))</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;"># Sigmoid函数的导数。</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;这是Sigmoid曲线的梯度。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__sigmoid_derivative(self,&nbsp;x):</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">x&nbsp;*&nbsp;(1&nbsp;-&nbsp;x)</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;<span style="color: black;">咱们</span><span style="color: black;">经过</span><span style="color: black;">实验</span>和错误的过程来训练神经网络。</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;每一次都<span style="color: black;">调节</span>权重</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;train(self,&nbsp;training_set_inputs,&nbsp;training_set_outputs,&nbsp;number_of_training_iterations):</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">for</span>&nbsp;iteration&nbsp;<span style="color: black;">in</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">range(number_of_training_iterations):</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;<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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;output&nbsp;=&nbsp;self.think(training_set_inputs)</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;计算错误(期望输出与预测输出之间的差异)。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error&nbsp;=&nbsp;training_set_outputs&nbsp;-&nbsp;output</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;<span style="color: black;">经过</span>输入乘以误差并再次乘以Sigmoid曲线的梯度。</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;这<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;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;adjustment&nbsp;=&nbsp;dot(training_set_inputs.T,&nbsp;error&nbsp;*&nbsp;self.__sigmoid_derivative(output))</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;<span style="color: black;">调节</span>权重</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">self.synaptic_weights&nbsp;+=&nbsp;adjustment</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;神经网络的思考</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;think(self,&nbsp;inputs):</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;self.__sigmoid(dot(inputs,&nbsp;self.synaptic_weights))</p><span style="color: black;">if</span>&nbsp;__name__&nbsp;==&nbsp;<span style="color: black;">"__main__"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">:</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#初始化神经网络</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">neural_network&nbsp;=&nbsp;NeuralNetwork()</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">print</span>(<span style="color: black;">"Random&nbsp;starting&nbsp;synaptic&nbsp;weights:&nbsp;"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">print</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(neural_network.synaptic_weights)</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;训练数据及.<span style="color: black;">咱们</span>有4个样本每一个有三个输入</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;和1个输出.</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">training_set_inputs&nbsp;=&nbsp;array([,&nbsp;,&nbsp;,&nbsp;])</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;training_set_outputs&nbsp;=&nbsp;array([]).T</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;<span style="color: black;">运用</span>训练数据及训练模型.</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;重复10000次,每次进行微小的<span style="color: black;">调节</span>.</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">neural_network.train(training_set_inputs,&nbsp;training_set_outputs,&nbsp;10000)</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">print</span>(<span style="color: black;">"New&nbsp;synaptic&nbsp;weights&nbsp;after&nbsp;training:&nbsp;"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">print</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(neural_network.synaptic_weights)</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">#&nbsp;用新<span style="color: black;">状况</span>对模型进行测试</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">print</span>(<span style="color: black;">"Considering&nbsp;new&nbsp;situation&nbsp;&nbsp;-&gt;&nbsp;?:&nbsp;"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">print</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(neural_network.think(array()))</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">运行结果如下:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Random&nbsp;starting&nbsp;synaptic&nbsp;weights:&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">[[-0.16595599]</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;[&nbsp;0.44064899]</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;[-0.99977125]]</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">New&nbsp;synaptic&nbsp;weights&nbsp;after&nbsp;training:&nbsp;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">[[&nbsp;9.67299303]</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;[-0.2078435&nbsp;]</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;[-4.62963669]]</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Considering&nbsp;new&nbsp;situation&nbsp;&nbsp;-&gt;&nbsp;?:</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"></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>Python<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>并预测了0.99993704。正确的答案是1。非常接近!传统的电脑程序<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>




xuaiqiang 发表于 2024-8-20 17:24:53

这夸赞甜到心里,让我感觉温暖无比。

4zhvml8 发表于 2024-10-5 12:53:00

你说得对,我们一起加油,未来可期。

nykek5i 发表于 2024-10-28 12:26:33

在遇到你之前,我对人世间是否有真正的圣人是怀疑的。

m5k1umn 发表于 2024-11-11 06:30:32

说得好啊!我在外链论坛打滚这么多年,所谓阅人无数,就算没有见过猪走路,也总明白猪肉是啥味道的。
页: [1]
查看完整版本: 9行Python代码创立一个简单神经网络