{"id":141,"date":"2023-08-04T01:08:54","date_gmt":"2023-08-03T17:08:54","guid":{"rendered":"https:\/\/www.kakosci.com\/?p=141"},"modified":"2023-10-28T09:21:16","modified_gmt":"2023-10-28T01:21:16","slug":"pytorch%e5%ae%9e%e7%8e%b0%e5%b9%bf%e4%b9%89%e5%9b%9e%e5%bd%92%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9cgrnn","status":"publish","type":"post","link":"https:\/\/www.kakosci.com\/index.php\/2023\/08\/04\/pytorch%e5%ae%9e%e7%8e%b0%e5%b9%bf%e4%b9%89%e5%9b%9e%e5%bd%92%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9cgrnn\/","title":{"rendered":"Pytorch\u5b9e\u73b0\u5e7f\u4e49\u56de\u5f52\u795e\u7ecf\u7f51\u7edcGRNN"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>\u3010\u60b2\u62a5\u3011<\/strong>\u8fd9\u7bc7\u6587\u7ae0\u7684\u4ee3\u7801\u6ca1\u6709\u6279\u91cf\u9884\u6d4b\uff0c\u770b\u4e86\u57fa\u672c\u539f\u7406\u540e\u53ef\u4ee5\u79fb\u6b65\u65b0\u6587\u7ae0\uff1a<a href=\"https:\/\/www.kakosci.com\/index.php\/2023\/10\/27\/tensor%ef%bc%8c%e5%b9%bf%e6%92%ad%e6%9c%ba%e5%88%b6%e4%b8%8egrnn%e9%87%8d%e6%9e%84\/\">Tensor\uff0c\u5e7f\u64ad\u673a\u5236\u4e0eGRNN\u91cd\u6784 \u2013 KAKO Academy of Sciences (kakosci.com)<\/a> \uff0c\u975e\u5e38\u597d\u66f4\u65b0\uff0c\u4f7f\u6211\u7684\u5f20\u91cf\u5347\u7ef4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">General regression neural network\uff0c\u7ef4\u57fa\u767e\u79d1\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/General_regression_neural_network\" target=\"_blank\">General regression neural network &#8211; Wikipedia<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u7bc7\u6db5\u76d6\u4e86\u5e7f\u4e49\u56de\u5f52\u795e\u7ecf\u7f51\u7edc\u7684\u539f\u7406\u53ca\u5176\u63a8\u5bfc\uff0c\u5176\u795e\u7ecf\u7f51\u7edc\u7684\u6784\u5efa\uff0c\u5e76\u6839\u636e\u5176\u9ad8\u5ea6\u53ef\u5e76\u884c\u5316\u7684\u7279\u5f81\uff0c\u4f7f\u7528\u4e86Pytorch\u6765\u6784\u5efa\u7b97\u6cd5\uff0c\u5229\u7528GPU\u6765\u52a0\u901f\u7b97\u6cd5\u7684\u8fd0\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bfe\u9898\u9879\u76ee\u7684\u7f18\u6545\u5f00\u59cb\u63a5\u89e6\u8fd9\u4e2a\u7b97\u6cd5\uff0c\u4f46\u4f3c\u4e4e\u627e\u4e0d\u5230\u54ea\u4e2a\u5305\u91cc\u6709\u80fd\u76f4\u63a5\u8c03\u7528\u7684\u5b9e\u73b0\uff1f\u4e0d\u8fc7\u53cd\u6b63\u4e5f\u4e0d\u662f\u5f88\u590d\u6742\uff0c\u76f4\u63a5\u6765\u52a8\u624b\u5b9e\u73b0\u597d\u5566\u3002\u5148\u6839\u636e\u8bba\u6587<sup>[1]<\/sup>\u6574\u7406\u4e00\u4e0b\u7b97\u6cd5\u7684\u539f\u7406\u548c\u6211\u7684\u63a8\u5bfc\u601d\u8def\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u5b9a\u5f53\u524d\u5b58\u5728\u4e00\u4e2a\u5df2\u77e5\u7684\u8054\u5408\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\uff08probability density function\uff0c\u540e\u6587\u7b80\u79f0<strong>pdf<\/strong>\uff09\uff1a\\(f\\left (x,y \\right)\\)\uff0c\u5176\u4e2d\\(x\\)\u4ee3\u8868\u4e00\u4e2a\u968f\u673a\u5411\u91cf\uff0c\\(y\\)\u5219\u4ee3\u8868\u4e00\u4e2a\u6807\u91cf\u7684\u968f\u673a\u53d8\u91cf\uff0c\u73b0\u5728\u7684\u95ee\u9898\u662f\uff1a\u5728\\(x\\)\u4e3a\u7279\u5b9a\u503c\\(X\\)\u7684\u6761\u4ef6\u4e0b\uff0c\\(y\\)\u7684\u671f\u671b\u662f\u591a\u5c11\uff1f\u5373\u6c42\u6761\u4ef6\u671f\u671b\\(E\\left [y|X \\right]\\)\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u4e0a\u4e00\u4e2a\u6761\u4ef6\u6982\u7387\u516c\u5f0f\uff1a$$f_{y|X}(y|X)=\\frac{f(X,y)}{f_{X}(X)}\\cdots (1)$$<br>\u5176\u4e2d\\(f_{y|X}\\)\u662f\u6761\u4ef6\u6982\u7387\u5bc6\u5ea6\uff0c\\(f\\)\u662f\u6700\u5f00\u59cb\u8bf4\u7684pdf\uff0c\u800c\\(f_{X}\\)\u5219\u4ee3\u8868\u5411\u91cf\\(x\\)\u7684\u6982\u7387\u5bc6\u5ea6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u5f00\u59cb\u5c55\u5f00\u6761\u4ef6\u671f\u671b\uff1a$$E[y|X]=\\int_{-\\infty}^{\\infty}yf_{y|X}(y|X)\\mathrm{d}y$$\u4e8e\u662f\u6839\u636e\u516c\u5f0f(1)\uff1a$$=\\int_{-\\infty}^{\\infty}\\frac{yf(X,y)}{f_{X}(X)}\\mathrm{d}y=\\frac{\\int_{-\\infty}^{\\infty}yf(X,y)\\mathrm{d}y}{f_{X}(X)}$$\u6700\u7ec8\u5f97\u5230\uff1a$$E[y|X]=\\frac{\\int_{-\\infty}^{\\infty}yf(X,y)\\mathrm{d}y}{\\int_{-\\infty}^{\\infty}f(X,y)\\mathrm{d}y}\\cdots (2)$$<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u73b0\u5728\u6211\u4eec\u628a\u76ee\u5149\u653e\u5728\u6c42\\(f(X,y)\\)\u4e0a\u6765\uff0c\u4e8e\u662f\u6b63\u5f0f\u8fdb\u5165GRNN\u7b97\u6cd5\u7684\u6838\u5fc3\u5185\u5bb9\u4e4b\u2014\u2014\u9ad8\u65af\u51fd\u6570\uff0c\u5e76\u5c06\u6837\u672c\u5f15\u5165\u5230\u7b97\u6cd5\u4e2d\uff0c\u5229\u7528\u6837\u672c\u6765\u4f30\u8ba1\u6982\u7387\u5bc6\u5ea6\u3002\u8bbe\\(x\\)\u548c\\(y\\)\u7684\u5df2\u77e5\u6837\u672c\u4e3a\\(X^{i}\\)\u548c\\(Y^{i}\\)\uff0c\\(i=1,2,\\dots,n\\)\uff0c\\(n\\)\u662f\u6837\u672c\u6570\u91cf\uff0c\u8bbe\\(p\\)\u4e3a\u5411\u91cf\\(x\\)\u7684\u7ef4\u6570\uff0c\u4e0b\u9762\u6211\u5c06\u7ed9\u51fa\u4e00\u4e32<strong>\u53c8\u81ed\u53c8\u957f)-:<\/strong>\u7684\u516c\u5f0f\uff1a<br>$$\\hat{f}(X,y)=\\frac{1}{n}\\sum_{i=1}^{n}\\prod_{j=1}^{p}[\\frac{1}{\\sqrt{2\\pi}\\sigma}exp(-\\frac{(X_j-X_j^i)^2}{2\\sigma^2})]\\cdot\\frac{1}{\\sqrt{2\\pi}}exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})$$<br>$$=\\frac{1}{(2\\pi)^{\\frac{(p+1)}{2}}\\sigma^{(p+1)}}\\frac{1}{n}\\sum_{i=1}^{n}exp(-\\frac{(X-X^i)^{T}(X-X^i)}{2\\sigma^2})exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\cdots (3)$$<br>\u4ed4\u7ec6\u770b\u5c31\u4f1a\u660e\u767d\uff0c\u516c\u5f0f(3)\u5b9e\u9645\u4e0a\u505a\u7684\u4e8b\u60c5\u5c31\u662f\u5148\u6784\u5efa\\(p+1\\)\u7ef4\u7684\u8054\u5408\u9ad8\u65af\u5206\u5e03\uff0c\\(p\\)\u7ef4\u6765\u81ea\\(x\\)\u800c\u52a0\u4e0a\u7684\u4e00\u4e2a\u7ef4\u5ea6\u5c31\u662f\\(y\\)\u3002\u63a5\u7740\u6839\u636e\\(n\\)\u4e2a\u6837\u672c\u751f\u6210\u4e86\\(n\\)\u4e2a\u9ad8\u65af\u5206\u5e03\uff0c\u53d6\u5bf9\u5e94\u6837\u672c\u7684\u503c\u4f5c\u4e3a\u6bcf\u4e2a\u9ad8\u65af\u5206\u5e03\u7684\u5747\u503c\\(\\mu\\)\uff0c\u4f46\u65b9\u5dee\\(\\sigma\\)\u7edf\u4e00\uff0c\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u65b9\u5dee\u5c06\u662fGRNN\u552f\u4e00\u7684\u8d85\u53c2\u6570\uff0c\u4e5f\u53ef\u4ee5\u79f0\u4e3a<strong>\u5e73\u6ed1\u53c2\u6570<\/strong>\u3002\u6700\u540e\u5c06\u8fd9\\(n\\)\u4e2a\u9ad8\u65af\u5206\u5e03\u52a0\u8d77\u6765\u518d\u9664\u4ee5\\(n\\)\uff0c\u4ee5\u4fdd\u8bc1\u751f\u6210\u7684\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u5728\u6574\u4e2a\u7a7a\u95f4\u4e0a\u7684\u79ef\u5206\u4e3a1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u8fd9\u4e00\u6b65\u5176\u5b9e\u5f88\u5bb9\u6613\u7406\u89e3\uff0c\u60f3\u8c61\u5728\u7ecf\u5178\u7684\u4e8c\u7ef4\u5750\u6807\u7cfb\u4e2d\uff0cx\u8f74\u4e0a\u6709\u8bb8\u591a\u70b9\uff0c\u4ee5\u6bcf\u4e00\u4e2a\u70b9\u4e3a\u4e2d\u5fc3\u6784\u5efa\u65b9\u5dee\u4e00\u81f4\u7684\u6b63\u6001\u5206\u5e03\uff0c\u5e76\u52a0\u5728\u4e00\u8d77\u518d\u9664\u4ee5\u70b9\u7684\u4e2a\u6570\uff0c\u5c31\u5f97\u5230\u4e86\u4e00\u4e2a\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\u3002\u6700\u7ec8\u5728x\u8f74\u4e0a\u7684\u70b9\u5bc6\u96c6\u7684\u5730\u65b9\u6982\u7387\u5bc6\u5ea6\u5c31\u9ad8\uff0c\u70b9\u7a00\u758f\u7684\u5730\u65b9\u6982\u7387\u5bc6\u5ea6\u5c31\u4f4e\uff0c\u800c\u4e00\u5f00\u59cb\u7edf\u4e00\u7684\u65b9\u5dee\u5219\u51b3\u5b9a\u4e86\u6700\u7ec8\u56fe\u50cf\u7684\u5f62\u72b6\uff0c\u65b9\u5dee\u9ad8\u5219\u6982\u7387\u5bc6\u5ea6\u66f4\u5747\u5300\uff0c\u8d77\u4f0f\u66f4\u7f13\uff0c\u53cd\u4e4b\u5219\u66f4\u9661\uff0c\u6545\u79f0\u5e73\u6ed1\u53c2\u6570\u3002\u4e8e\u662f\u6839\u636e\u5f97\u5230\u7684\u6982\u7387\u5bc6\u5ea6\u51fd\u6570\uff0c\u6211\u4eec\u5c31\u80fd\u591f\u4f30\u8ba1\u65b0\u6765\u7684\u70b9\u843d\u5728x\u8f74\u67d0\u4e2a\u4f4d\u7f6e\u7684\u6982\u7387\u662f\u591a\u5c11\u3002\uff08\u4ece\u8fd9\u91cc\u4e5f\u80fd\u770b\u51fa\u5582\u7ed9GRNN\u7684\u8bad\u7ec3\u6837\u672c\u4e00\u5b9a\u8981\u8986\u76d6\u7684\u60c5\u51b5\u591f\u5168\u9762\uff0c\u8be5\u7b97\u6cd5\u5728\u7f3a\u4e4f\u8bad\u7ec3\u7684\u7a7a\u95f4\u4e0a\u51e0\u4e4e\u6ca1\u6709\u9884\u6d4b\u80fd\u529b:-\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f97\u5230\u6982\u7387\u5bc6\u5ea6\\(f(X,y)\\)\u7684\u4f30\u8ba1\u4e4b\u540e\uff0c\u5c31\u53ef\u4ee5\u56de\u5230\u516c\u5f0f(2)\uff0c\u5f00\u59cb\u8ba1\u7b97\u6761\u4ef6\u671f\u671b\u4e86\uff0c\u6b64\u65f6\u516c\u5f0f(2)\u6539\u6210\u5982\u4e0b\u5f62\u5f0f\uff1a$$\\hat{Y}(X)=\\frac{\\int_{-\\infty}^{\\infty}y\\hat{f}(X,y)\\mathrm{d}y}{\\int_{-\\infty}^{\\infty}\\hat{f}(X,y)\\mathrm{d}y}$$<br>\u5e26\u5165\u516c\u5f0f(3)\uff0c\u6d88\u53bb\u7cfb\u6570\uff1a$$=\\frac{\\int_{-\\infty}^{\\infty}y\\sum_{i=1}^{n}exp(-\\frac{(X-X^i)^{T}(X-X^i)}{2\\sigma^2})exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\mathrm{d}y}{\\int_{-\\infty}^{\\infty}\\sum_{i=1}^{n}exp(-\\frac{(X-X^i)^{T}(X-X^i)}{2\\sigma^2})exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\mathrm{d}y}$$<br>\u6c42\u548c\u7684\u79ef\u5206\u7b49\u4e8e\u79ef\u5206\u7684\u6c42\u548c\uff1a$$=\\frac{\\sum_{i=1}^{n}\\int_{-\\infty}^{\\infty}y\\cdot exp(-\\frac{(X-X^i)^{T}(X-X^i)}{2\\sigma^2})exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\mathrm{d}y}{\\sum_{i=1}^{n}\\int_{-\\infty}^{\\infty}exp(-\\frac{(X-X^i)^{T}(X-X^i)}{2\\sigma^2})exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\mathrm{d}y}$$<br>$$=\\frac{\\sum_{i=1}^{n}exp(-\\frac{(X-X^i)^{T}(X-X^i)}{2\\sigma^2})\\int_{-\\infty}^{\\infty}y\\cdot exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\mathrm{d}y}{\\sum_{i=1}^{n}exp(-\\frac{(X-X^i)^{T}(X-X^i)}{2\\sigma^2})\\int_{-\\infty}^{\\infty}exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\mathrm{d}y}$$<br>\u73b0\u5728\u5bf9\u516c\u5f0f\u5206\u5b50\u5206\u6bcd\u540e\u65b9\u7684\u79ef\u5206\u5f0f\u5b50\u8fdb\u884c\u5316\u7b80\uff0c\u5177\u4f53\u65b9\u6cd5\u5c31\u662f\u6784\u9020\u9ad8\u65af\u51fd\u6570\uff1a<br>$$\\int_{-\\infty}^{\\infty}exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\mathrm{d}y=\\sqrt{2\\pi}\\sigma\\int_{-\\infty}^{\\infty}\\frac{1}{\\sqrt{2\\pi}\\sigma}exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\mathrm{d}y=\\sqrt{2\\pi}\\sigma$$<br>$$\\int_{-\\infty}^{\\infty}y\\cdot exp(-\\frac{(Y-Y^i)^2}{2\\sigma^2})\\mathrm{d}y=\\sqrt{2\\pi}\\sigma\\int_{-\\infty}^{\\infty}y\\cdot N(Y^i,\\sigma^2)\\mathrm{d}y=\\sqrt{2\\pi}\\sigma\\cdot Y^i$$<br>\u8ba1\u7b97\u5373\u5c06\u5b8c\u6210\uff0c\u8fd9\u91cc\u6211\u4eec\u5148\u8bbe\\(D_i^2=(X-X^i)^{T}(X-X^i)\\)\uff0c\u5373\u6d4b\u8bd5\u6837\u672c\\(X\\)\u4e0e\u6bcf\u4e2a\u8bad\u7ec3\u6837\u672c\u4e4b\u95f4\u6b27\u6c0f\u8ddd\u79bb\u7684\u5e73\u65b9\u3002<br>\u6700\u7ec8\u5f97\u5230\u4e86GRNN\u7684\u516c\u5f0f\uff1a$$\\hat{Y}(X)=\\frac{\\sum_{i=1}^{n}Y^{i}exp(-\\frac{D_i^2}{2\\sigma^2})}{\\sum_{i=1}^{n}exp(-\\frac{D_i^2}{2\\sigma^2})}\\cdots (4)$$<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u8bf4GRNN\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u53ea\u6709\u524d\u5411\u4f20\u64ad\u7684\u7edf\u8ba1\u5b66\u4e60\u65b9\u6cd5\u2026\u2026\u800c\u56e0\u4e3a\u6ca1\u6709\u9700\u8981\u88ab\u8fed\u4ee3\u4f18\u5316\u7684\u53c2\u6570\uff0c\u5b83\u7684\u8bad\u7ec3\u8fc7\u7a0b\u53ea\u6709\u63a5\u6536\u8bad\u7ec3\u6837\u672c\u8fd9\u4e00\u6b65\u9aa4\u800c\u5df2\uff0c\u53cd\u800c\u5728\u9884\u6d4b\u65f6\u6d4b\u8bd5\u6837\u672c\u9700\u8981\u8ddf\u6bcf\u4e00\u4e2a\u8bad\u7ec3\u6837\u672c\u53d1\u751f\u4f5c\u7528\uff0c\u8017\u65f6\u96c6\u4e2d\u5728\u9884\u6d4b\u8fd9\u4e00\u6b65\u9aa4\u3002\u7136\u540e\u4f60\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u4f3c\u4e4e\u4e0a\u9762\u7684\u8ba1\u7b97\u4e2d\\(y\\)\u4e00\u76f4\u90fd\u662f\u4e00\u4e2a\u6807\u91cf\uff0c\u90a3\u5982\u679c\u9047\u5230\u591a\u4e2a\u8f93\u51fa\u7ef4\u5ea6\u7684\u60c5\u51b5\u8be5\u600e\u4e48\u529e\uff1f\u5f53\u7136\u662f\u6bcf\u4e2a\u8f93\u51fa\u7ef4\u5ea6\u5355\u72ec\u4f5c\u4e3a\u4e00\u6b21\u8ba1\u7b97\uff0c\u63a5\u4e0b\u6765\u7684\u795e\u7ecf\u7f51\u7edc\u6784\u5efa\u90e8\u5206\u4e5f\u662f\u7efc\u5408\u4e86\u591a\u7ef4\u5ea6\u8f93\u51fa\u7684\u60c5\u51b5\uff08\u5177\u4f53\u8868\u73b0\u5728\u6c42\u548c\u5c42\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7b97\u6cd5\u7684\u539f\u7406\u548c\u63a8\u5bfc\u4ee5\u4e0a\u5c31\u662f\u5168\u90e8\u4e86\uff0c\u63a5\u4e0b\u6765\u9700\u8981\u5c06\u516c\u5f0f(4)\u8f6c\u5316\u4e3a\u795e\u7ecf\u7f51\u7edc\u7684\u5f62\u5f0f\uff0c\u4e0d\u8fc7\u8fd9\u91cc\u5c31\u4e0d\u753b\u795e\u7ecf\u7f51\u7edc\u7684\u793a\u610f\u56fe\u4e86\uff0c\u76f4\u63a5\u7528\u77e9\u9635\u7684\u5f62\u5f0f\u8868\u8fbe\u3002\u5c06\u6784\u5efa\u4e00\u4e2a\u5177\u6709\u9ad8\u65af\u5c42\u3001\u6c42\u548c\u5c42\u548c\u8f93\u51fa\u5c42\u7684\u795e\u7ecf\u7f51\u7edc\uff0c\u5176\u4e2d\u6c42\u548c\u5c42\u4e0e\u8f93\u51fa\u5c42\u5c06\u5728\u5177\u4f53\u5b9e\u73b0\u4e2d\u5408\u5e76\u4e3a\u4e00\u4e2a\u5c42\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u9ad8\u65af\u5c42\uff08\u5373\\(exp(-\\frac{D_i^2}{2\\sigma^2})\\)\uff09\uff1a<\/strong><br>\u8bbe\u5411\u91cf\\(x\\)\u4e3a\u6d4b\u8bd5\u8f93\u5165\u6837\u672c\uff0c\u77e9\u9635\\(t\\)\u4e3a\u8bad\u7ec3\u8f93\u5165\u6837\u672c\u96c6\u5408\uff0c\u6bcf\u4e00\u4e2a\u8f93\u5165\u6837\u672c\u7684\u7ef4\u5ea6\u5747\u4e3a\\(p\\)\uff0c\u4e00\u5171\u6709\\(n\\)\u4e2a\u8bad\u7ec3\u6837\u672c\uff0cGauss\u5373\u516c\u5f0f\\(exp(-\\frac{D_i^2}{2\\sigma^2})\\)\uff0c\u5373\u4ee4\u6d4b\u8bd5\u8f93\u5165\u6837\u672c\u4e0e\u6bcf\u4e2a\u8bad\u7ec3\u8f93\u5165\u6837\u672c\u7ecf\u8fc7\u4e00\u6b21\u8ba1\u7b97\uff0c\u5f97\u5230\u884c\u5411\u91cf\\(g\\)\uff0c\u6d41\u7a0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"272\" src=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u9ad8\u65af\u5c42-1024x272.jpg\" alt=\"\" class=\"wp-image-209\" srcset=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u9ad8\u65af\u5c42-1024x272.jpg 1024w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u9ad8\u65af\u5c42-300x80.jpg 300w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u9ad8\u65af\u5c42-768x204.jpg 768w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u9ad8\u65af\u5c42-1536x408.jpg 1536w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u9ad8\u65af\u5c42-2048x544.jpg 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">\u5176pytorch\u7c7b\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">class GaussLayer(nn.Module):\n    def __init__(self, training_inputs, sigma):\n        super(GaussLayer, self).__init__()\n        self.training_inputs = training_inputs\n        self.sigma = sigma  # smoothing parameter\n\n    def forward(self, x):\n        out = x - self.training_inputs\n        out = (out ** 2).sum(axis=1)\n        out = - out \/ (2 * self.sigma ** 2)\n        out = torch.exp(out)\n        return out<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6c42\u548c\u4e0e\u8f93\u51fa\u5c42\uff1a<\/strong><br>\u5411\u91cf\\(g\\)\u81ea\u8eab\u6c42\u548c\u5f97\u5230\\(s_0\\)\uff0c\u5bf9\u5e94\u516c\u5f0f(4)\u7684\u5206\u6bcd\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/s0-1024x112.jpg\" alt=\"\" class=\"wp-image-210\" width=\"428\" height=\"46\" srcset=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/s0-1024x112.jpg 1024w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/s0-300x33.jpg 300w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/s0-768x84.jpg 768w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/s0-1536x168.jpg 1536w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/s0-2048x225.jpg 2048w\" sizes=\"auto, (max-width: 428px) 100vw, 428px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">\u8bad\u7ec3\u8f93\u51fa\u6837\u672c\\(y\\)\u540c\u6837\u5199\u6210\u77e9\u9635\u7684\u5f62\u5f0f\uff0c\u5176\u4e2d\\(k\\)\u4ee3\u8868\u5355\u4e2a\u8f93\u51fa\u6837\u672c\u7684\u7ef4\u5ea6\u3002\u884c\u5411\u91cf\\(g\\)\u4e0e\u77e9\u9635\\(y\\)\u7684\u6bcf\u4e00\u884c\u7ecf\u8fc7\u6c42\u548c\u5c42\\(Sum\\)\u8fdb\u884c\u4e00\u6b21\u5185\u79ef\uff0c\u5f97\u5230\u5411\u91cf\\(s\\)\uff0c\u5411\u91cf\\(s\\)\u518d\u9664\u4ee5\u524d\u9762\u6c42\u5f97\u7684\\(s_0\\)\uff0c\u6700\u7ec8\u5f97\u5230\u9884\u6d4b\u8f93\u51fa\u5411\u91cf\\(o\\)\uff0c\u6d41\u7a0b\u56fe\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"246\" src=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u6c42\u548c\u4e0e\u8f93\u51fa\u5c42\u66f4\u6b63-1024x246.jpg\" alt=\"\" class=\"wp-image-224\" srcset=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u6c42\u548c\u4e0e\u8f93\u51fa\u5c42\u66f4\u6b63-1024x246.jpg 1024w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u6c42\u548c\u4e0e\u8f93\u51fa\u5c42\u66f4\u6b63-300x72.jpg 300w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u6c42\u548c\u4e0e\u8f93\u51fa\u5c42\u66f4\u6b63-768x185.jpg 768w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u6c42\u548c\u4e0e\u8f93\u51fa\u5c42\u66f4\u6b63-1536x369.jpg 1536w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/08\/GRNN\u6c42\u548c\u4e0e\u8f93\u51fa\u5c42\u66f4\u6b63-2048x493.jpg 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">\u5176pytorch\u7c7b\u4ee3\u7801\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">class SumAndOutputLayer(nn.Module):\n    def __init__(self, training_outputs):\n        super(SumAndOutputLayer, self).__init__()\n        self.training_outputs = training_outputs\n\n    def forward(self, x):\n        trans = self.training_outputs.T\n        s0 = x.sum()\n        out = (x * trans).sum(axis=1)  # Summation Layer\n        out = out \/ s0  # Output Layer\n        return out<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u770b\u5230\uff0c\u77e9\u9635\u5316\u540e\u7684\u6d41\u7a0b\u4e2d\uff0c\u7edd\u5927\u90e8\u5206\u6b65\u9aa4\u90fd\u7b26\u5408Pytorch\u7684\u5e7f\u64ad\u673a\u5236\uff0c\u56e0\u6b64\u975e\u5e38\u9002\u5408\u901a\u8fc7\u81ea\u5b9a\u4e49\u5c42\u6765\u5b9e\u73b0\u5176\u6574\u4e2a\u8fd0\u7b97\u8fc7\u7a0b\u3001\u518d\u5c06\u6a21\u578b\u548c\u6570\u636e\u653e\u5728GPU\u4e0a\u8fd0\u884c\uff0c\u53ef\u4ee5\u6781\u5927\u5730\u6539\u5584GRNN\u5728\u9884\u6d4b\u65f6\u7684\u8017\u65f6\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u5c06\u7ed9\u51fa\u5b8c\u6574\u7684\u4ee3\u7801\uff0c\u5176\u4e2dGRNN\u7c7b\u4e2d\u5305\u542b\u4e86\u6570\u636e\u5f52\u4e00\u5316\u548c\u8fd8\u539f\u7684\u6b65\u9aa4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cpytorch\u5b9e\u73b0\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf\u7c7b\u578b\u4e0e\u4e0a\u6587\u6240\u4f7f\u7528\u7684\u5411\u91cf\u3001\u77e9\u9635\u7684\u7ed3\u6784\u6709\u6240\u5dee\u5f02\u3002\u6587\u4e2d\u4e0d\u8bba\u662f\u5217\u5411\u91cf\u8fd8\u662f\u884c\u5411\u91cf\uff0c\u5728\u4ee3\u7801\u4e2d\u5747\u8868\u73b0\u4e3a\u4e00\u7ef4\u5f20\u91cf\uff1b\u77e9\u9635\u7528\u4e8c\u7ef4\u5f20\u91cf\u8868\u793a\uff0c\u6587\u4e2d\u6bcf\u5217\u4ee3\u8868\u4e00\u4e2a\u6837\u672c\uff0c\u4f46\u4ee3\u7801\u4e2d\u6bcf\u884c\u4ee3\u8868\u4e00\u4e2a\u6837\u672c\uff1b\u800c\u6ce8\u610f\u8d85\u53c2\\(\\sigma\\)\u5728\u8c03\u7528\u7c7b\u4e4b\u524d\u4e5f\u9700\u8981\u5148\u8f6c\u5316\u4e3a\u96f6\u7ef4\u5f20\u91cf\u3002\u518d\u8005\uff0c\u5f53\u8bad\u7ec3\u8f93\u5165\u6837\u672c\u6216\u8bad\u7ec3\u8f93\u51fa\u6837\u672c\uff08\u7279\u522b\u662f\u8f93\u51fa\u5e38\u6709\u8fd9\u79cd\u60c5\u51b5\uff09\u4e3a\u4e00\u7ef4\uff0c\u6240\u6709\u6837\u672c\u653e\u5728\u4e00\u4e2a\u4e00\u7ef4\u5f20\u91cf\u4e0b\u65f6\uff0c\u5fc5\u987b\u5148\u4f7f\u7528a = a.unsqueeze(axis=1)\u8fdb\u884c\u5347\u7ef4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">import torch\nfrom torch import nn\n\n\nclass GaussLayer(nn.Module):\n    def __init__(self, training_inputs, sigma):\n        super(GaussLayer, self).__init__()\n        self.training_inputs = training_inputs\n        self.sigma = sigma  # smoothing parameter\n\n    def forward(self, x):\n        out = x - self.training_inputs\n        out = (out ** 2).sum(axis=1)\n        out = - out \/ (2 * self.sigma ** 2)\n        out = torch.exp(out)\n        return out\n\n\nclass SumAndOutputLayer(nn.Module):\n    def __init__(self, training_outputs):\n        super(SumAndOutputLayer, self).__init__()\n        self.training_outputs = training_outputs\n\n    def forward(self, x):\n        trans = self.training_outputs.T\n        s0 = x.sum()\n        out = (x * trans).sum(axis=1)  # Summation Layer\n        out = out \/ s0  # Output Layer\n        return out\n\n\nclass CudaGeneralRegNN:\n    def __init__(self):\n        self.t = None  # training samples (INPUT)\n        self.y = None  # training samples (OUTPUT)\n        self.sigma = None  # smoothing parameter\n        self.t_mean = None  # mean of each feature (INPUT)\n        self.t_std = None  # std of each feature (INPUT)\n        self.y_mean = None  # mean of each feature (OUTPUT)\n        self.y_std = None  # std of each feature (OUTPUT)\n        self.net = None  # General Regression Neural Network\n        self.device = None  # cpu\/cuda\n\n    def fit(self, t_samples, y_samples, sigma):\n        self.device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')\n        self.t = t_samples\n        self.y = y_samples\n        self.sigma = sigma\n        self.t_mean = self.t.mean(axis=0)\n        self.t_std = self.t.std(axis=0)\n        self.y_mean = self.y.mean(axis=0)\n        self.y_std = self.y.std(axis=0)\n\n        # Normalization\n        self.t = (self.t - self.t_mean) \/ self.t_std\n        self.y = (self.y - self.y_mean) \/ self.y_std\n\n        self.t = self.t.to(device=self.device)\n        self.y = self.y.to(device=self.device)\n        self.sigma = self.sigma.to(device=self.device)\n\n        self.net = nn.Sequential(\n            GaussLayer(self.t, self.sigma),\n            SumAndOutputLayer(self.y)\n        )\n        self.net = self.net.to(device=self.device)\n\n    def predict(self, x):\n        x = (x - self.t_mean) \/ self.t_std\n        x = x.to(device=self.device)\n\n        out = self.net(x)\n\n        out = out.to(device=torch.device('cpu'))\n        out = out * self.y_std + self.y_mean\n\n        return out\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8036\u6574\u7406\u5b8c\u4e86\uff0c\u5176\u5b9e\u8bba\u6587<sup>[1]<\/sup>\u53ea\u7ed9\u51fa\u4e86\u4e00\u4e9b\u6bd4\u8f83\u7c97\u7565\u7684\u516c\u5f0f\uff0c\u5f88\u591a\u90e8\u5206\u662f\u6211\u81ea\u5df1\u63a8\u5bfc\u7684\uff0c\u75b2\u308c\u305f\u2026<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u53c2\u8003\u6587\u732e\uff1a<\/strong><br><em>[1]D. F. Specht, &#8220;A general regression neural network,&#8221; in IEEE Transactions on Neural Networks, vol. 2, no. 6, pp. 568-576, Nov. 1991, doi: 10.1109\/72.97934.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3010\u60b2\u62a5\u3011\u8fd9\u7bc7\u6587\u7ae0\u7684\u4ee3\u7801\u6ca1\u6709\u6279\u91cf\u9884\u6d4b\uff0c\u770b\u4e86\u57fa\u672c\u539f\u7406\u540e\u53ef\u4ee5\u79fb\u6b65\u65b0\u6587\u7ae0\uff1aTensor\uff0c&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,43,45],"tags":[35,37,38,36,39,41,40,42],"class_list":["post-141","post","type-post","status-publish","format-standard","hentry","category-python","category-43","category-45","tag-grnn","tag-python","tag-pytorch","tag-36","tag-39","tag-41","tag-40","tag-42"],"_links":{"self":[{"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/posts\/141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/comments?post=141"}],"version-history":[{"count":80,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":367,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions\/367"}],"wp:attachment":[{"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}