{"id":36449,"date":"2024-11-01T09:48:35","date_gmt":"2024-11-01T09:48:35","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36449"},"modified":"2024-11-01T11:53:08","modified_gmt":"2024-11-01T11:53:08","slug":"deep-learning-pytorch-course-lenet-5","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36449\/","title":{"rendered":"Deep Learning PyTorch Course, LeNet-5"},"content":{"rendered":"<p><body><\/p>\n<p>Deep learning has gained tremendous popularity in various fields of data science in recent years. It has become a very useful tool for solving problems in diverse domains. In this course, we will take a closer look at one of the well-known deep learning architectures, LeNet-5.<\/p>\n<h2>What is LeNet-5?<\/h2>\n<p>LeNet-5 is a convolutional neural network (CNN) architecture developed by researchers including Yann LeCun in 1998. It is a useful model for recognizing images, primarily used for handwritten digit recognition. This model follows the basic structure of CNN and consists of several layers. LeNet-5 is composed of the following layers:<\/p>\n<ul>\n<li><strong>Input Layer:<\/strong> Grayscale image of 32&#215;32 pixels.<\/li>\n<li><strong>Convolution Layer (C1):<\/strong> Generates a feature map of size 28&#215;28 using 6 filters (5&#215;5).<\/li>\n<li><strong>Pooling Layer (S2):<\/strong> Generates 6 feature maps of size 14&#215;14 through average pooling.<\/li>\n<li><strong>Convolution Layer (C3):<\/strong> Uses 16 filters to generate a feature map of size 10&#215;10.<\/li>\n<li><strong>Pooling Layer (S4):<\/strong> Generates 16 feature maps of size 5&#215;5 through average pooling.<\/li>\n<li><strong>Convolution Layer (C5):<\/strong> Generates the final feature map using 120 filters (5&#215;5).<\/li>\n<li><strong>Fully Connected Layer (F6):<\/strong> Outputs the final result with 84 neurons.<\/li>\n<li><strong>Output Layer:<\/strong> Classifies into 10 classes (0-9).<\/li>\n<\/ul>\n<h2>The Importance of LeNet-5<\/h2>\n<p>LeNet-5 is one of the foundational architectures of CNN, forming the basis for many deep networks. This model has brought many innovations to the field of image recognition, and various modified models still exist today. Thanks to the simplicity and efficiency of LeNet-5, it performs well on many datasets.<\/p>\n<h2>Implementing LeNet-5<\/h2>\n<p>Now, let&#8217;s implement LeNet-5 using PyTorch. PyTorch is a user-friendly deep learning framework widely used in various research and industry applications. Additionally, PyTorch has the advantage of using dynamic computation graphs.<\/p>\n<h3>Environment Setup<\/h3>\n<p>First, we need to install the necessary libraries and set up the environment. Use the following code to install PyTorch and torchvision:<\/p>\n<pre><code>pip install torch torchvision<\/code><\/pre>\n<h3>Implementing LeNet-5 Model<\/h3>\n<p>Now let&#8217;s implement the structure of LeNet-5:<\/p>\n<pre><code>import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nclass LeNet5(nn.Module):\n    def __init__(self):\n        super(LeNet5, self).__init__()\n        self.conv1 = nn.Conv2d(1, 6, kernel_size=5)\n        self.conv2 = nn.Conv2d(6, 16, kernel_size=5)\n        self.fc1 = nn.Linear(16 * 5 * 5, 120)\n        self.fc2 = nn.Linear(120, 84)\n        self.fc3 = nn.Linear(84, 10)\n\n    def forward(self, x):\n        x = F.relu(self.conv1(x))\n        x = F.avg_pool2d(x, kernel_size=2, stride=2)\n        x = F.relu(self.conv2(x))\n        x = F.avg_pool2d(x, kernel_size=2, stride=2)\n        x = x.view(-1, 16 * 5 * 5)\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = self.fc3(x)\n        return x\n<\/code><\/pre>\n<h3>Preparing Dataset for Model Training<\/h3>\n<p>LeNet-5 will be trained using the MNIST dataset. You can easily download and load the data using torchvision. Use the following code to prepare the MNIST dataset:<\/p>\n<pre><code>from torchvision import datasets, transforms\n\ntransform = transforms.Compose([\n    transforms.Grayscale(num_output_channels=1),\n    transforms.Resize((32, 32)),\n    transforms.ToTensor(),\n    transforms.Normalize((0.5,), (0.5,))\n])\n\ntrain_dataset = datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\ntest_dataset = datasets.MNIST(root='.\/data', train=False, download=True, transform=transform)\n\ntrain_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)\ntest_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=64, shuffle=False)\n<\/code><\/pre>\n<h3>Model Training<\/h3>\n<p>To train the model, we need to set up a loss function and an optimization algorithm. Here, we will use Cross Entropy Loss and the Adam optimizer:<\/p>\n<pre><code>device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel = LeNet5().to(device)\ncriterion = nn.CrossEntropyLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=0.001)\n\nnum_epochs = 5\nfor epoch in range(num_epochs):\n    for i, (images, labels) in enumerate(train_loader):\n        images = images.to(device)\n        labels = labels.to(device)\n\n        # Forward pass\n        outputs = model(images)\n        loss = criterion(outputs, labels)\n\n        # Backward and optimize\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n\n        if (i + 1) % 100 == 0:\n            print(f'Epoch [{epoch + 1}\/{num_epochs}], Step [{i + 1}\/{len(train_loader)}], Loss: {loss.item():.4f}')\n<\/code><\/pre>\n<h3>Model Evaluation<\/h3>\n<p>After training is completed, you can evaluate the model&#8217;s performance. We will check the accuracy using the test dataset:<\/p>\n<pre><code>model.eval()\nwith torch.no_grad():\n    correct = 0\n    total = 0\n    for images, labels in test_loader:\n        images = images.to(device)\n        labels = labels.to(device)\n        outputs = model(images)\n        _, predicted = torch.max(outputs.data, 1)\n        total += labels.size(0)\n        correct += (predicted == labels).sum().item()\n\n    print(f'Accuracy of the model on the test images: {100 * correct \/ total:.2f}%')\n<\/code><\/pre>\n<h3>Conclusion<\/h3>\n<p>In this course, we examined the process of implementing and training the LeNet-5 architecture using PyTorch. LeNet-5 is a good example for understanding and practicing the fundamentals of CNN. Based on this model, more complex network architectures or various applications can be developed. As the next step, we recommend exploring deeper network structures or datasets.<\/p>\n<h3>References<\/h3>\n<ul>\n<li><a href=\"http:\/\/yann.lecun.com\/exdb\/publis\/pdf\/lecun-98.pdf\">LeNet-5 Paper<\/a><\/li>\n<li><a href=\"https:\/\/pytorch.org\/\">Official PyTorch Website<\/a><\/li>\n<li><a href=\"http:\/\/yann.lecun.com\/exdb\/mnist\/\">MNIST Dataset<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Deep learning has gained tremendous popularity in various fields of data science in recent years. It has become a very useful tool for solving problems in diverse domains. In this course, we will take a closer look at one of the well-known deep learning architectures, LeNet-5. What is LeNet-5? LeNet-5 is a convolutional neural network &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36449\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, LeNet-5&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[149],"tags":[],"class_list":["post-36449","post","type-post","status-publish","format-standard","hentry","category-pytorch-study"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Deep Learning PyTorch Course, LeNet-5 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/36449\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deep Learning PyTorch Course, LeNet-5 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Deep learning has gained tremendous popularity in various fields of data science in recent years. It has become a very useful tool for solving problems in diverse domains. In this course, we will take a closer look at one of the well-known deep learning architectures, LeNet-5. What is LeNet-5? LeNet-5 is a convolutional neural network &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, LeNet-5&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36449\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:48:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:53:08+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/36449\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36449\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, LeNet-5\",\"datePublished\":\"2024-11-01T09:48:35+00:00\",\"dateModified\":\"2024-11-01T11:53:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36449\/\"},\"wordCount\":456,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36449\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36449\/\",\"name\":\"Deep Learning PyTorch Course, LeNet-5 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:48:35+00:00\",\"dateModified\":\"2024-11-01T11:53:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36449\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36449\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36449\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, LeNet-5\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Deep Learning PyTorch Course, LeNet-5 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/36449\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, LeNet-5 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Deep learning has gained tremendous popularity in various fields of data science in recent years. It has become a very useful tool for solving problems in diverse domains. In this course, we will take a closer look at one of the well-known deep learning architectures, LeNet-5. What is LeNet-5? LeNet-5 is a convolutional neural network &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, LeNet-5\"","og_url":"https:\/\/atmokpo.com\/w\/36449\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:48:35+00:00","article_modified_time":"2024-11-01T11:53:08+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/36449\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36449\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, LeNet-5","datePublished":"2024-11-01T09:48:35+00:00","dateModified":"2024-11-01T11:53:08+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36449\/"},"wordCount":456,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36449\/","url":"https:\/\/atmokpo.com\/w\/36449\/","name":"Deep Learning PyTorch Course, LeNet-5 - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:48:35+00:00","dateModified":"2024-11-01T11:53:08+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36449\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36449\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36449\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, LeNet-5"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=36449"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36449\/revisions"}],"predecessor-version":[{"id":36450,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36449\/revisions\/36450"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}