{"id":1121,"date":"2025-04-23T12:10:56","date_gmt":"2025-04-23T03:10:56","guid":{"rendered":"https:\/\/www.dogrow.net\/nnet\/?p=1121"},"modified":"2025-04-24T14:28:13","modified_gmt":"2025-04-24T05:28:13","slug":"34%e3%80%90pytorch%e3%81%a7mnist-5%e3%80%91%e5%ad%a6%e7%bf%92%e6%b8%88%e3%81%bf%e3%83%91%e3%83%a9%e3%83%a1%e3%83%bc%e3%82%bf%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e6%8c%87%e5%ae%9a%e5%8f%af","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/nnet\/34%e3%80%90pytorch%e3%81%a7mnist-5%e3%80%91%e5%ad%a6%e7%bf%92%e6%b8%88%e3%81%bf%e3%83%91%e3%83%a9%e3%83%a1%e3%83%bc%e3%82%bf%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e6%8c%87%e5%ae%9a%e5%8f%af\/","title":{"rendered":"(34)\u3010PyTorch\u3067MNIST #5\u3011\u5b66\u7fd2\u6e08\u307f\u30d1\u30e9\u30e1\u30fc\u30bf\u30d5\u30a1\u30a4\u30eb\u3092\u6307\u5b9a\u53ef\u80fd\u306b\u3059\u308b\u3002"},"content":{"rendered":"<h1 class=\"my_h\">\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u524d\u56de\u306e\u6295\u7a3f <a href=\"https:\/\/www.dogrow.net\/nnet\/blog33%e3%80%90pytorch%e3%81%a7mnist-4%e3%80%91%e3%83%97%e3%83%ad%e3%82%b0%e3%83%a9%e3%83%a0%e3%81%ae%e4%bf%9d%e5%ae%88%e6%80%a7%e3%82%92%e5%90%91%e4%b8%8a%e3%81%95%e3%81%9b%e3%82%8b%e3%80%82\/\" target=\"_blank\">(33)\u3010PyTorch\u3067MNIST #4\u3011\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4fdd\u5b88\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u3002<\/a> \u3067\u6b8b\u4ef6\u3068\u306a\u3063\u3066\u3044\u305f <span class=\"my_fc_deeppink\">\u5b66\u7fd2\u6e08\u307f\u30d1\u30e9\u30e1\u30fc\u30bf\u30d5\u30a1\u30a4\u30eb\u306e\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u6307\u5b9a<\/span> \u3092\u5b9f\u88c5\u3057\u305f\u3044\u3002<\/p>\n<p>\u3053\u308c\u306b\u3088\u308a\u3001\u8907\u6570\u306e\u5b9f\u884c\u7d50\u679c\u3092\u4fdd\u6301\u3001\u518d\u73fe\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002<br \/>\n<span class=\"my_fc_gray\">\u6280\u8853\u7684\u306b\u306f\u7279\u7b46\u3059\u3079\u304d\u3053\u3068\u306f\u306a\u3044\u304c\u2026<\/span><\/p>\n<h3 class=\"my_h\">\u5b66\u7fd2\u6e08\u307f\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u3064\u3044\u3066<\/h3>\n<p>\u30fb\u62e1\u5f35\u5b50 pth \u306f PyTorch \u3067\u5b66\u7fd2\u6e08\u307f\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u969b\u306e\u6a19\u6e96\u30fb\u6163\u7fd2\u7684\u306a\u5f62\u5f0f\u3060\u3002<br \/>\n\u30fb\u3053\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u306f\u3001Python\u30aa\u30d6\u30b8\u30a7\u30af\u30c8 (state_dict\u306a\u3069) \u3092 torch.save() \u3067\u4fdd\u5b58\u3057\u3066\u3044\u308b\u3002<br \/>\n\u30fb\u901a\u5e38\u306f dict\uff08\u4f8b\uff1amodel.state_dict()\uff09\u304c\u30b7\u30ea\u30a2\u30e9\u30a4\u30ba\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u30fb\u5b9f\u4f53\u306f Python\u306e pickle \u5f62\u5f0f\uff08\u30d0\u30a4\u30ca\u30ea\u30c7\u30fc\u30bf\uff09\u3067\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u3002<\/p>\n<h1 class=\"my_h\">\u30102\u3011\u3084\u3063\u3066\u307f\u308b<\/h1>\n<p>\u4eca\u56de\u5909\u66f4\u3059\u308b\u30d5\u30a1\u30a4\u30eb\u306f\u3001\u30cf\u30a4\u30e9\u30a4\u30c8\u8868\u793a\u3057\u305f 2\u30d5\u30a1\u30a4\u30eb\u306e\u307f\u3060\u3002<br \/>\n\u4ed6\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u3001\u305d\u306e\u307e\u307e\u4f7f\u7528\u3067\u304d\u308b\u3002<br \/>\n<table class=\"my_tbl_simple\">\n<tr><td>1<\/td><td>dataset_MNIST.py<\/td><td>MNIST\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306b\u4f9d\u5b58\u3059\u308b\u51e6\u7406\u3092\u96c6\u7d04\u30fb\u96a0\u853d<\/td><\/tr><tr><td>2<\/td><td>net_model.py<\/td><td>\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u69cb\u6210\u3092\u5b9a\u7fa9<\/td><\/tr><tr><td>3<\/td><td>trainer.py<\/td><td>\u5b66\u7fd2\u30fb\u30c6\u30b9\u30c8\u51e6\u7406\u3092\u5b9f\u88c5\uff08\u5206\u985e\u554f\u984c\u7528\uff09<\/td><\/tr><tr><td>4<\/td><td><span class=\"my_fc_deeppinkB\">control_train.py<\/span><\/td><td>\u5b66\u7fd2\u5b9f\u884c\u3092\u5236\u5fa1<\/td><\/tr><tr><td>5<\/td><td><span class=\"my_fc_deeppinkB\">control_test.py<\/span><\/td><td>\u30c6\u30b9\u30c8\u5b9f\u884c\u3092\u5236\u5fa1<\/td><\/tr>\n<\/table> <\/p>\n<h2 class=\"my_h\">1) \u30d7\u30ed\u30b0\u30e9\u30e0\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n<h3 class=\"my_h\">(1) control_train.py<\/h3>\n<p>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30aa\u30d7\u30b7\u30e7\u30f3 <span class=\"my_fc_deeppink\">-p<\/span> \u3067\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u6307\u5b9a\u3059\u308b\u3002<br \/>\n\u6307\u5b9a\u304c\u306a\u304b\u3063\u305f\u5834\u5408\u3001\u56fa\u5b9a\u540d\uff0b\u65e5\u6642\u5206\u79d2\u3067\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u81ea\u52d5\u751f\u6210\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom torch.utils.data import DataLoader\r\nfrom net_model import Net\r\nfrom trainer import Trainer\r\nfrom dataset_MNIST import MyDataset\r\nimport torch\r\nimport argparse\r\nfrom datetime import datetime\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef execTrain( save_path ):\r\n    # \u30c7\u30fc\u30bf\r\n    dataset = MyDataset()\r\n    train_loader = DataLoader(dataset.get_train_dataset(), batch_size=64, shuffle=True)\r\n    test_loader = DataLoader(dataset.get_test_dataset(), batch_size=1000, shuffle=False)\r\n    # \u74b0\u5883\r\n    device = torch.device(&quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;)\r\n    model = Net()\r\n    trainer = Trainer(model, device)\r\n    # \u5b9f\u884c\r\n    trainer.train(train_loader, epochs=3)\r\n    trainer.test(test_loader)\r\n    trainer.save(save_path)\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef main():\r\n    parser = argparse.ArgumentParser(description=&quot;Train and save MNIST model&quot;)\r\n    parser.add_argument('-p', type=str, help='Filename to save trained model')\r\n    args = parser.parse_args()\r\n    # \u30d5\u30a1\u30a4\u30eb\u540d\u306e\u6c7a\u5b9a\r\n    if args.p:\r\n        save_path = args.p\r\n    else:\r\n        now = datetime.now().strftime(&quot;%Y%m%d_%H%M%S&quot;)\r\n        save_path = f&quot;learned_model_{now}.pth&quot;\r\n    execTrain(save_path)\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nif __name__ == &quot;__main__&quot;:\r\n    main()\r\n<\/pre>\n<h3 class=\"my_h\">(2) control_test.py<\/h3>\n<p>\u3053\u3061\u3089\u306f <span class=\"my_fc_deeppink\">-p<\/span> \u30aa\u30d7\u30b7\u30e7\u30f3\u6307\u5b9a\u304c\u5fc5\u9808\u3060\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport torch\r\nfrom torch.utils.data import DataLoader\r\nfrom net_model import Net\r\nfrom trainer import Trainer\r\nfrom dataset_MNIST import MyDataset\r\nimport argparse\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef execTest( prmfile ):\r\n    # \u30c7\u30fc\u30bf\r\n    dataset = MyDataset()\r\n    test_loader = DataLoader(dataset.get_test_dataset(), batch_size=1000, shuffle=False)\r\n    # \u74b0\u5883\r\n    device = torch.device(&quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;)\r\n    model = Net()\r\n    trainer = Trainer(model, device)\r\n    # \u5b9f\u884c\r\n    trainer.load(prmfile)\r\n    trainer.test(test_loader)\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef main():\r\n    parser = argparse.ArgumentParser(description=&quot;Load and test trained MNIST model.&quot;)\r\n    parser.add_argument('-p', type=str, required=True, help='Filename of trained model to load')\r\n    args = parser.parse_args()\r\n    execTest(args.p)\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\nif __name__ == &quot;__main__&quot;:\r\n    main()\r\n<\/pre>\n<h2 class=\"my_h\">2) \u5b9f\u884c\u7d50\u679c<\/h2>\n<p>2\u56de\u5b66\u7fd2\u3092\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u30c6\u30b9\u30c8\u7d50\u679c\u306f\u30011\u56de\u76ee\u304c 97.19%, 2\u56de\u76ee\u304c 97.03%\u3060\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_train.py <span class=\"my_fc_yellow\">-p AAA.pth<\/span>\r\n[1\/3] Epoch complete\r\n[2\/3] Epoch complete\r\n[3\/3] Epoch complete\r\nTest Accuracy: <span class=\"my_fc_yellow\">97.19%<\/span>\r\nModel saved to AAA.pth\r\n$ python control_train.py <span class=\"my_fc_yellow\">-p BBB.pth<\/span>\r\n[1\/3] Epoch complete\r\n[2\/3] Epoch complete\r\n[3\/3] Epoch complete\r\nTest Accuracy: <span class=\"my_fc_yellow\">97.03%<\/span>\r\nModel saved to BBB.pth\r\n<\/pre>\n<p>1\u56de\u76ee\u30012\u56de\u76ee\u305d\u308c\u305e\u308c\u306e\u5b66\u7fd2\u6e08\u307f\u30d1\u30e9\u30e1\u30fc\u30bf\u30d5\u30a1\u30a4\u30eb\u3092\u6307\u5b9a\u3057\u3001\u30c6\u30b9\u30c8\u3092\u5358\u72ec\u5b9f\u884c\u3059\u308b\u3002<br \/>\n\u5b66\u7fd2\u5b9f\u884c\u6642\u3068\u540c\u3058\u7d50\u679c\u304c\u5f97\u3089\u308c\u305f\u306e\u3067 OK\uff01<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_test.py <span class=\"my_fc_yellow\">-p AAA.pth<\/span>\r\nModel loaded from AAA.pth\r\nTest Accuracy: <span class=\"my_fc_yellow\">97.19%<\/span>\r\n$ python control_test.py <span class=\"my_fc_yellow\">-p BBB.pth<\/span>\r\nModel loaded from BBB.pth\r\nTest Accuracy: <span class=\"my_fc_yellow\">97.03%<\/span>\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068 \u524d\u56de\u306e\u6295\u7a3f (33)\u3010PyTorch\u3067MNIST #4\u3011\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4fdd\u5b88\u6027\u3092\u5411\u4e0a\u3055\u305b\u308b\u3002 \u3067\u6b8b\u4ef6\u3068\u306a\u3063\u3066\u3044\u305f \u5b66\u7fd2\u6e08\u307f\u30d1\u30e9\u30e1\u30fc\u30bf\u30d5\u30a1\u30a4\u30eb\u306e\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u6307\u5b9a \u3092\u5b9f\u88c5\u3057\u305f\u3044\u3002 \u3053\u308c\u306b\u3088\u308a\u3001\u8907\u6570\u306e\u5b9f\u884c\u7d50\u679c\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/nnet\/34%e3%80%90pytorch%e3%81%a7mnist-5%e3%80%91%e5%ad%a6%e7%bf%92%e6%b8%88%e3%81%bf%e3%83%91%e3%83%a9%e3%83%a1%e3%83%bc%e3%82%bf%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab%e3%82%92%e6%8c%87%e5%ae%9a%e5%8f%af\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,16,6],"tags":[],"class_list":["post-1121","post","type-post","status-publish","format-standard","hentry","category-mnist","category-pytorch","category-6"],"views":693,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/comments?post=1121"}],"version-history":[{"count":22,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"predecessor-version":[{"id":1216,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1121\/revisions\/1216"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}