{"id":42798,"date":"2024-12-01T15:19:01","date_gmt":"2024-12-01T07:19:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42798\/"},"modified":"2024-12-01T15:19:01","modified_gmt":"2024-12-01T07:19:01","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88%e6%8c%87%e9%92%88%e8%b5%8b%e5%80%bc%e4%bc%9a%e5%af%bc%e8%87%b4%e5%8f%98%e9%87%8f%e8%b5%8b%e5%80%bc%e4%b8%8d%e6%80%bb%e6%98%af%e7%b2%98%e4%bd%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42798\/","title":{"rendered":"\u4e3a\u4ec0\u4e48\u6307\u9488\u8d4b\u503c\u4f1a\u5bfc\u81f4\u53d8\u91cf\u8d4b\u503c\u4e0d\u603b\u662f\u7c98\u4f4f\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>\u4e3a\u4ec0\u4e48\u6307\u9488\u8d4b\u503c\u4f1a\u5bfc\u81f4\u53d8\u91cf\u8d4b\u503c\u4e0d\u603b\u662f\u7c98\u4f4f\uff1f<\/span><\/p>\n<h1>\u4e3a\u4ec0\u4e48\u6307\u9488\u8d4b\u503c\u4f1a\u5bfc\u81f4\u53d8\u91cf\u8d4b\u503c\u4e0d\u603b\u662f\u7c98\u4f4f\uff1f<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 09:54:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u4e3a\u4ec0\u4e48\u6307\u9488\u8d4b\u503c\u4f1a\u5bfc\u81f4\u53d8\u91cf\u8d4b\u503c\u4e0d\u603b\u662f\u7c98\u4f4f\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u7d22\u5f15\u7684\u6307\u9488\u5206\u914d\u5728 <code>adddata(..)<\/code> \u4e2d\u59cb\u7ec8\u4e0d\u4e00\u81f4\u3002\u6211\u9884\u8ba1\u5185\u5b58\u5730\u5740\u4f1a\u968f\u7740\u5e95\u5c42\u6570\u7ec4\u5927\u5c0f\u7684\u589e\u52a0\u800c\u79fb\u52a8\u3002<\/p>\n<p>\u884c\u4e3a\uff1a\u6211\u5206\u914d\u7ed9\u53d8\u91cf <code>a<\/code>\uff0c\u7136\u540e\u5206\u914d <code>b = a*0.2<\/code>\uff0c\u7136\u540e\u5206\u914d <code>y = sig(b)<\/code>\uff0c\u6700\u540e <code>b = y<\/code>\u3002\u6709\u65f6\u5728\u4e0b\u4e00\u4e2a\u5faa\u73af\u4e2d <code>b == y<\/code> || <code>b == a*0.2<\/code>\u3002\u5b83\u5728\u591a\u6b21\u6267\u884c\u4e2d\u5b8c\u5168\u4e00\u81f4\u3002<\/p>\n<p>\u6211\u5236\u4f5c\u4e86\u4e00\u4e2a\u66f4\u7b80\u5355\u3001\u66f4\u5b8c\u6574\u7684\u4ee3\u7801\u7248\u672c\u3002<\/p>\n<pre>package main\n\nimport(\n    \"fmt\"\n    \"math\"\n)\n\nfunc main(){\n    \/\/structure setup\n    l := lots{}; l.adddata(2); l.adddata(2); l.adddata(2); l.adddata(2)\n    \n    l.val[0].y[0] = 0.20700021\n    l.val[0].y[1] = 0.30003001\n    l.propagate()\n}\n\ntype data struct{\n    y []float64\n}\n\ntype pair struct {\n    one *data\n    two *data\n}\n\n\/\/ lots is the biggest part of the structure\n\/\/ the problem seems to occure when this is introduced\ntype lots struct{\n    val []data\n    join []pair\n}\n\n\/\/ adddata appends a data struct and a pair struct to\n\/\/ the corresponding parts of lots struct\nfunc (l *lots)adddata(size int){\n    l.val = append(l.val, data{make([]float64, size)})\n    \n    \/\/ should be skipped first call only\n    if(len(l.join) &lt; len(l.val)-1){\n        fmt.println(\"len of l.val: \", len(l.val)) \n        l.join = append(l.join, pair{})\n        l.join[len(l.join)-1].one = &amp;l.val[len(l.val)-2]\n        l.join[len(l.join)-1].two = &amp;l.val[len(l.val)-1]\n    }\n}\n\n\/\/ propagate \nfunc (l *lots)propagate(){\n    for _, v := range l.join{\n        v.travel()\n    }\n}\n\n\/\/ travel modifies values going from p.one -&gt; p.two\nfunc (p *pair) travel(){\n    fmt.println(\"p.one.y: \", p.one.y)\n    p.mathy()\n    fmt.println(\"p.two.y: \", p.two.y)\n    \n    p.two.y = sigmoid(p.two.y)\n    fmt.println(\"p.two.y: \", p.two.y)\n}\n\nfunc (p *pair) mathy(){\n    for i := range p.one.y {\n        p.two.y[i] = p.one.y[i] * p.one.y[i]\n    }\n}\n\n\/\/ sigmoid seems to be causing some problems.\n\/\/ works fine on it's own though\nfunc sigmoid(x []float64)(y []float64){\n    y = make([]float64, len(x))\n    for i := range x{\n        y[i] = 1.\/(1.+math.exp(-x[i]))\n    }\n    return\n}<\/pre>\n<p>\u6211\u5e0c\u671b <code>p.two.y: [#'s]<\/code> \u7684#&#8217;s \u59cb\u7ec8\u7b49\u4e8e <code>p.one.y: [#'s]<\/code> \u7684\u4ee5\u4e0b\u884c\u3002\u6211\u5f97\u5230\u7684\u8f93\u51fa\u5e76\u4e0d\u603b\u662f\u76f8\u7b49\u3002\u6709\u65f6\uff0c<code>p.one.y: [#'s]<\/code> #&#8217;s \u7b49\u4e8e\u4e0a\u4e00\u884c\u4e2d\u7684 <code>p.two.y: [#'s]<\/code>\uff1b\u8be5\u503c\u88ab\u8986\u76d6\uff0c\u7136\u540e\u8be5\u503c\u53c8\u56de\u6765\u4e86\u3002<\/p>\n<pre>p.one.y:  [0.20700021 0.30003001]\np.two.y:  [0.04284908694004409 0.0900180069006001]\/\/\/\/\/\/\/ bad\np.two.y:  [0.5107106330188076 0.5224893174114301]      \/\/ overwritten\np.one.y:  [0.04284908694004409 0.0900180069006001]\/\/\/\/\/\/\/ reappeared\np.two.y:  [0.0018360442515954571 0.008103241566356488]\np.two.y:  [0.5004590109339528 0.5020257993066767]\/\/\/\/ overwritten\np.one.y:  [0.5004590109339528 0.5020257993066767]\/\/\/\/ good\np.two.y:  [0.25045922162499035 0.25202990316950763]\np.two.y:  [0.5622895277500193 0.5626760660176802]<\/pre>\n<p>\u6211\u5c1d\u8bd5\u51cf\u5c11\u51fd\u6570\u5d4c\u5957\uff0c\u5f53\u6211\u5c06\u6240\u6709\u5185\u5bb9\u653e\u5165 <code>propagate()<\/code> \u51fd\u6570\u5e76\u4f7f\u7528 sigmoid \u51fd\u6570\u76f4\u63a5\u5206\u914d\u7ed9 <code>p.two.y[i]<\/code> \u65f6\uff0c\u5b83\u8d77\u4f5c\u7528\u4e86\u3002 \uff08\u4e0b\uff09<\/p>\n<pre>\/\/ propagate \nfunc (l *lots)propagate(){\n    for _, p := range l.join{\n        fmt.println(\"p.one.y: \", p.one.y)\n        \n        for i := range p.one.y {\n            p.two.y[i] = p.one.y[i] * p.one.y[i]\n        }\n        fmt.println(\"p.two.y: \", p.two.y)\n        \n        \/\/ using this extra variable causes the problem of inconsistent assignment\n        \/\/y := make([]float64, len(p.two.y))\n        for i := range p.two.y{\n            \/\/y[i] = 1.\/(1.+math.exp(-p.two.y[i]))\n            p.two.y[i] = 1.\/(1.+math.exp(-p.two.y[i]))\n        }\n        \/\/p.two.y = y\n        \n        fmt.println(\"p.two.y: \", p.two.y)\n    }\n}<\/pre>\n<p>\u8fd9\u4e2a\u7248\u672c\u63d0\u4f9b\u4e86\u5f88\u597d\u7684\u6570\u636e\uff0c\u4f46\u5265\u593a\u4e86\u6211\u559c\u6b22\u7684\u5f88\u591a\u4e13\u4e1a\u77e5\u8bc6\u3002<\/p>\n<pre>p.one.y:  [0.20700021 0.30003001]\np.two.y:  [0.04284908694004409 0.0900180069006001]\np.two.y:  [0.5107106330188076 0.5224893174114301]\/\/\/\/\np.one.y:  [0.5107106330188076 0.5224893174114301]\/\/\/\/ Good\np.two.y:  [0.2608253506784712 0.27299508680906215]\np.two.y:  [0.564839170446528 0.5678280461350629]\/\/\/\/\np.one.y:  [0.564839170446528 0.5678280461350629]\/\/\/\/ Good\np.two.y:  [0.3190432884707219 0.3224286899775631]\np.two.y:  [0.5790910765397528 0.5799160282084651]<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u95ee\u9898\u5728\u4e8e\u6784\u5efa\u8981\u5f15\u7528\u7684\u5207\u7247\u65f6\u7684\u6307\u9488\u5206\u914d\u3002\u5730\u5740\u4e0d\u65ad\u53d8\u5316\u3002<\/p>\n<pre>func main(){\n    var lump []int\n    \n    \/\/ a loop to build a slice of `int`'s from 0 size to 8 size\n    \/\/ and print each index address\n    for i:= 0; i &lt; 8; i++{\n        lump = append(lump, int(i))\n        fmt.printf(\"addr of lump[%v]: %p\\n\",i, &amp;lump[i])\n    }\n    \n    fmt.println()\n    \n    \/\/ a loop to look at the addresses of each index\n    for i := range lump{\n        fmt.printf(\"addr of lump[%v]: %p\\n\",i, &amp;lump[i])\n    }\n}<\/pre>\n<p>\u68c0\u67e5\u672a\u5728\u8fde\u7eed\u5185\u5b58\u4f4d\u7f6e\u4e2d\u521b\u5efa\u7684\u5730\u5740\u3002<\/p>\n<pre>\/\/while building the slice\n\/\/ notice the addresses making big jumps\naddr of lump[0]: 0xc00000a0c8\naddr of lump[1]: 0xc00000a0f8\naddr of lump[2]: 0xc00000e3b0\naddr of lump[3]: 0xc00000e3b8\naddr of lump[4]: 0xc00000c2e0\naddr of lump[5]: 0xc00000c2e8\naddr of lump[6]: 0xc00000c2f0\naddr of lump[7]: 0xc00000c2f8\n\n\/\/after building the slice\n\/\/ notice all address being sequential\naddr of lump[0]: 0xc00000c2c0\naddr of lump[1]: 0xc00000c2c8\naddr of lump[2]: 0xc00000c2d0\naddr of lump[3]: 0xc00000c2d8\naddr of lump[4]: 0xc00000c2e0\naddr of lump[5]: 0xc00000c2e8\naddr of lump[6]: 0xc00000c2f0\naddr of lump[7]: 0xc00000c2f8<\/pre>\n<p>\u60a8\u53ef\u4ee5\u8f6c\u79fb\u5230 c\/c++\uff0c\u5728\u90a3\u91cc\u60a8\u53ef\u4ee5\u968f\u7740\u6570\u7ec4\u5927\u5c0f\u7684\u589e\u52a0\u5904\u7406\u6240\u6709\u5185\u5b58\u8c03\u6574\u3002\u6216\u8005\u5148\u6784\u5efa\u4e00\u4e2a\u5207\u7247\uff0c\u7136\u540e\u518d\u6784\u5efa\u53e6\u4e00\u4e2a\u5207\u7247\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u4e3a\u4ec0\u4e48\u6307\u9488\u8d4b\u503c\u4f1a\u5bfc\u81f4\u53d8\u91cf\u8d4b\u503c\u4e0d\u603b\u662f\u7c98\u4f4f\uff1f \u4e3a\u4ec0\u4e48\u6307\u9488\u8d4b\u503c\u4f1a\u5bfc\u81f4\u53d8\u91cf\u8d4b\u503c\u4e0d\u603b\u662f\u7c98\u4f4f\uff1f \u6765\u6e90\uff1astackoverflow 2024-04-26 09:54:34 0\u6d4f\u89c8 \u6536\u85cf \u6765\u5230\u7c73\u4e91\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u4e3a\u4ec0\u4e48\u6307\u9488\u8d4b\u503c\u4f1a\u5bfc\u81f4\u53d8\u91cf\u8d4b\u503c\u4e0d\u603b\u662f\u7c98\u4f4f\uff1f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u95ee\u9898\u5185\u5bb9 \u7d22\u5f15\u7684\u6307\u9488\u5206\u914d\u5728 adddata(..) \u4e2d\u59cb\u7ec8\u4e0d\u4e00\u81f4\u3002\u6211\u9884\u8ba1\u5185\u5b58\u5730\u5740\u4f1a\u968f\u7740\u5e95\u5c42\u6570\u7ec4\u5927\u5c0f\u7684\u589e\u52a0\u800c\u79fb\u52a8\u3002 \u884c\u4e3a\uff1a\u6211\u5206\u914d\u7ed9\u53d8\u91cf a\uff0c\u7136\u540e\u5206\u914d b = a*0.2\uff0c\u7136\u540e\u5206\u914d y = sig(b)\uff0c\u6700\u540e b = y\u3002\u6709\u65f6\u5728\u4e0b\u4e00\u4e2a\u5faa\u73af\u4e2d b == y || b == a*0.2\u3002\u5b83\u5728\u591a\u6b21\u6267\u884c\u4e2d\u5b8c\u5168\u4e00\u81f4\u3002 \u6211\u5236\u4f5c\u4e86\u4e00\u4e2a\u66f4\u7b80\u5355\u3001\u66f4\u5b8c\u6574\u7684\u4ee3\u7801\u7248\u672c\u3002 package main import( &#8220;fmt&#8221; &#8220;math&#8221; ) func main(){ \/\/structure setup l := lots{}; l.adddata(2); l.adddata(2); l.adddata(2); l.adddata(2) l.val[0].y[0] = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42798","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42798"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42798\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}