{"id":15308,"date":"2024-11-09T15:01:42","date_gmt":"2024-11-09T07:01:42","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=15308"},"modified":"2025-12-26T16:42:56","modified_gmt":"2025-12-26T08:42:56","slug":"implement-a-super-simple-neural-network-in-99-line-codes","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2024\/11\/implement-a-super-simple-neural-network-in-99-line-codes\/","title":{"rendered":"99\u884c\u4ee3\u7801\u6784\u5efa\u6781\u7b80\u7684\u795e\u7ecf\u7f51\u7edc"},"content":{"rendered":"\n\n\n\n<p style=\"margin-top:2px\">\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4e5f\u6ca1\u60f3\u523099\u884c\u4ee3\u7801\u5c31\u591f\u4e86\uff0c\u539f\u4ee5\u4e3a\u91cc\u9762\u6709\u4e2a\u201c\u68af\u5ea6\u4e0b\u964d\u201d\uff0c\u4ee3\u7801\u884c\u6570\u5e94\u8be5\u662f\u6570\u767e\u7ea7\u522b\u5427\uff0c\u5b9e\u9645\u5b8c\u6210\u540e\uff0c\u53d1\u73b0\u52a0\u4e0a\u6ce8\u91ca\uff08\u7ea635%\uff09\u624d99\u884c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u201c\u6781\u7b80\u201d\u795e\u7ecf\u7f51\u7edc\u7684\u7ed3\u6784<\/h4>\n\n\n\n<p>\u5148\u6765\u770b\u201c\u6781\u7b80\u201d\u6709\u591a\u7b80\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e00\u4e2a\u8f93\u5165\u5c42\u3001\u4e00\u4e2a\u8f93\u51fa\u5c42\uff0c\u4e2d\u95f4\u6ca1\u6709\u9690\u85cf\u5c42<\/li>\n\n\n\n<li>\u8f93\u5165\u5c42\u7684\u6837\u672c\u6570\u636e\u5c31\u4e00\u4e2a\u7279\u6027\uff08feature\uff09\uff0c\u603b\u8ba16\u4e2a\u6837\u672c<\/li>\n\n\n\n<li>\u8fd9\u662f\u4e8c\u5206\u7c7b\u95ee\u9898\uff0c\u6240\u4ee5\u8f93\u51fa\u5c42\u5c31\u4e00\u4e2a\u8f93\u51fa\u503c<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">   x -&gt;   w*x + b   -&gt;   logistic function  -&gt; output\n        ----------------------------------\n             |                    |\n             V                    V\n         one neuron     activation function<\/code><\/pre>\n\n\n\n<p>\u5728\u540e\u7eed\u7684\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u6784\u9020\u4e86\u516d\u4e2a\u6837\u672c\u7528\u4e8e\u8be5\u795e\u7ecf\u7f51\u7edc\u7684\u8bad\u7ec3\u3002<\/p>\n\n\n\n<p>\u9274\u4e8e\u8fd9\u4e2a\u201c\u6781\u7b80\u201d\u795e\u7ecf\u7f51\u7edc\uff0c\u6ca1\u6709\u4efb\u4f55\u9690\u85cf\u5c42\uff0c\u6240\u4ee5\uff0c\u8fd9\u4e5f\u662f\u4e00\u4e2a\u5178\u578b\u7684\u201clogistic regression\u201d\u95ee\u9898\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u524d\u7f6e\u77e5\u8bc6<\/h4>\n\n\n\n<p>\u4f60\u9700\u8981\u4e86\u89e3\u5982\u4e0b\u7684\u524d\u7f6e\u77e5\u8bc6\uff0c\u4ee5\u5f88\u597d\u7684\u7406\u89e3\u8be5\u795e\u7ecf\u7f51\u7edc\u7684\u5b9e\u73b0\u4e0e\u8bad\u7ec3\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e86\u89e3\u795e\u7ecf\u7f51\u7edc\u7684\u57fa\u7840\uff1a\u6d45\u5c42\u795e\u7ecf\u7f51\u7edc<\/li>\n\n\n\n<li>\u4e86\u89e3 \u68af\u5ea6\u4e0b\u964d\u7b97\u6cd5\uff0c\u4e86\u89e3\u57fa\u672c\u6700\u4f18\u5316\u7b97\u6cd5\u6982\u5ff5\uff0c\u4e86\u89e3\u94fe\u5f0f\u6cd5\u5219<\/li>\n\n\n\n<li>\u4e86\u89e3 logistic function \u7684\u57fa\u672c\u7279\u6027<\/li>\n\n\n\n<li>\u4e86\u89e3 Python \u548c NumPy \u7684\u57fa\u672c\u4f7f\u7528<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">\u95ee\u9898\u63cf\u8ff0\u4e0e\u7b26\u53f7\u7ea6\u5b9a<\/h4>\n\n\n\n<p>\u7528\u4e8e\u8bad\u7ec3\u7684\u6837\u672c\u6570\u636e\u6709\\((x,\\hat{y}) \\)\uff1a \\( (1,0)\u3001(2,0)\u3001(3,0)\u3001(4,0)\u3001(5,1)\u3001(6,1) \\)\u3002<\/p>\n\n\n\n<p>\u4e00\u4e2a\u5177\u4f53\u7684\u6837\u672c\uff0c\u5728\u4e0b\u9762\u7684\u516c\u5f0f\u4e2d\u901a\u5e38\u4f7f\u7528 \\( (x^{(j)}, y^{(j)}) \\)\u8868\u793a\uff0c \u5176\u4e2d\uff0c\\( j = 1&#8230;m \\)\u3002<\/p>\n\n\n\n<p>\\( \\hat{y} \\)\u5219\u8868\u793a\u6839\u636e\u53c2\u6570\u8ba1\u7b97\u51fa\u7684\u9884\u6d4b\u503c\uff0c\u66f4\u4e3a\u5177\u4f53\u7684 \\( \\hat{y}^{(j)} \\)\u8868\u793a \\(x = x^{(j)} \\)\u65f6\u7684\u9884\u6d4b\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6784\u5efa\u76ee\u6807\u51fd\u6570<\/h4>\n\n\n\n<p>\u4ece\u6837\u672c\u6570\u636e\u53ef\u4ee5\u770b\u5230\uff0c\u8fd9\u662f\u4e00\u4e2a\u4e8c\u5206\u7c7b\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528<code>logistic function<\/code>\u4f5c\u4e3a\u8f93\u51fa\u5c42\u7684\u6fc0\u6d3b\u51fd\u6570\uff0c\u5982\u679c\u8f93\u51fa\u503c\u5927\u4e8e0.5\uff0c\u5219\u9884\u6d4b\u4e3a<code>1<\/code>\uff0c\u5426\u5219\u9884\u6d4b\u4e3a<code>0<\/code>\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u4efb\u4f55\u4e00\u4e2a\u6837\u672c\uff0c\u5c31\u53ef\u4ee5\u5982\u4e0b\u51fd\u6570\u4f5c\u4e3a<code>logistic function<\/code>\u7684\u635f\u5931\u51fd\u6570\\( L \\)\uff1a<\/p>\n\n\n<p>$$<br \/>\nL(y,\\hat{y}) = &#8211; (yln(\\hat{y}) + (1-y)ln(1-\\hat{y}))<br \/>\n$$<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5168\u5c40\u7684\u76ee\u6807\u51fd\u6570\u5c31\u662f\uff1a<\/p>\n\n\n<p>$$<br \/>\n\\begin{aligned}<br \/>\nJ(w,b) &#038; = \\frac{1}{m} \\sum\\limits_{j=0}^{m} L(y^{(j)},\\hat{y}^{(j)}) \\\\<br \/>\n&#038; =  \\frac{1}{m} \\sum\\limits_{j=0}^{m} &#8211; (y^{(j)}ln(\\hat{y}^{(j)}) + (1-y^{(j)})ln(1-\\hat{y}^{(j)}))<br \/>\n\\end{aligned}<br \/>\n$$<\/p>\n\n\n\n<p>\u5176\u4e2d \\( m \\)\u8868\u793a\u603b\u6837\u672c\u6570\u91cf\uff0c\u8fd9\u91cc\u53d6\u503c\u662f6\u3002\u5728\u8fd9\u4e2a\u6781\u7b80\u7684\u795e\u7ecf\u7f51\u7edc\u4e2d \\( \\hat{y} \\)\u6709\u5982\u4e0b\u8ba1\u7b97\u8868\u8fbe\u5f0f\uff1a<\/p>\n\n\n<p>$$<br \/>\n\\hat{y} = \\frac{1}{1+e^{-(wx+b)}}<br \/>\n$$<\/p>\n\n\n\n<p>\u6700\u7ec8\uff0c\u8be5\u795e\u7ecf\u7f51\u7edc\u7684\u53c2\u6570\u6c42\u89e3\uff08\u4e5f\u5c31\u662f\u201c\u8bad\u7ec3\u201d\uff09\u8fc7\u7a0b\uff0c\u5c31\u662f\u6c42\u89e3\u5982\u4e0b\u7684\u6781\u503c\u95ee\u9898\uff1a<\/p>\n\n\n<p>$$<br \/>\n(w,b) = \\min_{w, b} J(w, b) = \\min_{w,b} \\frac{1}{m} \\sum\\limits_{j=0}^{m} L(y^{(j)},\\hat{y}^{(j)})<br \/>\n$$<\/p>\n\n\n\n<p>\u76ee\u6807\u51fd\u6570\u8ba1\u7b97\u7684\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">def cost_function(w_p,b_p,x_p,y_p):\n    c = 0\n    for i in range(m):\n        y = function_f(x_p[i],w_p,b_p)\n        c += -y_p[i]*math.log(y) - (1-y_p[i])*math.log(1-y)\n    return c<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u68af\u5ea6\u8ba1\u7b97<\/h4>\n\n\n\n<p>\u524d\u9762\u4ecb\u7ecd\u4e86\u5f88\u591a\u68af\u5ea6\u7684\u5185\u5bb9\uff0c\u8fd9\u91cc\u4e0d\u518d\u8be6\u8ff0\u3002\u5728\u8fd9\u4e2a\u5177\u4f53\u7684\u95ee\u9898\u4e2d\uff0c\u9700\u8981\u6c42\u89e3\u7684\u68af\u5ea6\u4e3a\uff1a<\/p>\n\n\n<p>$$<br \/>\n(\\frac{\\partial J}{\\partial w},\\frac{\\partial J}{\\partial b})<br \/>\n$$<\/p>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c\u7b80\u5355\u5c55\u793a\u8be5\u68af\u5ea6\u7684\u8ba1\u7b97\uff0c\u4e3b\u8981\u9700\u8981\u4f7f\u7528\u7684\u662f\u94fe\u5f0f\u6cd5\u5219\u548c\u57fa\u672c\u7684\u6c42\u5bfc\/\u5fae\u5206\u8fd0\u7b97\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u4e3a\u4e86\u4fbf\u4e8e\u8ba1\u7b97\uff0c\u8fd9\u91cc\u8bb0\uff1a<\/p>\n\n\n<p>$$<br \/>\n\\begin{aligned}<br \/>\n\\hat{y} &#038; = \\frac{1}{1+e^{-z}} \\\\<br \/>\nz &#038; = w*x + b<br \/>\n\\end{aligned}<br \/>\n$$<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u6839\u636e\u94fe\u5f0f\u6cd5\u5219\u5bb9\u6613\u6709\uff1a<\/p>\n\n\n<p>$$<br \/>\n\\frac{\\partial L}{\\partial w} = \\frac{\\partial L}{\\partial \\hat{y}} * \\frac{\\partial \\hat{y}}{\\partial z} * \\frac{\\partial z}{\\partial w} \\\\<br \/>\n\\frac{\\partial L}{\\partial b} = \\frac{\\partial L}{\\partial \\hat{y}} * \\frac{\\partial \\hat{y}}{\\partial z} * \\frac{\\partial z}{\\partial b}<br \/>\n$$<\/p>\n\n\n\n<p>\u8fd9\u5176\u4e2d\uff0c\\( \\frac{\\partial L}{\\partial \\hat{y}} \\) \u548c \\( \\frac{\\partial \\hat{y}}{\\partial z} \\)\u7565\u6709\u4e00\u4e9b\u8ba1\u7b97\u91cf\uff0c\\( \\frac{\\partial z}{\\partial w} \\) \u548c\\( \\frac{\\partial z}{\\partial b} \\)\u6bd4\u8f83\u7b80\u5355\uff0c\u5177\u4f53\u7684\uff1a<\/p>\n\n\n<p>$$<br \/>\n\\begin{aligned}<br \/>\n\\frac{\\partial L}{\\partial \\hat{y}} * \\frac{\\partial \\hat{y}}{\\partial z} &#038;  = \\hat{y} &#8211; y \\\\<br \/>\n\\frac{\\partial z}{\\partial w} &#038; = x \\\\<br \/>\n\\frac{\\partial z}{\\partial b} &#038; = 1<br \/>\n\\end{aligned}<br \/>\n$$<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u6700\u7ec8\u7684\u68af\u5ea6\u8ba1\u7b97\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n<p>$$<br \/>\n\\begin{aligned}<br \/>\n\\frac{\\partial J}{\\partial w} &#038; = \\frac{1}{m} \\sum\\limits_{j=1}^{m} (\\hat{y}^{(j)} &#8211; y^{(j)})*x^{(j)} \\\\<br \/>\n\\frac{\\partial J}{\\partial b} &#038; = \\frac{1}{m} \\sum\\limits_{j=1}^{m} (\\hat{y}^{(j)} &#8211; y^{(j)})<br \/>\n\\end{aligned}<br \/>\n$$<\/p>\n\n\n\n<p>\u5728\u5b9e\u9645\u7684\u8ba1\u7b97\u4e2d\uff0c\u5148\u901a\u8fc7\u6b63\u5411\u4f20\u64ad\uff08Forward Propagation\uff09\u8ba1\u7b97\u51fa\\( \\hat{y}^{(j)} \\)\uff0c\u7136\u540e\u5728\u8ba1\u7b97\u51fa\u68af\u5ea6\u3002\u6b64\u5916\uff0c\u53ef\u4ee5\u4f7f\u7528<code>NumPy<\/code>\u7684<code>ndarray<\/code>\u7b80\u5316\u8868\u8fbe\uff0c\u540c\u65f6\u589e\u52a0\u8ba1\u7b97\u7684\u5e76\u884c\u6027\u3002\u8fd9\u91cc\u4e3a\u4fbf\u4e8e\u7406\u89e3\uff0c\u5168\u90e8\u90fd\u4f7f\u7528\u6807\u91cf\u8ba1\u7b97\uff0c\u5728\u6587\u7ae0\u7684\u6700\u540e\u4e5f\u63d0\u4f9b\u4e86NumPy\u7684\u5bf9\u5e94\u5b9e\u73b0\u3002<\/p>\n\n\n\n<p>\u6b63\u5411\u4f20\u64ad\u8ba1\u7b97\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># function_f: \n# x   : scalar\n# w   : scalar\n# b   : scalar\ndef function_f(x,w,b):  \n    return 1\/(1+math.exp(-(x*w+b)))<\/code><\/pre>\n\n\n\n<p>\u68af\u5ea6\uff08\u53cd\u5411\u4f20\u64ad\uff09\u8ba1\u7b97\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\"># Gradient caculate \n# x_p: x_train\n# y_p: y_train\n# w_p: current w\n# b_p: current b\ndef gradient_caculate(x_p,y_p,w_p,b_p):\n    gradient_w,gradient_b = (0.,0.)\n    for i in range(m):\n        gradient_w += x_p[i]*(function_f(x_p[i],w_p,b_p)-y_p[i])\n        gradient_b += function_f(x_p[i],w_p,b_p)-y_p[i]\n    return gradient_w,gradient_b<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u68af\u5ea6\u4e0b\u964d\u8fed\u4ee3<\/h4>\n\n\n\n<p>\u8fd9\u91cc\u8bbe\u7f6e\u8fed\u4ee3\u6b21\u6570\u4e3a<code>50000<\/code>\u6b21\uff0c\u5b66\u4e60\u7387\u8bbe\u7f6e\u4e3a<code>0.01<\/code>\uff0c\u5f53\u8fed\u4ee3\u76ee\u6807\u51fd\u6570\u53d8\u5316\u503c\u5c0f\u4e8e<code>0.000001<\/code>\u65f6\u4e5f\u505c\u6b62\u8fed\u4ee3\uff08\u8fd9\u5e76\u4e0d\u662f\u5fc5\u987b\u7684\uff09\u3002\u5177\u4f53\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">iteration_count = 50000\nlearning_rate = 0.01\ncost_reduce_threshold = 0.000001<\/code><\/pre>\n\n\n\n<p>\u4e8e\u662f\u53c8\u5982\u4e0b\u68af\u5ea6\u4e0b\u964d\u8fed\u4ee3\u8fc7\u7a0b\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">cost_last = 0\nfor i in range(iteration_count):\n    grad_w,grad_b = gradient_caculate(x_train,y_train,w,b)\n    w = w - learning_rate*grad_w\n    b = b - learning_rate*grad_b\n    cost_current = cost_function(w,b,x_train,y_train)\n    if i &gt;= iteration_count\/2 and cost_last - cost_current&lt;= cost_reduce_threshold:\n        print(\"iteration: {:5d},cost_current:{:f},cost_last:{:f},cost reduce:{:f}\".format( i+1,cost_current,cost_last,cost_last-cost_current))\n        break\n    if (i+1)%(iteration_count\/10) == 0:\n        print(\"iteration: {:5d},cost_current:{:f},cost_last:{:f},cost reduce:{:f}\".format( i+1,cost_current,cost_last,cost_last-cost_current))\n    cost_last = cost_current<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u9884\u6d4b<\/h4>\n\n\n\n<p>\u5b8c\u6210\u8bad\u7ec3\u540e\uff0c\u5219\u53ef\u4ee5\u5bf9\u8f93\u5165\u503c\u8fdb\u884c\u9884\u6d4b\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">print(\"after the training, parameter w = {:f} and b = {:f}\".format(w,b))\n\nfor i in range(m):\n    y = function_f(x_train[i],w,b)\n    p  = 0\n    if y&gt;= 0.5: p  = 1\n    print(\"sample: x[{:d}]:{:d},y[{:d}]:{:d}; the prediction is {:d} with probability:{:4f}\".format(i,x_train[i],i,y_train[i],p,y))<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u4ea7\u751f\u5982\u4e0b\u7684\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">after the training, parameter w = 5.056985 and b = -22.644516\nsample: x[0]:0,y[0]:0; the prediction is 0 with probability:0.000000\nsample: x[1]:1,y[1]:0; the prediction is 0 with probability:0.000000\nsample: x[2]:2,y[2]:0; the prediction is 0 with probability:0.000004\nsample: x[3]:3,y[3]:0; the prediction is 0 with probability:0.000568\nsample: x[4]:4,y[4]:0; the prediction is 0 with probability:0.081917\nsample: x[5]:5,y[5]:1; the prediction is 1 with probability:0.933417<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5728\u5b8c\u6210\u8bad\u7ec3\u540e\u7684\u8fd9\u4e2a\u6781\u7b80\u795e\u7ecf\u7f51\u7edc\u80fd\u591f\u8f83\u4e3a\u51c6\u786e\u7684\u9884\u6d4b\u6837\u672c\u4e2d\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5b8c\u6574\u7684\u4ee3\u7801<\/h4>\n\n\n\n<p>\u5b8c\u6210\u5728\u7684\u4ee3\u7801\u53ef\u4ee5\u5728 GitHub \u4e0a\u67e5\u770b\uff1a<a href=\"https:\/\/github.com\/orczhou\/ssnn\/\">https:\/\/github.com\/orczhou\/ssnn\/<\/a>  \u3002\u5305\u62ec\u4e86\u4e09\u4e2a\u6bb5\u7a0b\u5e8f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ssnn_ant.py : \u6700\u4e3a\u57fa\u7840\u7684\u4e0a\u8ff0\u795e\u7ecf\u7f51\u7edc\u7684\u5b9e\u73b0<\/li>\n\n\n\n<li>ssnn_ant_np.py : \u4f7f\u7528numpy\u5bf9\u4e0a\u8ff0\u5b9e\u73b0\u8fdb\u884c\u5411\u91cf\u5316<\/li>\n\n\n\n<li>ssnn_ant_tf.py : \u4f7f\u7528 TensorFlow \u6846\u67b6\u5b9e\u73b0\u4e0a\u8ff0\u7a0b\u5e8f<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u91cc\u4e5f\u7b80\u5355\u5217\u51fa\u76f8\u5173\u7a0b\u5e8f\u5982\u4e0b\uff08\u6700\u65b0\u4ee3\u7801\u53ef\u4ee5\u53c2\u8003\u4e0a\u8ff0 GitHub \u4ed3\u5e93\uff09\uff1a<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">ssnn_ant.py <\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python line-numbers\">\"\"\"\nsuper simple neural networks \n  * only one neuron in only the one hidden layer\n  * input x is scalar (one-dimension)\n  * using logistic function as the activation function\n\ninput layer:\n    x: scalar \nparameters: \n    w: scalar\n    b: scalar\noutput:\n    y \\in [0,1] or  p \\in {0,1}\nstructure:\n         \n   x -&gt;   w*x + b   -&gt;   logistic function  -&gt; output\n        -----------      -----------------\n             |                    |\n             V                    V\n         one neuron     activation function\n\nabout it:\n    it's a simple project for human learning how machine learning \n    by orczhou.com\n\"\"\"\nimport numpy as np\nimport math\n\n# function_f: \n# x   : scalar\n# w   : scalar\n# b   : scalar\ndef function_f(x,w,b):  \n    return 1\/(1+math.exp(-(x*w+b)))\n\n# initial w,b\nw,b = (0,0)\n\n# samples\nx_train = np.array([0,1,2,3,4,5])\ny_train = np.array([0,0,0,0,0,1])\n#y_train = np.array([0,0,0,1,1,1])\n\n# m for sample counts\nm = x_train.shape[0]\n\niteration_count = 50000\nlearning_rate   = 0.01\ncost_reduce_threshold = 0.000001\n\n# Gradient caculate \n# x_p: x_train\n# y_p: y_train\n# w_p: current w\n# b_p: current b\ndef gradient_caculate(x_p,y_p,w_p,b_p):\n    gradient_w,gradient_b = (0.,0.)\n    for i in range(m):\n        gradient_w += x_p[i]*(function_f(x_p[i],w_p,b_p)-y_p[i])\n        gradient_b += function_f(x_p[i],w_p,b_p)-y_p[i]\n    return gradient_w,gradient_b\n\ndef cost_function(w_p,b_p,x_p,y_p):\n    c = 0\n    for i in range(m):\n        y = function_f(x_p[i],w_p,b_p)\n        c += -y_p[i]*math.log(y) - (1-y_p[i])*math.log(1-y)\n    return c\n\nabout_the_train = '''\\\ntry to train the model with:\n  learning rate: {:f}\n  max iteration : {:d}\n  cost reduction threshold: {:f}\n\\\n'''\nprint(about_the_train.format(learning_rate,iteration_count,cost_reduce_threshold))\n\n# start training\ncost_last = 0\nfor i in range(iteration_count):\n    grad_w,grad_b = gradient_caculate(x_train,y_train,w,b)\n    w = w - learning_rate*grad_w\n    b = b - learning_rate*grad_b\n    cost_current = cost_function(w,b,x_train,y_train)\n    if i &gt;= iteration_count\/2 and cost_last - cost_current&lt;= cost_reduce_threshold:\n        print(\"iteration: {:5d},cost_current:{:f},cost_last:{:f},cost reduce:{:f}\".format( i+1,cost_current,cost_last,cost_last-cost_current))\n        break\n    if (i+1)%(iteration_count\/10) == 0:\n        print(\"iteration: {:5d},cost_current:{:f},cost_last:{:f},cost reduce:{:f}\".format( i+1,cost_current,cost_last,cost_last-cost_current))\n    cost_last = cost_current\n\nprint(\"after the training, parameter w = {:f} and b = {:f}\".format(w,b))\n\nfor i in range(m):\n    y = function_f(x_train[i],w,b)\n    p  = 0\n    if y&gt;= 0.5: p  = 1\n    print(\"sample: x[{:d}]:{:d},y[{:d}]:{:d}; the prediction is {:d} with probability:{:4f}\".format(i,x_train[i],i,y_train[i],p,y))<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u4f7f\u7528NumPy\u5411\u91cf\u5316 ssnn_ant_np.py <\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">\"\"\"\nsuper simple neural networks(using numpy,snn.py not using numpy)\n  * only one neuron in only the one hidden layer\n  * input x is scalar (0-dimension)\n  * using logistic function as the activation function\n\ninput layer:\n    x: scalar\nparameters:\n    w: scalar\n    b: scalar\noutput:\n    y \\in [0,1] or  p \\in {0,1}\nstructure:\n\n   x -&gt;   w*x + b   -&gt;   logistic function  -&gt; output\n        -----------      -----------------\n             |                    |\n             V                    V\n         one neuron     activation function\n\nabout it:\n    it's a simple project for human learning how machine learning\n    by orczhou.com\n\"\"\"\nimport numpy as np\nimport math\n\n# function_f:\n# x   : scalar or ndarray\n# w   : scalar\n# b   : scalar\ndef function_f(x,w,b):\n    return 1\/(1+np.exp(-(x*w+b)))\n\n# initial w,b\nw,b = (0,0)\n\n# samples\nx_train = np.array([0,1,2,3,4,5])\ny_train = np.array([0,0,0,0,0,1])\n#y_train = np.array([0,0,0,1,1,1])\n\n# m for sample counts\nm = x_train.shape[0]\n\niteration_count = 50000\nlearning_rate   = 0.01\ncost_reduce_threshold = 0.000001\n\n# Gradient caculate\n# w_p: current w\n# b_p: current b\ndef gradient_caculate(w_p,b_p):\n    gradient_w = np.sum((function_f(x_train,w_p,b_p) - y_train)*x_train)\n    gradient_b = np.sum(function_f(x_train,w_p,b_p) - y_train)\n    return gradient_w,gradient_b\n\ndef cost_function(w_p,b_p,x_p,y_p):\n    hat_y = function_f(x_p,w_p,b_p)\n    c = np.sum(-y_p*np.log(hat_y) - (1-y_p)*np.log(1-hat_y))\n    return c\/m\n\nabout_the_train = '''\\\ntry to train the model with:\n  learning rate: {:f}\n  max iteration : {:d}\n  cost reduction threshold: {:f}\n\\\n'''\nprint(about_the_train.format(learning_rate,iteration_count,cost_reduce_threshold))\n\n# start training\ncost_last = 0\nfor i in range(iteration_count):\n    grad_w,grad_b = gradient_caculate(w,b)\n    w = w - learning_rate*grad_w\n    b = b - learning_rate*grad_b\n    cost_current = cost_function(w,b,x_train,y_train)\n    if i &gt;= iteration_count\/2 and cost_last - cost_current&lt;= cost_reduce_threshold:\n        print(\"iteration: {:5d},cost_current:{:f},cost_last:{:f},cost reduce:{:f}\".format( i+1,cost_current,cost_last,cost_last-cost_current))\n        break\n    if (i+1)%(iteration_count\/10) == 0:\n        print(\"iteration: {:5d},cost_current:{:f},cost_last:{:f},cost reduce:{:f}\".format( i+1,cost_current,cost_last,cost_last-cost_current))\n    cost_last = cost_current\n\nprint(\"after the training, parameter w = {:f} and b = {:f}\".format(w,b))\n\nfor i in range(m):\n    y = function_f(x_train[i],w,b)\n    p  = 0\n    if y&gt;= 0.5: p  = 1\n    print(\"sample: x[{:d}]:{:d},y[{:d}]:{:d}; the prediction is {:d} with probability:{:4f}\".format(i,x_train[i],i,y_train[i],p,y))<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h5 class=\"wp-block-heading\">\u4f7f\u7528TensorFlow\u5b9e\u73b0\u8be5\u529f\u80fd<\/h5>\n\n\n\n<p>\u8fd9\u91cc\u4e5f\u662f\u4f7f\u7528 TensorFlow \u5bf9\u4e0a\u8ff0\u95ee\u9898\u4e2d\u7684\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\u5e76\u9884\u6d4b\u3002\u8be6\u7ec6\u4ee3\u7801\u548c TensorFlow \u8f93\u51fa\u53c2\u8003\u5c0f\u7ed3\u201cTensorFlow \u4ee3\u7801\u201d\u548c\u201cTensorFlow\u7684\u8f93\u51fa\u201d\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5bf9\u5176\u8bad\u7ec3\u7ed3\u679c\u505a\u7b80\u8981\u7684\u5206\u6790\u3002\u5728\u8f93\u51fa\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230\u8bad\u7ec3\u540e\u7684\u53c2\u6570\u5206\u522b\u662f\uff1a\\(  w = 1.374991  \\quad b =  -5.9958787 \\)\uff0c\u90a3\u4e48\u5bf9\u5e94\u7684\u9884\u6d4b\u8868\u8fbe\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<p>$$ \\frac{1}{1+e^{-(w*x+b)}} $$<\/p>\n\n\n\n<p>\u4ee3\u5165 \\(  x = 1 \\)\uff0c\u5176\u8ba1\u7b97\u7ed3\u679c\u4e3a\uff1a\\( np.float64(0.009748092866213252) \\)\uff0c\u8fd9\u4e0e TensorFlow \u8f93\u51fa\u7684 \\( [0.00974809] \\) \u662f\u4e00\u81f4\u7684\uff0c\u8fd9\u4e5f\u9a8c\u8bc1\u4e86\u8bad\u7ec3\u7a0b\u5e8f\u5176\u5b9e\u73b0\u4e0e\u7406\u89e3\u7684\u4e8b\u5b8c\u5168\u4e00\u81f4\u7684\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">TensorFlow \u4ee3\u7801 ssnn_ant_tf.py <\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import tensorflow as tf\nimport numpy as np\n\ntf.random.set_seed(1)\nX_train = np.array([[1], [2], [3], [4], [5],[6]], dtype=float)\ny_train = np.array([[0], [0], [0], [0], [1],[1]], dtype=float)\n\nmodel = tf.keras.Sequential([\n    tf.keras.layers.Input(shape=(1,)),\n    tf.keras.layers.Dense(units=1, activation='sigmoid')\n])\n\n# model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])\nmodel.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.1), loss='binary_crossentropy', metrics=['accuracy'])\n\nmodel.fit(X_train, y_train, epochs=1000, verbose=0)\nmodel.summary()\n\nmodel.evaluate(X_train,  y_train, verbose=2)\n\npredictions = model.predict(X_train)\nprint(\"Predictions:\", predictions)\n\nfor layer in model.layers:\n    weights, biases = layer.get_weights()\n    print(\"weights::\", weights)\n    print(\"biases:\", biases)<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">TensorFlow\u7684\u8f93\u51fa<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">Model: \"sequential\"\n\u250f\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2533\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2513\n\u2503 Layer (type)                         \u2503 Output Shape                \u2503         Param # \u2503\n\u2521\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2547\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2547\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2529\n\u2502 dense (Dense)                        \u2502 (None, 1)                   \u2502               2 \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n Total params: 4 (20.00 B)\n Trainable params: 2 (8.00 B)\n Non-trainable params: 0 (0.00 B)\n Optimizer params: 2 (12.00 B)\n1\/1 - 0s - 32ms\/step - accuracy: 1.0000 - loss: 0.1856\n1\/1 \u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501\u2501 0s 10ms\/step\nPredictions: \n [[0.00974809]\n [0.03747462]\n [0.13343701]\n [0.37850127]\n [0.7066308 ]\n [0.90500087]]\nweights:: [[1.374991]]\nbiases: [-5.9958787]<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8865\u5145\u8bf4\u660e<\/h4>\n\n\n\n<p>\u4e00\u4e2a\u4e00\u822c\u7684\u524d\u9988\u795e\u7ecf\u7f51\u7edc\uff08FNN\uff09\u901a\u5e38\u81f3\u5c11\u9700\u8981\u4e00\u4e2a\u9690\u85cf\u5c42\uff0c\u5e76\u4e14\u5728\u9690\u85cf\u5c42\u6709\u591a\u4e2a\u795e\u7ecf\u5143\u3002\u4e00\u4e2a\u5177\u6709\u591a\u4e2a\u795e\u7ecf\u5143\u7684\u591a\u5c42\u7f51\u7edc\u7684\u7ed3\u6784\u548c\u8bad\u7ec3\uff0c\u5176\u590d\u6742\u5ea6\u4f1a\u9ad8\u5f88\u591a\uff0c\u540e\u7eed\u4f1a\u518d\u505a\u4ecb\u7ecd\u3002\u672c\u6587\u5b9e\u73b0\u4ee3\u7801\u867d\u7136\u53ea\u670999\u884c\u4ee3\u7801\uff0c\u53bb\u6389\u6ce8\u91ca\u53ea\u670970\u884c\u4ee3\u7801\uff0c\u4f46\u9ebb\u96c0\u867d\u5c0f\u3001\u4e94\u810f\u4ff1\u5168\uff0c\u5305\u542b\u4e86\u68af\u5ea6\u4e0b\u964d\u7684\u5b9e\u73b0\u3001\u94fe\u5f0f\u6cd5\u5219\u7684\u5e94\u7528\u7b49\uff0c\u5982\u679c\u7406\u89e3\u4e86\u8be5\u793a\u4f8b\uff0c\u5219\u53ef\u4ee5\u5f88\u597d\u5e2e\u52a9\u5f00\u53d1\u8005\u6253\u597d\u57fa\u7840\uff0c\u4ee5\u4fbf\u66f4\u597d\u7684\u7406\u89e3\u66f4\u4e3a\u590d\u6742\u7684\u795e\u7ecf\u7f51\u7edc\u7684\u8bad\u7ec3\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u5728\u8ba1\u7b97\u4e2d\u7701\u7565\u4e86\u4e00\u4e9b\u6c42\u5bfc\u8ba1\u7b97\uff0c\u5176\u4e2d\u7565\u5fae\u6709\u4e00\u4e9b\u590d\u6742\u5ea6\u7684\u662f \\( \\frac{\\partial L}{\\partial \\hat{y}} * \\frac{\\partial \\hat{y}}{\\partial z} \\)\uff0c\u611f\u5174\u8da3\u7684\u53ef\u4ee5\u81ea\u884c\u8865\u5168\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u4e5f\u6ca1\u60f3\u523099\u884c\u4ee3\u7801\u5c31\u591f\u4e86\uff0c\u539f\u4ee5\u4e3a\u91cc\u9762\u6709\u4e2a\u201c\u68af\u5ea6\u4e0b\u964d\u201d\uff0c\u4ee3\u7801\u884c\u6570\u5e94\u8be5\u662f\u6570\u767e\u7ea7\u522b\u5427\uff0c\u5b9e\u9645\u5b8c\u6210\u540e\uff0c\u53d1\u73b0\u52a0\u4e0a\u6ce8\u91ca\uff08\u7ea635%\uff09\u624d99\u884c\u3002<\/p>\n","protected":false},"author":1,"featured_media":20620,"comment_status":"open","ping_status":"closed","sticky":false,"template":"wp-custom-template-a-1440-px-width-template","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[137],"tags":[],"class_list":["post-15308","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-learning-more"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/15308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=15308"}],"version-history":[{"count":75,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/15308\/revisions"}],"predecessor-version":[{"id":21983,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/15308\/revisions\/21983"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media\/20620"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=15308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=15308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=15308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}