{"id":167,"date":"2026-10-04T10:32:11","date_gmt":"2026-10-04T02:32:11","guid":{"rendered":"https:\/\/99website.top\/blog\/?p=167"},"modified":"2026-10-04T10:32:11","modified_gmt":"2026-10-04T02:32:11","slug":"attentionisallyouneed-1%ef%bc%9a%e9%87%8d%e6%8b%be%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c","status":"publish","type":"post","link":"https:\/\/99website.top\/blog\/?p=167","title":{"rendered":"AttentionIsAllYouNeed #1\uff1a\u91cd\u62fe\u795e\u7ecf\u7f51\u7edc"},"content":{"rendered":"\n<h1>Attention Is All You Need &#8211; #1\uff1a\u91cd\u62fe\u795e\u7ecf\u7f51\u7edc<\/h1>\n<h3>\u5199\u5728\u524d\u9762<\/h3>\n<p>\u4eca\u5929\u53c8\u770b\u4e86\u4e00\u904dClaude Opus 5.5\u751f\u6210\u7684MV\uff0c\u771f\u7684\u8d85\u7ea7\u9707\u64bc\uff0c\u4e5f\u89c9\u5f97\u6211\u4e5f\u662f\u65f6\u5019\u8be5\u770b\u770b\u90a3\u795e\u79d8\u5df2\u4e45\u7684<code>Transformer<\/code>\u5230\u5e95\u662f\u4e2a\u5565\u4e86\uff0c\u8fd9\u4e2a\u7cfb\u5217\u7684\u535a\u5ba2\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\uff0c\u8fd9\u662f\u6211\u4e2a\u4eba\u7684\u5b66\u4e60\u7b14\u8bb0\uff0c<strong>\u4e0d\u5b8c\u5168\u6b63\u786e\uff01<\/strong>\uff0c\u8fd9\u4e0d\u662f\u6559\u7a0b\uff0c\u800c\u662f\u4e2a\u4eba\u7684\u7b14\u8bb0\uff0c\u5982\u6709\u95ee\u9898\u6b22\u8fce\u8385\u4e34\u6307\u5bfc\uff01<\/p>\n<p>\u672c\u6b21\u7684\u5b66\u4e60\u65c5\u7a0b\u7531GPT\u5927\u4eba\u5e26\u9886<\/p>\n<h2>\u795e\u7ecf\u5143 &#8211; Neuron<\/h2>\n<p><strong>\u795e\u7ecf\u5143 = \u628a\u51e0\u4e2a\u8f93\u5165\u6309\u4e0d\u540c\u91cd\u8981\u7a0b\u5ea6\u52a0\u8d77\u6765\uff0c\u518d\u7ecf\u8fc7\u4e00\u4e2a\u51fd\u6570\uff0c\u5f97\u5230\u4e00\u4e2a\u8f93\u51fa\u3002<\/strong><\/p>\n<p>\u6570\u5b66\u4e0a\u7684\u6838\u5fc3\uff1a<\/p>\n<div class=\"katex math multi-line\">\nz = w_1 x_1 + w_2 x_2 + \\cdots + w_n x_n + b\n<\/div>\n<p>\u7136\u540e<\/p>\n<div class=\"katex math multi-line\">\ny = f(z)\n<\/div>\n<p>\u53c2\u6570\u7684\u610f\u4e49\uff1a<\/p>\n<ul>\n<li>\n<p><span class=\"katex math inline\">x_i<\/span>\uff1a\u8f93\u5165<\/p>\n<\/li>\n<li>\n<p><span class=\"katex math inline\">w_i<\/span>\uff1a\u6743\u91cd\uff0c\u8868\u793a\u201c\u8fd9\u4e2a\u8f93\u5165\u6709\u591a\u91cd\u8981\u201d<\/p>\n<\/li>\n<li>\n<p><span class=\"katex math inline\">b<\/span>\uff1a\u504f\u7f6e\uff0c\u53ef\u4ee5\u7406\u89e3\u6210\u201c\u6574\u4f53\u5f80\u4e0a\/\u5f80\u4e0b\u632a\u4e00\u70b9\u201d\uff0c\u8ba9\u795e\u7ecf\u5143\u4e0d\u6b62\u80fd\u5b66\u4e60\u8fc7\u539f\u70b9\u7684\u6570\u636e<\/p>\n<\/li>\n<li>\n<p><span class=\"katex math inline\">f<\/span>\uff1a\u6fc0\u6d3b\u51fd\u6570<\/p>\n<\/li>\n<li>\n<p><span class=\"katex math inline\">y<\/span>\uff1a\u795e\u7ecf\u5143\u6700\u7ec8\u8f93\u51fa<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u795e\u7ecf\u5143\u201c\u5b66\u4e60\u201d\u7684\u4e1c\u897f\uff0c\u4e3b\u8981\u5c31\u662f\u8fd9\u4e9b <span class=\"katex math inline\">w<\/span> \u548c <span class=\"katex math inline\">b<\/span>\u3002<\/strong><\/p>\n<blockquote>\n<p>\u4e00\u4e2a\u4eba\u5de5\u795e\u7ecf\u5143\u63a5\u6536\u591a\u4e2a\u8f93\u5165 x\uff0c\n\u6bcf\u4e2a\u8f93\u5165\u4e58\u4ee5\u5bf9\u5e94\u6743\u91cd w\uff0c\n\u5c06\u7ed3\u679c\u6c42\u548c\u540e\u52a0\u4e0a\u504f\u7f6e b\uff0c\n\u6700\u540e\u7ecf\u8fc7\u6fc0\u6d3b\u51fd\u6570\uff0c\u5f97\u5230\u8f93\u51fa\u3002<\/p>\n<\/blockquote>\n<p>\u4e00\u4e2a\u6570\u5b66\u6838\u5fc3\u795e\u7ecf\u5143\uff1a<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Neuron():\n    def __init__(self, weights, bias):\n        self.weights = weights\n        self.bias = bias\n\n    def forward(self, inputs):\n        sigma = 0\n        for x, w in zip(inputs, self.weights):\n            sigma += x * w\n        sigma += self.bias\n        return sigma\n<\/code><\/pre>\n<h3>\u6fc0\u6d3b\u51fd\u6570<\/h3>\n<p>GPT\u8bf4\u73b0\u5728\u7684\u8fd9\u4e2a\u795e\u7ecf\u5143\u8fd8\u7f3a\u6fc0\u6d3b\u51fd\u6570\uff0c\u6211\u770b\u770b\u600e\u4e48\u4e2a\u4e8b<\/p>\n<p>\u6fc0\u6d3b\u51fd\u6570\u5e72\u5565\u7528\u7684<\/p>\n<p>\u6fc0\u6d3b\u51fd\u6570\u6700\u6838\u5fc3\u7684\u4f5c\u7528\u5c31\u4e00\u53e5\u8bdd\uff1a<\/p>\n<p><strong>\u7ed9\u795e\u7ecf\u7f51\u7edc\u52a0\u5165\u201c\u975e\u7ebf\u6027\u201d\u3002<\/strong><\/p>\n<p>\u4e0d\u7136\u5806\u518d\u591a\u5c42\u795e\u7ecf\u5143\uff0c\u6700\u540e\u672c\u8d28\u4e0a\u8fd8\u662f\u4e00\u4e2a\u5927\u53f7<del>\u7ebf\u6027\u51fd\u6570<\/del>\uff08GPT\u7ea0\u6b63\u8bf4\u662f\u7ebf\u6027\/\u4eff\u5c04\u53d8\u6362\uff09\u3002<\/p>\n<p>GPT\u63a8\u8350\u6211\u5b66\u4e60\u7684\u7b2c\u4e00\u4e2a\u6fc0\u6d3b\u51fd\u6570\u662f<code>ReLU<\/code>\uff0c\u4f46\u662f\u6211\u7b2c\u4e00\u6b21\u5b66\u4e60\u7684\u65f6\u5019\u7ed9\u6211\u63a8\u8350\u7684<code>sigmoid<\/code><\/p>\n<p><code>ReLU<\/code>\u957f\u8fd9\u4e2a\u6837\u5b50<\/p>\n<div class=\"katex math multi-line\">\n\\text {ReLU}(x) = \\text {max}(0,x)\n<\/div>\n<p>\u8f93\u5165\u8d1f\u6570 -&gt; \u8f93\u51fa 0<\/p>\n<p>\u8f93\u5165\u6b63\u6570 -&gt; \u539f\u6837\u8f93\u51fa<\/p>\n<h4><code>sigmoid<\/code>\u548c<code>ReLU<\/code><\/h4>\n<blockquote>\n<p><strong>Sigmoid\uff1a\u5e73\u6ed1\u3001\u8f93\u51fa (0,1)\uff0c\u4f46\u5bb9\u6613\u9971\u548c\u3002<\/strong>\n<strong>ReLU\uff1a\u7b80\u5355\u3001\u8bad\u7ec3\u65b9\u4fbf\uff0c\u5e38\u7528\u4e8e\u9690\u85cf\u5c42\u3002<\/strong><\/p>\n<\/blockquote>\n<p>GPT\u8bf4Sigmoid\u5e38\u7528\u4e8e\u4e8c\u5206\u7c7b\u8f93\u51fa\u5c42\uff0cReLU\u5e38\u7528\u4e8e\u9690\u85cf\u5c42\uff0c\u56e0\u4e3aSigmoid\u5bb9\u6613<strong>\u68af\u5ea6\u6d88\u5931<\/strong><\/p>\n<p>\u5347\u7ea7\u795e\u7ecf\u5143\uff01<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Neuron():\n    def __init__(self, weights, bias):\n        self.weights = weights\n        self.bias = bias\n\n    def ReLU(self, x):\n        return max(0, x)\n\n    def forward(self, inputs):\n        z = 0\n        for x, w in zip(inputs, self.weights):\n            z += x * w\n        z += self.bias\n\n        return self.ReLU(z)\n<\/code><\/pre>\n<h2>\u795e\u7ecf\u5143\u5c42 &#8211; Layer<\/h2>\n<p>\u4e00\u4e2a <code>Layer<\/code> \u672c\u8d28\u4e0a\u5c31\u662f\uff1a<\/p>\n<p><strong>\u5f88\u591a\u4e2a <code>Neuron<\/code> \u5e76\u6392\u653e\u5728\u4e00\u8d77\uff0c\u7136\u540e\u628a\u540c\u4e00\u4efd\u8f93\u5165\u5206\u522b\u5582\u7ed9\u6bcf\u4e2a\u795e\u7ecf\u5143\u3002<\/strong><\/p>\n<p>\u4ee3\u7801\uff01<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Layer():\n    def __init__(self, neurons):\n        self.neurons = neurons\n\n    def forward(self, inputs):\n        neurons_output = []\n        for neuron in self.neurons:\n            neurons_output.append(neuron.forward(inputs))\n\n        return neurons_output\n<\/code><\/pre>\n<p>\u540c\u4e00\u4efd <code>inputs<\/code> \u2192 \u5582\u7ed9\u8fd9\u4e00\u5c42\u7684\u6bcf\u4e2a\u795e\u7ecf\u5143 \u2192 \u6536\u96c6\u6bcf\u4e2a\u8f93\u51fa \u2192 \u8fd4\u56de\u5217\u8868\u3002<\/p>\n<h2>\u795e\u7ecf\u7f51\u7edc &#8211; Network<\/h2>\n<p>\u795e\u7ecf\u7f51\u7edc\u5c31\u662f\u8ba9\u6570\u636e\u80fd\u7a7f\u8fc7\u6bcf\u4e00\u5c42<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Network():\n    def __init__(self, layers):\n        self.layers = layers\n\n    def forward(self, inputs):\n        layers_output = inputs\n        for layer in self.layers:\n            layers_output = layer.forward(layers_output)\n\n        return layers_output\n<\/code><\/pre>\n<h2>\u8ba9\u795e\u7ecf\u5143\u5b66\u4e60<\/h2>\n<h3>Loss &#8211; \u9519\u4e86\u591a\u5c11<\/h3>\n<p>\u73b0\u5728\u7684\u8fd9\u4e2a\u795e\u7ecf\u7f51\u7edc\u53ea\u662f\u4e00\u4e2a\u6846\u67b6\uff0c\u60f3\u8ba9\u673a\u5668\u5b66\u4e60\uff0c\u5f97\u8ba9\u795e\u7ecf\u5143\u80fd\u88ab\u8bad\u7ec3<\/p>\n<p>\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\u7684\u539f\u7406\u662f<strong>\u8ba9\u7f51\u7edc\u5148\u731c\uff0c\u7136\u540e\u770b\u5b83\u9519\u4e86\u591a\u5c11\uff0c\u518d\u6839\u636e\u201c\u9519\u7684\u65b9\u5411\u201d\u4e00\u70b9\u70b9\u4fee\u6539\u6743\u91cd\u548c\u504f\u7f6e\u3002<\/strong><\/p>\n<p>\u6240\u4ee5\u73b0\u5728\u5fc5\u987b\u5177\u5907\u77e5\u9053\u9519\u8bef\u548c\u4e00\u6b65\u4e00\u6b65\u5f80\u56de\u8d70\uff0c\u4fee\u6539\u9519\u8bef\u7684\u80fd\u529b<\/p>\n<p><strong>\u524d\u5411\u4f20\u64ad \u2192 \u8ba1\u7b97\u635f\u5931 \u2192 \u53cd\u5411\u4f20\u64ad \u2192 \u66f4\u65b0\u53c2\u6570<\/strong><\/p>\n<p>\u8bad\u7ec3\u5176\u5b9e\u5c31\u662f\u4e0d\u65ad\u95ee\uff1a<\/p>\n<blockquote>\n<p>\u201c\u5982\u679c\u6211\u628a\u8fd9\u4e2a\u6743\u91cd\u7a0d\u5fae\u8c03\u5927\u4e00\u70b9\uff0cLoss \u4f1a\u53d8\u5927\u8fd8\u662f\u53d8\u5c0f\uff1f\u201d<\/p>\n<\/blockquote>\n<blockquote>\n<p>\u201c\u5982\u679c\u6211\u628a\u90a3\u4e2a bias \u8c03\u5c0f\u4e00\u70b9\uff0cLoss \u4f1a\u600e\u6837\uff1f\u201d<\/p>\n<\/blockquote>\n<p>\u67d0\u4e2a\u53c2\u6570\u53d8\u5316\u65f6 Loss \u600e\u4e48\u53d8\u5316\uff0c\u53ef\u4ee5\u7528\u5bf9\u5e94\u7684<strong>\u504f\u5bfc\u6570<\/strong>\u63cf\u8ff0\u3002\u628a\u6240\u6709\u53c2\u6570\u5bf9\u5e94\u7684\u504f\u5bfc\u6570\u7ec4\u5408\u8d77\u6765\uff0c\u5c31\u5f97\u5230<strong>\u68af\u5ea6<\/strong>\u3002<\/p>\n<p>GPT\u4e3e\u7684\u8fd9\u4e2a\u504f\u5bfc\u6570\u7684\u4f8b\u5b50\uff0c\u6211\u53bb\u6ca1\u89c1\u8fc7\u7684\u7b26\u53f7<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial w}\n<\/div>\n<p>\u5b83\u8bfb\u4f5c\uff1a<\/p>\n<p><strong>\u201c<span class=\"katex math inline\">L<\/span> \u5bf9 <span class=\"katex math inline\">w<\/span> \u7684\u504f\u5bfc\u6570\u201d<\/strong><\/p>\n<p>\u8fd9\u4e2a\u516c\u5f0f\u8868\u793a<\/p>\n<p><strong>\u6743\u91cd <span class=\"katex math inline\">w<\/span> \u53d1\u751f\u53d8\u5316\u65f6\uff0c\u635f\u5931 <span class=\"katex math inline\">L<\/span> \u7684\u53d8\u5316\u901f\u5ea6\u548c\u65b9\u5411\u3002<\/strong><\/p>\n<p>\u4eba\u8bdd\u5c31\u662f<strong><span class=\"katex math inline\">w<\/span> \u52a8\u4e00\u70b9\u65f6\uff0cLoss \u4f1a\u5f80\u54ea\u4e2a\u65b9\u5411\u53d8\u3001\u53d8\u5f97\u6709\u591a\u5feb\u3002<\/strong><\/p>\n<p>\u6211\u7684\u5988\u5440\u8fd9\u662f\u5565\u4e1c\u897f\uff0c\u8bf4\u662f\u504f\u5bfc\u6570<\/p>\n<p>\u5bfc\u6570\uff08Derivative\uff09\u662f\u63cf\u8ff0\u51fd\u6570\u5728\u67d0\u4e00\u70b9\u7684\u53d8\u5316\u7387\u7684\u6570\u5b66\u5de5\u5177\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u8bbe\u6709\u51fd\u6570 <span class=\"katex math inline\">y=f(x)<\/span>\uff0c\u5728\u67d0\u4e00\u70b9 <span class=\"katex math inline\">x0<\/span> \u5904\uff0c\u5bfc\u6570\u7684\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<div class=\"katex math multi-line\">\nf'(x_0) = \\lim_{\\Delta x \\rightarrow0} \\frac{\\Delta y}{\\Delta x} = \\lim_{\\Delta x \\rightarrow 0} \\frac{f(x_0 + \\Delta x) &#8211; f(x_0)}{\\Delta x}\n<\/div>\n<p>\u5c31\u662f\u53d8\u5316\u91cf\u8d8b\u8fd1\u4e8e0\u7684\u65f6\u5019\u7684\u659c\u7387\uff0c\u81ea\u53d8\u91cf\u53d8\u5316\u91cf\u8d8b\u8fd1\u4e8e 0 \u65f6\uff0c\u51fd\u6570\u5e73\u5747\u53d8\u5316\u7387\u7684\u6781\u9650\uff1b\u51e0\u4f55\u4e0a\u5c31\u662f\u66f2\u7ebf\u5728\u8be5\u70b9\u5207\u7ebf\u7684\u659c\u7387<\/p>\n<p>\u4e24\u4e2a\u70b9\u8d8a\u6765\u8d8a\u8fd1\uff0c\u5272\u7ebf\u6700\u540e\u8d34\u6210\u4e86\u5207\u7ebf\uff1b\u5272\u7ebf\u659c\u7387\u7684\u6781\u9650\uff0c\u5c31\u662f\u5bfc\u6570\u3002<\/p>\n<p>\uff08\u5272\u7ebf\u5c31\u662f\u66f2\u7ebf\u4e0a\u7684\u4e24\u4e2a\u70b9\u8fde\u7ebf\uff09<\/p>\n<p>\u8fd8\u6709\u8fd9\u4e2a\u957f\u5f97\u50cf6\u7684\uff0c\u6211\u67e5\u67e5<\/p>\n<p>\u90a3\u4e2a\u957f\u5f97\u50cf6\u7684\u4e1c\u897f<\/p>\n<p><span class=\"katex math inline\">\\partial<\/span>\u53eb<strong>\u504f\u5bfc\u7b26\u53f7<\/strong><\/p>\n<p>\u5728\u504f\u5bfc\u6570\u91cc\uff0c\u9664\u4e86\u76ee\u6807\u53d8\u91cf\uff0c\u5176\u4ed6\u7684\u53d8\u91cf\u88ab\u5f53\u6210\u5e38\u6570\u6765\u770b<\/p>\n<p>GPT\u5927\u4eba\u7684\u4f8b\u5b50\uff1a<\/p>\n<blockquote>\n<p>\u5047\u8bbe\u4f60\u7ad9\u5728\u4e00\u4e2a\u5c71\u5761\u4e0a\uff0c\u9ad8\u5ea6\uff1a<\/p>\n<div class=\"katex math multi-line\"> H(x,y) <\/div>\n<p>\u53d6\u51b3\u4e8e\u4f60\u4e1c\u897f\u65b9\u5411\u7684\u4f4d\u7f6e <span class=\"katex math inline\">x<\/span> \u548c\u5357\u5317\u65b9\u5411\u7684\u4f4d\u7f6e <span class=\"katex math inline\">y<\/span>\u3002<\/p>\n<p>\u90a3\u4e48\uff1a<\/p>\n<div class=\"katex math multi-line\"> \\frac{\\partial H}{\\partial x} <\/div>\n<p>\u8868\u793a\uff1a<\/p>\n<p><strong>\u6211\u53ea\u5f80\u4e1c\u897f\u65b9\u5411\u8d70\uff0c\u5761\u6709\u591a\u9661\u3002<\/strong><\/p>\n<p>\u800c\uff1a<\/p>\n<div class=\"katex math multi-line\"> \\frac{\\partial H}{\\partial y} <\/div>\n<p>\u8868\u793a\uff1a<\/p>\n<p><strong>\u6211\u53ea\u5f80\u5357\u5317\u65b9\u5411\u8d70\uff0c\u5761\u6709\u591a\u9661\u3002<\/strong><\/p>\n<p>\u4f60\u7ad9\u7684\u4f4d\u7f6e\u6ca1\u53d8\uff0c\u4f46\u4e24\u4e2a\u65b9\u5411\u7684\u5761\u5ea6\u53ef\u80fd\u5b8c\u5168\u4e0d\u4e00\u6837\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u504f\u5bfc\u6570\u3002<\/p>\n<\/blockquote>\n<p><strong>\u504f\u5bfc\u6570\u672c\u8d28\u4e0a\u4ecd\u7136\u662f\u5bfc\u6570\uff0c\u53ea\u662f\u4e13\u95e8\u5904\u7406\u591a\u53d8\u91cf\u51fd\u6570\u3002<\/strong><\/p>\n<h3>Gradient &#8211; \u68af\u5ea6<\/h3>\n<p>\u73b0\u5728\u53ef\u4ee5\u8bf4\u56de\u5230\u68af\u5ea6\u5566<\/p>\n<p>\u5047\u8bbe\u4e00\u4e2a\u795e\u7ecf\u5143\u6709\u4e24\u4e2a\u6743\u91cd\u548c\u4e00\u4e2a\u504f\u7f6e\uff1a<\/p>\n<div class=\"katex math multi-line\">\nL=L(w_1,w_2,b)\n<\/div>\n<p>\u6211\u4eec\u5206\u522b\u7b97\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial w_1}, \\qquad \\frac{\\partial L}{\\partial w_2}, \\qquad \\frac{\\partial L}{\\partial b} \n<\/div>\n<p>\u7136\u540e\u628a\u5b83\u4eec\u88c5\u5728\u4e00\u8d77\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\nabla L= \\left( \\frac{\\partial L}{\\partial w_1}, \\frac{\\partial L}{\\partial w_2}, \\frac{\\partial L}{\\partial b} \\right)\n<\/div>\n<p>\u8fd9\u4e2a\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\nabla L\n<\/div>\n<p>\u5c31\u53eb <strong>\u68af\u5ea6 gradient<\/strong>\u3002<\/p>\n<p><strong>\u68af\u5ea6\u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u5411\u91cf\uff0c\u5b83\u6307\u5411\u591a\u5143\u51fd\u6570\u503c\u589e\u52a0\u6700\u5feb\u7684\u65b9\u5411\uff0c\u5e76\u4e14\u5176\u5927\u5c0f\u8868\u793a\u53d8\u5316\u7387\u7684\u5feb\u6162<\/strong>\u3002<\/p>\n<p>\u6211\u624d\u660e\u767d\uff0c\u68af\u5ea6\u7684\u6bcf\u4e2a\u5206\u91cf\u8868\u793a\u4f60<strong>\u53ea<\/strong>\u6cbf\u7740\u8fd9\u4e2a\u5206\u91cf\u8868\u793a\u7684\u6743\u91cd\u518d\u8d70\u4e00\u70b9\u4ee5\u540e\uff0cLoss\u4f1a\u5f80\u54ea\u91cc\u53d8\uff0c\u53d8\u5f97\u5feb\u6162\uff0c\u6570\u5b57\u8d8a\u5927\u8d8a\u5feb\u3002<\/p>\n<p>\u6bd4\u5982\u4e00\u4e2a\u5206\u91cf\u662f\u6b63\u6570\uff0c\u5c31\u8bf4\u660e<strong>\u5728\u5f53\u524d\u70b9\u9644\u8fd1<\/strong>\uff0c\u8fd9\u4e2a\u6743\u91cd\u589e\u5927\u4e00\u70b9\uff0cLoss \u4e5f\u4f1a\u589e\u5927\uff1b\u6743\u91cd\u51cf\u5c0f\u4e00\u70b9\uff0cLoss \u5219\u4f1a\u51cf\u5c0f\u3002\u68af\u5ea6\u5206\u91cf\u7684\u7edd\u5bf9\u503c\u8d8a\u5927\uff0c\u8bf4\u660e Loss \u5bf9\u8fd9\u4e2a\u6743\u91cd\u7684\u53d8\u5316\u8d8a\u654f\u611f<\/p>\n<p>\u5982\u679c\u662f\u8d1f\u6570\u5462\uff0c\u5c31\u8bf4\u660e<strong>\u5728\u5f53\u524d\u70b9\u9644\u8fd1<\/strong>\uff0c\u8fd9\u4e2a\u6743\u91cd\u589e\u5927\u4e00\u70b9\uff0cLoss \u53cd\u800c\u4f1a\u51cf\u5c0f\uff1b\u6743\u91cd\u51cf\u5c0f\u4e00\u70b9\uff0cLoss \u5219\u4f1a\u589e\u5927\u3002\u540c\u6837\uff0c\u68af\u5ea6\u5206\u91cf\u7684\u7edd\u5bf9\u503c\u8d8a\u5927\uff0c\u8bf4\u660e Loss \u5bf9\u8fd9\u4e2a\u6743\u91cd\u7684\u53d8\u5316\u8d8a\u654f\u611f<\/p>\n<p>\u4e3a\u4ec0\u4e48\u662f\u5f53\u524d\u70b9\u9644\u8fd1\u5462\uff0c\u56e0\u4e3a\u5bfc\u6570\u63cf\u7ed8\u7684\u662f\u6781\u9650\uff0c\u662f\u5728\u5f53\u524d\u8fd9\u4e2a\u5730\u65b9\uff0c\u659c\u7387\u6216\u8005\u53d8\u5316\u7387\u7684\u60c5\u51b5\uff0c\u5982\u679c\u8d70\u8fdc\u4e86\u5c31\u4e0d\u4e00\u5b9a\u662f\u8fd9\u6837\u4e86<\/p>\n<p>GPT\u7684\u6bd4\u55bb\uff1a<\/p>\n<blockquote>\n<p>\u68af\u5ea6\u662f Loss \u5bf9\u6240\u6709\u53c2\u6570\u7684\u201c\u53d8\u5316\u65b9\u5411\u8bf4\u660e\u4e66\u201d\u3002<\/p>\n<\/blockquote>\n<blockquote>\n<p>\u68af\u5ea6\u4e0b\u964d\uff0c\u5c31\u662f\u6cbf\u7740 Loss \u589e\u957f\u6700\u5feb\u65b9\u5411\u7684\u53cd\u65b9\u5411\u8c03\u6574\u53c2\u6570\u3002<\/p>\n<\/blockquote>\n<p>GPT\u95ee\u6211\u5982\u4f55\u8868\u793a\u9519\u4e86\u591a\u5c11\uff0c\u6211\u56de\u7b54<\/p>\n<div class=\"katex math multi-line\">\ne = y &#8211; \\hat{y}\n<\/div>\n<p>GPT\u8bf4\u8fd9\u6837\u4f1a\u51fa\u73b0\u8d1f\u6570\uff0c\u800c\u4e14\u6837\u672c\u4f1a\u76f8\u4e92\u62b5\u6d88\uff0c\u6240\u4ee5\u5e38\u89c1\u7684\u505a\u6cd5\u662f<strong>\u5e73\u65b9<\/strong><\/p>\n<div class=\"katex math multi-line\">\nL = (y &#8211; \\hat{y})^2\n<\/div>\n<p>\u8fd9\u6837\u8fd8\u597d\u6c42\u5bfc\uff0c\u53ef\u4ee5\u7b97\u504f\u5bfc\u6570\u4e86<\/p>\n<p>GPT\u8ba9\u6211\u81ea\u5df1\u5199\u4e00\u4e0b\u504f\u5bfc\u6570\u771f\u6b63\u957f\u4ec0\u4e48\u6837\uff0c\u6211\u5199\u4e86<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial \\hat y}\n=\\lim _{h\\rightarrow 0}\\dfrac{\\left( y-\\left( \\widehat{y}+h\\right) \\right) ^{2}-\\left( y-\\widehat{y}\\right) ^{2}}{h}\n<\/div>\n<p>\u5979\u7ed9\u6211\u5c55\u5f00\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial \\hat y}\n=\n-2(y-\\hat y)\n}\n<\/div>\n<p>\u5982\u679c <span class=\"katex math inline\">\\hat y&gt;y<\/span>\uff0c\u90a3\u4e48 <span class=\"katex math inline\">\\frac{\\partial L}{\\partial\\hat y}&gt;0<\/span>\uff0c\u6cbf\u8d1f\u68af\u5ea6\u65b9\u5411\u4f1a\u8ba9\u9884\u6d4b\u503c <span class=\"katex math inline\">\\hat y<\/span> \u503e\u5411\u4e8e\u51cf\u5c0f\n\u5982\u679c <span class=\"katex math inline\">\\hat y&lt;y<\/span>\uff0c\u90a3\u4e48\u8be5\u504f\u5bfc\u4e3a\u8d1f\uff0c\u6cbf\u8d1f\u68af\u5ea6\u65b9\u5411\u4f1a\u8ba9\u9884\u6d4b\u503c <span class=\"katex math inline\">\\hat y<\/span> \u503e\u5411\u4e8e\u589e\u5927<\/p>\n<p>\u8fd9\u4e2a<code>Loss<\/code>\u516c\u5f0f\u53eb\u5e73\u65b9\u8bef\u5dee<\/p>\n<p><strong>\u62ff\u9884\u6d4b\u503c <span class=\"katex math inline\">\\hat y<\/span> \u548c\u6b63\u786e\u7b54\u6848 <span class=\"katex math inline\">y<\/span> \u6bd4\u8f83\uff0c\u7136\u540e\u7ed9\u201c\u9519\u5f97\u6709\u591a\u4e25\u91cd\u201d\u6253\u4e00\u4e2a\u5206\u3002<\/strong><\/p>\n<h3>Chain Rule- \u94fe\u5f0f\u6cd5\u5219<\/h3>\n<p>\u8fd9\u91cc\u6682\u65f6\u5ffd\u7565\u6fc0\u6d3b\u51fd\u6570<\/p>\n<p>GPT\u4e3e\u4e86\u4e2a\u6700\u7b80\u5355\u7684\u795e\u7ecf\u5143\u4f8b\u5b50<\/p>\n<div class=\"katex math multi-line\">\n\\hat{y} = wx+b\n<\/div>\n<p>\u5b83\u7684Loss\u4e3a<\/p>\n<div class=\"katex math multi-line\">\nL = (y &#8211; \\hat{y})^2\n<\/div>\n<p>\u73b0\u5728\u60f3\u77e5\u9053<span class=\"katex math inline\">w<\/span>\u52a8\u4e00\u70b9\uff0cLoss\u4f1a\u600e\u4e48\u53d8<\/p>\n<p>\u4e5f\u5c31\u662f\u504f\u5bfc\u6570<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial w}\n<\/div>\n<p>\u4f46\u662fLoss \u5e76\u4e0d\u662f\u76f4\u63a5\u7531 <span class=\"katex math inline\">w<\/span> \u7b97\u51fa\u6765\u7684\uff0c\u800c\u662f <span class=\"katex math inline\">w<\/span> \u5148\u5f71\u54cd\u9884\u6d4b\u503c <span class=\"katex math inline\">\\hat y<\/span>\uff0c\u518d\u7531 <span class=\"katex math inline\">\\hat y<\/span> \u5f71\u54cd Loss\uff0c\u6240\u4ee5\u8981\u5199\u4e24\u90e8\u5206<\/p>\n<p>\u5148\u7b97\u51faw\u548c\u5e3d\u5b50\u7b26\u53f7y\u7684\uff0c\u56e0\u4e3aw\u53d8\u4e86\u7b97\u51fa\u7684\u5e3d\u5b50\u7b26\u53f7y\u624d\u53d8\uff0c\u6240\u4ee5y\u5728\u4e0a\u5934<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial \\hat{y}}{\\partial w}\n<\/div>\n<p>\u518d\u7b97\u4e00\u4e2a\u5e3d\u5b50\u7b26\u53f7y\u548cL\u7684<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial \\hat{y}}\n<\/div>\n<p>\u6700\u540e\u4e58\u5728\u4e00\u8d77<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial w} = \\frac{\\partial L}{\\partial \\hat{y}} \\cdot \\frac{\\partial \\hat{y}}{\\partial w}\n<\/div>\n<p>\u8fd9\u5c31\u662f<strong>\u94fe\u5f0f\u6cd5\u5219<\/strong><\/p>\n<p>\u5728\u524d\u9762\u7684\u7b14\u8bb0\u91cc\u5df2\u7ecf\u7b97\u8fc7<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial \\hat{y}} = -2(y-\\hat{y}) = 2(\\hat{y}-y)\n<\/div>\n<p>\u7136\u540e\u795e\u7ecf\u5143\u5f0f\u5b50\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\hat{y} = wx+b\n<\/div>\n<p>\u5728\u504f\u5bfc\u6570\u91cc\uff0c\u54b1\u4eec\u7684\u76ee\u6807\u662fw\uff0c\u6240\u4ee5x\u548cb\u90fd\u5148\u53d8\u6210\u5e38\u6570\uff0c\u53c8\u56e0\u4e3a\u662f\u4e00\u6b21\u51fd\u6570\uff0c\u6240\u4ee5w\u7684\u659c\u7387\u5c31\u662fx<\/p>\n<p>\u90a3\u4e48\u5bf9\u4e8e\u5e3d\u5b50y\u7684\u504f\u5bfc\u6570\u5c31\u662f<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial \\hat y}{\\partial w}=x\n}\n<\/div>\n<p>\u6240\u4ee5\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{ \\frac{\\partial L}{\\partial w} = 2(\\hat y-y)x }\n<\/div>\n<p>\u540c\u7406\uff0c\u5bf9\u504f\u7f6e\u6765\u8bb2\uff0c\u56e0\u4e3a\u662f1\u6b21\u51fd\u6570\uff0c\u4e0d\u7ba1\u5728\u54ea\u91cc\u659c\u7387\u90fd\u662fk\uff0c\u53c8\u56e0\u4e3aw\u548cx\u53d8\u6210\u5e38\u6570\u4e86\uff0cb\u7cfb\u6570\u662f1\uff0c\u6240\u4ee5<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial \\hat y}{\\partial b}=1\n<\/div>\n<p>\u6240\u4ee5\u5bf9\u504f\u7f6e\u7684\u68af\u5ea6\u5c31\u662f<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial b}\n=\n2(\\hat y-y)\n}\n<\/div>\n<p>\u6743\u91cd\u548c\u504f\u7f6e\u7684\u68af\u5ea6\u90fd\u4f1a\u7b97\u4e86\uff0c\u4f46\u662fGPT\u8bf4\u54b1\u4eec\u7684\u6fc0\u6d3b\u51fd\u6570\u4e5f\u9700\u8981\u5bfc\u6570\uff0c\u56e0\u4e3a\u53cd\u5411\u4f20\u64ad\u8981\u77e5\u9053\u524d\u9762\u8fd9\u4e2a\u91cf\u52a8\u540e\u9762\u8ddf\u7740\u52a8\u591a\u5c11\uff0c\u6fc0\u6d3b\u51fd\u6570\u4f5c\u4e3a\u4f20\u64ad\u8fc7\u7a0b\u4e2d\u7684\u4e00\u5458\uff0c\u80af\u5b9a\u4e5f\u9700\u8981\u5bfc\u6570<\/p>\n<p><code>ReLU<\/code>\u7684\u5bfc\u6570\u5f88\u76f4\u89c2\uff0c\u56e0\u4e3a<code>ReLU<\/code>\u8fd9\u4e2a\u51fd\u6570\u672c\u8d28\u4e0a\u662f<\/p>\n<div class=\"katex math multi-line\">\n\\text{ReLU}(x)=\\left\\{\n\\begin{aligned}\n0 , &amp; x&lt;0\\\\\nx , &amp; x&gt;0\n\\end{aligned}\n\\right.\n<\/div>\n<p>\u6240\u4ee5\u5f53x&lt;0\u7684\u65f6\u5019\u5bfc\u6570\u4e3a0\uff0cx&gt;0\u7684\u65f6\u5019\u5bfc\u6570\u4e3a1<\/p>\n<p>\u81f3\u4e8e <span class=\"katex math inline\">x=0<\/span>\uff0c\u4e25\u683c\u6570\u5b66\u4e0a\u90a3\u91cc\u6709\u4e2a\u6298\u89d2\uff0c\u5bfc\u6570\u4e0d\u5b58\u5728\uff1b\u5b9e\u9645\u795e\u7ecf\u7f51\u7edc\u5b9e\u73b0\u91cc\u4e00\u822c\u76f4\u63a5\u7ea6\u5b9a\u6210 0 \u6216\u5176\u4ed6\u56fa\u5b9a\u503c\u3002<\/p>\n<p>\u5bf9\u4e8e\u6211\u4e4b\u524d\u5199\u7684\u795e\u7ecf\u5143\u4ee3\u7801\uff0c\u795e\u7ecf\u5143\u5728\u6fc0\u6d3b\u51fd\u6570\u524d\u4ea7\u751f\u7684\u7ed3\u679c\u53ebz\uff0c\u6240\u4ee5\u73b0\u5728\u8fd9\u4e2a\u7b80\u5355\u795e\u7ecf\u5143\u5c31\u662f<\/p>\n<div class=\"katex math multi-line\">\nz = wx+b\n<\/div>\n<p>\u800c\u901a\u8fc7\u6fc0\u6d3b\u51fd\u6570\u540e\u7684\u7ed3\u679c\u53eb\u5e3d\u5b50y\uff0c\u771f\u6b63\u6b63\u786e\u7684\u7b54\u6848\u53eby<\/p>\n<p>\u6240\u4ee5\u8981\u6c42 Loss \u5bf9\u6743\u91cd <span class=\"katex math inline\">w<\/span> \u7684\u68af\u5ea6\uff0c\u5c31\u8981\u6839\u636e\u94fe\u5f0f\u6cd5\u5219\uff0c\u628a\u8fd9\u6761\u8def\u5f84\u4e0a\u5404\u90e8\u5206\u7684\u5bfc\u6570\u4e58\u8d77\u6765\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial w}\n=\n\\frac{\\partial L}{\\partial \\hat y}\n\\cdot\n\\frac{\\partial \\hat y}{\\partial z}\n\\cdot\n\\frac{\\partial z}{\\partial w}\n<\/div>\n<p>\u5176\u4e2d<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial \\hat y}{\\partial z}\n<\/div>\n<p>\u5c31\u662f\u6fc0\u6d3b\u51fd\u6570\u7684\u5bfc\u6570<\/p>\n<h4>\u5173\u4e8eDead ReLU<\/h4>\n<p>\u4e0d\u96be\u770b\u51fa<span class=\"katex math inline\">z&lt;0<\/span> \u65f6\uff0c\u68af\u5ea6\u786e\u5b9e\u76f4\u63a5\u88ab\u4e58\u6210 0\uff0c\u56e0\u4e3aReLU\u7684\u5bfc\u6570\u4e3a0<\/p>\n<p>\u90a3\u4e3a\u4ec0\u4e48\u8fd8\u8981\u7528ReLU\u5462\uff1f<\/p>\n<p>\u5173\u952e\u5728\u4e8e<strong>\u5728 <span class=\"katex math inline\">z&gt;0<\/span> \u7684\u4e00\u6574\u7247\u533a\u57df\uff0cReLU \u7684\u5bfc\u6570\u6c38\u8fdc\u662f 1<\/strong>\uff0c\u8fd9\u786e\u4fdd\u4e86\u5728ReLU\u7684\u6b63\u533a\u57df\uff0c\u68af\u5ea6\u7ecf\u8fc7 ReLU \u8fd9\u4e00\u73af\u65f6\u4e0d\u4f1a\u88ab ReLU \u7f29\u5c0f\uff0c\u56e0\u4e3a\u6b64\u65f6\u5b83\u7684\u5bfc\u6570\u4e3a 1\uff0c\u4e581\u600e\u4e48\u4f1a\u53d8\u5462<\/p>\n<p>\u4e4b\u540e\u8fd8\u6709\u4eba\u641e\u51fa\u4e86Leaky ReLU<\/p>\n<p>\u5f53\u8f93\u5165\u4e3a\u8d1f\u6570\u65f6\uff0c\u4e0d\u8ba9 ReLU \u7684\u8f93\u51fa\u76f4\u63a5\u53d8\u6210 0\uff0c\u800c\u662f\u4fdd\u7559\u4e00\u4e2a\u5f88\u5c0f\u7684\u8d1f\u659c\u7387\u3002<\/p>\n<div class=\"katex math multi-line\">\nf(x)=\n\\begin{cases}\nx,&amp;x&gt;0\\\\\n0.01x,&amp;x&lt;0\n\\end{cases}\n<\/div>\n<h3>Backpropagation &#8211; \u53cd\u5411\u4f20\u64ad<\/h3>\n<p>\u4eca\u5929\u7684\u7b2c\u4e00\u4e2a\u76ee\u6807\uff01\u8ba999\u795e\u7ecf\u7f51\u7edc\uff08\u96fe\uff09\u5b66\u4f1aAND\uff01OR\uff01XOR\uff01<\/p>\n<p>\u65e2\u7136\u8981\u5b66AND\uff0c\u5f97\u5148\u51c6\u5907\u8bad\u7ec3\u6750\u6599<\/p>\n<p>AND\u5c31\u662f\u4e24\u4e2a\u8f93\u5165\u90fd\u662f1\u7684\u65f6\u5019\uff0c\u8f93\u51fa1\uff0c\u5176\u4ed6\u5168\u662f\u96f6\u86cb\uff0c\u8bad\u7ec3\u6750\u6599\u5f88\u7b80\u5355\uff1a<\/p>\n<div class=\"table-responsive\"><table class=\"table\">\n<thead>\n<tr>\n<th><span class=\"katex math inline\">x_1<\/span><\/th>\n<th><span class=\"katex math inline\">x_2<\/span><\/th>\n<th>\u6b63\u786e\u7b54\u6848 <span class=\"katex math inline\">y<\/span><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>1<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n<p>\u795e\u7ecf\u5143\u53d8\u6210\uff1a<\/p>\n<div class=\"katex math multi-line\">\nz = w_1 x_1 + w_2 x_2 + b\n<\/div>\n<p>\u7ecf\u8fc7<code>ReLU<\/code>\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\hat{y} = \\text{ReLU}(z)\n<\/div>\n<p>\u7136\u540e\u5e73\u65b9\u5f97\u5230Loss\uff1a<\/p>\n<div class=\"katex math multi-line\">\nL = (y &#8211; \\hat{y})^2\n<\/div>\n<p>\u610f\u4e49\u5c0f\u7b14\u8bb0\uff1a<\/p>\n<div class=\"katex math multi-line\">\ny=\\text{\u6b63\u786e\u7b54\u6848}\\\\\n\\hat y=\\text{\u795e\u7ecf\u7f51\u7edc\u9884\u6d4b}\\\\\nL=\\text{\u9884\u6d4b\u9519\u4e86\u591a\u5c11}\n<\/div>\n<h4>\u4f55\u4e3a\u53cd\u5411\u4f20\u64ad<\/h4>\n<p>\u53cd\u5411\u4f20\u64ad\u5c31\u662f\u53cd\u8fc7\u6765\u95ee\uff1a<\/p>\n<blockquote>\n<p>Loss \u4e3a\u4ec0\u4e48\u8fd9\u4e48\u5927\uff1f<\/p>\n<\/blockquote>\n<p>\u60f3\u77e5\u9053\u8fd9\u4e2a\uff0c\u5c31\u95ee\uff1a<\/p>\n<blockquote>\n<p><span class=\"katex math inline\">\\hat{y}<\/span> \u52a8\u4e00\u70b9\uff0cLoss \u4f1a\u600e\u4e48\u53d8\uff1f<\/p>\n<\/blockquote>\n<p>\u518d\u95ee\uff1a<\/p>\n<blockquote>\n<p><span class=\"katex math inline\">z<\/span> \u52a8\u4e00\u70b9\uff0c<span class=\"katex math inline\">\\hat{y}<\/span> \u4f1a\u600e\u4e48\u53d8\uff1f<\/p>\n<\/blockquote>\n<p>\u518d\u95ee\uff1a<\/p>\n<blockquote>\n<p><span class=\"katex math inline\">w_1<\/span> \u52a8\u4e00\u70b9\uff0c<span class=\"katex math inline\">z<\/span> \u4f1a\u600e\u4e48\u53d8\uff1f<\/p>\n<\/blockquote>\n<p>\u53cd\u5411\u4f20\u64ad\u5c31\u662f\u5728<strong>\u5012\u7740\u8ba1\u7b97\u6bcf\u4e00\u5c42\u7684\u53d8\u5316\u7387<\/strong><\/p>\n<p>\u4e24\u4e2a\u53c2\u6570\uff0c\u4e00\u4e2a\u4e00\u4e2a\u6765\uff0c\u5148\u6c42 <span class=\"katex math inline\">w_1<\/span> \u7684\u68af\u5ea6\uff1a<\/p>\n<p>\u5b83\u6b63\u5411\u8fd0\u7b97\u7684\u65f6\u5019\u7ecf\u8fc7\u4e86\uff1a<\/p>\n<div class=\"katex math multi-line\">\nw_1\n\\rightarrow\nz\n\\rightarrow\n\\hat y\n\\rightarrow\nL\n<\/div>\n<p>\u6240\u4ee5\u6839\u636e\u94fe\u5f0f\u6cd5\u5219\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial w_1}\n=\n\\frac{\\partial L}{\\partial\\hat y}\n\\cdot\n\\frac{\\partial\\hat y}{\\partial z}\n\\cdot\n\\frac{\\partial z}{\\partial w_1}\n}\n<\/div>\n<p>\u8fd9\u5176\u5b9e\u7279\u522b\u50cf\uff1a<\/p>\n<blockquote>\n<p><span class=\"katex math inline\">w_1<\/span> \u5bf9 <span class=\"katex math inline\">z<\/span> \u4ea7\u751f\u591a\u5c11\u5f71\u54cd\n\u00d7\n<span class=\"katex math inline\">z<\/span> \u5bf9 <span class=\"katex math inline\">\\hat y<\/span> \u4ea7\u751f\u591a\u5c11\u5f71\u54cd\n\u00d7\n<span class=\"katex math inline\">\\hat y<\/span> \u5bf9 Loss \u4ea7\u751f\u591a\u5c11\u5f71\u54cd<\/p>\n<\/blockquote>\n<p>\u4e00\u8def\u4e58\u8d77\u6765\uff1a<\/p>\n<blockquote>\n<p><strong><span class=\"katex math inline\">w_1<\/span> \u6700\u7ec8\u5bf9 Loss \u4ea7\u751f\u591a\u5c11\u5f71\u54cd\u3002<\/strong><\/p>\n<\/blockquote>\n<p>\u8fd9\u5c31\u662f\u94fe\u5f0f\u6cd5\u5219\u5728\u795e\u7ecf\u7f51\u7edc\u91cc\u7684\u6838\u5fc3\u610f\u4e49<\/p>\n<p>\u4e00\u4e2a\u4e00\u4e2a\u7b97\u5457<\/p>\n<p>Loss\u5bf9<span class=\"katex math inline\">\\hat{y}<\/span>\u7684\u5bfc\u6570\uff0c\u524d\u9762\u7b97\u8fc7\u4e86\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial\\hat y}\n=\n2(\\hat y-y)\n}\n<\/div>\n<p>\u6309\u7167\u54b1\u4eec\u7684\u8bad\u7ec3\u6750\u6599\uff0c\u5047\u8bbe<span class=\"katex math inline\">y=1<\/span>\uff0c\u4f46\u662f\u795e\u7ecf\u7f51\u7edc\u731c<span class=\"katex math inline\">y=0.6<\/span><\/p>\n<p>\u90a3\u4e48<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial\\hat y}\n=\n2(0.6-1)\n=\n-0.8\n<\/div>\n<p>\u504f\u5bfc\u6570\u4e3a\u8d1f\u7684\u610f\u5473\u7740<strong>\u5f53\u524d\u4f4d\u7f6e\uff0c\u5982\u679c<span class=\"katex math inline\">\\hat{y}<\/span>\u53d8\u5c0f\uff0cLoss\u53d8\u5927<\/strong><\/p>\n<p>\u7ee7\u7eed\u6c42<code>ReLU<\/code>\u7684\u5bfc\u6570<\/p>\n<p>\u524d\u9762\u4e5f\u8bf4\u8fc7\u4e86\uff0c\u4ed6\u7684\u5bfc\u6570\u5c31\u662f<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial\\hat y}{\\partial z}\n=\n\\begin{cases}\n1,&amp;z&gt;0\\\\\n0,&amp;z&lt;0\n\\end{cases}\n<\/div>\n<p>\u4e3a\u4e86\u65b9\u4fbf\uff0c\u53ef\u4ee5\u5199\u6210<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\text{ReLU}'(z)\n}\n<\/div>\n<p>\u6240\u4ee5<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial\\hat y}{\\partial z}=\\text{ReLU}'(z)\n}\n<\/div>\n<p>\u6700\u540e\u6c42<span class=\"katex math inline\">z<\/span> \u5bf9 <span class=\"katex math inline\">w_1<\/span> \u7684\u5bfc\u6570<\/p>\n<p>\u795e\u7ecf\u5143\u516c\u5f0f\uff1a<\/p>\n<div class=\"katex math multi-line\">\nz = w_1 x_1 + w_2 x_2 + b\n<\/div>\n<p>\u8981\u6c42\u7684\u504f\u5bfc\u6570\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial z}{\\partial w_1}\n<\/div>\n<p>\u6240\u4ee5\u628a\u9664\u4e86<span class=\"katex math inline\">w_1<\/span>\u7684\u53d8\u91cf\u5168\u770b\u6210\u5e38\u6570\uff0c\u5f0f\u5b50\u4e5f\u5c31\u53d8\u6210\u4e86\u4ee5<span class=\"katex math inline\">w_1<\/span>\u4e3a\u81ea\u53d8\u91cf\u7684\u4e00\u6b21\u51fd\u6570\uff0c\u4e00\u6b21\u51fd\u6570\u6c42\u5bfc\u7ed3\u679c\u4e3a\u659c\u7387\uff0c\u6240\u4ee5\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial z}{\\partial w_1} = x_1\n}\n<\/div>\n<p>\uff01\uff01\uff01\u5728\u8fd9\u91ccGPT\u8ddf\u6211\u8bf4\uff0c\u5982\u679c<\/p>\n<div class=\"katex math multi-line\">\nx_1 = 1\n<\/div>\n<p>\u90a3\u4e48<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial z}{\\partial w_1} = 1\n<\/div>\n<p>\u8868\u793a\u5982\u679c <span class=\"katex math inline\">w_1<\/span> \u589e\u52a00.1, \u90a3 <span class=\"katex math inline\">z<\/span> \u4e5f\u589e\u52a00.1<\/p>\n<p>\u4f46\u662f\u5982\u679c<\/p>\n<div class=\"katex math multi-line\">\nx_1 = 0\n<\/div>\n<p>\u90a3\u4e48<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial z}{\\partial w_1} = 0\n<\/div>\n<p>\u4e5f\u5c31\u610f\u5473\u7740 <span class=\"katex math inline\">w_1<\/span> \u600e\u4e48\u53d8\uff0c<span class=\"katex math inline\">z<\/span> \u90fd\u4e0d\u4f1a\u53d8\uff0c\u4e5f\u5c31\u662f\u8bf4\u660e<strong>\u8fd9\u4e00\u6761\u8bad\u7ec3\u6570\u636e\u4e0d\u4f1a\u4fee\u6539 <span class=\"katex math inline\">w_1<\/span><\/strong><\/p>\n<p>\u8fd9\u4e2a\u5f88\u91cd\u8981<\/p>\n<p>\u6700\u540e\u8d85\u7ea7\u62fc\u88c5\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial w_1}\n=\n2(\\hat y-y)\n\\operatorname{ReLU}'(z)\nx_1\n}\n<\/div>\n<p>\u8fd9\u5c31\u662f  <strong><span class=\"katex math inline\">w_1<\/span> \u7684\u68af\u5ea6<\/strong> <\/p>\n<p><span class=\"katex math inline\">w_2<\/span> \u540c\u7406\uff1a<\/p>\n<p>\u6240\u4ee5\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{ \\frac{\\partial L}{\\partial w_2} = 2(\\hat y-y) \\operatorname{ReLU}'(z) x_2 }\n<\/div>\n<p>\u6ce8\u610f\u5230\u96f7\u9706\u89c4\u5f8b\uff0c\u6743\u91cd\u68af\u5ea6\u516c\u5f0f\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial w_i}\n=\n2(\\hat y-y)\n\\operatorname{ReLU}'(z)\nx_i\n}\n<\/div>\n<p>\u6709\u51e0\u4e2a\u8f93\u5165\u5c31\u6709\u51e0\u4e2a <span class=\"katex math inline\">w_i<\/span><\/p>\n<h5>\u504f\u7f6e\u600e\u4e48\u7b97\uff1f<\/h5>\n<p>\u795e\u7ecf\u5143\u516c\u5f0f\uff1a<\/p>\n<div class=\"katex math multi-line\">\nz=w_1x_1+w_2x_2+b\n<\/div>\n<p>\u54b1\u4eec\u5148\u6c42<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial z}{\\partial b}\n<\/div>\n<p>\u9664\u4e86b\uff0c\u90fd\u5f53\u6210\u5e38\u6570<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial z}{\\partial b}=1\n}\n<\/div>\n<p>\u90a3\u4e48<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial b}\n=\n\\frac{\\partial L}{\\partial\\hat y}\n\\frac{\\partial\\hat y}{\\partial z}\n\\frac{\\partial z}{\\partial b}\n<\/div>\n<p>\u6240\u4ee5\u6700\u540e\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial b}\n=\n2(\\hat y-y)\n\\operatorname{ReLU}'(z)\n}\n<\/div>\n<hr>\n<p>\u5b8c\u6574\u68af\u5ea6\uff1a<\/p>\n<p>\u5bf9\u4e8e\uff1a<\/p>\n<div class=\"katex math multi-line\">\nz=w_1x_1+w_2x_2+b\n<\/div>\n<div class=\"katex math multi-line\">\n\\hat y=\\operatorname{ReLU}(z)\n<\/div>\n<div class=\"katex math multi-line\">\nL=(y-\\hat y)^2\n<\/div>\n<p>\u6211\u4eec\u6709\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{ \\frac{\\partial L}{\\partial w_1} = 2(\\hat y-y)\\operatorname{ReLU}'(z)x_1 }\n<\/div>\n<div class=\"katex math multi-line\">\n\\boxed{ \\frac{\\partial L}{\\partial w_2} = 2(\\hat y-y)\\operatorname{ReLU}'(z)x_2 }\n<\/div>\n<div class=\"katex math multi-line\">\n\\boxed{ \\frac{\\partial L}{\\partial b} = 2(\\hat y-y)\\operatorname{ReLU}'(z) } \n<\/div>\n<p>\u8fd9\u4e09\u4e2a\u6570\u5c31\u662f\u53cd\u5411\u4f20\u64ad\u7b97\u51fa\u6765\u7684\u4e1c\u897f<\/p>\n<hr>\n<h3>Gradient Descent &#8211; \u68af\u5ea6\u4e0b\u964d<\/h3>\n<p>\u8981\u8ba9\u795e\u7ecf\u5143\u771f\u6b63\u5b66\u4e60\uff0c\u80af\u5b9a\u9700\u8981\u4fee\u6539\u6743\u91cd\u5566<\/p>\n<p>\u4e4b\u524d\u54b1\u4eec\u8bf4\u5230\uff0c\u6743\u91cd\u8d8a\u6309\u7167\u68af\u5ea6\u7684\u65b9\u5411\u53d8\u5316\uff0cLoss\u5c31\u8d8a\u6765\u8d8a\u5927<\/p>\n<p>\u4f46\u662f\u54b1\u4eec\u9700\u8981\u8ba9Loss\u53d8\u5c0f\uff0c\u56e0\u4e3aLoss\u7531\u8bef\u5dee\u8ba1\u7b97\u800c\u6765<\/p>\n<p>\u6240\u4ee5\u54b1\u4eec\u8981\u53cd\u5176\u9053\u800c\u884c\u4e4b\uff0c\u6309\u7167\u68af\u5ea6\u76f8\u53cd\u7684\u65b9\u5411\u8d70\uff0c\u4e5f\u5c31\u662f<strong>\u505a\u51cf\u6cd5<\/strong><\/p>\n<p>\u68af\u5ea6\u4e0b\u964d\uff1a<\/p>\n<div class=\"katex math multi-line\">\nw_{\\text{new}}\n=\nw_{\\text{old}}\n&#8211;\n\\eta\n\\frac{\\partial L}{\\partial w}\n<\/div>\n<p>\u5176\u4e2d\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\eta\n<\/div>\n<p>\u5c31\u662f<code>learning rate<\/code>\uff0c\u5b66\u4e60\u7387<\/p>\n<p>\u5b66\u4e60\u7387\u662f\u8bad\u7ec3\u8005\u6307\u5b9a\u7684<\/p>\n<p>\u5b83\u7684\u5927\u5c0f\u5224\u65ad\u65b9\u6cd5\u5f88\u76f4\u89c2\uff1a<\/p>\n<ul>\n<li><strong>Loss \u7a33\u5b9a\u4e0b\u964d<\/strong>\uff1a\u8bf4\u660e\u8fd9\u4e2a\u5b66\u4e60\u7387\u5927\u6982\u7387\u8fd8\u884c<\/li>\n<li><strong>Loss \u4e00\u4f1a\u513f\u5927\u4e00\u4f1a\u513f\u5c0f\uff0c\u75af\u72c2\u9707\u8361<\/strong>\uff1a\u5b66\u4e60\u7387\u592a\u5927\uff0c\u6bd4\u5982 <code>0.1<\/code> \u53ef\u80fd\u5c31\u8fc8\u8fc7\u5934\u4e86<\/li>\n<li><strong>Loss \u964d\u5f97\u7279\u522b\u7279\u522b\u6162<\/strong>\uff1a\u5b66\u4e60\u7387\u53ef\u80fd\u592a\u5c0f\uff0c\u6bd4\u5982 <code>0.000001<\/code><\/li>\n<li><strong>Loss \u76f4\u63a5\u7206\u70b8\u3001\u53d8\u6210\u5de8\u5927\u6570\u5b57\u751a\u81f3 NaN<\/strong>\uff1a\u901a\u5e38\u4e5f\u662f\u5b66\u4e60\u7387\u8fc7\u5927<\/li>\n<\/ul>\n<p>\u5047\u8bbe\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\eta = 0.1\n<\/div>\n<p>\u90a3\u4e48\uff1a<\/p>\n<div class=\"katex math multi-line\">\nw_1\n\\leftarrow\nw_1\n&#8211;\n0.1\n\\frac{\\partial L}{\\partial w_1}\\\\\n\\\\\nw_2\n\\leftarrow\nw_2\n&#8211;\n0.1\n\\frac{\\partial L}{\\partial w_2}\\\\\n\\\\\nb\n\\leftarrow\nb\n&#8211;\n0.1\n\\frac{\\partial L}{\\partial b}\n<\/div>\n<h4>\u624b\u7b97\u4e00\u904d\uff01<\/h4>\n<p>GPT\u8bf4\uff1a\u54b1\u4eec\u73b0\u5728\u4e0d\u5199\u4e00\u884c\u4ee3\u7801\uff01\u4f60\u7ed9\u6211\u624b\u7b97\uff01<\/p>\n<p>\u7136\u540e\u7ed9\u51fa\u795e\u7ecf\u5143\u521d\u59cb\u53c2\u6570\uff1a<\/p>\n<div class=\"katex math multi-line\">\nw_1=0.2\\\\\nw_2=0.3\\\\\nb=0.1\n<\/div>\n<p>\u5b66\u4e60\u73870.1<\/p>\n<p>\u73b0\u5728\u5582\u8fdb\u53bb AND \u7684\u8fd9\u4e00\u6761\u6570\u636e\uff1a<\/p>\n<div class=\"katex math multi-line\">\n[x_1,x_2]=[1,1]\n<\/div>\n<p>\u6b63\u786e\u7b54\u6848\uff1a<\/p>\n<div class=\"katex math multi-line\">\ny=1\n<\/div>\n<p>\u7528\u6570\u5b66\u81ea\u5df1\u6309\u7167\u4e0b\u9762\u8fd9\u4e2a\u987a\u5e8f\u7b97\uff1a\n1. \u7b97<\/p>\n<div class=\"katex math multi-line\">\nz=w_1x_1+w_2x_2+b\n<\/div>\n<ol>\n<li>\u7b97<\/li>\n<\/ol>\n<div class=\"katex math multi-line\">\n\\hat y=\\operatorname{ReLU}(z)\n<\/div>\n<ol>\n<li>\u7b97<\/li>\n<\/ol>\n<div class=\"katex math multi-line\">\nL=(y-\\hat y)^2\n<\/div>\n<ol>\n<li>\u7b97<\/li>\n<\/ol>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial\\hat y}\n=\n2(\\hat y-y)\n<\/div>\n<ol>\n<li>\u5224\u65ad<\/li>\n<\/ol>\n<div class=\"katex math multi-line\">\n\\operatorname{ReLU}'(z)\n<\/div>\n<p>\u662f\u591a\u5c11\n6. \u7b97<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial z}{\\partial w_1}=x_1\n<\/div>\n<div class=\"katex math multi-line\">\n\\frac{\\partial z}{\\partial w_2}=x_2\n<\/div>\n<div class=\"katex math multi-line\">\n\\frac{\\partial z}{\\partial b}=1\n<\/div>\n<ol>\n<li>\u6700\u540e\u7b97\uff1a<\/li>\n<\/ol>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial w_1}\n<\/div>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial w_2}\n<\/div>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial b}\n<\/div>\n<ol>\n<li>\u518d\u66f4\u65b0\uff1a<\/li>\n<\/ol>\n<div class=\"katex math multi-line\">\nw_1&#8242;\n=\nw_1-\\eta\\frac{\\partial L}{\\partial w_1}\n<\/div>\n<div class=\"katex math multi-line\">\nw_2&#8242;\n=\nw_2-\\eta\\frac{\\partial L}{\\partial w_2}\n<\/div>\n<div class=\"katex math multi-line\">\nb&#8217;\n=\nb-\\eta\\frac{\\partial L}{\\partial b}\n<\/div>\n<p>\u5f00\u59cb\u7b97\uff01\uff01\uff01<\/p>\n<div class=\"katex math multi-line\">\nz=0.2+0.3+0.1=0.6\\\\\n\\\\\n\\widehat{y}=ReLU\\left( z\\right) =0.6\\\\\n\\\\\nL=\\left( y-\\widehat{y}\\right) ^{2}=0.16\\\\\n\\\\\n\\dfrac{\\partial L}{\\partial \\widehat{y}}=-0.8\\\\\n\\\\\nReLU&#8217;\\left( z\\right) =1\\\\\n\\\\\n\\dfrac{\\partial z}{\\partial w_{1}}=x_{1}=1\\\\\n\\\\\n\\dfrac{\\partial z}{\\partial w_{2}}=x_{2}=1\\\\\n\\\\\n\\dfrac{\\partial L}{\\partial b}=\\dfrac{\\partial L}{\\partial \\widehat{y}}\\cdot \\dfrac{\\partial \\widehat{y}}{\\partial z}\\cdot \\dfrac{\\partial z}{\\partial b}=-0.8\\\\\n\\\\\n\\dfrac{\\partial L}{\\partial w_{1}}=\\dfrac{\\partial L}{\\partial \\widehat{y}}\\cdot \\dfrac{\\partial \\widehat{y}}{\\partial z}\\cdot \\dfrac{\\partial z}{\\partial w_{1}}=-0.8\\\\\n\\\\\n\\dfrac{\\partial L}{\\partial w_{2}}=\\dfrac{\\partial L}{\\partial \\widehat{y}}\\cdot \\dfrac{\\partial \\widehat{y}}{\\partial z}\\cdot \\dfrac{\\partial z}{\\partial w_{2}}=-0.8\\\\\n\\\\\nw_{1}&#8217;=w_{1}-\\eta \\dfrac{\\partial L}{\\partial w_{1}}=0.2+0.08=0.28\\\\\n\\\\\nw_{2}&#8217;=w_{2}-\\eta \\dfrac{\\partial L}{\\partial w_{2}}=0.3+0.08=0.38\\\\\n\\\\\nb&#8217;=b-\\eta \\dfrac{\\partial L}{\\partial b}=0.1+0.08=0.18\n<\/div>\n<p>\u597d\u8036\uff01\uff01\u7b97\u51fa\u6765\u5566\uff01<\/p>\n<p>\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u7ed9python\u4ee3\u7801\u91cc\u7684\u795e\u7ecf\u5143\u5199<code>backward()<\/code>\u5566~<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Neuron():\n    def __init__(self, weights, bias):\n        self.weights = weights\n        self.bias = bias\n\n    def ReLU(self, x):\n        return max(0, x)\n\n    def forward(self, inputs):\n        self.inputs = inputs #\u4fdd\u5b58\u8f93\u5165\uff0cbackward\u65f6\u8fd8\u5f97\u7528\n        z = 0\n        for x, w in zip(inputs, self.weights):\n            z += x * w\n        z += self.bias\n\n        self.z = z #\u4fdd\u5b58z\n        self.haty = self.ReLU(z) # \u4fdd\u5b58haty\n\n        return self.haty\n\n    def ReLU_derivative(self, z):\n        if z &gt; 0:\n            return 1\n        else:\n            return 0\n\n    def backward(self, y, learning_rate):\n        # \u9700\u8981\u771f\u6b63\u7684\u7ed3\u679cy\uff0c\u8fd8\u6709\u5b66\u4e60\u7387eta\n        dL_dhaty = 2*(self.haty-y)\n        dhaty_dz = self.ReLU_derivative(self.z)\n        dL_dz = dL_dhaty * dhaty_dz\n\n        #\u66f4\u65b0\u6743\u91cd\n        for i in range(len(self.weights)):\n            dz_dwi = self.inputs[i]\n            dL_dw = dL_dz * dz_dwi\n\n            self.weights[i] -= learning_rate * dL_dw\n\n        #\u66f4\u65b0\u504f\u7f6e\n        dz_db = 1\n        dL_db = dL_dz * dz_db\n\n        self.bias -= learning_rate * dL_db\n\n<\/code><\/pre>\n<p>\u8fd9\u5c31\u662f\u5355\u4e2a\u795e\u7ecf\u5143\u7684<code>backward()<\/code><\/p>\n<p>\u8dd1\u4e00\u4e2a\u8bad\u7ec3\u8bd5\u8bd5\u770b\uff01<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">neuron = Neuron([0.5,0.5],0.5) #\u5b9e\u4f8b\u5316\u4e00\u4e2a\u795e\u7ecf\u5143~\u7136\u540e\u7ed9\u6240\u6709\u6743\u91cd\u7ed9\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u5c31\u8bbe\u4e3a0.5\u5427\ntrain_data = [\n    (1,1,1),\n    (0,1,0),\n    (1,0,0),\n    (0,0,0)\n]\n\nfor epoch in range(30000):\n    for sample in range(4):\n        neuron.forward([train_data[sample][0],train_data[sample][1]])\n        neuron.backward(train_data[sample][2], 0.01)\n\nfor sample in range(4):\n    print(neuron.forward([train_data[sample][0],train_data[sample][1]]))\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-powershell\">python -u &quot;j:\\99\u7684\u535a\u5ba2\\AttentionIsAllYouNeed\\MLP.py&quot;\n0.999999999999992\n2.6645352591003757e-15\n2.6645352591003757e-15\n0\n<\/code><\/pre>\n<p>\u6210\u529f\u4e86\uff01\uff01\uff01\u5355\u4e2a\u795e\u7ecf\u5143\u8bad\u7ec3\u6210\u529f\uff01\uff01\uff01*\u5f00\u82b1<\/p>\n<p>\u4f46\u662f\u5982\u679c\u6269\u5927\u5230\u4e00\u5c42\u795e\u7ecf\u5143\u7684\u8303\u56f4\u5462<\/p>\n<p>\u4e00\u5c42\u5f53\u4e2d\u6bcf\u4e00\u4e2a\u795e\u7ecf\u5143\u90fd\u6709\u81ea\u5df1\u7684\u6743\u91cd\uff0c\u76f8\u5bf9\u5e94\u7684\uff0c<strong>\u4e00\u4e2a\u8f93\u5165 <span class=\"katex math inline\">x_i<\/span> \u4f1a\u540c\u65f6\u8fde\u5230\u8fd9\u4e00\u5c42\u91cc\u7684\u591a\u4e2a\u795e\u7ecf\u5143<\/strong> <\/p>\n<p><strong>\u6240\u4ee5\u53cd\u5411\u4f20\u64ad\u65f6\uff0c\u8fd9\u4e9b\u795e\u7ecf\u5143\u4f20\u56de\u6765\u7684\u68af\u5ea6\u8981\u52a0\u8d77\u6765\u3002<\/strong><\/p>\n<p>\u5047\u8bbe\u8fd9\u4e00\u5c42\u6709\u4e09\u4e2a\u795e\u7ecf\u5143\uff1a<\/p>\n<div class=\"katex math multi-line\">\na_1=\\operatorname{ReLU}(z_1)\\\\\na_2=\\operatorname{ReLU}(z_2)\\\\\na_3=\\operatorname{ReLU}(z_3)\\\\\n<\/div>\n<p>\u6bcf\u4e2a\u795e\u7ecf\u5143\u90fd\u6709\u81ea\u5df1\u7684\u4e00\u5957\u6743\u91cd\uff0c\u5bf9\u5e94\u8f93\u5165<\/p>\n<div class=\"katex math multi-line\">\nz_1=w_{11}x_1+w_{12}x_2+b_1\\\\\nz_2=w_{21}x_1+w_{22}x_2+b_2\\\\\nz_3=w_{31}x_1+w_{32}x_2+b_3\n<\/div>\n<p>\u6765\u770b\u73b0\u5728\u5c1a\u672a\u4fee\u6539\u7684<code>Layer<\/code>\u7c7b\u7684\u4ee3\u7801<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Layer():\n    def __init__(self, neurons):\n        self.neurons = neurons\n\n    def forward(self, inputs):\n        neurons_output = []\n        for neuron in self.neurons:\n            neurons_output.append(neuron.forward(inputs))\n\n        return neurons_output\n<\/code><\/pre>\n<p>\u628a\u8f93\u5165\u5206\u522b\u4f20\u5165\u4e00\u5c42\u5f53\u4e2d\u7684\u6240\u6709\u795e\u7ecf\u5143\uff0c\u5e76\u4e14\u83b7\u5f97\u6bcf\u4e00\u4e2a\u795e\u7ecf\u5143\u7684\u8f93\u51fa<\/p>\n<p>\u65e2\u7136\u4e00\u5c42\u4e0d\u6b62\u4e00\u4e2a\u795e\u7ecf\u5143\uff0c\u90a3Layer\u6536\u5230\u7684\u81ea\u7136\u4e5f\u5c31\u4e0d\u53ea\u6709\u4e00\u4e2a\u68af\u5ea6<\/p>\n<p>\u90a3\u5f53\u53cd\u5411\u4f20\u64ad\u7684\u65f6\u5019\uff0c\u53ea\u9700\u8981\u5206\u522b\u544a\u8bc9\u8fd9\u4e00\u5c42\u7684\u6bcf\u4e2a\u795e\u7ecf\u5143\uff1a<\/p>\n<p><strong>Loss \u5bf9\u8fd9\u4e2a\u795e\u7ecf\u5143\u8f93\u51fa\u7684\u68af\u5ea6\u662f\u591a\u5c11\u3002<\/strong><\/p>\n<p>\u5047\u8bbe\u8fd9\u4e00\u5c42\u4e09\u4e2a\u795e\u7ecf\u5143\u7684\u8f93\u51fa\u5206\u522b\u4e3a\uff1a<\/p>\n<div class=\"katex math multi-line\">\na_1,\\quad a_2,\\quad a_3\n<\/div>\n<p>\u4ece\u540e\u9762\u4f20\u56de\u6765\u7684\u5e94\u8be5\u662f\u4e00\u7ec4\u68af\u5ea6\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\left[\n\\frac{\\partial L}{\\partial a_1},\n\\frac{\\partial L}{\\partial a_2},\n\\frac{\\partial L}{\\partial a_3}\n\\right]\n}\n<\/div>\n<p>\u6bcf\u4e2a\u795e\u7ecf\u5143\u6536\u5230\u68af\u5ea6\u4ee5\u540e\uff0c\u7b97\u6cd5\u8ddf\u5355\u4e2a\u795e\u7ecf\u5143\u7684<code>backward()<\/code>\u7b97\u6cd5\u662f\u4e00\u6837\u7684<\/p>\n<p>\u53ea\u4e0d\u8fc7\u4ece<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial z}\n=\n\\frac{\\partial L}{\\partial\\hat y}\nReLU'(z)\n<\/div>\n<p>\u53d8\u6210<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial z_1}\n=\n\\frac{\\partial L}{\\partial a_1} ReLU'(z_1)\n}\n<\/div>\n<p>\u56e0\u4e3a\u8fd9\u4e00\u7ec4\u91cc\u7684\u6bcf\u4e2a\u68af\u5ea6\u5e76\u4e0d\u662f\u76f4\u63a5\u4ece\u6700\u7ec8\u7ed3\u679c <span class=\"katex math inline\">\\hat{y}<\/span> \u5f97\u5230\u7684\uff0c\u800c\u662f\u4ece\u524d\u9762\u4e00\u5c42\u7684\u795e\u7ecf\u5143\u5f97\u5230\u7684\u68af\u5ea6\uff0c\u56e0\u4e3a\u94fe\u5f0f\u6548\u5e94\u662f\u4e58\u6cd5\uff0c\u6240\u4ee5\u5728\u8fd9\u91cc\u66ff\u6362\u6ca1\u6709\u95ee\u9898<\/p>\n<p>\u795e\u7ecf\u5143\u7ecf\u8fc7<code>backward()<\/code>\u4e4b\u540e\uff0c\u6210\u529f\u4fee\u6539\u4e86\u81ea\u5df1\u7684\u6743\u91cd\uff0c\u597d\u8036\uff01<\/p>\n<p>\u4f46\u662f\u95ee\u9898\u6765\u4e86\uff0c\u5b83\u600e\u4e48\u628a\u81ea\u5df1\u7684\u68af\u5ea6\u7ed9\u4e0a\u4e00\u5c42\u5462\uff1f\u4ed6\u600e\u4e48\u8ddf\u4e0a\u4e00\u5c42\u8bf4\uff0c\u4ed6\u4eec\u7684\u8f93\u51fa\u5bf9\u6700\u7ec8\u7684Loss\u6709\u591a\u5927\u5f71\u54cd\u5462\uff1f<\/p>\n<p>\u987a\u6c34\u63a8\u821f\u5199\u5230<code>Network<\/code><\/p>\n<p>\u65e2\u7136\u6709\u591a\u5c42\u4e86\uff0c\u6bcf\u4e2a\u5c42\u7684\u5206\u5de5\u4e5f\u5c31\u53d8\u5f97\u4e0d\u540c<\/p>\n<p>\u6700\u4e00\u5f00\u59cb\u63a5\u53d7\u8f93\u5165\u7684\u5c42\u53eb\u8f93\u5165\u5c42<\/p>\n<p>\u6700\u540e\u8f93\u51fa\u4e1c\u897f\u7ed9\u4eba\u7c7b\u770b\u7684\uff0c\u53eb\u8f93\u51fa\u5c42<\/p>\n<p>\u4e2d\u95f4\u7684\u795e\u7ecf\u5143\u5c42\u53eb\u9690\u85cf\u5c42<\/p>\n<p>\u95ee\u9898\u6765\u4e86\uff1a<\/p>\n<p><strong><span class=\"katex math inline\">x_1<\/span> \u5f71\u54cd\u4e86\u51e0\u4e2a\u795e\u7ecf\u5143\uff1f<\/strong><\/p>\n<p>\u4e09\u4e2a\uff01<\/p>\n<p>\u56e0\u4e3a\u6b63\u5411\u8fd0\u7b97\u65f6 <span class=\"katex math inline\">x_1<\/span> \u540c\u65f6\u7ed9\u4e09\u4e2a\u795e\u7ecf\u5143\u4f20\u5165\u4e86\u8f93\u5165\uff0c\u6240\u4ee5 <span class=\"katex math inline\">x_1<\/span> \u81ea\u7136\u901a\u8fc7\u4e09\u6761\u8def\u5f71\u54cd\u6700\u7ec8Loss<\/p>\n<p>\u4e8e\u662f Loss \u5bf9 <span class=\"katex math inline\">x_1<\/span> \u7684\u603b\u5f71\u54cd\u5fc5\u987b\u628a\u4e09\u6761\u8def<strong>\u52a0\u8d77\u6765<\/strong>\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial x_1}\n=\n\\left(\\text{\u8def\u5f841\u7684\u68af\u5ea6}\\right)\n+\n\\left(\\text{\u8def\u5f842\u7684\u68af\u5ea6}\\right)\n+\n\\left(\\text{\u8def\u5f843\u7684\u68af\u5ea6}\\right)\n}\n<\/div>\n<p>\u5168\u90e8\u52a0\u8d77\u6765<\/p>\n<p>\u6bcf\u4e00\u6761\u8def\u5f84\u600e\u4e48\u7b97\u5462\uff1f<\/p>\n<p>\u5bf9\u4e8e\u4e00\u5c42\u7684\u5355\u4e2a\u795e\u7ecf\u5143\uff0c\u53c2\u6570\u5230\u6743\u91cd\u5230Loss\u7684\u8def\u5f84\u5982\u4e0b\uff08\u7528\u793a\u4f8b\u5c42\u7684\u7b2c\u4e00\u4e2a\u795e\u7ecf\u5143\u4e3e\u4f8b\u5b50\uff09\uff1a<\/p>\n<div class=\"katex math multi-line\">\nx_1\\rightarrow z_1\\rightarrow  L\n<\/div>\n<p>\u6ce8\u610f\u8fd9\u91cc\u7684 <span class=\"katex math inline\">x_1<\/span> \u662f\u4e0a\u4e00\u5c42\u7684\u8f93\u51fa\u5206\u53d1\u7ed9\u6bcf\u4e2a\u795e\u7ecf\u5143\uff0c\u53d8\u6210\u8fd9\u4e00\u5c42\u6bcf\u4e2a\u795e\u7ecf\u5143\u7684\u8f93\u5165\u7684<\/p>\n<p>\u90a3\u4e48\u53cd\u5411\u4f20\u64ad\u65f6\uff0c\u8fd9\u4e00\u5c42\u7684\u8f93\u5165\uff0c\u5c31\u662f\u4e0a\u4e00\u5c42\u7684\u8f93\u51fa\uff0c\u6240\u4ee5backward\u8981\u8fd4\u56deLoss\u5bf9\u8fd9\u4e00\u5c42\u6bcf\u4e2a\u8f93\u5165\u7684\u68af\u5ea6\uff0c\u4e5f\u5c31\u76f8\u5f53\u4e8e\u4e0a\u4e00\u5c42\u6bcf\u4e2a\u795e\u7ecf\u5143\u8f93\u51fa\u7684\u68af\u5ea6<\/p>\n<p>\u6240\u4ee5\u5bf9\u5e94\u7684\u504f\u5bfc\u6570\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial L}{\\partial x_1}\n\\Bigg|_{\\text{\u795e\u7ecf\u51431}}\n=\n\\frac{\\partial L}{\\partial z_1}\n\\cdot\n\\frac{\\partial z_1}{\\partial x_1}\n<\/div>\n<p>\u800c\uff1a<\/p>\n<div class=\"katex math multi-line\">\nz_1=w_{11}x_1+w_{12}x_2+b_1\n<\/div>\n<p>\u6240\u4ee5\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\frac{\\partial z_1}{\\partial x_1} = w_{11}\n<\/div>\n<p>\u56e0\u6b64\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\text{\u795e\u7ecf\u51431\u4f20\u7ed9 }x_1\\text{ \u7684\u68af\u5ea6}\n=\n\\frac{\\partial L}{\\partial z_1}w_{11}\n}\n<\/div>\n<p>\u5176\u4ed6\u7684\u795e\u7ecf\u5143\u4ee5\u6b64\u7c7b\u63a8<\/p>\n<p>\u6240\u4ee5\u5bf9\u4e8e\u4e00\u5c42\u7684\u4e00\u4e2a\u8f93\u5165 <span class=\"katex math inline\">x_1<\/span>\uff0c\u603b\u5171\u7684\u516c\u5f0f\u662f\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\frac{\\partial L}{\\partial x_1}\n=\n\\frac{\\partial L}{\\partial z_1}w_{11}\n+\n\\frac{\\partial L}{\\partial z_2}w_{21}\n+\n\\frac{\\partial L}{\\partial z_3}w_{31}\n}\n<\/div>\n<p>\u4e3a\u4ec0\u4e48\u5fc5\u987b\u201c\u52a0\u8d77\u6765\u201d\uff1f<\/p>\n<p>\u62ff\u4e00\u4e2a\u7279\u522b\u7b80\u5355\u7684\u51fd\u6570\uff1a<\/p>\n<div class=\"katex math multi-line\">\nL=x^2+x^3\n<\/div>\n<p>\u8fd9\u91cc <span class=\"katex math inline\">x<\/span> \u901a\u8fc7\u4e24\u6761\u8def\u5f71\u54cd <span class=\"katex math inline\">L<\/span>\uff1a<\/p>\n<div class=\"katex math multi-line\">\nx\\rightarrow x^2\n<\/div>\n<p>\u548c\uff1a<\/p>\n<div class=\"katex math multi-line\">\nx\\rightarrow x^3\n<\/div>\n<p>\u6240\u4ee5\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\frac{dL}{dx} = 2x+3x^2\n<\/div>\n<p>\u4e0d\u662f\u9009\u5176\u4e2d\u4e00\u4e2a\u3002<\/p>\n<p>\u800c\u662f<strong>\u4e24\u4e2a\u5f71\u54cd\u76f8\u52a0<\/strong>\u3002<\/p>\n<p>\u795e\u7ecf\u7f51\u7edc\u4e5f\u662f\u4e00\u6837\u3002<\/p>\n<p>\u4e00\u4e2a\u8f93\u5165\u8fde\u63a5\u5230\u591a\u4e2a\u795e\u7ecf\u5143\uff1a<\/p>\n<pre class=\"prettyprint linenums\"><code>x\n\u251c\u2192 \u795e\u7ecf\u51431\n\u251c\u2192 \u795e\u7ecf\u51432\n\u2514\u2192 \u795e\u7ecf\u51433\n<\/code><\/pre>\n<p>\u4e09\u4e2a\u795e\u7ecf\u5143\u6700\u540e\u90fd\u4f1a\u5f71\u54cd Loss\u3002<\/p>\n<p>\u6240\u4ee5\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\\text{\u603b\u68af\u5ea6 = \u6bcf\u6761\u8def\u5f84\u68af\u5ea6\u4e4b\u548c}}\n<\/div>\n<p>\u8bf4\u4e86\u8fd9\u4e48\u591a\uff0c<code>Layer<\/code>\u7684<code>backward()<\/code>\u5230\u5e95\u5e72\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u5b83\u8981\u5148\u6536\u5230\u4e0b\u4e00\u5c42\u7ed9\u4ed6\u7684\u4e09\u4e2a\u795e\u7ecf\u5143\u7684\u68af\u5ea6\uff0c\u7136\u540e\u628a\u6bcf\u4e2a\u68af\u5ea6\u5206\u53d1\u7ed9\u6bcf\u4e2a\u795e\u7ecf\u5143<\/p>\n<p>\u6bcf\u4e2a\u795e\u7ecf\u5143\u901a\u8fc7\u81ea\u5df1\u7684<code>backward()<\/code>\u66f4\u65b0\u81ea\u5df1\u7684\u6743\u91cd\u548c\u504f\u7f6e\uff0c\u5e76\u4e14\u8ba1\u7b97Loss\u5bf9\u8fd9\u4e2a\u795e\u7ecf\u5143\u5404\u4e2a\u8f93\u5165\u7684\u68af\u5ea6\uff0c\u8fd4\u56de\u51fa\u53bb\u7ed9\u4e0a\u4e00\u5c42\uff0c\u7531\u4e8e\u5f53\u524d\u5c42\u7684\u8f93\u5165\u5c31\u662f\u4e0a\u4e00\u5c42\u7684\u8f93\u51fa\uff0c\u6240\u4ee5\u8fd9\u4e9b\u8f93\u5165\u5bf9Loss\u7684\u68af\u5ea6\u4e5f\u5c31\u6210\u4e3a\u4e86\u4e0a\u4e00\u5c42\u8f93\u51fa\u5bf9 Loss \u7684\u68af\u5ea6\u3002\u8fd9\u6837\u4e00\u5c42\u4e00\u5c42\u5411\u524d\u4f20\u9012\uff0c\u53cd\u5411\u4f20\u64ad\u5c31\u6210\u7acb\u4e86<\/p>\n<div class=\"admonition warning\">\n<p class=\"admonition-title\">Warning<\/p>\n<p>\u4e00\u5b9a\u8981\u7528\u8001\u6743\u91cd\u7b97\u5b8c\u68af\u5ea6\u518d\u66f4\u65b0\u6210\u65b0\u6743\u91cd<\/p>\n<\/div>\n<p>OK\uff01\u63a5\u4e0b\u6765\u6539\u4ee3\u7801\uff01<\/p>\n<p>\u9996\u5148\u8981\u5148\u4fee\u6539<code>Neuron<\/code>\u7684<code>backward()<\/code>\u8ba9\u5b83\u5177\u5907\u8ba1\u7b97\u5e76\u8fd4\u56deLoss\u5bf9\u5f53\u524d\u795e\u7ecf\u5143\u8f93\u5165\u7684\u5f71\u54cd<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Neuron():\n    def __init__(self, weights, bias):\n        self.weights = weights\n        self.bias = bias\n\n    def ReLU(self, x):\n        return max(0, x)\n\n    def forward(self, inputs):\n        self.inputs = inputs #\u4fdd\u5b58\u8f93\u5165\uff0cbackward\u65f6\u8fd8\u5f97\u7528\n        z = 0\n        for x, w in zip(inputs, self.weights):\n            z += x * w\n        z += self.bias\n\n        self.z = z #\u4fdd\u5b58z\n        self.haty = self.ReLU(z) # \u4fdd\u5b58haty\n\n        return self.haty\n\n    def ReLU_derivative(self, z):\n        if z &gt; 0:\n            return 1\n        else:\n            return 0\n\n    def backward(self, output_gradient, learning_rate):\n        # \u9700\u8981\u771f\u6b63\u7684\u7ed3\u679cy\uff0c\u8fd8\u6709\u5b66\u4e60\u7387eta,\u628a\u6b63\u786e\u7b54\u6848y\u548cLoss\u7684\u8ba1\u7b97\u76f4\u63a5\u53d8\u4e3aoutput_gradient\uff0c\u8868\u793a\u4ece\u540e\u4e00\u5c42\u7684backward\u8fd4\u56de\u503c\u4e2d\u76f4\u63a5\u62ff\u68af\u5ea6\n        input_gradient = []\n        dhaty_dz = self.ReLU_derivative(self.z)\n        dL_dz = output_gradient * dhaty_dz\n\n        #\u66f4\u65b0\u6743\u91cd\n        for i in range(len(self.weights)):\n            dz_dwi = self.inputs[i]\n            dL_dw = dL_dz * dz_dwi\n\n            #\u8ba1\u7b97Loss\u5bf9\u5f53\u524d\u8fd9\u4e2a\u795e\u7ecf\u5143\u7684\u5f53\u524d\u8f93\u5165\u7684\u68af\u5ea6\n            input_gradient.append(dL_dz * self.weights[i]) #\u4e00\u5b9a\u8981\u5728\u66f4\u65b0\u6743\u91cd\u4e4b\u524d\u8ba1\u7b97\u8f93\u5165\u68af\u5ea6\uff01\n\n            self.weights[i] -= learning_rate * dL_dw\n\n        #\u66f4\u65b0\u504f\u7f6e\n        dz_db = 1\n        dL_db = dL_dz * dz_db\n\n        self.bias -= learning_rate * dL_db\n\n        return input_gradient\n<\/code><\/pre>\n<p>\u7136\u540e\u5199<code>Layer<\/code>\u7684<code>backward()<\/code>\uff0c\u5f97\u5230\u4e0b\u4e00\u6b21\u7684\u8fd4\u56de\u503c\uff0c\u7136\u540e\u7ed9\u6bcf\u4e2a\u795e\u7ecf\u5143\u5206\u53d1\uff0c\u518d\u6536\u96c6\u6bcf\u4e2a\u795e\u7ecf\u5143\u7ed9\u56de\u7684\u68af\u5ea6\uff0c\u6700\u540e\u52a0\u4e00\u8d77\u8fd4\u56de\u7ed9\u4e0a\u4e00\u5c42<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Layer():\n    def __init__(self, neurons):\n        self.neurons = neurons\n\n    def forward(self, inputs):\n        neurons_output = []\n        for neuron in self.neurons:\n            neurons_output.append(neuron.forward(inputs))\n\n        return neurons_output\n\n    def backward(self,output_gradient,learning_rate):\n        input_gradient = [0] * len(self.neurons[0].inputs) #\u76f4\u63a5\u8282\u7701\u6389input\u53c2\u6570\uff0c\u4ece\u4efb\u610f\u4e00\u4e2a\u795e\u7ecf\u5143\u91cc\u8bfb\u53d6\u8f93\u5165\u6570\u91cf\n        for neuronIdx in range(len(self.neurons)): #\u904d\u5386\u8fd9\u4e00\u5c42\u7684\u6bcf\u4e2a\u795e\u7ecf\u5143\n            neuron = self.neurons[neuronIdx] #\u5f97\u5230\u76ee\u6807\u795e\u7ecf\u5143\n\n            #\u628aLoss\u5bf9\u5f53\u524d\u8fd9\u4e2a\u795e\u7ecf\u5143\u8f93\u51fa\u7684\u68af\u5ea6\u4ea4\u7ed9\u8fd9\u4e2a\u795e\u7ecf\u5143\n            #\u795e\u7ecf\u5143\u7684backward\u51fd\u6570\u5c06\u4f1a\u8fd4\u56de\u81ea\u5df1\u7684\u6bcf\u4e2a\u8f93\u5165\u5bf9Loss\u4ea7\u751f\u7684\u68af\u5ea6\u8d21\u732e\n            neuron_backward_gradients = neuron.backward(output_gradient[neuronIdx], learning_rate) \n            # \u628a\u4e0d\u540c\u795e\u7ecf\u5143\u5bf9\u540c\u4e00\u4e2a\u8f93\u5165\u7684\u68af\u5ea6\u8d21\u732e\u76f8\u52a0\n            for inputIdx in range(len(self.neurons[0].inputs)): \n                input_gradient[inputIdx] += neuron_backward_gradients[inputIdx]\n\n        #\u5f97\u5230\u6574\u5408\u4e86\u6240\u6709\u795e\u7ecf\u5143\u8f93\u5165\u7684\u68af\u5ea6\u7ec4\uff0c\u8fd4\u56de\n        #\u5f53\u524d\u5c42\u8f93\u5165=\u4e0a\u4e00\u5c42\u8f93\u51fa\n        return input_gradient\n<\/code><\/pre>\n<p>OK\uff01\u73b0\u5728<code>Layer<\/code>\u6b63\u786e\u7684\u7ed9\u795e\u7ecf\u5143\u5206\u53d1\u68af\u5ea6\uff0c\u4e5f\u53ef\u4ee5\u6574\u5408\u795e\u7ecf\u5143\u7684\u68af\u5ea6\u5566<\/p>\n<p>\u73b0\u5728\u5f00\u59cb\u5199<code>Network<\/code>\uff0c\u8ba9<code>Network<\/code>\u518d<code>Layer<\/code>\u5c42\u9762\u5206\u53d1\u4e0e\u6574\u5408\u68af\u5ea6<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Network():\n    def __init__(self, layers):\n        self.layers = layers\n\n    def forward(self, inputs):\n        layers_output = inputs\n        for layer in self.layers:\n            layers_output = layer.forward(layers_output)\n\n        return layers_output\n\n    def backward(self, output_gradient, learning_rate):\n        input_gradient = output_gradient\n\n        #\u5012\u7740\u904d\u5386\u6bcf\u4e00\u5c42\uff0c\u4ece\u6700\u5e95\u4e0b\u5f80\u4e0a\u4f20\u64ad\u68af\u5ea6\u7ec4\n        for layerIdx in range(len(self.layers) - 1,-1,-1): #range start stop step\n            layer = self.layers[layerIdx] #\u5f97\u5230\u5bf9\u5e94layer\n            input_gradient = layer.backward(input_gradient, learning_rate) #\u8c03\u7528layer\u7684backward\uff0c\u7531\u4e8e\u662f\u9006\u5411\u904d\u5386\uff0c\u6240\u4ee5\u73b0\u5728\u7684input_gradient\u5176\u5b9e\u662f\u4e0b\u4e00\u5c42\uff08\u76f8\u5bf9\u66f4\u9760\u8fd1\u8f93\u51fa\u5c42\uff09\u7684backward\u7ed9\u56de\u7684\u68af\u5ea6\n\n<\/code><\/pre>\n<p>\u81f3\u6b64\uff0c<code>Neuron<\/code>\uff0c<code>Layer<\/code>\uff0c<code>Network<\/code>\u7684\u6b63\u5411\u8fd0\u7b97\u4e0e\u53cd\u5411\u4f20\u64ad\uff0c\u642d\u5efa\u5b8c\u6210<\/p>\n<p>\u4e0b\u9762\u5b9e\u4f8b\u5316\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\u5427~<\/p>\n<p>\u4e3a\u4e86\u6d4b\u8bd5\u591a\u5c42\u7f51\u7edc\u7684\u524d\u5411\u4f20\u64ad\u548c\u53cd\u5411\u4f20\u64ad\uff0c\u8fd9\u91cc\u5148\u642d\u5efa\u4e00\u4e2a\u7ed3\u6784\u8f83\u5c0f\u7684\u7f51\u7edc\uff1a<\/p>\n<div class=\"katex math multi-line\">\n2\\rightarrow2\\rightarrow3\\rightarrow2\\rightarrow1\n<\/div>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>\u7b2c\u4e00\u4e2a <span class=\"katex math inline\">2<\/span>\uff1a\u8f93\u5165\u5c42\u6709 2 \u4e2a\u8f93\u5165\u8282\u70b9\uff1b<\/li>\n<li>\u7b2c\u4e8c\u4e2a <span class=\"katex math inline\">2<\/span>\uff1a\u7b2c\u4e00\u9690\u85cf\u5c42\u6709 2 \u4e2a\u795e\u7ecf\u5143\uff1b<\/li>\n<li><span class=\"katex math inline\">3<\/span>\uff1a\u7b2c\u4e8c\u9690\u85cf\u5c42\u6709 3 \u4e2a\u795e\u7ecf\u5143\uff1b<\/li>\n<li>\u7b2c\u4e09\u4e2a <span class=\"katex math inline\">2<\/span>\uff1a\u7b2c\u4e09\u9690\u85cf\u5c42\u6709 2 \u4e2a\u795e\u7ecf\u5143\uff1b<\/li>\n<li>\u6700\u540e\u7684 <span class=\"katex math inline\">1<\/span>\uff1a\u8f93\u51fa\u5c42\u6709 1 \u4e2a\u795e\u7ecf\u5143\u3002<\/li>\n<\/ul>\n<p>\u5f00\u5e72\uff01<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">\ntrain_data = [\n    (1,1,1),\n    (0,1,0),\n    (1,0,0),\n    (0,0,0)\n]\n\nnetwork = Network([\n    Layer([\n        Neuron([0.5,0.5],0.5),\n        Neuron([0.5,0.5],0.5)\n    ]),\n    Layer([\n        Neuron([0.5,0.5],0.5),\n        Neuron([0.5,0.5],0.5),\n        Neuron([0.5,0.5],0.5)\n    ]),\n    Layer([\n        Neuron([0.5,0.5,0.5],0.5),\n        Neuron([0.5,0.5,0.5],0.5) #\u7b2c\u4e09\u5c42\u7684\u6743\u91cd\u5fc5\u987b\u53d8\u6210\u4e09\u4e2a\uff0c\u56e0\u4e3a\u4e0a\u4e00\u5c42\u6709\u4e09\u4e2a\u795e\u7ecf\u5143\n    ]),\n    Layer([\n        Neuron([0.5,0.5],0.5)\n    ])\n])\n\nfor epoch in range(20000):\n    for sample in range(len(train_data)):\n        haty = network.forward([train_data[sample][0], train_data[sample][1]]) #\u5f97\u5230\u795e\u7ecf\u7f51\u7edc\u6700\u7ec8\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u8ba1\u7b97Loss\n        dL_dhaty = 2*(haty[0]-train_data[sample][2])\n        network.backward([dL_dhaty],0.01) #\u4ece\u8fd9\u91cc\u4f20\u5165\u68af\u5ea6\u7684\u65f6\u5019\u4f20\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a1\u7684\u5217\u8868\uff0c\u56e0\u4e3a\u8f93\u51fa\u5c42\u53ea\u6709\u4e00\u4e2a\u795e\u7ecf\u5143\n\nprint(network.forward([train_data[0][0], train_data[0][1]]))\nprint(network.forward([train_data[1][0], train_data[1][1]]))\nprint(network.forward([train_data[2][0], train_data[2][1]]))\nprint(network.forward([train_data[3][0], train_data[3][1]]))\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u4e00\u5b9a\u8981\u7279\u522b\u6ce8\u610f\u795e\u7ecf\u7f51\u7edc\u7684\u6743\u91cd\u6570\u91cf\u95ee\u9898\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u4e86\u56db\u5c42<code>Layer<\/code>\uff0c\u7b2c\u4e8c\u5c42\u6709\u4e09\u4e2a\u795e\u7ecf\u5143\uff0c\u6240\u4ee5\u7b2c\u4e09\u6b21\u7684\u4e24\u4e2a\u795e\u7ecf\u5143\u6bcf\u5c42\u8981\u6709\u4e09\u4e2a\u8f93\u5165\uff0c\u5982\u56fe<\/p>\n<p><img alt=\"f4e65c47-0176-4db2-82fd-5f8a8110f93f\" src=\"https:\/\/99website.top\/blog\/wp-content\/themes\/Kizumi\/assets\/images\/loading.gif\" data-src=\"https:\/\/99website.top\/blog\/wp-content\/uploads\/2026\/10\/99blog-5b414f599d9e8cccbf2a.jpg\" class=\"lazy\" loading=\"lazy\"><\/p>\n<p>\u6709\u591a\u5c11\u8f93\u5165\u5c31\u6709\u591a\u5c11\u6743\u91cd<\/p>\n<p>\u6700\u7ec8\u8bad\u7ec3\u7ed3\u679c\u5982\u4e0b<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-powershell\">python -u &quot;j:\\99\u7684\u535a\u5ba2\\AttentionIsAllYouNeed\\MLP.py&quot;\n[0.9999999999999987]\n[0]\n[1.1102230246251565e-15]\n[0]\n<\/code><\/pre>\n<p>\u591a\u5c42 MLP \u548c\u5355\u4e2a\u795e\u7ecf\u5143\u6700\u7ec8\u90fd\u6210\u529f\u5b66\u4e60\u4e86 AND \u8fd0\u7b97\uff0c\u8f93\u51fa\u90fd\u975e\u5e38\u63a5\u8fd1\u7406\u8bba\u503c <span class=\"katex math inline\">0<\/span> \u548c <span class=\"katex math inline\">1<\/span>\u3002MLP \u7684\u90e8\u5206\u8f93\u51fa\u5728\u6570\u503c\u4e0a\u66f4\u63a5\u8fd1\u76ee\u6807\u503c\uff0c\u4f46\u8fd9\u79cd <span class=\"katex math inline\">10^{-15}<\/span> \u91cf\u7ea7\u7684\u5dee\u5f02\u57fa\u672c\u5c5e\u4e8e\u6d6e\u70b9\u6570\u8bef\u5dee\uff0c\u4e0d\u80fd\u636e\u6b64\u8ba4\u4e3a MLP \u7684\u8bad\u7ec3\u6548\u679c\u663e\u8457\u4f18\u4e8e\u5355\u4e2a\u795e\u7ecf\u5143\u3002<\/p>\n<p>\u4f46\u662f\u6309\u7406\u6765\u8bf4\u591a\u5c42\u795e\u7ecf\u5143\u5e94\u8be5\u6bd4\u5355\u4e2a\u795e\u7ecf\u5143\u80fd\u529b\u591a\u591a\u4e86\u554a<\/p>\n<p>GPT\u63d0\u51fa\u6362\u7528XOR\uff0c\u4e5f\u5c31\u662f\u5f02\u6216\u8fd0\u7b97\u8bd5\u8bd5\u770b<\/p>\n<p>\u66f4\u6539<code>train_data<\/code>\u7136\u540e\u7528\u4e24\u79cd\u65b9\u6cd5\u91cd\u65b0\u5f00\u59cb\u8bad\u7ec3<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">train_data = [\n    (1,1,0),\n    (0,1,1),\n    (1,0,1),\n    (0,0,0)\n]\n\nneuron = Neuron([0.5,0.5],0.5) #\u5b9e\u4f8b\u5316\u4e00\u4e2a\u795e\u7ecf\u5143~\u7136\u540e\u7ed9\u6240\u6709\u6743\u91cd\u7ed9\u4e00\u4e2a\u521d\u59cb\u503c\uff0c\u5c31\u8bbe\u4e3a0.5\u5427\n\nfor epoch in range(30000):\n    for sample in range(4):\n        neuron.forward([train_data[sample][0],train_data[sample][1]])\n        neuron.backward(train_data[sample][2], 0.01)\n\nfor sample in range(4):\n    print(neuron.forward([train_data[sample][0],train_data[sample][1]]))\n\nprint(&quot;------------------&quot;)\n\nnetwork = Network([\n    Layer([\n        Neuron([0.5,0.5],0.5),\n        Neuron([0.5,0.5],0.5)\n    ]),\n    Layer([\n        Neuron([0.5,0.5],0.5),\n        Neuron([0.5,0.5],0.5),\n        Neuron([0.5,0.5],0.5)\n    ]),\n    Layer([\n        Neuron([0.5,0.5,0.5],0.5),\n        Neuron([0.5,0.5,0.5],0.5) #\u7b2c\u4e09\u5c42\u7684\u6743\u91cd\u5fc5\u987b\u53d8\u6210\u4e09\u4e2a\uff0c\u56e0\u4e3a\u4e0a\u4e00\u5c42\u6709\u4e09\u4e2a\u795e\u7ecf\u5143\n    ]),\n    Layer([\n        Neuron([0.5,0.5],0.5)\n    ])\n])\n\nfor epoch in range(30000):\n    for sample in range(len(train_data)):\n        haty = network.forward([train_data[sample][0], train_data[sample][1]]) #\u5f97\u5230\u795e\u7ecf\u7f51\u7edc\u6700\u7ec8\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u8ba1\u7b97Loss\n        dL_dhaty = 2*(haty[0]-train_data[sample][2])\n        network.backward([dL_dhaty],0.01) #\u4ece\u8fd9\u91cc\u4f20\u5165\u68af\u5ea6\u7684\u65f6\u5019\u4f20\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a1\u7684\u5217\u8868\uff0c\u56e0\u4e3a\u8f93\u51fa\u5c42\u53ea\u6709\u4e00\u4e2a\u795e\u7ecf\u5143\n\nprint(network.forward([train_data[0][0], train_data[0][1]]))\nprint(network.forward([train_data[1][0], train_data[1][1]]))\nprint(network.forward([train_data[2][0], train_data[2][1]]))\nprint(network.forward([train_data[3][0], train_data[3][1]]))\n<\/code><\/pre>\n<p>\u5f00\u59cb\u8bad\u7ec3\uff01<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-powershell\">python -u &quot;j:\\99\u7684\u535a\u5ba2\\AttentionIsAllYouNeed\\MLP.py&quot;\n0.1599999999999991\n0\n0\n0\n------------------\n[0.5002121244846724]\n[0.4998725743427884]\n[0.4999440043584395]\n[0.4996051249246106]\n<\/code><\/pre>\n<p>\u5929\u554a\uff01\u4e3a\u4ec0\u4e48\u5b66\u4e0d\u4f1a\u4e86\uff01\uff1f<\/p>\n<p>\u591a\u5c42\u795e\u7ecf\u7f51\u7edc\u7adf\u7136\u5b66\u4e0d\u4f1a\uff0c\u95ee\u9898\u51fa\u5728\u54ea\u91cc\u5462\uff1f<\/p>\n<p>\u56e0\u4e3a XOR \u7684\u6807\u7b7e\u6b63\u597d\u662f\uff1a<\/p>\n<div class=\"katex math multi-line\">\n0,1,1,0\n<\/div>\n<p>\u5e73\u5747\u503c\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\frac{0+1+1+0}{4}=0.5\n<\/div>\n<p>\u5bf9\u4e8e\u5e73\u65b9\u8bef\u5dee\u6765\u8bf4\uff0c\u5982\u679c\u6a21\u578b\u53ea\u80fd\u8f93\u51fa\u4e00\u4e2a\u5dee\u4e0d\u591a\u56fa\u5b9a\u7684\u503c\uff0c\u90a3\u4e48 <strong>0.5 \u6070\u597d\u5c31\u662f\u6700\u4f18\u5e38\u6570\u9884\u6d4b<\/strong>\u3002<\/p>\n<p>\u66f4\u5173\u952e\u7684\u539f\u56e0\u5728\u4f60\u7684\u521d\u59cb\u5316\uff1a<\/p>\n<pre class=\"prettyprint linenums\"><code>Neuron([0.5,0.5],0.5),Neuron([0.5,0.5],0.5)\n<\/code><\/pre>\n<p>\u540c\u4e00\u5c42\u6240\u6709\u795e\u7ecf\u5143\uff1a<\/p>\n<ul>\n<li>\u6743\u91cd\u5b8c\u5168\u4e00\u6837\uff1b<\/li>\n<li>bias \u5b8c\u5168\u4e00\u6837\uff1b<\/li>\n<li>\u6536\u5230\u7684\u8f93\u5165\u5b8c\u5168\u4e00\u6837\u3002<\/li>\n<\/ul>\n<p>\u90a3\u4e48 forward \u65f6\u5b83\u4eec\u8f93\u51fa\u4e5f\u6c38\u8fdc\u4e00\u6837\u3002<\/p>\n<p>\u6bd4\u5982\u7b2c\u4e00\u5c42\uff1a<\/p>\n<pre class=\"prettyprint linenums\"><code>Neuron A: [0.5, 0.5], b=0.5\nNeuron B: [0.5, 0.5], b=0.5\n<\/code><\/pre>\n<p>\u8f93\u5165\u4e00\u6837\uff1a<\/p>\n<div class=\"katex math multi-line\">\n[x_1,x_2] \n<\/div>\n<p>\u6240\u4ee5\uff1a<\/p>\n<div class=\"katex math multi-line\">\nz_A=z_B\n<\/div>\n<p>\u4e8e\u662f\uff1a<\/p>\n<div class=\"katex math multi-line\">\na_A=a_B\n<\/div>\n<p>\u53cd\u5411\u4f20\u64ad\u65f6\uff0c\u5b83\u4eec\u6536\u5230\u7684\u68af\u5ea6\u4e5f\u4f1a\u56e0\u4e3a\u5bf9\u79f0\u7ed3\u6784\u800c\u4e00\u6837\uff0c\u4e8e\u662f\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\Delta w_A=\\Delta w_B\n<\/div>\n<p>\u8bad\u7ec3\u4e00\u6b21\u4ee5\u540e\u8fd8\u662f\u4e00\u6837\u3002<\/p>\n<p>\u518d\u8bad\u7ec3\uff1a<\/p>\n<p>\u8fd8\u662f\u4e00\u6837\u3002<\/p>\n<p><strong>\u5b83\u4eec\u4e00\u8f88\u5b50\u90fd\u957f\u5f97\u4e00\u6a21\u4e00\u6837\u3002<\/strong><\/p>\n<p>\u8fd9\u53eb\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\\text{\u5bf9\u79f0\u6027\u95ee\u9898\uff08symmetry\uff09}}\n<\/div>\n<p>\u6240\u4ee5\u795e\u7ecf\u7f51\u7edc\u4e0d\u80fd\u628a\u540c\u4e00\u5c42\u6240\u6709\u795e\u7ecf\u5143\u521d\u59cb\u5316\u6210\u5b8c\u5168\u4e00\u6837\u7684\u53c2\u6570\u3002<\/p>\n<p>\u4e0d\u7136\u4f60\u867d\u7136\u8868\u9762\u4e0a\u5199\u4e86 3 \u4e2a\u795e\u7ecf\u5143\uff0c\u5b9e\u9645\u4e0a\u5b83\u4eec\u5e72\u7684\u662f\u5b8c\u5168\u4e00\u6837\u7684\u4e8b\uff0c\u76f8\u5f53\u4e8e\u590d\u5236\u7c98\u8d34\u3002<\/p>\n<p>\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u771f\u6b63\u7684\u795e\u7ecf\u7f51\u7edc\u901a\u5e38\u8981\u7528<strong>\u968f\u673a\u521d\u59cb\u5316<\/strong>\u3002<\/p>\n<p>GPT\u89e3\u91ca\u8fc7\u540e\uff0c\u8305\u585e\u987f\u5f00\uff01\u4fee\u6539\u795e\u7ecf\u5143\u548c\u795e\u7ecf\u7f51\u7edc\u6743\u91cd\u521d\u59cb\u5316\uff1a<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">train_data = [\n    (1,1,0),\n    (0,1,1),\n    (1,0,1),\n    (0,0,0)\n]\n\ndef rw(count):\n\n    if count == 1:\n        return random.uniform(-0.5, 0.5)\n\n    return [\n        random.uniform(-0.5, 0.5)\n        for _ in range(count)\n    ]\n\nneuron = Neuron(rw(2), rw(1)) #\u5b9e\u4f8b\u5316\u4e00\u4e2a\u795e\u7ecf\u5143~\u7136\u540e\u7ed9\u6240\u6709\u6743\u91cd-0.5~0.5\u7684\u968f\u673a\u503c\n\nfor epoch in range(30000):\n    for sample in range(4):\n        neuron.forward([train_data[sample][0],train_data[sample][1]])\n        neuron.backward(train_data[sample][2], 0.01)\n\nfor sample in range(4):\n    print(neuron.forward([train_data[sample][0],train_data[sample][1]]))\n\nprint(&quot;------------------&quot;)\n\nnetwork = Network([\n    Layer([\n        Neuron(rw(2), rw(1)),\n        Neuron(rw(2), rw(1))\n    ]),\n    Layer([\n        Neuron(rw(2), rw(1)),\n        Neuron(rw(2), rw(1)),\n        Neuron(rw(2), rw(1))\n    ]),\n    Layer([\n        Neuron(rw(3), rw(1)),\n        Neuron(rw(3), rw(1)) #\u7b2c\u4e09\u5c42\u7684\u6743\u91cd\u5fc5\u987b\u53d8\u6210\u4e09\u4e2a\uff0c\u56e0\u4e3a\u4e0a\u4e00\u5c42\u6709\u4e09\u4e2a\u795e\u7ecf\u5143\n    ]),\n    Layer([\n        Neuron(rw(2), rw(1))\n    ])\n])\n\nfor epoch in range(30000):\n    for sample in range(len(train_data)):\n        haty = network.forward([train_data[sample][0], train_data[sample][1]]) #\u5f97\u5230\u795e\u7ecf\u7f51\u7edc\u6700\u7ec8\u7ed9\u51fa\u7684\u7b54\u6848\uff0c\u8ba1\u7b97Loss\n        dL_dhaty = 2*(haty[0]-train_data[sample][2])\n        network.backward([dL_dhaty],0.01) #\u4ece\u8fd9\u91cc\u4f20\u5165\u68af\u5ea6\u7684\u65f6\u5019\u4f20\u5165\u4e00\u4e2a\u957f\u5ea6\u4e3a1\u7684\u5217\u8868\uff0c\u56e0\u4e3a\u8f93\u51fa\u5c42\u53ea\u6709\u4e00\u4e2a\u795e\u7ecf\u5143\n\nprint(network.forward([train_data[0][0], train_data[0][1]]))\nprint(network.forward([train_data[1][0], train_data[1][1]]))\nprint(network.forward([train_data[2][0], train_data[2][1]]))\nprint(network.forward([train_data[3][0], train_data[3][1]]))\n<\/code><\/pre>\n<p>\u5f97\u5230\u7684\u8bad\u7ec3\u7ed3\u679c\uff1a<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-powershell\">0\n0\n0\n0\n------------------\n[0.33328798802883947]\n[1.0066657597605764]\n[0.33328798802883947]\n[0.33328798802883947]\n<\/code><\/pre>\n<p>\u6211\u53bb\u8fd9\u600e\u4e48\u529e\u545c\u545c\u545c\uff0cGPT\u8bf4\u6211\u8fd9\u662f\u649e\u4e0a\u4e86\u524d\u9762\u63d0\u5230\u7684ReLU Dead\uff0c\u5979\u8ba9\u6211\u628a\u6700\u540e\u4e00\u4e2a\u795e\u7ecf\u5143\u7684\u6fc0\u6d3b\u51fd\u6570\u6362\u6210<code>Sigmoid<\/code><\/p>\n<p>\u5728\u6b64\u4e4b\u524d\uff0c\u4ed6\u8ba9\u6211\u6bcf\u4e2a\u795e\u7ecf\u5143\u90fd\u6253\u4e2a\u70b9<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">output_neuron = network.layers[-1].neurons[0]\n\nprint(\n    &quot;z =&quot;, output_neuron.z,\n    &quot;output =&quot;, output_neuron.haty\n)\n<\/code><\/pre>\n<p>\u8fd0\u884c\u56db\u904d\uff0c\u5f97\u5230\u7ed3\u679c\uff1a<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-powershell\">z = -0.18422243332148341 output = 0\nz = -0.44705199068033413 output = 0\nz = 0.3337361586688892 output = 0.3337361586688892\nz = 0.49989797996319696 output = 0.49989797996319696\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0cReLU \u5728\u90e8\u5206\u6837\u672c\u4e0a\u8fdb\u5165\u4e86\u8d1f\u533a\u95f4\uff0c\u4f7f\u8fd9\u4e9b\u6837\u672c\u5bf9\u5e94\u7684\u8f93\u51fa\u88ab\u622a\u65ad\u4e3a 0\uff0c\u540c\u65f6\u68af\u5ea6\u4e5f\u4f1a\u53d8\u6210 0\u3002<\/p>\n<p>\u5bf9\u4e8e XOR \u8fd9\u79cd\u4e8c\u5206\u7c7b\u8f93\u51fa\uff0c\u5728\u8f93\u51fa\u5c42\u4f7f\u7528 Sigmoid \u4f1a\u66f4\u52a0\u5408\u9002\uff1a\u5b83\u80fd\u628a\u8f93\u51fa\u9650\u5236\u5728 <span class=\"katex math inline\">0\\sim1<\/span> \u4e4b\u95f4\uff0c\u540c\u65f6\u4e0d\u4f1a\u50cf ReLU \u4e00\u6837\u5728\u8d1f\u534a\u8f74\u76f4\u63a5\u4ea7\u751f 0 \u68af\u5ea6\u3002<\/p>\n<p>\u9686\u91cd\u4ecb\u7ecd\uff01<\/p>\n<p>Sigmoid\uff01\uff01\uff01<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\sigma(z)=\\frac{1}{1+e^{-z}}\n}\n<\/div>\n<p>\u5b83\u6700\u5927\u7684\u7279\u70b9\uff1a<\/p>\n<div class=\"katex math multi-line\">\n0&lt;\\sigma(z)&lt;1\n<\/div>\n<p>\u6240\u4ee5\u5f88\u9002\u5408XOR\u6700\u7ec8\u8f93\u51fa\uff1a<\/p>\n<ul>\n<li>\u63a5\u8fd1 0 \u2192 \u5224\u65ad\u4e3a 0<\/li>\n<li>\u63a5\u8fd1 1 \u2192 \u5224\u65ad\u4e3a 1<\/li>\n<\/ul>\n<p>Sigmoid\u7684\u5bfc\u6570\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\boxed{\n\\sigma'(z)\n=\n\\sigma(z)(1-\\sigma(z))\n}\n<\/div>\n<p>\u7ed9<code>Neuron<\/code>\u7c7b\u52a0\u4e0a<code>activation<\/code>\u53c2\u6570\uff0c<code>relu<\/code>\u8868\u793a\u6fc0\u6d3b\u51fd\u6570\u4f7f\u7528ReLU\uff0c<code>sigmoid<\/code>\u8868\u793a\u6fc0\u6d3b\u51fd\u6570\u4f7f\u7528<code>Sigmoid<\/code><\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">class Neuron():\n    def __init__(self, weights, bias,activation=&quot;relu&quot;):\n        self.weights = weights\n        self.bias = bias\n        self.activation = activation\n\n    def sigmoid(self, x):\n        return 1 \/ (1 + math.exp(-x))\n\n    def sigmoid_derivative(self, x):\n        return self.sigmoid(x) * (1 - self.sigmoid(x))\n\n    def ReLU(self, x):\n        return max(0, x)\n\n    def forward(self, inputs):\n        self.inputs = inputs #\u4fdd\u5b58\u8f93\u5165\uff0cbackward\u65f6\u8fd8\u5f97\u7528\n        z = 0\n        for x, w in zip(inputs, self.weights):\n            z += x * w\n        z += self.bias\n\n        self.z = z #\u4fdd\u5b58z\n\n        if self.activation == &quot;sigmoid&quot;:\n            self.haty = self.sigmoid(z) # \u4fdd\u5b58haty\n        else:\n            self.haty = self.ReLU(z) # \u4fdd\u5b58haty\n\n        return self.haty\n\n    def ReLU_derivative(self, z):\n        if z &gt; 0:\n            return 1\n        else:\n            return 0\n\n    def backward(self, output_gradient, learning_rate):\n        # \u9700\u8981\u771f\u6b63\u7684\u7ed3\u679cy\uff0c\u8fd8\u6709\u5b66\u4e60\u7387eta,\u628a\u6b63\u786e\u7b54\u6848y\u548cLoss\u7684\u8ba1\u7b97\u76f4\u63a5\u53d8\u4e3aoutput_gradient\n        # \u5bf9\u4e8e\u8f93\u51fa\u5c42\uff0c\u5b83\u7531 Loss \u51fd\u6570\u8ba1\u7b97\n        # \u5bf9\u4e8e\u9690\u85cf\u5c42\uff0c\u5b83\u7531\u540e\u9762\u7684\u5c42\u53cd\u5411\u4f20\u64ad\u5f97\u5230\n        input_gradient = []\n        if self.activation == &quot;sigmoid&quot;:\n            dhaty_dz = self.sigmoid_derivative(self.z)\n        else:\n            dhaty_dz = self.ReLU_derivative(self.z)\n        dL_dz = output_gradient * dhaty_dz\n\n        #\u66f4\u65b0\u6743\u91cd\n        for i in range(len(self.weights)):\n            dz_dwi = self.inputs[i]\n            dL_dw = dL_dz * dz_dwi\n\n            #\u8ba1\u7b97Loss\u5bf9\u5f53\u524d\u8fd9\u4e2a\u795e\u7ecf\u5143\u7684\u5f53\u524d\u8f93\u5165\u7684\u68af\u5ea6\n            input_gradient.append(dL_dz * self.weights[i]) #\u4e00\u5b9a\u8981\u5728\u66f4\u65b0\u6743\u91cd\u4e4b\u524d\u8ba1\u7b97\u8f93\u5165\u68af\u5ea6\uff01\n\n            self.weights[i] -= learning_rate * dL_dw\n\n        #\u66f4\u65b0\u504f\u7f6e\n        dz_db = 1\n        dL_db = dL_dz * dz_db\n\n        self.bias -= learning_rate * dL_db\n\n        return input_gradient\n<\/code><\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-powershell\">0.500625780921828\n0.5006257809185757\n0.49999999999939715\n0.499999999996145\n------------------\n[0.003973229901095833]\n[0.9874908529876903]\n[0.9874908529876903]\n[0.003973229901095833]\nz = -5.524194793805875 output = 0.003973229901095833\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\u795e\u7ecf\u7f51\u7edc\u5df2\u7ecf\u6210\u529f\u5de5\u4f5c\uff01\uff01\uff01\u597d\u8036\uff01\uff01\uff01<\/p>\n<p>\u800c\u4e14\u7531\u4e8e\u5355\u795e\u7ecf\u5143\u7684\u5c40\u9650\u6027\uff0c\u4e00\u4e2a\u795e\u7ecf\u5143\u6c38\u8fdc\u65e0\u6cd5\u5b66\u4f1aXOR\uff0c\u56e0\u4e3aXOR\u5728\u5e73\u9762\u4e0a\u662f\u4ea4\u53c9\u5206\u5e03\u7684\uff0c\u4e00\u4e2a\u795e\u7ecf\u5143\u7684\u51b3\u7b56\u90e8\u5206\u53ea\u80fd\u4ea7\u751f\u7ebf\u6027\u7684\u51b3\u7b56\u8fb9\u754c\uff0c\u4e0d\u53ef\u80fd\u6709\u4e00\u6761\u76f4\u7ebf\u5206\u5f00\u5728\u5e73\u9762\u4e0a\u4ea4\u53c9\u7684\u56db\u4e2a\u70b9<\/p>\n<p>\u5728\u591a\u6b21\u6d4b\u8bd5\u540e\uff0c\u6211\u53d1\u73b0<code>ReLU Dead<\/code>\u8fd8\u662f\u4f1a\u5076\u53d1\uff0c\u6240\u4ee5\u6253\u7b97\u628a<code>ReLU<\/code>\u6362\u6210<code>Leaky ReLU<\/code><\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-python\">def ReLU(self, x): #\u6362\u6210Leaky ReLU\n    if x &gt; 0:\n        return x\n    else:\n        return 0.01*x\n    # return max(0, x)\n\ndef ReLU_derivative(self, z):\n    if z &gt; 0:\n        return 1\n    else:\n        return 0.01\n<\/code><\/pre>\n<p>\u8fd8\u662f\u4f1a\u5076\u53d1\u6027\u51fa\u73b0\u7c7b\u4f3c\u5982\u4e0b\u60c5\u51b5<\/p>\n<pre class=\"prettyprint linenums\"><code class=\"language-powershell\">[0.66720386594263]\n[0.6670821681118739]\n[0.6670820167103273]\n[0.007197252325401114]\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u5df2\u7ecf\u4e0d\u5c5e\u4e8e Dead ReLU\uff0c\u56e0\u4e3a Leaky ReLU \u5728\u8d1f\u533a\u95f4\u4ecd\u7136\u5b58\u5728\u68af\u5ea6\u3002\u66f4\u53ef\u80fd\u7684\u539f\u56e0\u662f\u968f\u673a\u521d\u59cb\u5316\u4f7f\u7f51\u7edc\u5728\u8fd9\u6b21\u8bad\u7ec3\u4e2d\u843d\u5165\u4e86\u4e00\u4e2a\u8f83\u5dee\u7684\u89e3\uff0c\u4f7f\u90e8\u5206\u8f93\u5165\u7ecf\u8fc7\u9690\u85cf\u5c42\u540e\u5f97\u5230\u975e\u5e38\u63a5\u8fd1\u7684\u5185\u90e8\u8868\u793a\u3002<\/p>\n<p>\u6709\u8da3\u7684\u662f\uff0c\u524d\u4e09\u4e2a\u8f93\u51fa\u90fd\u63a5\u8fd1\uff1a<\/p>\n<div class=\"katex math multi-line\">\n0.667\\approx\\frac23\n<\/div>\n<p>\u800c\u5bf9\u5e94\u7684\u4e09\u4e2a XOR \u6807\u7b7e\u6070\u597d\u4e3a (0,1,1)\uff0c\u5176\u5e73\u5747\u503c\u6b63\u662f\uff1a<\/p>\n<div class=\"katex math multi-line\">\n\\frac{0+1+1}{3}=\\frac23\n<\/div>\n<p>\u8fd9\u8bf4\u660e\u7f51\u7edc\u867d\u7136\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u4f46\u5e76\u6ca1\u6709\u6210\u529f\u533a\u5206\u8fd9\u4e09\u4e2a\u6837\u672c\uff0c\u800c\u662f\u9009\u62e9\u4e86\u4e00\u4e2a\u80fd\u591f\u964d\u4f4e\u603b\u4f53 Loss \u7684\u6298\u4e2d\u8f93\u51fa\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u6fc0\u6d3b\u51fd\u6570\u5e76\u4e0d\u662f\u5f71\u54cd\u8bad\u7ec3\u7ed3\u679c\u7684\u552f\u4e00\u56e0\u7d20\uff0c\u795e\u7ecf\u7f51\u7edc\u7684\u53c2\u6570\u521d\u59cb\u5316\u540c\u6837\u975e\u5e38\u91cd\u8981\u3002<\/p>\n<p>\u800c\u795e\u7ecf\u7f51\u7edc\u6743\u91cd\u521d\u59cb\u5316\u8fd9\u6761\u8def\uff0c\u53c8\u662f\u4e00\u6761\u65b0\u7684\uff0c\u4efb\u91cd\u800c\u9053\u8fdc\u7684\u9053\u8def\u3002<\/p>\n<h3>\u5199\u5728\u6700\u540e<\/h3>\n<p>\u5929\u554a\u6211\u771f\u7684\u53c8\u5199\u51fa\u4e86\u4e00\u4e2a\u795e\u7ecf\u7f51\u7edc\uff01\uff01\uff01\u51c6\u786e\u6765\u8bf4\u8fd9\u4e2a\u53eb\u591a\u5c42\u611f\u77e5\u673a\uff08Multi-Layer Perceptron\uff09<\/p>\n<p>\u8fd9\u4e2a\u7cfb\u5217\u7684\u535a\u5ba2\u672c\u610f\u662f\u60f3\u572899\u7684\u5c0f\u7ad9\u4e0a\u4fdd\u5b58\u81ea\u5df1\u5199\u7684\u7b14\u8bb0\u5e76\u7ed9\u6211\u7684\u597d\u670b\u53cb\u5206\u4eab\uff0c\u5982\u679c\u6709\u95ee\u9898\u6b22\u8fce\u968f\u65f6\u63d0\u51fa\u54e6~<\/p>\n<p>\u8fd9\u6b21\u590d\u4e60\uff0c\u6211\u66f4\u52a0\u6df1\u523b\u7684\u7406\u89e3\u4e86\u57fa\u672c\u795e\u7ecf\u7f51\u7edc\u7684\u5de5\u4f5c\u539f\u7406\uff0c\u4e5f\u8ba9\u6211\u89c9\u5f97\u673a\u5668\u5b66\u4e60\u771f\u7684\u5f88\u7f8e\u3002<\/p>\n<p>\u611f\u8c22\u4f60\u770b\u5230\u8fd9\u91cc\uff0c\u4e5f\u611f\u8c22\u8fd9\u51e0\u5929\u8f9b\u82e6\u5b66\u4e60\u7684\u81ea\u5df1\u3002<\/p>\n<p>\u665a\u5b89\uff0c\u597d\u68a6~~<\/p>\n<p>99<\/p>\n<p>2026\/10\/4<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Attention Is All You Need &#8211; #1\uff1a\u91cd\u62fe\u795e\u7ecf\u7f51\u7edc \u5199\u5728\u524d\u9762 \u4eca\u5929\u53c8\u770b\u4e86\u4e00\u904dClaude Opus 5.5\u751f\u6210\u7684MV\uff0c\u771f\u7684\u8d85\u7ea7\u9707\u64bc\uff0c\u4e5f\u89c9\u5f97\u6211\u4e5f\u662f\u65f6\u5019\u8be5\u770b\u770b\u90a3\u795e\u79d8\u5df2\u4e45\u7684Transformer\u5230\u5e95\u662f\u4e2a\u5565\u4e86\uff0c\u8fd9\u4e2a\u7cfb\u5217\u7684\u535a\u5ba2\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\uff0c\u8fd9\u662f\u6211\u4e2a\u4eba\u7684\u5b66\u4e60\u7b14\u8bb0\uff0c\u4e0d\u5b8c\u5168\u6b63\u786e\uff01\uff0c\u8fd9\u4e0d\u662f\u6559\u7a0b\uff0c\u800c\u662f\u4e2a\u4eba\u7684\u7b14\u8bb0\uff0c\u5982\u6709\u95ee\u9898\u6b22\u8fce\u8385\u4e34\u6307\u5bfc\uff01 \u672c\u6b21\u7684\u5b66\u4e60\u65c5\u7a0b\u7531GPT\u5927\u4eba\u5e26\u9886 \u795e\u7ecf\u5143 &#038;#8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25,23,26,24],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-ai","category-transformer","category-26","category-24"],"_links":{"self":[{"href":"https:\/\/99website.top\/blog\/index.php?rest_route=\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/99website.top\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/99website.top\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/99website.top\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/99website.top\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=167"}],"version-history":[{"count":0,"href":"https:\/\/99website.top\/blog\/index.php?rest_route=\/wp\/v2\/posts\/167\/revisions"}],"wp:attachment":[{"href":"https:\/\/99website.top\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/99website.top\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/99website.top\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}