{"id":1233,"date":"2023-04-16T13:21:05","date_gmt":"2023-04-16T05:21:05","guid":{"rendered":"http:\/\/www.wayln.com\/?p=1233"},"modified":"2023-04-16T14:15:28","modified_gmt":"2023-04-16T06:15:28","slug":"%e7%ba%bf%e6%80%a7%e5%9b%9e%e5%bd%92","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=1233","title":{"rendered":"\u7ebf\u6027\u56de\u5f52"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_transparent no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#i\"><span class=\"toc_number toc_depth_1\">1<\/span> \u7ebf\u6027\u56de\u5f52\u95ee\u9898<\/a><ul><li><a href=\"#i-2\"><span class=\"toc_number toc_depth_2\">1.1<\/span> \u95ee\u9898\u63cf\u8ff0<\/a><\/li><li><a href=\"#i-3\"><span class=\"toc_number toc_depth_2\">1.2<\/span> \u95ee\u9898\u89e3\u6790<\/a><\/li><li><a href=\"#i-4\"><span class=\"toc_number toc_depth_2\">1.3<\/span> \u89e3\u51b3\u65b9\u6848<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<p>[TOC]<\/p>\n<h1><span id=\"i\">\u7ebf\u6027\u56de\u5f52\u95ee\u9898<\/span><\/h1>\n<h2><span id=\"i-2\">\u95ee\u9898\u63cf\u8ff0<\/span><\/h2>\n<p>\u7ebf\u6027\u56de\u5f52\uff08Linear Regression\uff09\u662f\u56de\u5f52\u95ee\u9898\u4e2d\u7684\u4e00\u79cd\uff0c\u7ebf\u6027\u56de\u5f52\u5047\u8bbe\u76ee\u6807\u503c\u548c\u7279\u5f81\u4e4b\u95f4\u7ebf\u6027\u76f8\u5173\uff0c\u5373\u6ee1\u8db3\u4e00\u4e2a\u591a\u5143\u4e00\u6b21\u65b9\u7a0b\uff0c\u901a\u8fc7\u6784\u5efa\u635f\u5931\u51fd\u6570\uff0c\u6765\u6c42\u89e3\u635f\u5931\u51fd\u6570\u6700\u5c0f\u65f6\u7684\u53c2\u6570W\u548cb,<\/p>\n<div class=\"katex math multi-line no-emojify\">\\hat{y}=Wx+b\n<\/div>\n<p>\u5f0f\u4e2d\uff0c<span class=\"katex math inline\">\\hat{y}<\/span>\u4e3a\u9884\u6d4b\u503c\uff1b\u81ea\u53d8\u91cfx\u548c\u56e0\u53d8\u91cfy\u662f\u5df2\u77e5\u7684\u3002\u6211\u4eec\u60f3\u5b9e\u73b0\u7684\u662f\u9884\u6d4b\u65b0\u589e\u4e00\u4e2ax,\u5176\u5bf9\u5e94\u7684y\u662f\u591a\u5c11\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u6784\u5efa\u8fd9\u4e2a\u51fd\u6570\u5173\u7cfb\uff0c\u76ee\u6807\u662f\u901a\u8fc7\u5df2\u77e5\u6570\u636e\u70b9\uff0c\u6c42\u89e3\u7ebf\u6027\u6a21\u578b\u4e2dW\u548cb\u4e24\u4e2a\u53c2\u6570\u3002<br \/>\n\u4e0b\u56fe\u6240\u793a\u4e3a\u7ebf\u6027\u51fd\u6570\u548c\u5b9e\u9645\u503c\u53d8\u5dee\u793a\u610f\u56fe\uff0c\u5176\u4e2d\u504f\u79bb\u76f4\u7ebf\u7684\u70b9\u4f4d\u81ea\u53d8\u91cfx\u548c\u56e0\u53d8\u91cfy\u7684\u5b9e\u9645\u503c\uff0c\u8fd9\u4e9b\u70b9\u5230\u76f4\u7ebfy\u65b9\u5411\u7684\u8ddd\u79bb\u7ebf\u6bb5\u4e3a\u8bef\u5dee\u503c\u3002<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_91ae8f1e2a1a5473816e8a8e554c8770.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2023\/04\/wp_editor_md_91ae8f1e2a1a5473816e8a8e554c8770.jpg\" alt=\"\" \/><\/a><\/p>\n<h2><span id=\"i-3\">\u95ee\u9898\u89e3\u6790<\/span><\/h2>\n<p>\u6c42\u89e3\u6700\u4f73\u53c2\u6570\uff0c\u9700\u8981\u4e00\u4e2a\u6807\u51c6\u6765\u5bf9\u7ed3\u679c\u8fdb\u884c\u8861\u91cf\uff0c\u4e3a\u6b64\u6211\u4eec\u9700\u8981\u5b9a\u91cf\u5316\u4e00\u4e2a\u76ee\u6807\u51fd\u6570\u5f0f\uff0c\u4ee5\u4fbf\u8ba1\u7b97\u673a\u5728\u6c42\u89e3\u8fc7\u7a0b\u4e2d\u4e0d\u65ad\u5730\u4f18\u5316\u3002<br \/>\n\u9488\u5bf9\u4efb\u4f55\u6a21\u578b\u6c42\u89e3\u95ee\u9898\uff0c\u6700\u7ec8\u90fd\u53ef\u4ee5\u5f97\u5230\u4e00\u7ec4\u9884\u6d4b\u503c<span class=\"katex math inline\">\\hat{y}<\/span>,\u5bf9\u6bd4\u5df2\u6709\u7684\u771f\u5b9e\u503cy\uff0c\u6570\u636e\u884c\u6570n\uff0c\u53ef\u4ee5\u5c06\u635f\u5931\u51fd\u6570\u5b9a\u4e49\u5982\u4e0b<\/p>\n<div class=\"katex math multi-line no-emojify\">L=\\frac{1}{n}\\sum_{i=1}^{n}(\\hat{y}_i-y_i)^2\n<\/div>\n<p>\u5373\u9884\u6d4b\u503c\u548c\u771f\u5b9e\u503c\u4e4b\u95f4\u7684\u5e73\u5747\u7684\u5e73\u65b9\u8ddd\u79bb\uff0c\u5728\u7edf\u8ba1\u5b66\u4e2d\uff0c\u4e00\u822c\u79f0\u4e3a\u5747\u65b9\u8bef\u5dee\uff08Mean square Error,MAE\uff09,\u628a\u4e4b\u524d\u7684\u51fd\u6570\u5f0f\u5e26\u5165\u635f\u5931\u51fd\u6570\uff0c\u5e76\u4e14\u5c06\u9700\u8981\u6c42\u89e3\u7684\u53c2\u6570W\u548cb\u770b\u505a\u51fd\u6570L\u7684\u81ea\u53d8\u91cf\uff0c\u53ef\u5f97<\/p>\n<div class=\"katex math multi-line no-emojify\">L\uff08W\uff0cb\uff09=\\frac{1}{n}\\sum_{i=1}^n(Wx_i+b-y_i)^2\n<\/div>\n<p>\u73b0\u5728\u7684\u4efb\u52a1\u662f\u6c42\u89e3\u5f53L\u6700\u5c0f\u65f6\uff0cW\u548cb\u7684\u503c\uff0c\u5373\u6838\u5fc3\u76ee\u6807\u4f18\u5316\u5f0f\u4e3a<\/p>\n<div class=\"katex math multi-line no-emojify\">(W^*,b^*)=arg\\underset{(W,b)}{min}\\sum_{i=1}^n(Wx_i+b-y_i)^2\n<\/div>\n<h2><span id=\"i-4\">\u89e3\u51b3\u65b9\u6848<\/span><\/h2>\n<p>\u4e00\u822c\u91c7\u7528\u68af\u5ea6\u4e0b\u964d(Gradient Descent)\u6cd5\u6c42\u89e3\u7ebf\u6027\u56de\u5f52\u95ee\u9898\u3002\u68af\u5ea6\u4e0b\u964d\u7684\u6838\u5fc3\u5185\u5bb9\u662f\u5bf9\u81ea\u53d8\u91cf\u8fdb\u884c\u4e0d\u65ad\u7684\u66f4\u65b0\uff08\u5bf9W\u548cb\u6c42\u504f\u5bfc\u6570\uff09\uff0c\u4f7f\u5f97\u76ee\u6807\u51fd\u6570\u4e0d\u65ad\u903c\u8fd1\u6700\u5c0f\u503c\uff0c\u5176\u4e2d\uff0c<span class=\"katex math inline\">\\alpha<\/span>\u662f\u6df1\u5ea6\u5b66\u4e60\u91cc\u9762\u7684\u5b66\u4e60\u7387\u3002<\/p>\n<div class=\"katex math multi-line no-emojify\">W\\leftarrow W-\\alpha\\frac{\\partial L}{\\partial W}<\/p>\n<\/div>\n<div class=\"katex math multi-line no-emojify\">b\\leftarrow b-\\alpha\\frac{\\partial L}{\\partial b}\n<\/div>\n<p>\u4f7f\u7528\u68af\u5ea6\u4e0b\u964d\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u51f8\u95ee\u9898\u6c42\u5f97\u5168\u5c40\u6700\u4f18\u89e3\uff1b\u5bf9\u4e8e\u975e\u51f8\u95ee\u9898\uff0c\u5219\u53ef\u4ee5\u6c42\u5f97\u5c40\u90e8\u6700\u4f18\u89e3<\/p>\n<pre><code class=\"language-CSharp line-numbers\">  NDArray train_X = np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.18f, 7.59f, 2.167f, 7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f);\n            NDArray train_Y = np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f, 2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f);\n            int n_samples = (int)train_X.shape[0];\n\n            \/\/\u521d\u59cb\u5316\u6743\u91cd\u548c\u504f\u7f6e\u9879\n            var W = tf.Variable(0f, name: \"weight\");\n            var b = tf.Variable(0f, name: \"bias\");\n            float learning_rate = 0.01f;\n            var optimizer = KerasApi.keras.optimizers.SGD(learning_rate);\n\n            \/\/\u8fd0\u884c\u4f18\u5316\u5668\u8fdb\u884c\u6743\u91cd\u548c\u504f\u7f6e\u9879\u66f4\u65b0\n            int training_steps = 1000;\n            int display_step = 50;\n            foreach (var step in range(1,training_steps+1))\n            {\n                using var g = tf.GradientTape();\n                \/\/\u7ebf\u6027\u56de\u5f52\u6a21\u578b(W*x+b)\n                var pred = W * train_X + b;\n                \/\/MSE\n                var loss = tf.reduce_sum(tf.pow(pred - train_Y, 2)) \/ n_samples;\n\n                var gradients = g.gradient(loss, (W, b));\n\n                \/\/\u5e94\u7528\u68af\u5ea6\u4e0b\u964d\u4f18\u5316\u5668\u66f4\u65b0W\u548cb\n                optimizer.apply_gradients(zip(gradients, (W, b)));\n                if(step%display_step==0)\n                {\n                    pred = W * train_X + b;\n                    loss = tf.reduce_sum(tf.pow(pred - train_Y, 2)) \/ n_samples;\n                    list1.Items.Add($\"step:{step},loss:{loss.numpy()},W:{W.numpy()},b:{b.numpy()}\");\n                }\n\n\n            }\n\n\n        }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u7ebf\u6027\u56de\u5f52\u95ee\u98981.1 \u95ee\u9898\u63cf\u8ff01.2 \u95ee\u9898\u89e3\u67901.3 \u89e3\u51b3\u65b9\u6848 [TOC] \u7ebf\u6027\u56de\u5f52\u95ee\u9898 \u95ee [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,50,49,2],"tags":[],"class_list":["post-1233","post","type-post","status-publish","format-standard","hentry","category-c","category-tensorflow","category-49","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1233"}],"version-history":[{"count":5,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1233\/revisions"}],"predecessor-version":[{"id":1238,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1233\/revisions\/1238"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}