{"id":8608,"date":"2019-10-21T10:38:46","date_gmt":"2019-10-21T01:38:46","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=8608"},"modified":"2020-05-28T10:08:45","modified_gmt":"2020-05-28T01:08:45","slug":"%ec%84%a0%ed%98%95%ed%9a%8c%ea%b7%80%eb%aa%a8%eb%8d%b8%ec%97%90-%eb%8c%80%ed%95%9c-pytorch%eb%a5%bc-%ec%9d%b4%ec%9a%a9%ed%95%9c-%eb%91%90%ea%b0%80%ec%a7%80-%ec%a0%91%ea%b7%bc","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=8608","title":{"rendered":"\uc120\ud615\ud68c\uadc0\ubaa8\ub378\uc5d0 \ub300\ud55c PyTorch\ub97c \uc774\uc6a9\ud55c \ub450\uac00\uc9c0 \uc811\uadfc"},"content":{"rendered":"<p>\uc544\ub798\uc640 \uac19\uc740 \uc2dd\uc744 \ud68c\uadc0\ud558\ub294 \ubaa8\ub378\uc744 \uad6c\ud558\ub294 \ub450\uac00\uc9c0 \uc811\uadfc\uc744 PyTorch\ub85c \uc0b4\ud3b4\ubcf8\ub2e4.<\/p>\n<p><center><\/p>\n<p class=\"ql-center-displayed-equation\" style=\"line-height: 20px;\"><span class=\"ql-right-eqno\"> &nbsp; <\/span><span class=\"ql-left-eqno\"> &nbsp; <\/span><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/ql-cache\/quicklatex.com-d7a67769045071f2872e380165e22a1f_l3.png\" height=\"20\" width=\"143\" class=\"ql-img-displayed-equation quicklatex-auto-format\" alt=\"&#36;&#36;&#121;&#61;&#49;&#43;&#53;&#97;&#43;&#55;&#98;&#36;&#36;\" title=\"Rendered by QuickLaTeX.com\"\/><\/p>\n<p><\/center><\/p>\n<p>\uc989, \uc785\ub825\uac12(a, b)\uc5d0 \ub300\ud55c \ucd9c\ub825\uac12 y\uac00 100\uac1c \uc8fc\uc5b4\uc9c0\uace0, \uc774 \ub370\uc774\ud130\ub97c \ud1b5\ud574 \uc0c1\uc218\ud56d\uc778 1\uacfc \uacc4\uc218 5, 7\uc744 \uad6c\ud558\ub294 \uac83\uc774 \ubb38\uc81c\ub2e4. \ubb3c\ub860 y\uc5d0\ub294 \uc624\ucc28\uac00 \ubc18\uc601\ub418\uc5b4 \uc788\ub2e4. \uccab\ubc88\uc9f8 \uc811\uadfc\uc740 \ub2e4\uc74c\uacfc \uac19\ub2e4. \uc190\uc2e4\ud568\uc218\ub294 \ud3c9\uade0\ucd5c\uc18c\uc81c\uacf1\uc744, \uc5ed\uc804\ud30c\ub97c \ud1b5\ud55c \ucd5c\uc801\uac12 \uc218\ub834\uc744 \uc704\ud55c \uae30\uc6b8\uae30\ub97c \uad6c\ud574 \ubc18\uc601\ud55c \ud559\uc2b5\ub960\uc740 0.01\uc744 \uc0ac\uc6a9\ud588\ub2e4. \uc544\ub798\uc758 \ucf54\ub4dc\uc758 \uacbd\uc6b0 \uae30\uc6b8\uae30\ub97c \uad6c\ud558\uae30 \uc704\ud55c \ubc29\ubc95\uc744 PyTorch\uc758 \uc5ed\uc804\ud30c\ub97c \uc774\uc6a9\ud55c \uac83\uc774\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport torch\r\nfrom matplotlib import pyplot as plt\r\n\r\nweight_true = torch.Tensor([1,5,7]) # y = 1 + 5a + 7b\r\nX = torch.cat([torch.ones(100,1),torch.randn(100,2)], 1)\r\ny = torch.mv(X, weight_true) + torch.randn(100)\r\nweight = torch.randn(3, requires_grad=True)\r\n\r\nlr = 0.01\r\n\r\nlosses = []\r\n\r\nfor epoch in range(1000):\r\n    weight.grad = None\r\n\r\n    y_pred = torch.mv(X, weight)\r\n    loss = torch.mean((y - y_pred)**2)\r\n    loss.backward()\r\n\r\n    weight.data = weight.data - lr*weight.grad.data\r\n\r\n    losses.append(loss.item())\r\n\r\nprint(weight)\r\n\r\nplt.plot(losses)\r\nplt.show()\r\n<\/pre>\n<p>\ub450\ubc88\uc9f8 \uc811\uadfc\uc740 \ub2e4\uc74c\uacfc \uac19\ub2e4. \uc55e\uc11c \uc9c1\uc811 \ud558\ub098 \ud558\ub098 \uac1c\ubc1c\uc790\uac00 \uc9c0\uc815\ud588\ub358 \uac83\ub4e4\uc5d0 \ub300\ud55c \ubaa8\ub4c8\uc744 \uc0ac\uc6a9\ud55c \uacbd\uc6b0\uc774\ub2e4.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">\r\nimport torch\r\nfrom torch import nn, optim\r\nfrom matplotlib import pyplot as plt\r\n\r\nweight_true = torch.Tensor([1,5,7]) # y = 1 + 5a + 7b\r\nX = torch.cat([torch.ones(100,1),torch.randn(100,2)], 1)\r\ny = torch.mv(X, weight_true) + torch.randn(100)\r\n\r\nnet = nn.Linear(in_features=3, out_features=1, bias=False)\r\noptimizer = optim.SGD(net.parameters(), lr=0.01)\r\nloss_fn = nn.MSELoss()\r\n\r\nlosses = []\r\n\r\nfor epoch in range(1000):\r\n    optimizer.zero_grad()\r\n\r\n    y_pred = net(X)\r\n    loss = loss_fn(y_pred.view_as(y), y)\r\n    loss.backward()\r\n\r\n    optimizer.step()\r\n\r\n    losses.append(loss.item())\r\n\r\nprint(net.weight)\r\n\r\nplt.plot(losses)\r\nplt.show()\r\n<\/pre>\n<p>\ub450 \uacbd\uc6b0 \ubaa8\ub450 \uc2e4\ud589\ud558\uba74 \uc544\ub798\uc640 \uac19\uc740 \uc190\uc2e4\uac12\uc5d0 \ub300\ud55c \uadf8\ub798\ud504\uc640 \ucd94\ub860\ub41c \uc0c1\uc218\uc640 \ub450\uacc4\uc218 \uac12\uc774 \ucf58\uc194\uc5d0 \ucd9c\ub825\ub41c\ub2e4.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.gisdeveloper.co.kr\/wp-content\/uploads\/2019\/11\/loss_graph.png\" alt=\"\" width=\"1026\" height=\"875\" class=\"aligncenter size-full wp-image-8613\" \/><\/p>\n<div>\ntensor([0.9295, 4.9402, 7.0627], requires_grad=True)<\/p>\n<div>\n","protected":false},"excerpt":{"rendered":"<p>\uc544\ub798\uc640 \uac19\uc740 \uc2dd\uc744 \ud68c\uadc0\ud558\ub294 \ubaa8\ub378\uc744 \uad6c\ud558\ub294 \ub450\uac00\uc9c0 \uc811\uadfc\uc744 PyTorch\ub85c \uc0b4\ud3b4\ubcf8\ub2e4. &nbsp; &nbsp; \uc989, \uc785\ub825\uac12(a, b)\uc5d0 \ub300\ud55c \ucd9c\ub825\uac12 y\uac00 100\uac1c \uc8fc\uc5b4\uc9c0\uace0, \uc774 \ub370\uc774\ud130\ub97c \ud1b5\ud574 \uc0c1\uc218\ud56d\uc778 1\uacfc \uacc4\uc218 5, 7\uc744 \uad6c\ud558\ub294 \uac83\uc774 \ubb38\uc81c\ub2e4. \ubb3c\ub860 y\uc5d0\ub294 \uc624\ucc28\uac00 \ubc18\uc601\ub418\uc5b4 \uc788\ub2e4. \uccab\ubc88\uc9f8 \uc811\uadfc\uc740 \ub2e4\uc74c\uacfc \uac19\ub2e4. \uc190\uc2e4\ud568\uc218\ub294 \ud3c9\uade0\ucd5c\uc18c\uc81c\uacf1\uc744, \uc5ed\uc804\ud30c\ub97c \ud1b5\ud55c \ucd5c\uc801\uac12 \uc218\ub834\uc744 \uc704\ud55c \uae30\uc6b8\uae30\ub97c \uad6c\ud574 \ubc18\uc601\ud55c \ud559\uc2b5\ub960\uc740 0.01\uc744 \uc0ac\uc6a9\ud588\ub2e4. \uc544\ub798\uc758 \ucf54\ub4dc\uc758 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=8608\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;\uc120\ud615\ud68c\uadc0\ubaa8\ub378\uc5d0 \ub300\ud55c PyTorch\ub97c \uc774\uc6a9\ud55c \ub450\uac00\uc9c0 \uc811\uadfc&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[132,1],"tags":[],"class_list":["post-8608","post","type-post","status-publish","format-standard","hentry","category-deep-machine-learning","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/8608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8608"}],"version-history":[{"count":6,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/8608\/revisions"}],"predecessor-version":[{"id":9369,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/8608\/revisions\/9369"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8608"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}