{"id":36513,"date":"2024-11-01T09:49:07","date_gmt":"2024-11-01T09:49:07","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36513"},"modified":"2024-11-01T11:52:53","modified_gmt":"2024-11-01T11:52:53","slug":"deep-learning-pytorch-course-what-is-an-autoencoder","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36513\/","title":{"rendered":"Deep Learning PyTorch Course, What is an Autoencoder"},"content":{"rendered":"<p><body><\/p>\n<p>The autoencoder, a field of deep learning, is a representative technique of unsupervised learning and a model that compresses and reconstructs input data. In this course, we will start with the concept of autoencoders and take a closer look at how to implement them in PyTorch.<\/p>\n<h2>1. Concept of Autoencoders<\/h2>\n<p>An Autoencoder is a neural network-based unsupervised learning algorithm. It comprises an encoder and a decoder, where the encoder compresses the input data into a latent space and the decoder reconstructs this latent space data back into the original data format.<\/p>\n<h3>1.1 Encoder and Decoder<\/h3>\n<p>The autoencoder consists of the following two main components:<\/p>\n<ul>\n<li><strong>Encoder:<\/strong> Converts the input data into latent variables. In this process, the dimensionality of the input data is reduced while preserving most of the information.<\/li>\n<li><strong>Decoder:<\/strong> Reconstructs the original data from the latent variables created by the encoder. The reconstructed data should be most similar to the input data.<\/li>\n<\/ul>\n<h3>1.2 Purpose of Autoencoders<\/h3>\n<p>The primary aim of autoencoders is to automatically learn the essential characteristics of input data and compress and reconstruct the data in a way that minimizes information loss. This allows various applications such as data denoising, dimensionality reduction, and generative modeling.<\/p>\n<h2>2. Structure of Autoencoders<\/h2>\n<p>The structure of an autoencoder can generally be divided into three layers:<\/p>\n<ul>\n<li><strong>Input Layer:<\/strong> The layer where the input data enters.<\/li>\n<li><strong>Latent Space:<\/strong> The intermediate layer where data is encoded, usually with a lower dimension than the input layer.<\/li>\n<li><strong>Output Layer:<\/strong> The layer that outputs the reconstructed data.<\/li>\n<\/ul>\n<h2>3. Implementing Autoencoders in PyTorch<\/h2>\n<p>Now that we understand the basic concepts and structure of autoencoders, let\u2019s implement them using PyTorch. In this example, we will use a simple MNIST dataset to encode and decode digit images.<\/p>\n<h3>3.1 Installing PyTorch<\/h3>\n<p>You can install PyTorch using the following command:<\/p>\n<pre><code>pip install torch torchvision<\/code><\/pre>\n<h3>3.2 Loading the Dataset<\/h3>\n<p>We will use the <code>datasets<\/code> module from the torchvision library to load the MNIST dataset.<\/p>\n<div class=\"code-container\">\n<pre><code>import torch\nfrom torchvision import datasets, transforms\n\n# Load and transform MNIST dataset\ntransform = transforms.Compose([transforms.ToTensor(), transforms.Lambda(lambda x: x.view(-1))])\nmnist_data = datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\nmnist_loader = torch.utils.data.DataLoader(mnist_data, batch_size=64, shuffle=True)<\/code><\/pre>\n<\/div>\n<h3>3.3 Defining the Autoencoder Class<\/h3>\n<p>Now, let&#8217;s create a simple autoencoder class that defines the encoder and decoder.<\/p>\n<div class=\"code-container\">\n<pre><code>import torch.nn as nn\n\nclass Autoencoder(nn.Module):\n    def __init__(self):\n        super(Autoencoder, self).__init__()\n        # Encoder\n        self.encoder = nn.Sequential(\n            nn.Linear(28 * 28, 128),\n            nn.ReLU(True),\n            nn.Linear(128, 64),\n            nn.ReLU(True))\n        \n        # Decoder\n        self.decoder = nn.Sequential(\n            nn.Linear(64, 128),\n            nn.ReLU(True),\n            nn.Linear(128, 28 * 28),\n            nn.Sigmoid())\n    \n    def forward(self, x):\n        x = self.encoder(x)\n        x = self.decoder(x)\n        return x<\/code><\/pre>\n<\/div>\n<h3>3.4 Training the Model<\/h3>\n<p>Having prepared the model, we will proceed to training. We will use Mean Squared Error (MSE) as the loss function and Adam as the optimizer.<\/p>\n<div class=\"code-container\">\n<pre><code>import torch.optim as optim\n\n# Initialize model, loss function, and optimizer\nmodel = Autoencoder()\ncriterion = nn.MSELoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Train the model\nnum_epochs = 10\nfor epoch in range(num_epochs):\n    for data in mnist_loader:\n        img, _ = data\n        # Initialize activated parameters and loss\n        optimizer.zero_grad()\n        # Forward pass of the model\n        output = model(img)\n        loss = criterion(output, img)\n        # Backward pass and optimization\n        loss.backward()\n        optimizer.step()\n\n    print(f'Epoch [{epoch + 1}\/{num_epochs}], Loss: {loss.item():.4f}')<\/code><\/pre>\n<\/div>\n<h3>3.5 Visualizing the Results<\/h3>\n<p>Once training is completed, you can visualize the original images and the reconstructed images to check the results.<\/p>\n<div class=\"code-container\">\n<pre><code>import matplotlib.pyplot as plt\n\n# Visualizing the network's output\nwith torch.no_grad():\n    for data in mnist_loader:\n        img, _ = data\n        output = model(img)\n        break\n\n# Comparing original images and reconstructed images\nplt.figure(figsize=(9, 2))\nfor i in range(8):\n    # Original image\n    plt.subplot(2, 8, i + 1)\n    plt.imshow(img[i].view(28, 28), cmap='gray')\n    plt.axis('off')\n    \n    # Reconstructed image\n    plt.subplot(2, 8, i + 9)\n    plt.imshow(output[i].view(28, 28), cmap='gray')\n    plt.axis('off')\nplt.show()<\/code><\/pre>\n<\/div>\n<h2>4. Use Cases of Autoencoders<\/h2>\n<p>Autoencoders can be applied in various fields. Here are some use cases:<\/p>\n<ul>\n<li><strong>Dimensionality Reduction:<\/strong> Useful for reducing unnecessary dimensions of data while retaining important information.<\/li>\n<li><strong>Denoising:<\/strong> Can be used to remove noise from input data.<\/li>\n<li><strong>Anomaly Detection:<\/strong> Learns the patterns of normal data and can identify abnormal data with respect to these patterns.<\/li>\n<li><strong>Data Generation:<\/strong> Can also be used to generate new data.<\/li>\n<\/ul>\n<h2>5. Conclusion<\/h2>\n<p>Through this course, we have learned the basic concepts, structure, and implementation methods of autoencoders in PyTorch. Autoencoders are powerful tools that can be effectively applied to various problems. In the future, we hope you utilize autoencoders to conduct various experiments.<\/p>\n<h2>6. References<\/h2>\n<p>Below are materials and references used in this course:<\/p>\n<ul>\n<li>Goodfellow, I., Bengio, Y., &#038; Courville, A. (2016). Deep Learning. MIT Press.<\/li>\n<li>Official PyTorch documentation: <a href=\"https:\/\/pytorch.org\/docs\/stable\/index.html\">https:\/\/pytorch.org\/docs\/stable\/index.html<\/a><\/li>\n<li>MNIST dataset: <a href=\"http:\/\/yann.lecun.com\/exdb\/mnist\/\">http:\/\/yann.lecun.com\/exdb\/mnist\/<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The autoencoder, a field of deep learning, is a representative technique of unsupervised learning and a model that compresses and reconstructs input data. In this course, we will start with the concept of autoencoders and take a closer look at how to implement them in PyTorch. 1. Concept of Autoencoders An Autoencoder is a neural &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36513\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, What is an Autoencoder&#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-36513","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, What is an Autoencoder - \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\/36513\/\" \/>\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, What is an Autoencoder - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"The autoencoder, a field of deep learning, is a representative technique of unsupervised learning and a model that compresses and reconstructs input data. In this course, we will start with the concept of autoencoders and take a closer look at how to implement them in PyTorch. 1. Concept of Autoencoders An Autoencoder is a neural &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, What is an Autoencoder&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36513\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:49:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:52:53+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\/36513\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36513\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, What is an Autoencoder\",\"datePublished\":\"2024-11-01T09:49:07+00:00\",\"dateModified\":\"2024-11-01T11:52:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36513\/\"},\"wordCount\":532,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36513\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36513\/\",\"name\":\"Deep Learning PyTorch Course, What is an Autoencoder - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:49:07+00:00\",\"dateModified\":\"2024-11-01T11:52:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36513\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36513\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36513\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, What is an Autoencoder\"}]},{\"@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, What is an Autoencoder - \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\/36513\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, What is an Autoencoder - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"The autoencoder, a field of deep learning, is a representative technique of unsupervised learning and a model that compresses and reconstructs input data. In this course, we will start with the concept of autoencoders and take a closer look at how to implement them in PyTorch. 1. Concept of Autoencoders An Autoencoder is a neural &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, What is an Autoencoder\"","og_url":"https:\/\/atmokpo.com\/w\/36513\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:49:07+00:00","article_modified_time":"2024-11-01T11:52:53+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\/36513\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36513\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, What is an Autoencoder","datePublished":"2024-11-01T09:49:07+00:00","dateModified":"2024-11-01T11:52:53+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36513\/"},"wordCount":532,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36513\/","url":"https:\/\/atmokpo.com\/w\/36513\/","name":"Deep Learning PyTorch Course, What is an Autoencoder - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:49:07+00:00","dateModified":"2024-11-01T11:52:53+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36513\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36513\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36513\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, What is an Autoencoder"}]},{"@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\/36513","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=36513"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36513\/revisions"}],"predecessor-version":[{"id":36514,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36513\/revisions\/36514"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}