{"id":36355,"date":"2024-11-01T09:47:46","date_gmt":"2024-11-01T09:47:46","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36355"},"modified":"2024-11-01T11:00:15","modified_gmt":"2024-11-01T11:00:15","slug":"deep-learning-with-gan-using-pytorch-literary-club-for-notorious-offenders","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36355\/","title":{"rendered":"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders"},"content":{"rendered":"<p><body><\/p>\n<p>Generative Adversarial Networks (GANs) are considered one of the most innovative advancements in deep learning. GAN consists of two neural networks: a Generator and a Discriminator. The Generator creates data, while the Discriminator determines whether the data is real or fake. This competitive structure helps to enhance each other&#8217;s performance. In this course, we will build a GAN using PyTorch and perform data generation around the theme of &#8216;The Literary Club for Bad Criminals&#8217; in an interesting way.<\/p>\n<h2>1. Basic Structure and Principles of GAN<\/h2>\n<p>The operation of GAN works as follows:<\/p>\n<ul>\n<li><strong>Generator<\/strong>: Takes random noise (z) as input and generates realistic data.<\/li>\n<li><strong>Discriminator<\/strong>: Determines whether the input data is real or generated by the Generator.<\/li>\n<li>The Generator tries to deceive the Discriminator, while the Discriminator tries to differentiate between the two. As this competition continues, both networks progress further.<\/li>\n<\/ul>\n<h2>2. Preparing Required Libraries and Datasets<\/h2>\n<p>Install PyTorch and other necessary libraries. Then you will need to choose the dataset to prepare the data for this process. In this example, we will use the MNIST dataset to generate images of numbers. The MNIST dataset is composed of images of handwritten digits.<\/p>\n<h3>2.1 Setting Up the Environment<\/h3>\n<pre><code>pip install torch torchvision<\/code><\/pre>\n<h3>2.2 Loading the Dataset<\/h3>\n<pre><code>import torch\nfrom torchvision import datasets, transforms\nfrom torch.utils.data import DataLoader\n\n# Load MNIST Dataset\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize((0.5,), (0.5,))\n])\n\nmnist_dataset = datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\ndata_loader = DataLoader(dataset=mnist_dataset, batch_size=64, shuffle=True)<\/code><\/pre>\n<h2>3. Constructing the GAN Model<\/h2>\n<p>We define the Generator and Discriminator models to implement the Generative Adversarial Network.<\/p>\n<h3>3.1 Generator Model<\/h3>\n<pre><code>import torch.nn as nn\n\nclass Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.model = nn.Sequential(\n            nn.Linear(100, 256),\n            nn.ReLU(),\n            nn.Linear(256, 512),\n            nn.ReLU(),\n            nn.Linear(512, 1024),\n            nn.ReLU(),\n            nn.Linear(1024, 28 * 28),  # MNIST image size\n            nn.Tanh()  # Adjust the pixel value range of generated images to -1 ~ 1\n        )\n\n    def forward(self, z):\n        img = self.model(z)\n        img = img.view(img.size(0), 1, 28, 28)  # Transform into image shape\n        return img<\/code><\/pre>\n<h3>3.2 Discriminator Model<\/h3>\n<pre><code>class Discriminator(nn.Module):\n    def __init__(self):\n        super(Discriminator, self).__init__()\n        self.model = nn.Sequential(\n            nn.Linear(28 * 28, 512),\n            nn.LeakyReLU(0.2),\n            nn.Linear(512, 256),\n            nn.LeakyReLU(0.2),\n            nn.Linear(256, 1),\n            nn.Sigmoid()  # Output value between 0 and 1\n        )\n\n    def forward(self, img):\n        img_flat = img.view(img.size(0), -1)  # Flatten the image\n        validity = self.model(img_flat)\n        return validity<\/code><\/pre>\n<h2>4. Training Process of GAN<\/h2>\n<p>The training process of GAN is carried out as follows:<\/p>\n<ul>\n<li>Provide real images and generated images to the Discriminator to calculate its loss.<\/li>\n<li>Update the Generator to make the generated images closer to the real ones.<\/li>\n<li>Repeat this process to help each network improve.<\/li>\n<\/ul>\n<h3>4.1 Defining Loss Function and Optimizers<\/h3>\n<pre><code>import torch.optim as optim\n\n# Create instances of Generator and Discriminator\ngenerator = Generator()\ndiscriminator = Discriminator()\n\n# Set loss function and optimizer\nadversarial_loss = nn.BCELoss()\noptimizer_G = optim.Adam(generator.parameters(), lr=0.0002, betas=(0.5, 0.999))\noptimizer_D = optim.Adam(discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999))<\/code><\/pre>\n<h3>4.2 Training Loop<\/h3>\n<pre><code>num_epochs = 200\nfor epoch in range(num_epochs):\n    for i, (imgs, _) in enumerate(data_loader):\n        # Label for real data: 1\n        real_imgs = imgs\n        valid = torch.ones(imgs.size(0), 1)  # Ground truth for real images\n        fake = torch.zeros(imgs.size(0), 1)  # Ground truth for fake images\n\n        # Train Discriminator\n        optimizer_D.zero_grad()\n        z = torch.randn(imgs.size(0), 100)  # Sample random noise\n        generated_imgs = generator(z)  # Generated images\n        real_loss = adversarial_loss(discriminator(real_imgs), valid)\n        fake_loss = adversarial_loss(discriminator(generated_imgs.detach()), fake)\n        d_loss = (real_loss + fake_loss) \/ 2\n        d_loss.backward()\n        optimizer_D.step()\n\n        # Train Generator\n        optimizer_G.zero_grad()\n        g_loss = adversarial_loss(discriminator(generated_imgs), valid)\n        g_loss.backward()\n        optimizer_G.step()\n\n    print(f'Epoch {epoch}\/{num_epochs} | D Loss: {d_loss.item()} | G Loss: {g_loss.item()}')<\/code><\/pre>\n<h2>5. Visualizing Results<\/h2>\n<p>After training is completed, we visualize the generated images to check the results.<\/p>\n<pre><code>import matplotlib.pyplot as plt\n\n# Visualize generated images\ndef show_generated_images(generator, num_images=16):\n    z = torch.randn(num_images, 100)  # Sample random noise\n    generated_images = generator(z)\n    generated_images = generated_images.detach().numpy()\n\n    fig, axs = plt.subplots(4, 4, figsize=(10, 10))\n    for i in range(4):\n        for j in range(4):\n            axs[i, j].imshow(generated_images[i * 4 + j, 0], cmap='gray')\n            axs[i, j].axis('off')\n    plt.show()\n\nshow_generated_images(generator)<\/code><\/pre>\n<p>In this way, you can build and train a GAN and verify the generated images. The potential applications of GANs are vast, and they can facilitate creative tasks. Now, you can take a step closer to the world of GANs!<\/p>\n<h2>6. Conclusion<\/h2>\n<p>Generative Adversarial Networks are a very interesting area of deep learning, actively used in many research and development projects. In this course, we explored the basic principles and structures of GAN using PyTorch and covered the process of building and training deep learning models. I hope you gain a deep understanding and interest in GAN through this course and that it greatly helps you in your future deep learning journey.<\/p>\n<p><script>\n        \/\/ Google Analytics additional code\n    <\/script><br \/>\n<\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generative Adversarial Networks (GANs) are considered one of the most innovative advancements in deep learning. GAN consists of two neural networks: a Generator and a Discriminator. The Generator creates data, while the Discriminator determines whether the data is real or fake. This competitive structure helps to enhance each other&#8217;s performance. In this course, we will &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36355\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders&#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":[113],"tags":[],"class_list":["post-36355","post","type-post","status-publish","format-standard","hentry","category-gan-deep-learning-course"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders - \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\/36355\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Generative Adversarial Networks (GANs) are considered one of the most innovative advancements in deep learning. GAN consists of two neural networks: a Generator and a Discriminator. The Generator creates data, while the Discriminator determines whether the data is real or fake. This competitive structure helps to enhance each other&#8217;s performance. In this course, we will &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36355\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:47:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:00:15+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\/36355\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36355\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders\",\"datePublished\":\"2024-11-01T09:47:46+00:00\",\"dateModified\":\"2024-11-01T11:00:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36355\/\"},\"wordCount\":410,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"GAN deep learning course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36355\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36355\/\",\"name\":\"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:47:46+00:00\",\"dateModified\":\"2024-11-01T11:00:15+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36355\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36355\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36355\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders\"}]},{\"@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 with GAN using PyTorch, Literary Club for Notorious Offenders - \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\/36355\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Generative Adversarial Networks (GANs) are considered one of the most innovative advancements in deep learning. GAN consists of two neural networks: a Generator and a Discriminator. The Generator creates data, while the Discriminator determines whether the data is real or fake. This competitive structure helps to enhance each other&#8217;s performance. In this course, we will &hellip; \ub354 \ubcf4\uae30 \"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders\"","og_url":"https:\/\/atmokpo.com\/w\/36355\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:47:46+00:00","article_modified_time":"2024-11-01T11:00:15+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\/36355\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36355\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders","datePublished":"2024-11-01T09:47:46+00:00","dateModified":"2024-11-01T11:00:15+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36355\/"},"wordCount":410,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["GAN deep learning course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36355\/","url":"https:\/\/atmokpo.com\/w\/36355\/","name":"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:47:46+00:00","dateModified":"2024-11-01T11:00:15+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36355\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36355\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36355\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning with GAN using PyTorch, Literary Club for Notorious Offenders"}]},{"@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\/36355","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=36355"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36355\/revisions"}],"predecessor-version":[{"id":36356,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36355\/revisions\/36356"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}