{"id":310,"date":"2023-10-27T23:07:15","date_gmt":"2023-10-27T15:07:15","guid":{"rendered":"https:\/\/www.kakosci.com\/?p=310"},"modified":"2024-11-18T17:40:04","modified_gmt":"2024-11-18T09:40:04","slug":"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","status":"publish","type":"post","link":"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\/","title":{"rendered":"Tensor\uff0c\u5e7f\u64ad\u673a\u5236\u4e0eGRNN\u91cd\u6784"},"content":{"rendered":"\n<p>\u524d\u7bc7\uff1a<a href=\"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\/\">Pytorch\u5b9e\u73b0\u5e7f\u4e49\u56de\u5f52\u795e\u7ecf\u7f51\u7edcGRNN \u2013 KAKO Academy of Sciences (kakosci.com)<\/a><\/p>\n\n\n\n<p>\u4f7f\u7528\u7684\u8fc7\u7a0b\u4e2d\u6e10\u6e10\u53d1\u73b0\u539f\u672c\u7684\u4ee3\u7801\u6709\u4e2a\u91cd\u5927\u7f3a\u9677\uff0c\u90a3\u5c31\u662f\u4e0d\u80fd\u6279\u91cf\u9884\u6d4b\uff08\uff09\u53ea\u80fd\u7528\u5faa\u73af\u628a\u6837\u672c\u4e00\u4e2a\u4e00\u4e2a\u585e\u8fdb\u6a21\u578b\uff0c\u4e0d\u4ec5\u4e11\u964b\u800c\u4e14\u6548\u7387\u975e\u5e38\u4f4e\u4e0b\uff0c\u8981\u9884\u6d4b\u7684\u6837\u672c\u91cf\u4e00\u4e0a\u6765\u5c31\u5f97\u8dd1\u534a\u5929\u3002\u5176\u5b9e\u4e00\u5f00\u59cb\u4e5f\u60f3\u505a\u6279\u91cf\u8f93\u5165\u4f46\u6280\u672f\u4e0a\u4e0d\u77e5\u9053\u600e\u4e48\u7528\u5f20\u91cf\u5e7f\u64ad\u6765\u5b8c\u6210\u67d0\u79cd\u8fd0\u7b97\u56e0\u6b64\u4e0d\u4e86\u4e86\u4e4b\uff0c\u540e\u6765\u6df1\u5165\u5b66\u4e60\u4e86\u4e0bBroadcast\u624d\u7ec8\u4e8e\u660e\u767d\u8981\u600e\u4e48\u505a\u2026\u2026<\/p>\n\n\n\n<p>\u76f4\u63a5\u5f15\u5165\u95ee\u9898\uff0c\u73b0\u6709\u4e00\u4e2a\u8bad\u7ec3\u8f93\u5165\u6837\u672c\u7684\u77e9\u9635\\(t\\)\uff0c\u5f62\u72b6\u4e3a\\((n,p)\\)\uff0c\u5373n\u4e2a\u7279\u5f81\u6570\u4e3ap\u7684\u6837\u672c\uff1b\u518d\u6709\u4e00\u4e2a\u6d4b\u8bd5\u8f93\u5165\u6837\u672c\u77e9\u9635\\(x\\)\uff0c\u5f62\u72b6\u4e3a\\((m,p)\\)\u3002\u6309\u7167\u539f\u672c\u7684\u7b97\u6cd5\uff0c\u6211\u4f1a\u628a\u6d4b\u8bd5\u6837\u672c\u62c6\u6210m\u4e2a\uff0c\u7136\u540e\u5206\u522b\u4e0e\u6574\u4e2a\u8bad\u7ec3input\u77e9\u9635\u8fdb\u884c\u9ad8\u65af\u8fd0\u7b97\uff08Gauss\u5373\u516c\u5f0f\\(exp(-\\frac{D_i^2}{2\\sigma^2})\\)\uff0c\\(D\\)\u4ee3\u8868\u4e24\u4e2a\u6837\u672c\u95f4\u7684\u6b27\u6c0f\u8ddd\u79bb\uff09\u5e76\u5f97\u5230m\u4e2a\u957f\u5ea6\u4e3an\u7684\u4e00\u7ef4\u5411\u91cf\uff0c\u5373\u9ad8\u65af\u5c42\u7684\u8f93\u51fa\u3002\u73b0\u5728\u82e5\u60f3\u8981\u6539\u4e3a\u4f7f\u7528\u6574\u4e2a\u77e9\u9635x\u548c\u77e9\u9635t\u76f4\u63a5\u5e7f\u64ad\u8fd0\u7b97\u5e76\u5f97\u5230\u5f62\u72b6\u4e3a\\((m,n)\\)\u7684\u8f93\u51fa\u77e9\u9635\uff0c\u8981\u600e\u4e48\u529e\uff1f<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"197\" src=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/\u56fe1-1024x197.png\" alt=\"\" class=\"wp-image-324\" srcset=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/\u56fe1-1024x197.png 1024w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/\u56fe1-300x58.png 300w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/\u56fe1-768x147.png 768w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/\u56fe1-1536x295.png 1536w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/\u56fe1-2048x393.png 2048w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/\u56fe1-670x129.png 670w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u7b54\u6848\u662f\u7ef4\u5ea6\u6269\u5f20\uff0c\u4e0a\u56fe\u4e4d\u4e00\u770b\u50cf\u662f\u77e9\u9635\u4e58\u6cd5\uff08\u5982\u679c\u8f6c\u7f6e\u77e9\u9635t\uff09\uff0c\u4e5f\u786e\u5b9e\u7c7b\u4f3c\uff0c\u56e0\u4e3a\u77e9\u9635\u4e58\u6cd5\u4e5f\u53ef\u4ee5\u7528\u540c\u6837\u7684\u539f\u7406\u590d\u73b0\u3002\u4e24\u4e2a\u5f20\u91cf\u7b26\u5408\u4e00\u5b9a\u8981\u6c42\u4fbf\u53ef\u4ee5\u8fdb\u884c\u5e7f\u64ad\u8ba1\u7b97\uff0c\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u9996\u5148\u5728\u5f20\u91cft\u7684\u7b2c0\u7ef4\u5904\u589e\u52a0\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u4f7f\u5176\u5f62\u72b6\u53d8\u4e3a\\((1,n,p)\\)\uff0c\u518d\u7ed9\u5f20\u91cfx\u7684\u7b2c\u4e00\u7ef4\u5904\u4e5f\u589e\u52a0\u4e00\u4e2a\u7ef4\u5ea6\uff0c\u5f62\u72b6\u53d8\u4e3a\\((m,1,p)\\)\uff0c\u518d\u5bf9\u4e24\u4e2a\u77e9\u9635\u8fdb\u884c\u57fa\u672c\u7684\u56db\u5219\u8fd0\u7b97\uff0c\u6240\u8f93\u51fa\u7684\u77e9\u9635\u5f62\u72b6\u4e00\u5b9a\u4e3a\\((m,n,p)\\)\uff0c\u518d\u5bf9\u7b2c\u4e8c\u7ef4\u5ea6\uff08\u5373\u5f62\u72b6\u4e2dp\u6240\u5728\u7684\u7ef4\u5ea6\uff09\u6c42\u548c\u3001\u6c42\u6700\u5927\u6700\u5c0f\u503c\u6216\u6c42\u5e73\u5747\u7b49\u65b9\u5f0f\u964d\u7ef4\uff08\u5206\u60c5\u51b5\u4f7f\u7528\uff09\uff0c\u5c31\u80fd\u5f97\u5230\u5f62\u72b6\u4e3a\\((m,n)\\)\u7684\u5f20\u91cf\u3002<\/p>\n\n\n\n<p>\u5173\u4e8e\u5e7f\u64ad\u673a\u5236\u53ef\u53c2\u8003\u6587\u7ae0\uff1a <a href=\"https:\/\/zhuanlan.zhihu.com\/p\/402163854\" target=\"_blank\" rel=\"noopener\">Numpy\u5b66\u4e60\u2014\u2014\u5e7f\u64ad\u673a\u5236\u7406\u89e3 &#8211; \u77e5\u4e4e (zhihu.com)<\/a>\uff0c<a href=\"https:\/\/zhuanlan.zhihu.com\/p\/86997775\" target=\"_blank\" rel=\"noopener\">torch\u7684\u5e7f\u64ad\u673a\u5236(broadcast mechanism) &#8211; \u77e5\u4e4e (zhihu.com)<\/a> \uff0c\u4e0b\u9762\u662f\u9ad8\u65af\u5c42\u7684\u65b0\u4ee3\u7801\uff0c\u628a\u9ad8\u65af\u51fd\u6570\u5206\u89e3\u6210\u4e86\u51e0\u6b65\u6765\u505a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code has-medium-font-size\"><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.unsqueeze(0)  # Turn (n, p) to (1, n, p)\n        self.sigma = sigma  # smoothing parameter\n\n    def forward(self, x):\n        # x: tensor (Matrix) : (m, p)\n        uns_x = x.unsqueeze(1)  # Turn (m, p) to (m, 1, p)\n        out = uns_x - self.training_inputs  # shape: (m, n, p)\n        out = (out ** 2).sum(dim=2)\n        out = - out \/ (2 * self.sigma ** 2)\n        out = torch.exp(out)\n        # shape of output: (m, n)\n        return out<\/code><\/pre>\n\n\n\n<p>\u5230\u6c42\u548c\u5c42\u66f4\u79bb\u8c31\u7684\u4e8b\u60c5\u5c31\u53d1\u751f\u4e86\uff0c\u6b64\u65f6\u6c42\u548c\u5c42\u7684\u8f93\u5165\u53d8\u4e3a\u4e86\u524d\u4e00\u6b65\u5f97\u5230\u7684\u77e9\u9635g\uff0c\u5f62\u72b6\u4e3a\\((m,n)\\)\uff0c\u9700\u8981\u4e0e\u5f62\u72b6\u4e3a\\((n,k)\\)\u7684\u8bad\u7ec3\u6807\u7b7e\u77e9\u9635\u8ba1\u7b97\u5f97\u5230\u7ed3\u679c\u77e9\u9635o\uff08\u5f62\u72b6\\((m,k)\\)\uff09\uff08\u5177\u4f53\u8fd0\u7b97\u8fc7\u7a0b\u89c1<a rel=\"noreferrer noopener\" href=\"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\/\" target=\"_blank\">\u524d\u7bc7<\/a>\uff09\u3002\u6309\u7167\u540c\u6837\u7684\u7ef4\u5ea6\u6269\u5f20\u601d\u8def\uff0c\u5148\u628a\u4e24\u4e2a\u77e9\u9635\u5206\u522b\u6269\u5f20\u4e3a\\((m,n,1)\\)\u548c\\((1,n,k)\\)\uff0c\u8fdb\u884c\u7b97\u6570\u4e58\u6cd5\u7684\u64cd\u4f5c\u5f97\u5230\u5f62\u72b6\u4e3a\\((m,n,k)\\)\u7684\u7ed3\u679c\uff0c\u6700\u540e\u5bf9\u4e2d\u95f4\u7ef4\u5ea6\u6c42\u548c\u5373\u53ef\u3002\u7136\u800c\u4e0a\u8ff0\u64cd\u4f5c\u521a\u597d\u8ddf\u77e9\u9635\u4e58\u6cd5\u7b49\u4ef7\u2026\u2026\u6240\u4ee5\u6211\u8bf4\u77e9\u9635\u4e58\u6cd5\u53ef\u4ee5\u7528\u540c\u6837\u7684\u539f\u7406\u590d\u73b0\u3002\u56e0\u6b64\u8be5\u5c42\u7684\u4ee3\u7801\u80fd\u7528@ \u8fd0\u7b97\u7b26\u76f4\u63a5\u5b8c\u6210\u3002<\/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        s0 = x.sum(dim=1).unsqueeze(1)\n        out = (x @ self.training_outputs) \/ s0\n        return out<\/code><\/pre>\n\n\n\n<p>\u8fdb\u884c\u4e86\u4e0a\u8ff0\u4fee\u6539\u540e\u6a21\u578b\u5c31\u80fd\u6279\u91cf\u8f93\u5165\u6d4b\u8bd5\u6837\u672c\u4e86\uff0c\u5e76\u884c\u8fd0\u7b97\u4f7f\u4ee3\u7801\u660e\u663e\u5feb\u4e86\u5f88\u591a\uff0c\u8036\u3002\u9644\u8dd1\u6570\u636e\u96c6\u7684\u8c03\u53c2\u66f2\u7ebf\u548c\u5bf9\u6bd4\u9a8c\u8bc1\u6563\u70b9\u56fe\uff08\u51b3\u5b9a\u7cfb\u6570\u597d\u9ad8\uff0c\u592a\u597d\u4e86\uff09\u3002<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"571\" height=\"430\" src=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/adjust.png\" alt=\"\" class=\"wp-image-328\" srcset=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/adjust.png 571w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/adjust-300x226.png 300w\" sizes=\"auto, (max-width: 571px) 100vw, 571px\" \/><\/figure>\n<\/div><\/div>\n<\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"553\" height=\"433\" src=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/verification.png\" alt=\"\" class=\"wp-image-329\" srcset=\"https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/verification.png 553w, https:\/\/www.kakosci.com\/wp-content\/uploads\/2023\/10\/verification-300x235.png 300w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/figure>\n<\/div>\n\n\n<p>\u7136\u540e\u662f\u6700\u7ec8\u7684\u4ee3\u7801\uff0c\u5305\u542b\u4e86\u6807\u51c6\u5316\u548c\u53cd\u6807\u51c6\u5316\uff0c\u4ee5\u53ca\u5f00\u542fcuda\u548c\u8f6c\u79fb\u6570\u636e\u5230\u663e\u5361\u4e0a\u7684\u6b65\u9aa4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">\"\"\"\nAuthor: kageyamaRik\nCreation date: 2023\/10\/26\nMultidimensional input\/output feature\nBatch prediction\n\"\"\"\n\nimport 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.unsqueeze(0)  # Turn (n, p) to (1, n, p)\n        self.sigma = sigma  # smoothing parameter\n\n    def forward(self, x):\n        # x: tensor (Matrix) : (m, p)\n        uns_x = x.unsqueeze(1)  # Turn (m, p) to (m, 1, p)\n        out = uns_x - self.training_inputs  # shape: (m, n, p)\n        out = (out ** 2).sum(dim=2)\n        out = - out \/ (2 * self.sigma ** 2)\n        out = torch.exp(out)\n        # shape of output: (m, n)\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        s0 = x.sum(dim=1).unsqueeze(1)\n        out = (x @ self.training_outputs) \/ s0\n        return out\n\n\nclass GRNN:\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(dim=0)\n        self.t_std = self.t.std(dim=0)\n        self.y_mean = self.y.mean(dim=0)\n        self.y_std = self.y.std(dim=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><\/p>\n\n\n\n<p>\u8bb0\u5f55\u4e0b\u52a0\u6df1\u5370\u8c61\uff0c\u4f46\u5199\u5f97\u597d\u4e71\u5662\uff0c\u81ea\u5df1\u90fd\u8981\u770b\u6655\u4e86\uff08\uff09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7bc7\uff1aPytorch\u5b9e\u73b0\u5e7f\u4e49\u56de\u5f52\u795e\u7ecf\u7f51\u7edcGRNN \u2013 KAKO Academy &#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],"class_list":["post-310","post","type-post","status-publish","format-standard","hentry","category-python","category-43","category-45","tag-grnn","tag-python","tag-pytorch","tag-36","tag-39"],"_links":{"self":[{"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/posts\/310","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=310"}],"version-history":[{"count":25,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/posts\/310\/revisions"}],"predecessor-version":[{"id":805,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/posts\/310\/revisions\/805"}],"wp:attachment":[{"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/media?parent=310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/categories?post=310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kakosci.com\/index.php\/wp-json\/wp\/v2\/tags?post=310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}