{"id":1145,"date":"2025-04-23T13:08:50","date_gmt":"2025-04-23T04:08:50","guid":{"rendered":"https:\/\/www.dogrow.net\/nnet\/?p=1145"},"modified":"2025-04-24T14:44:07","modified_gmt":"2025-04-24T05:44:07","slug":"35%e3%80%90pytorch%e3%81%a7mnist-6%e3%80%91%e4%b8%8d%e6%ad%a3%e8%a7%a3%e7%94%bb%e5%83%8f%e3%81%a8%e5%88%a4%e5%ae%9a%e7%b5%90%e6%9e%9c%e3%82%92jpeg%e7%94%bb%e5%83%8f%e5%87%ba%e5%8a%9b%e3%81%99","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/nnet\/35%e3%80%90pytorch%e3%81%a7mnist-6%e3%80%91%e4%b8%8d%e6%ad%a3%e8%a7%a3%e7%94%bb%e5%83%8f%e3%81%a8%e5%88%a4%e5%ae%9a%e7%b5%90%e6%9e%9c%e3%82%92jpeg%e7%94%bb%e5%83%8f%e5%87%ba%e5%8a%9b%e3%81%99\/","title":{"rendered":"(35)\u3010PyTorch\u3067MNIST #6\u3011\u4e0d\u6b63\u89e3\u753b\u50cf\u3068\u5224\u5b9a\u7d50\u679c\u3092JPEG\u753b\u50cf\u51fa\u529b\u3059\u308b\u3002"},"content":{"rendered":"<h1 class=\"my_h\">\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>3 epochs\u3067\u6b63\u89e3\u7387\u304c 97%\u3068\u3044\u3046\u3053\u3068\u306f\u3001<br \/>\n10,000\u500b\u306e\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066\u3001\u4e0d\u6b63\u89e3\u304c 300\u500b\u3057\u304b\u306a\u304b\u3063\u305f\u3068\u3044\u3046\u3053\u3068\u3060\u3002<\/p>\n<p><span class=\"my_fc_crimsonBBig\">\u3053\u306e\u8ce2\u3044 AI\u304c\u3001\u3069\u3093\u306a\u753b\u50cf\u3092\u82e6\u624b\u3068\u3057\u3066\u3044\u308b\u306e\u304b\uff1f<\/span><br \/>\n\u3092\u898b\u3066\u307f\u305f\u3044\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/Screenshot-from-2025-04-24-06-26-33.png\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<h1 class=\"my_h\">\u30102\u3011\u3084\u3063\u3066\u307f\u308b<\/h1>\n<p>\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5909\u66f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u5909\u66f4\u65b9\u91dd\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<br \/>\n\u30fb\u30c6\u30b9\u30c8\u5b9f\u884c\u6642\u3001\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5bfe\u3059\u308b\u5224\u5b9a\u7d50\u679c\u3092\u8a18\u9332\u3057\u3066\u304a\u304f\u3002\u2192 \u753b\u50cf\u4f5c\u6210\u6642\u306b\u3053\u308c\u3092\u518d\u5229\u7528\u3059\u308b\u3002<br \/>\n\u30fb\u51fa\u529b\u753b\u50cf\u306e\u4e2d\u306b\u306f\u3001\u753b\u50cf\u3068\u5224\u5b9a\u7d50\u679c\uff08\u30e9\u30d9\u30eb\u3054\u3068\u306e\u51fa\u529b\u5024\uff09\u3092\u4e00\u7dd2\u306b\u8868\u793a\u3059\u308b\u3002<\/p>\n<h2 class=\"my_h\">1) \u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0<\/h2>\n<h3 class=\"my_h\">(1) trainer.py<\/h3>\n<p><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\">\u524d\u56de\u306e\u30d7\u30ed\u30b0\u30e9\u30e0<\/a> \u304b\u3089\u306e\u5909\u66f4\u70b9\u3092\u30cf\u30a4\u30e9\u30a4\u30c8\u8868\u793a\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; highlight: [31,32,33,42,43,44,45,46,50]; title: trainer.py; notranslate\" title=\"trainer.py\">\r\nimport torch\r\nimport torch.nn as nn\r\nimport torch.optim as optim\r\n\r\nclass Trainer:\r\n    #\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    def __init__(self, model, device, lr=0.001):\r\n        self.model = model.to(device)\r\n        self.device = device\r\n        self.criterion = nn.CrossEntropyLoss()\r\n        self.optimizer = optim.Adam(self.model.parameters(), lr=lr)\r\n\r\n    #\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    def train(self, train_loader, epochs=3):\r\n        self.model.train()\r\n        for epoch in range(epochs):\r\n            for data, target in train_loader:\r\n                data, target = data.to(self.device), target.to(self.device)\r\n                self.optimizer.zero_grad()\r\n                output = self.model(data)\r\n                loss = self.criterion(output, target)\r\n                loss.backward()\r\n                self.optimizer.step()\r\n            print(f&quot;&#x5B;{epoch+1}\/{epochs}] Epoch complete&quot;)\r\n\r\n    #\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    def test(self, test_loader):\r\n        self.model.eval()\r\n        correct = 0\r\n        total = 0\r\n        all_images = &#x5B;]      # \u51fa\u529b\u7528\uff1a\u5168\u753b\u50cf\u30c7\u30fc\u30bf\r\n        all_probs  = &#x5B;]      # \u51fa\u529b\u7528\uff1a\u5168\u30c6\u30b9\u30c8\u7d50\u679c\r\n        all_labels = &#x5B;]      # \u51fa\u529b\u7528\uff1a\u5168\u753b\u50cf\u30c7\u30fc\u30bf\u306e\u6b63\u89e3\u30e9\u30d9\u30eb\r\n        #----------------------------------------------------------------------\r\n        with torch.no_grad():\r\n            for data, target in test_loader:\r\n                data, target = data.to(self.device), target.to(self.device)\r\n                outputs = self.model(data)\r\n                _, predicted = torch.max(outputs.data, 1)\r\n                total += target.size(0)\r\n                correct += (predicted == target).sum().item()\r\n                # \u51fa\u529b\u7528\u306b\u4eca\u56de\u30d0\u30c3\u30c1\u306e\u30c6\u30b9\u30c8\u7d50\u679c\u3092\u9000\u907f\u3057\u3066\u304a\u304f\u3002\r\n                probs = torch.softmax(outputs, dim=1)\r\n                all_images.extend(data.cpu())\r\n                all_probs.extend(probs.cpu())\r\n                all_labels.extend(target.cpu())\r\n        #----------------------------------------------------------------------\r\n        acc = 100 * correct \/ total\r\n        print(f&quot;Test Accuracy: {acc:.2f}%&quot;)\r\n        return acc, all_images, all_probs, all_labels\r\n\r\n    #\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    def save(self, path):\r\n        torch.save(self.model.state_dict(), path)\r\n        print(f&quot;Model saved to {path}&quot;)\r\n\r\n    #\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n    def load(self, path):\r\n        self.model.load_state_dict(torch.load(path, map_location=self.device))\r\n        print(f&quot;Model loaded from {path}&quot;)\r\n<\/pre>\n<h3 class=\"my_h\">(2) control_test.py<\/h3>\n<p>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30aa\u30d7\u30b7\u30e7\u30f3\u3067 <span class=\"my_fc_deeppink\">-v<\/span> \u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u306b\u753b\u50cf\u51fa\u529b\u3059\u308b\u3088\u3046\u306b\u3057\u305f\u3002<br \/>\n\u753b\u50cf\u51fa\u529b\u95a2\u6570 output_results_image() \u3092\u65b0\u898f\u8ffd\u52a0\u3057\u305f\u3002<br \/>\ntarget=&#8221;_blank&#8221;>\u524d\u56de\u306e\u30d7\u30ed\u30b0\u30e9\u30e0<\/a> \u304b\u3089\u306e\u5909\u66f4\u70b9\u3092\u30cf\u30a4\u30e9\u30a4\u30c8\u8868\u793a\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; highlight: [6,7,8,9,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,78,79,80,81,87,89]; title: control_test.py; notranslate\" title=\"control_test.py\">\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\nimport matplotlib.pyplot as plt\r\nimport numpy as np\r\nimport os\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef output_results_image(images, probs_list, labels):\r\n    os.makedirs(&quot;results&quot;, exist_ok=True)  # \u51fa\u529b\u5148\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u6210\r\n    #for idx in range(len(images)):\r\n    for idx in range(1000):\r\n        prob = probs_list&#x5B;idx].numpy()     # prob \u306f\u9577\u305510\u306e\u78ba\u7387\u30d9\u30af\u30c8\u30eb\uff08\u5404\u6570\u5b57\u304c\u6b63\u89e3\u3067\u3042\u308b\u78ba\u7387\uff09\r\n        predicted_label = np.argmax(prob)  # \u78ba\u7387\u30d9\u30af\u30c8\u30eb\u306e\u4e2d\u3067\u6700\u3082\u5024\u304c\u5927\u304d\u3044\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\uff08\u3064\u307e\u308a\u4e88\u6e2c\u30e9\u30d9\u30eb\uff09\u3092\u53d6\u308a\u51fa\u3059\u3002\r\n        true_label = labels&#x5B;idx]           # \u3053\u306e\u753b\u50cf\u306e\u6b63\u89e3\u30e9\u30d9\u30eb\uff08\u6559\u5e2b\u30c7\u30fc\u30bf\uff09\u3092\u53d6\u308a\u51fa\u3059\u3002\r\n        if predicted_label == true_label:  # \u6b63\u89e3\u753b\u50cf\u306e\u5834\u5408\u306f\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3002\r\n            continue\r\n        nClasses = len(prob)\r\n\r\n        # images&#x5B;idx] \u306f\u5f62\u72b6\u304c (1, 28, 28) \u306e\u30c6\u30f3\u30bd\u30eb\uff081\u30c1\u30e3\u30cd\u30eb\u306e\u753b\u50cf\uff09\u306a\u306e\u3067\u3001\r\n        # .squeeze(0) \u3067\u5148\u982d\u306e\u6b21\u5143\uff081\uff09\u3092\u6d88\u3057\u3066 (28, 28) \u306b\u3057 .numpy() \u3067 NumPy \u914d\u5217\u306b\u5909\u63db\u3059\u308b\u3002\r\n        img = images&#x5B;idx].squeeze(0).numpy()\r\n\r\n        # \u753b\u50cf1\u679a\u3054\u3068\u306b\u65b0\u3057\u3044\u56f3\u3092\u4f5c\u308b\uff08\u30b5\u30a4\u30ba\u306f 4\u30a4\u30f3\u30c1 \u00d7 4\u30a4\u30f3\u30c1\uff09\r\n        # save\u5b9f\u884c\u6642\u306e dpi=150 \u3067\u753b\u50cf\u30b5\u30a4\u30ba\u3092\u66f4\u306b\u8abf\u6574\u53ef\u80fd\r\n        # fig \u306f\u56f3\u5168\u4f53\u3001ax \u306f\u305d\u306e\u4e2d\u306e\u63cf\u753b\u9818\u57df\r\n        fig, ax = plt.subplots(figsize=(3, 3))  # \u753b\u50cf\u3054\u3068\u306b\u65b0\u3057\u3044\u56f3\u3092\u4f5c\u6210\r\n        ax.set_facecolor('white')\r\n\r\n        # \u4e0a\u534a\u5206\u306b\u753b\u50cf\r\n        # \u753b\u50cf\u3092\u30b0\u30ec\u30fc\u30b9\u30b1\u30fc\u30eb\u3067\u8868\u793a\u3059\u308b\u3002\r\n        # X\u8ef8\u306f 0\u301c28\uff08matplotlib\u5185\u90e8\u306e\u4eee\u60f3\u7684\u306a\u5ea7\u6a19\u7cfb\u306e\u6570\u5024)\r\n        # Y\u8ef8\u306f 30\u301c56 \u306b\u3057\u3066\u30bb\u30eb\u5185\u306e\u4e0a\u5074(\u4e0a\u534a\u5206)\u306b\u8868\u793a\u3055\u308c\u308b\u3088\u3046\u306b\u3059\u308b\u3002\r\n        ax.imshow(img, cmap='gray', extent=&#x5B;0.4, 28.4, 30, 56])\r\n\r\n        # \u4e0b\u534a\u5206\u306b\u68d2\u30b0\u30e9\u30d5\uff08\u6b63\u89e3\u306f\u8d64\u3001\u4ed6\u306f\u7070\u8272\uff09\r\n        bar_colors = &#x5B;'red' if i == true_label else 'gray' for i in range(nClasses)]\r\n        # \u68d2\u30b0\u30e9\u30d5\u3092\u63cf\u304f\u3002\r\n        # Bar\u6a2a\u4f4d\u7f6e\uff08X\u5ea7\u6a19\uff09\uff1a 0, 3.2, 6.4, ..., 28.8\uff08\u30e9\u30d9\u30eb0\u301c9\u306e\u4f4d\u7f6e\uff09\r\n        # Bar\u9ad8\u3055\uff1a prob(=\u51fa\u529b\u5c64\u306e\u5024) * 28 \u2192 0\u301c28\u306e\u7bc4\u56f2\u306b\u30b9\u30b1\u30fc\u30ea\u30f3\u30b0\r\n        # Bar\u5e45\uff1a 3\r\n        x_positions = np.arange(nClasses) * 3.2\r\n        ax.bar(x_positions, prob * 28, width=3, bottom=0, color=bar_colors)\r\n\r\n        # \u8ef8\u8a2d\u5b9a\uff08\u6c34\u5e73\u65b9\u5411\u30b0\u30ea\u30c3\u30c9\u4ed8\u304d\uff09\r\n        # X\u8ef8\u306e\u76ee\u76db\u308a\u30920\u301c9\u306e\u30e9\u30d9\u30eb\u306b\u5bfe\u5fdc\u3055\u305b\u3066\u8868\u793a\u3059\u308b\u3002\r\n        ax.set_xticks(x_positions)\r\n        ax.set_xticklabels(&#x5B;str(i) for i in range(nClasses)], fontsize=8)\r\n        # Y\u8ef8\u30820.0\u301c1.0\u306e\u7bc4\u56f2\u306b\u76ee\u76db\u308a\u3092\u4ed8\u3051\u308b\u304c\u3001\u8868\u793a\u306f28\u30b9\u30b1\u30fc\u30eb\u306b\u5408\u308f\u305b\u305f\u4f4d\u7f6e\u306b\u306a\u308b\u3002\r\n        y_ticks = np.arange(0.0, 1.01, 0.1) * 28\r\n        ax.set_yticks(y_ticks)\r\n        ax.set_yticklabels(&#x5B;f&quot;{y:.1f}&quot; for y in np.arange(0.0, 1.01, 0.1)], fontsize=7)\r\n        # Y\u8ef8\u306b\u70b9\u7dda\u306e\u30b0\u30ea\u30c3\u30c9\u7dda\u3092\u5f15\u304f\u3002\u7dda\u306e\u8272\u306f\u8584\u3044\u30b0\u30ec\u30fc\u3001\u592a\u30550.5\r\n        ax.yaxis.grid(True, linestyle='--', linewidth=0.5, color='lightgray')\r\n\r\n        # \u4e0d\u8981\u306a\u4f59\u767d\u3092\u524a\u9664\u3002\u56f3\u5168\u4f53\u306e\u4f59\u767d\u3092\u81ea\u52d5\u3067\u8abf\u6574\u3057\u3066\u3001\u898b\u3084\u3059\u304f\u3059\u308b\u3002\r\n        plt.tight_layout()\r\n\r\n        # JPEG\u3068\u3057\u3066\u4fdd\u5b58\uff08\u4f8b\uff1aresults\/image_00000.jpg\uff09\r\n        filename = f&quot;results\/i_{idx:05}.jpg&quot;\r\n        plt.savefig(filename, dpi=150)\r\n        plt.close(fig)  # \u30e1\u30e2\u30ea\u7bc0\u7d04\u306e\u305f\u3081\u660e\u793a\u7684\u306b\u9589\u3058\u308b\r\n\r\n#\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\ndef execTest( prmfile, visualize ):\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    acc, images, probs, labels = trainer.test(test_loader)\r\n    # \u7d50\u679c\u8868\u793a\r\n    if visualize:\r\n        output_results_image(images, probs, labels)\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    parser.add_argument('-v', action='store_true', help='Visualize test result samples')\r\n    args = parser.parse_args()\r\n    execTest(prmfile=args.p, visualize=args.v)\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>\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30aa\u30d7\u30b7\u30e7\u30f3\u3067 <span class=\"my_fc_deeppink\">-v<\/span> \u3092\u6307\u5b9a\u3057\u3066\u30c6\u30b9\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_test.py -p AAA.pth -v\r\nModel loaded from AAA.pth\r\nTest Accuracy: 96.89%\r\n<\/pre>\n<p>\u3059\u308b\u3068\u3001\u76f4\u4e0b\u306e results \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u524d\u8ff0\u306e\u901a\u308a\u306e\u753b\u50cf\u304c\u51fa\u529b\u3055\u308c\u308b\u3002<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/Screenshot-from-2025-04-24-06-21-09.png\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<p>\u500b\u3005\u306e\u753b\u50cf\u306f\u3053\u3093\u306a\u4ed5\u69d8\u3067\u51fa\u529b\u3055\u308c\u308b\u3002<br \/>\n\u30fb\u4e0a\u534a\u5206\u306b\u3001\u30c6\u30b9\u30c8\u753b\u50cf\u3092\u8868\u793a\u3059\u308b\u3002<br \/>\n\u30fb\u4e0b\u534a\u5206\u306b\u3001\u30e9\u30d9\u30eb\u3054\u3068\u306e\u5224\u5b9a\u7d50\u679c\u3092\u8868\u793a\u3059\u308b\u3002\uff08\u51fa\u529b\u5c64\u306e\u5404\u30cb\u30e5\u30fc\u30ed\u30f3\u306e\u51fa\u529b\u5024\u3092softmax\u95a2\u6570\u306b\u901a\u3057\u305f\u5024\uff09<br \/>\n\u30fb\u8d64\u8272\u306e\u30d0\u30fc\u304c\u6b63\u89e3\u30e9\u30d9\u30eb\u3060\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00018.jpg\" alt=\"\" \/> <img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00038.jpg\" alt=\"\" \/> <img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00149.jpg\" alt=\"\" \/> <img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00225.jpg\" alt=\"\" \/> <img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00247.jpg\" alt=\"\" \/> <img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00259.jpg\" alt=\"\" \/> <img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00274.jpg\" alt=\"\" \/> <img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00290.jpg\" alt=\"\" \/> <img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00321.jpg\" alt=\"\" \/> <img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/i_00340.jpg\" alt=\"\" \/><\/p>\n<h1 class=\"my_h\">\u30103\u3011\u3055\u3089\u306b\u8ce2\u304f\u30c1\u30e5\u30fc\u30f3\u30cb\u30f3\u30b0\u3057\u305f AI\u3067\u3084\u3063\u3066\u307f\u308b<\/h1>\n<p>\u4e0a\u3067\u4f7f\u3063\u305f AI\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u69cb\u6210\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3002<br \/>\nINPUT[28][28] \u2192 Full[128] \u2192 ReLU \u2192 Full[64] \u2192 ReLU \u2192 Full[10]<\/p>\n<p>\u753b\u50cf\u8a8d\u8b58\u306b\u5bfe\u3057\u3066\u6709\u52b9\u306a CNN\u3092\u4f7f\u3063\u305f\u4ee5\u4e0b\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u69cb\u6210\u3067\u8a66\u3057\u3066\u307f\u308b\u3002<br \/>\nINPUT[28][28] \u2192 CNN[16][28][28] \u2192 ReLU \u2192 MaxPool[16][14][14] \u2192 CNN[32][14][14] \u2192 ReLU \u2192 MaxPool[32][7][7] \u2192 Full[256] \u2192 ReLU \u2192 Full[10]<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport torch.nn as nn\r\nimport torch\r\n\r\nclass Net(nn.Module):\r\n    def __init__(self):\r\n        super(Net, self).__init__()\r\n        # bs : batch_size\r\n        self.conv1 = nn.Conv2d(in_channels=1, out_channels=16, kernel_size=3, padding=1)  # &#x5B;bs]&#x5B;1]&#x5B;28]&#x5B;28] \u2192 &#x5B;bs]&#x5B;16]&#x5B;28]&#x5B;28]\r\n        self.pool  = nn.MaxPool2d(kernel_size=2, stride=2)                                #  \u2192 &#x5B;bs]&#x5B;16]&#x5B;14]&#x5B;14]               \r\n        self.conv2 = nn.Conv2d(in_channels=16, out_channels=32, kernel_size=3, padding=1) #  \u2192 &#x5B;bs]&#x5B;32]&#x5B;14]&#x5B;14] \u2192pool\u2192 &#x5B;bs]&#x5B;32]&#x5B;7]&#x5B;7]\r\n        self.fc1   = nn.Linear(in_features=32 * 7 * 7, out_features=256)                  #  \u2192 &#x5B;bs]&#x5B;32x7x7] \u2192 &#x5B;bs]&#x5B;256]\r\n        self.fc2   = nn.Linear(in_features=256, out_features=10)                          #  \u2192 &#x5B;bs]&#x5B;256] \u2192 &#x5B;bs]&#x5B;10] \r\n\r\n    def forward(self, x):\r\n        x = self.pool(torch.relu(self.conv1(x)))   #   CNN \u2192 ReLU \u2192 Max.pooling\r\n        x = self.pool(torch.relu(self.conv2(x)))   # \u2192 CNN \u2192 ReLU \u2192 Max.pooling\r\n        x = x.view(-1, 32 * 7 * 7)                 # \u2192 &#x5B;bs]&#x5B;32]&#x5B;7]&#x5B;7] \u2192 flatten &#x5B;bs]&#x5B;32x7x7=1568]\r\n        x = torch.relu(self.fc1(x))                # \u2192 &#x5B;1568] \u2192 FC\u2192 &#x5B;256] \u2192 ReLU\r\n        x = self.fc2(x)                            # \u2192 &#x5B;10]\r\n        return x\r\n<\/pre>\n<p>\u3064\u3044\u3067\u306b epoch\u6570\u3092 10\u306b\u5897\u3084\u3057\u3066\u5b66\u7fd2\u3092\u5b9f\u884c\u3057\u305f\u3002<br \/>\n \u2192 \u4e0b\u8a18\u306e\u901a\u308a\u3001\u6b63\u89e3\u7387\u306f 99.02% \u306b\u4e0a\u6607\u3057\u305f\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_train.py -p BBB.pth\r\n[1\/10] Epoch complete\r\n[2\/10] Epoch complete\r\n[3\/10] Epoch complete\r\n[4\/10] Epoch complete\r\n[5\/10] Epoch complete\r\n[6\/10] Epoch complete\r\n[7\/10] Epoch complete\r\n[8\/10] Epoch complete\r\n[9\/10] Epoch complete\r\n[10\/10] Epoch complete\r\nTest Accuracy: 99.02%\r\nModel saved to BBB.pth\r\n<\/pre>\n<p>\u7d9a\u3051\u3066\u3001NG\u753b\u50cf\u306e\u7d50\u679c\u3092 JPEG\u753b\u50cf\u30d5\u30a1\u30a4\u30eb\u51fa\u529b\u3055\u305b\u308b\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ python control_test.py -p BBB.pth -v\r\nModel loaded from BBB.pth\r\nTest Accuracy: 99.02%\r\n<\/pre>\n<p>\u6b63\u89e3\u7387 99%\u306e AI\u304c\u82e6\u624b\u3068\u3057\u305f\u753b\u50cf\u306f\u3053\u3093\u306a\u3082\u306e\u3060\u3063\u305f\u3002<\/p>\n<p>\u5168\u4f53\u7684\u306b\u3001\u300c\u60dc\u3057\u3044\uff01\u300d\u3068\u3044\u3046\u7d50\u679c\u304c\u898b\u3089\u308c\u306a\u304b\u3063\u305f\u3002<br \/>\n\u4eba\u304c\u76ee\u8996\u3059\u308c\u3070\u300c\u660e\u3089\u304b\u306b\u9055\u3046\u300d\u3068\u5224\u65ad\u3067\u304d\u308b\u753b\u50cf\u3067\u3082\u3001AI\u304c\u81ea\u4fe1\u6e80\u3005\u306b\u9593\u9055\u3048\u3066\u3044\u308b\u3082\u306e\u3070\u304b\u308a\u304c\u6b8b\u3063\u305f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.dogrow.net\/nnet\/wp-content\/uploads\/2025\/04\/Screenshot-from-2025-04-24-06-54-50.png\" alt=\"\" class=\"my_add_bs1\" \/><\/p>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068 3 epochs\u3067\u6b63\u89e3\u7387\u304c 97%\u3068\u3044\u3046\u3053\u3068\u306f\u3001 10,000\u500b\u306e\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066\u3001\u4e0d\u6b63\u89e3\u304c 300\u500b\u3057\u304b\u306a\u304b\u3063\u305f\u3068\u3044\u3046\u3053\u3068\u3060\u3002 \u3053\u306e\u8ce2\u3044 AI\u304c\u3001\u3069\u3093\u306a\u753b\u50cf\u3092\u82e6\u624b\u3068\u3057\u3066\u3044\u308b\u306e\u304b\uff1f \u3092\u898b\u3066\u307f\u305f\u3044\u3002\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/nnet\/35%e3%80%90pytorch%e3%81%a7mnist-6%e3%80%91%e4%b8%8d%e6%ad%a3%e8%a7%a3%e7%94%bb%e5%83%8f%e3%81%a8%e5%88%a4%e5%ae%9a%e7%b5%90%e6%9e%9c%e3%82%92jpeg%e7%94%bb%e5%83%8f%e5%87%ba%e5%8a%9b%e3%81%99\/\">\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-1145","post","type-post","status-publish","format-standard","hentry","category-mnist","category-pytorch","category-6"],"views":655,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1145","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=1145"}],"version-history":[{"count":29,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1145\/revisions"}],"predecessor-version":[{"id":1221,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/posts\/1145\/revisions\/1221"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/media?parent=1145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/categories?post=1145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/nnet\/wp-json\/wp\/v2\/tags?post=1145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}