{"id":1448,"date":"2022-01-19T10:18:55","date_gmt":"2022-01-19T01:18:55","guid":{"rendered":"https:\/\/aiacademy.jp\/media\/?p=1448"},"modified":"2022-01-19T10:19:36","modified_gmt":"2022-01-19T01:19:36","slug":"%e3%80%90%e5%88%9d%e5%bf%83%e8%80%85%e5%90%91%e3%81%91%e3%80%91pytorch-lightning%e3%81%a8%e3%81%af%e3%80%80","status":"publish","type":"post","link":"https:\/\/aiacademy.jp\/media\/?p=1448","title":{"rendered":"\u3010\u521d\u5fc3\u8005\u5411\u3051\u3011PyTorch Lightning\u3068\u306f\u3000"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"i-0\">PyTorch Lightning\u3068\u306f\u3000<\/h1>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"600\" src=\"https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2022\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2022-01-19-9.44.03-1024x600.png\" alt=\"\" class=\"wp-image-1450\" srcset=\"https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2022\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2022-01-19-9.44.03-1024x600.png 1024w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2022\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2022-01-19-9.44.03-300x176.png 300w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2022\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2022-01-19-9.44.03-768x450.png 768w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2022\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2022-01-19-9.44.03-1536x899.png 1536w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2022\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2022-01-19-9.44.03-2048x1199.png 2048w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2022\/01\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2022-01-19-9.44.03-940x550.png 940w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.pytorchlightning.ai\/\" rel=\"nofollow noopener\" target=\"_blank\">PyTorch Lightning<\/a>\uff08\u7565\uff1aPL\uff09\u3068\u306f\u3001PyTorch\u3067\u66f8\u304f\u5fc5\u8981\u306a\u51e6\u7406\u304c\u30e1\u30bd\u30c3\u30c9\u5316\u3055\u308c\u305f\u3053\u3068\u3067\u3001\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u7c21\u5358\u306b\u3067\u304d\u308b\u30d5\u30ec\u30fc\u30ef\u30fc\u30af\u3067\u3059\u3002PyTorch Lightning\u3092\u7528\u3044\u308b\u3053\u3067\u3001\u958b\u767a\u30b9\u30d4\u30fc\u30c9\u3092\u9ad8\u3081\u308b\u3053\u3068\u304c\u51fa\u6765\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/github.com\/PyTorchLightning\/pytorch-lightning\/raw\/master\/docs\/source\/_static\/images\/general\/pl_quick_start_full_compressed.gif\" alt=\"\"><\/figure>\n\n\n\n<p>\u5f15\u7528\uff1a<a href=\"https:\/\/github.com\/PyTorchLightning\/pytorch-lightning\" rel=\"nofollow noopener\" target=\"_blank\">https:\/\/github.com\/PyTorchLightning\/pytorch-lightning<\/a><meta charset=\"utf-8\"><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-1\"><meta charset=\"utf-8\">PyTorch Lightning\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b<\/h1>\n\n\n\n<p><\/p>\n\n\n\n<p><meta charset=\"utf-8\">PyTorch Lightning\u306f\u300cpip\u300d\u30b3\u30de\u30f3\u30c9\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u304c\u53ef\u80fd\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pip install pytorch-lightning<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-2\"><meta charset=\"utf-8\">PyTorch Lightning\u3092\u8aad\u307f\u8fbc\u3080<\/h1>\n\n\n\n<p>PyTorch Lightning\u3092\u5229\u7528\u3059\u308b\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306bimport\u3059\u308c\u3070\u826f\u3044\u3067\u3059\u3002\u306a\u304a\u3088\u304f\u540d\u524d\u306b\u306fpl\u3068\u3064\u3051\u308b\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pytorch_lightning as pl<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-3\">PyTorch Lightning\u3067MNIST\u3092\u5b9f\u65bd\u3059\u308b<\/h1>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4eca\u56de\u306fGoogle Colab\u74b0\u5883\u3092\u7528\u3044\u3066\u3001<meta charset=\"utf-8\">MNIST\u5206\u985e\u30e2\u30c7\u30eb\u3092\u4f5c\u308b\u30b3\u30fc\u30c9\u3092PL\u3067\u66f8\u3044\u3066\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u8aad\u307f\u8fbc\u307f\nimport os # <meta charset=\"utf-8\">os.getcwd()\u3092\u5229\u7528\u3059\u308b\u305f\u3081\u306b\u30a4\u30f3\u30dd\u30fc\u30c8\nimport torch\nfrom torch import nn\nimport torch.nn.functional as F\nfrom torchvision.datasets import MNIST\nfrom torch.utils.data import DataLoader, random_split\nfrom torchvision import transforms\nimport pytorch_lightning as pl\n\n# LightningModule (nn.Module \u306e\u30b5\u30d6\u30af\u30e9\u30b9) \u306e\u5b9a\u7fa9\nclass LitAutoEncoder(pl.LightningModule):\n    def __init__(self):\n        super().__init__()\n        self.encoder = nn.Sequential(nn.Linear(28 * 28, 128), nn.ReLU(), nn.Linear(128, 3))\n        self.decoder = nn.Sequential(nn.Linear(3, 128), nn.ReLU(), nn.Linear(128, 28 * 28))\n        \n    # \u4e88\u6e2c\/\u63a8\u8ad6\u30a2\u30af\u30b7\u30e7\u30f3\u3092\u5b9a\u7fa9\n    def forward(self, x):\n        embedding = self.encoder(x)\n        return embedding\n\n    # train \u30eb\u30fc\u30d7\u3092\u5b9a\u7fa9\n    def training_step(self, batch, batch_idx):\n        x, y = batch\n        x = x.view(x.size(0), -1)\n        z = self.encoder(x)\n        x_hat = self.decoder(z)\n        loss = F.mse_loss(x_hat, x)\n        self.log(\"train_loss\", loss)\n        return loss\n\n    def configure_optimizers(self):\n        optimizer = torch.optim.Adam(self.parameters(), lr=1e-3)\n        return optimizer\n\n# \u5b66\u7fd2\ndataset = MNIST(os.getcwd(), download=True, transform=transforms.ToTensor())\ntrain, val = random_split(dataset, [55000, 5000])\n\nautoencoder = LitAutoEncoder()\ntrainer = pl.Trainer()\ntrainer.fit(autoencoder, DataLoader(train), DataLoader(val))<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8a18\u306e\u3088\u3046\u306b\u3001PyTorch Lightning\u3092\u7528\u3044\u308b\u3068\u3001\uff14\uff10\u884c\u307b\u3069\u3067\u30b3\u30fc\u30c9\u304c\u66f8\u3051\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"i-4\">\u304a\u308f\u308a\u306b<\/h1>\n\n\n\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001PyTorch Lightning\u3068\u306f\u4f55\u304b\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u3001MNIST\u306e\u5206\u985e\u5668\u3092\u4f5c\u6210\u3059\u308b\u4e00\u9023\u306e\u30b3\u30fc\u30c9\u3092\u7d39\u4ecb\u81f4\u3057\u307e\u3057\u305f\u3002<meta charset=\"utf-8\">PyTorch Lightning\u306a\u3069\u3092\u72ec\u5b66\u3067\u52c9\u5f37\u3059\u308b\u3068\u304d\u306b\u3001\u308f\u304b\u3089\u306a\u3044\u3053\u3068\u304c\u51fa\u3066\u304d\u305f\u5834\u5408\u3001\u305d\u3053\u304b\u3089\u306a\u304b\u306a\u304b\u5148\u306b\u30b9\u30e0\u30fc\u30ba\u306b\u5b66\u7fd2\u3067\u304d\u306a\u3044\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u306e\u3088\u3046\u306a\u65b9\u304c\u6a5f\u68b0\u5b66\u7fd2\u3092\u5b66\u3076\u4e0a\u3067\u30aa\u30b9\u30b9\u30e1\u306a\u306e\u306f\u3001\u6a5f\u68b0\u5b66\u7fd2\u30a8\u30f3\u30b8\u30cb\u30a2\u304b\u3089\u3044\u3064\u3067\u3082\u8cea\u554f\u3067\u304d\u308b\u74b0\u5883\u3067\u5b66\u3076\u3053\u3068\u3067\u3059\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/aiacademy.jp\/bootcamp\">AI Academy Bootcamp<\/a>\u306a\u3089\u3001<a rel=\"noreferrer noopener\" href=\"https:\/\/aiacademy.jp\/bootcamp\" target=\"_blank\">\uff16\u30f6\u670835,000\u5186\u306b\u3066\u30c1\u30e3\u30c3\u30c8\u3067\u8cea\u554f\u3057\u653e\u984c\u306e\u74b0\u5883\u3067\u3001\u6a5f\u68b0\u5b66\u7fd2\u3084\u30c7\u30fc\u30bf\u5206\u6790\u304c\u5b66\u3079\u308b\u30b5\u30fc\u30d3\u30b9<\/a>\u3092\u63d0\u4f9b\u3057\u3066\u304a\u308a\u307e\u3059\u3002<br>\u6570\u5341\u540d\u5728\u7c4d\u3057\u3066\u3044\u308b\u30c7\u30fc\u30bf\u30b5\u30a4\u30a8\u30f3\u30c6\u30a3\u30b9\u30c8\u3084\u6a5f\u68b0\u5b66\u7fd2\u30a8\u30f3\u30b8\u30cb\u30a2\u306b\u8cea\u554f\u3057\u653e\u984c\u306e\u74b0\u5883\u3067\u30c7\u30fc\u30bf\u5206\u6790\u3001\u7d71\u8a08\u3001\u6a5f\u68b0\u5b66\u7fd2\u3001SQL\u7b49\u304c\u5b66\u3079\u307e\u3059\u3002AI\u4eba\u6750\u306b\u5fc5\u8981\u306a\u30b9\u30ad\u30eb\u3092\u52b9\u7387\u3088\u304f\u4f53\u7cfb\u7684\u306b\u8eab\u306b\u4ed8\u3051\u305f\u3044\u65b9\u306f\u662f\u975e\u3054\u691c\u8a0e\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/aiacademy.jp\/bootcamp\/\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"341\" src=\"https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-1024x341.png\" alt=\"\" class=\"wp-image-691\" srcset=\"https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-1024x341.png 1024w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-300x100.png 300w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-768x256.png 768w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi-940x313.png 940w, https:\/\/aiacademy.jp\/media\/wp-content\/uploads\/2021\/12\/bootcamp_ad_72ppi.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>PyTorch Lightning\u3068\u306f\u3000 PyTorch Lightning\uff08\u7565\uff1aPL\uff09\u3068\u306f\u3001PyTorch\u3067\u66f8\u304f\u5fc5\u8981\u306a\u51e6\u7406\u304c\u30e1\u30bd\u30c3\u30c9\u5316\u3055\u308c\u305f\u3053\u3068\u3067\u3001\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3092\u7c21\u5358\u306b\u3067\u304d\u308b\u30d5\u30ec\u30fc\u30ef\u30fc\u30af\u3067\u3059\u3002PyTorch Light &#8230; <\/p>\n","protected":false},"author":1,"featured_media":1455,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[],"class_list":{"0":"post-1448","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-pytorch"},"_links":{"self":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/1448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1448"}],"version-history":[{"count":9,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/1448\/revisions"}],"predecessor-version":[{"id":1459,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/posts\/1448\/revisions\/1459"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=\/wp\/v2\/media\/1455"}],"wp:attachment":[{"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aiacademy.jp\/media\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}