{"id":1554,"date":"2024-07-27T20:30:31","date_gmt":"2024-07-27T12:30:31","guid":{"rendered":"https:\/\/www.forillusion.com\/?p=1554"},"modified":"2025-02-14T11:39:02","modified_gmt":"2025-02-14T03:39:02","slug":"3-2-linear-regression-scratch","status":"publish","type":"post","link":"https:\/\/www.forillusion.com\/index.php\/3-2-linear-regression-scratch\/","title":{"rendered":"3.2 \u7ebf\u6027\u56de\u5f52\u7684\u4ece\u96f6\u5f00\u59cb\u5b9e\u73b0"},"content":{"rendered":"\n<p> <div class=\"has-toc have-toc\"><\/div><\/p>\n\n\n\n<p>\u5bfc\u5165\u6240\u9700\u8981\u7684\u5e93<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">import torch\nimport random<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u751f\u6210\u6570\u636e\u96c6<\/h2>\n\n\n\n<p>\u6784\u9020\u4e00\u4e2a\u7b80\u5355\u7684\u4eba\u5de5\u8bad\u7ec3\u6570\u636e\u96c6\uff0c\u8bbe\u8bad\u7ec3\u6570\u636e\u96c6\u6837\u672c\u6570\u4e3a1000\uff0c\u8f93\u5165\u4e2a\u6570\uff08\u7279\u5f81\u6570\uff09\u4e3a2\u3002\u4f7f\u7528\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u771f\u5b9e\u6743\u91cd$w=[2,-3.4]^T$\u548c\u504f\u5dee$b=4.2$\uff0c\u4ee5\u53ca\u4e00\u4e2a\u968f\u673a\u566a\u58f0\u9879$\\epsilon$\u751f\u6210\u6807\u7b7e\u3002<\/p>\n\n\n\n<p>\u5176\u4e2d\u566a\u58f0\u9879&nbsp;$\\epsilon$&nbsp;\u670d\u4ece\u5747\u503c\u4e3a0\u3001\u6807\u51c6\u5dee\u4e3a0.01\u7684\u6b63\u6001\u5206\u5e03\u3002\u566a\u58f0\u4ee3\u8868\u4e86\u6570\u636e\u96c6\u4e2d\u65e0\u610f\u4e49\u7684\u5e72\u6270\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">num_inputs=2   #\u7279\u5f81\u6570\nnum_examples=1000  #\u6837\u672c\u6570\ntrue_w=&#91;2,-3.4]  #\u771f\u5b9e\u6743\u91cd\ntrue_b=4.2   #\u771f\u5b9e\u504f\u5dee\nfeatures=torch.randn(num_examples,num_inputs,dtype=torch.float32)  #\u751f\u6210\u7279\u5f81\nlabels=true_w&#91;0]*features&#91;:,0]+true_w&#91;1]*features&#91;:,1]+true_b  #\u751f\u6210\u6807\u7b7e &#91;:,0]\u8868\u793a\u53d6\u6240\u6709\u884c\u7684\u7b2c0\u4e2a\u5143\u7d20\nlabels+=torch.tensor(torch.normal(0,0.01,size=labels.size()),dtype=torch.float32)  #\u52a0\u5165\u566a\u58f0\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bfb\u53d6\u6570\u636e<\/h2>\n\n\n\n<p>\u5728\u8bad\u7ec3\u6a21\u578b\u7684\u65f6\u5019\uff0c\u9700\u8981\u904d\u5386\u6570\u636e\u96c6\u5e76\u4e0d\u65ad\u8bfb\u53d6\u5c0f\u6279\u91cf\u6570\u636e\u6837\u672c\u3002\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u6bcf\u6b21\u8fd4\u56de<code>batch_size<\/code>\uff08\u6279\u91cf\u5927\u5c0f\uff09\u4e2a\u968f\u673a\u6837\u672c\u7684\u7279\u5f81\u548c\u6807\u7b7e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def data_iter(batch_size, features, labels):   #batch_size:\u6279\u91cf\u5927\u5c0f,features:\u7279\u5f81,labels:\u6807\u7b7e\n    num_examples=len(features)  #\u6837\u672c\u6570    \n    indices=list(range(num_examples))   #\u6837\u672c\u7684\u7d22\u5f15\n    random.shuffle(indices)  #\u6837\u672c\u8bfb\u53d6\u987a\u5e8f\u968f\u673a  shuffle() \u65b9\u6cd5\u5c06\u5e8f\u5217\u7684\u6240\u6709\u5143\u7d20\u968f\u673a\u6392\u5e8f\n    for i in range(0,num_examples,batch_size):  #\u6bcf\u6b21\u53d6batch_size\u4e2a\u6837\u672c\n        j=torch.LongTensor(indices&#91;i:min(i+batch_size,num_examples)])  #\u6700\u540e\u4e00\u6b21\u53ef\u80fd\u4e0d\u8db3\u4e00\u4e2abatch,LongTensor()\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5f20\u91cf\n        yield features.index_select(0,j),labels.index_select(0,j)   #index_select(0,j)\u53d6features\u548clabels\u7684\u7b2cj\u884c\n\nbatch_size = 10<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u521d\u59cb\u5316\u6a21\u578b\u53c2\u6570<\/h2>\n\n\n\n<p>\u5c06\u6743\u91cd\u521d\u59cb\u5316\u6210\u5747\u503c\u4e3a0\u3001\u6807\u51c6\u5dee\u4e3a0.01\u7684\u6b63\u6001\u968f\u673a\u6570\uff0c\u504f\u5dee\u5219\u521d\u59cb\u5316\u62100\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">w=torch.tensor(torch.normal(0,0.01,size=(num_inputs,1)),dtype=torch.float32)\nb=torch.zeros(1,dtype=torch.float32)<\/code><\/pre>\n\n\n\n<p>\u4e4b\u540e\u7684\u6a21\u578b\u8bad\u7ec3\u4e2d\uff0c\u9700\u8981\u5bf9\u8fd9\u4e9b\u53c2\u6570\u6c42\u68af\u5ea6\u6765\u8fed\u4ee3\u53c2\u6570\u7684\u503c\uff0c\u56e0\u6b64\u8981\u8ba9\u5b83\u4eec\u7684<code>requires_grad=True<\/code>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">w.requires_grad_(requires_grad=True) #\u8bbe\u7f6e\u4e3aTrue\u8868\u793a\u4f1a\u88ab\u6c42\u5bfc\nb.requires_grad_(requires_grad=True)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49\u6a21\u578b<\/h2>\n\n\n\n<p>\u4e0b\u9762\u662f\u7ebf\u6027\u56de\u5f52\u7684\u77e2\u91cf\u8ba1\u7b97\u8868\u8fbe\u5f0f\u7684\u5b9e\u73b0\u3002\u4f7f\u7528<code>mm<\/code>\u51fd\u6570\u505a\u77e9\u9635\u4e58\u6cd5\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def linreg(X,w,b):  #\u5b9a\u4e49\u7ebf\u6027\u56de\u5f52\u6a21\u578b\n    return torch.mm(X,w)+b  #torch.mm()\u77e9\u9635\u4e58\u6cd5<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49\u635f\u5931\u51fd\u6570<\/h2>\n\n\n\n<p>\u5148\u5c06\u771f\u5b9e\u503c<code>y<\/code class=\"python\">\u53d8\u5f62\u6210\u9884\u6d4b\u503c<code>y_hat<\/code>\u7684\u5f62\u72b6\u3002\u4ee5\u4e0b\u51fd\u6570\u8fd4\u56de\u7684\u7ed3\u679c\u4e5f\u5c06\u548c<code>y_hat<\/code>\u7684\u5f62\u72b6\u76f8\u540c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def squared_loss(y_hat,y):  #\u5b9a\u4e49\u635f\u5931\u51fd\u6570\n    return (y_hat-y.view(y_hat.size()))**2\/2  #\u8fd4\u56de\u7684\u662f\u5411\u91cf  view()\u51fd\u6570\u4f5c\u7528\u662f\u5c06\u4e00\u4e2a\u591a\u884c\u7684Tensor,\u62fc\u63a5\u6210\u4e00\u884c\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9a\u4e49\u4f18\u5316\u7b97\u6cd5<\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u7684<code>sgd<\/code>\u51fd\u6570\u5b9e\u73b0\u4e86\u5c0f\u6279\u91cf\u968f\u673a\u68af\u5ea6\u4e0b\u964d\u7b97\u6cd5\u3002\u5b83\u901a\u8fc7\u4e0d\u65ad\u8fed\u4ee3\u6a21\u578b\u53c2\u6570\u6765\u4f18\u5316\u635f\u5931\u51fd\u6570\u3002\u8fd9\u91cc\u81ea\u52a8\u6c42\u68af\u5ea6\u6a21\u5757\u8ba1\u7b97\u5f97\u6765\u7684\u68af\u5ea6\u662f\u4e00\u4e2a\u6279\u91cf\u6837\u672c\u7684\u68af\u5ea6\u548c\u3002\u5c06\u5b83\u9664\u4ee5\u6279\u91cf\u5927\u5c0f\u6765\u5f97\u5230\u5e73\u5747\u503c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def sgd(params,lr,batch_size):  #\u5b9a\u4e49\u4f18\u5316\u7b97\u6cd5,params:\u5f85\u4f18\u5316\u53c2\u6570,lr:\u5b66\u4e60\u7387,batch_size:\u6279\u91cf\u5927\u5c0f\n    for param in params:\n        param.data-=lr*param.grad\/batch_size  #\u6ce8\u610f\u8fd9\u91cc\u66f4\u6539param\u65f6\u7528\u7684param.data\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8bad\u7ec3\u6a21\u578b<\/h2>\n\n\n\n<p>\u5728\u8bad\u7ec3\u4e2d\uff0c\u5c06\u591a\u6b21\u8fed\u4ee3\u6a21\u578b\u53c2\u6570\u3002\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\uff0c\u6839\u636e\u5f53\u524d\u8bfb\u53d6\u7684\u5c0f\u6279\u91cf\u6570\u636e\u6837\u672c\uff08\u7279\u5f81<code>X<\/code>\u548c\u6807\u7b7e<code>y<\/code>\uff09\uff0c\u901a\u8fc7\u8c03\u7528\u53cd\u5411\u51fd\u6570<code>backward<\/code>\u8ba1\u7b97\u5c0f\u6279\u91cf\u968f\u673a\u68af\u5ea6\uff0c\u5e76\u8c03\u7528\u4f18\u5316\u7b97\u6cd5<code>sgd<\/code>\u8fed\u4ee3\u6a21\u578b\u53c2\u6570\u3002\u7531\u4e8e\u4e4b\u524d\u8bbe\u6279\u91cf\u5927\u5c0f<code>batch_size<\/code>\u4e3a10\uff0c\u6bcf\u4e2a\u5c0f\u6279\u91cf\u7684\u635f\u5931<code>l<\/code>\u7684\u5f62\u72b6\u4e3a(10, 1)\u3002\u7531\u4e8e\u53d8\u91cf<code>l<\/code>\u5e76\u4e0d\u662f\u4e00\u4e2a\u6807\u91cf\uff0c\u6240\u4ee5\u53ef\u4ee5\u8c03\u7528<code>.sum()<\/code>\u5c06\u5176\u6c42\u548c\u5f97\u5230\u4e00\u4e2a\u6807\u91cf\uff0c\u518d\u8fd0\u884c<code>l.backward()<\/code>\u5f97\u5230\u8be5\u53d8\u91cf\u6709\u5173\u6a21\u578b\u53c2\u6570\u7684\u68af\u5ea6\u3002\u6ce8\u610f\u5728\u6bcf\u6b21\u66f4\u65b0\u5b8c\u53c2\u6570\u540e\u4e0d\u8981\u5fd8\u4e86\u5c06\u53c2\u6570\u7684\u68af\u5ea6\u6e05\u96f6\u3002<\/p>\n\n\n\n<p>\u5728\u4e00\u4e2a\u8fed\u4ee3\u5468\u671f\uff08epoch\uff09\u4e2d\uff0c\u5c06\u5b8c\u6574\u904d\u5386\u4e00\u904d<code>data_iter<\/code>\u51fd\u6570\uff0c\u5e76\u5bf9\u8bad\u7ec3\u6570\u636e\u96c6\u4e2d\u6240\u6709\u6837\u672c\u90fd\u4f7f\u7528\u4e00\u6b21\uff08\u5047\u8bbe\u6837\u672c\u6570\u80fd\u591f\u88ab\u6279\u91cf\u5927\u5c0f\u6574\u9664\uff09\u3002\u8fd9\u91cc\u7684\u8fed\u4ee3\u5468\u671f\u4e2a\u6570<code>num_epochs<\/code>\u548c\u5b66\u4e60\u7387<code>lr<\/code>\u90fd\u662f\u8d85\u53c2\u6570\uff0c\u5206\u522b\u8bbe3\u548c0.03\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">lr=0.03  #\u5b66\u4e60\u7387\nnum_epochs=3  #\u8fed\u4ee3\u5468\u671f\nnet=linreg  #\u7f51\u7edc\nloss=squared_loss  #\u635f\u5931\u51fd\u6570\n\nfor epoch in range(num_epochs):  #\u8bad\u7ec3\u6a21\u578b\u4e00\u5171\u9700\u8981num_epochs\u4e2a\u8fed\u4ee3\u5468\u671f\n    # \u5728\u6bcf\u4e00\u4e2a\u8fed\u4ee3\u5468\u671f\u4e2d\uff0c\u4f1a\u4f7f\u7528\u8bad\u7ec3\u6570\u636e\u96c6\u4e2d\u6240\u6709\u6837\u672c\u4e00\u6b21\uff08\u5047\u8bbe\u6837\u672c\u6570\u80fd\u591f\u88ab\u6279\u91cf\u5927\u5c0f\u6574\u9664\uff09\u3002X\n    # \u548cy\u5206\u522b\u662f\u5c0f\u6279\u91cf\u6837 \u672c\u7684\u7279\u5f81\u548c\u6807\u7b7e\n    for X,y in data_iter(batch_size,features,labels):  #\u6bcf\u6b21\u53d6batch_size\u4e2a\u6837\u672c\n        l=loss(net(X,w,b),y).sum()  #\u8ba1\u7b97\u635f\u5931\n        l.backward()  #\u5c0f\u6279\u91cf\u7684\u635f\u5931\u5bf9\u6a21\u578b\u53c2\u6570\u6c42\u68af\u5ea6\n        sgd(&#91;w,b],lr,batch_size)  #\u66f4\u65b0\u6a21\u578b\u53c2\u6570\n        w.grad.data.zero_()  #\u68af\u5ea6\u6e05\u96f6\n        b.grad.data.zero_()  #\u68af\u5ea6\u6e05\u96f6\n    train_l=loss(net(features,w,b),labels)  #\u8bad\u7ec3\u96c6\u635f\u5931\n    print('epoch %d, loss %f' % (epoch + 1, train_l.mean().item()))  #mean()\u6c42\u5e73\u5747\u503c,item()\u5c06\u6807\u91cfTensor\u8f6c\u6362\u4e3aPython\u6570\u5b57\n\nprint(true_w, '\\n', w)\nprint(true_b, '\\n', b)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5bfc\u5165\u6240\u9700\u8981\u7684\u5e93 \u751f\u6210\u6570\u636e\u96c6 \u6784\u9020\u4e00\u4e2a\u7b80\u5355\u7684\u4eba\u5de5\u8bad\u7ec3\u6570\u636e\u96c6\uff0c\u8bbe\u8bad\u7ec3\u6570\u636e\u96c6\u6837\u672c\u6570\u4e3a1000\uff0c\u8f93\u5165\u4e2a\u6570\uff08\u7279\u5f81\u6570\uff09\u4e3a2\u3002\u4f7f\u7528\u7ebf\u6027\u56de\u5f52\u6a21\u578b\u771f &#8230;<\/p>","protected":false},"author":1,"featured_media":1559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,3],"tags":[45,44,12,22],"class_list":["post-1554","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-46","category-3","tag-45","tag-44","tag-12","tag-22"],"_links":{"self":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/comments?post=1554"}],"version-history":[{"count":1,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1554\/revisions"}],"predecessor-version":[{"id":1723,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1554\/revisions\/1723"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media\/1559"}],"wp:attachment":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media?parent=1554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/categories?post=1554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/tags?post=1554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}