{"id":36415,"date":"2024-11-01T09:48:18","date_gmt":"2024-11-01T09:48:18","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36415"},"modified":"2024-11-01T11:00:00","modified_gmt":"2024-11-01T11:00:00","slug":"deep-learning-with-gan-using-pytorch-training-process","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36415\/","title":{"rendered":"Deep Learning with GAN Using PyTorch, Training Process"},"content":{"rendered":"<p><body><\/p>\n<p>Generative Adversarial Network (GAN) is a neural network architecture introduced by Ian Goodfellow and colleagues in 2014, consisting of two competing neural networks: the Generator and the Discriminator. GANs are primarily used in fields such as image generation, transformation, and reconstruction, and are particularly popular for creating high-resolution photographs and artworks. In this article, we will take a detailed look at the overall structure and training process of GANs using PyTorch.<\/p>\n<h2>1. Structure of GAN<\/h2>\n<p>GAN consists of two main components:<\/p>\n<ul>\n<li><strong>Generator (G)<\/strong>: A network that takes a random noise vector as input and transforms it into a fake sample that resembles real data.<\/li>\n<li><strong>Discriminator (D)<\/strong>: A network that determines whether the input sample is real data or fake data created by the generator. The discriminator must be able to distinguish real data from fake data generated by the generator as effectively as possible.<\/li>\n<\/ul>\n<p>These two networks are structured to compete with each other to perform better than the opponent. The generator is gradually improved to generate more plausible data, while the discriminator is trained to distinguish increasingly sophisticated data.<\/p>\n<h2>2. Training Process of GAN<\/h2>\n<p>The training process of GAN proceeds through the following steps:<\/p>\n<ol>\n<li>A random noise vector is generated and input to the generator.<\/li>\n<li>The generator transforms the noise vector into a fake sample.<\/li>\n<li>The discriminator receives both the real data and the generated fake data as input.<\/li>\n<li>The discriminator predicts whether each sample is real data or fake data.<\/li>\n<li>The generator is updated via a loss function to make the discriminator classify fake samples as real data. Conversely, the discriminator is updated to better distinguish between real and fake data.<\/li>\n<\/ol>\n<h2>3. Implementing GAN Using PyTorch<\/h2>\n<p>Now let&#8217;s write the code to implement GAN using PyTorch. We will create a GAN to generate handwritten digits using the MNIST dataset.<\/p>\n<h3>3.1 Install Required Libraries<\/h3>\n<pre><code>pip install torch torchvision matplotlib<\/code><\/pre>\n<h3>3.2 Prepare the Dataset<\/h3>\n<p>Let&#8217;s load the MNIST dataset. In PyTorch, data can be easily downloaded through the torchvision library.<\/p>\n<pre><code>import torch\nfrom torchvision import datasets, transforms\n\n# Data transformations\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize((0.5,), (0.5,))\n])\n\n# Download and load the dataset\ntrain_dataset = datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\ntrain_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)\n<\/code><\/pre>\n<h3>3.3 Implementing the Generator and Discriminator<\/h3>\n<p>Now let&#8217;s define the two core components of GAN: the generator and the discriminator. We will use a simple fully connected neural network for the generator and a CNN to process the images for the discriminator.<\/p>\n<pre><code>import torch.nn as nn\n\n# Generator model\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),\n            nn.Tanh()\n        )\n    \n    def forward(self, z):\n        return self.model(z).view(-1, 1, 28, 28)\n\n# Discriminator model\nclass Discriminator(nn.Module):\n    def __init__(self):\n        super(Discriminator, self).__init__()\n        self.model = nn.Sequential(\n            nn.Flatten(),\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()\n        )\n    \n    def forward(self, x):\n        return self.model(x)\n<\/code><\/pre>\n<h3>3.4 Setting Up Loss Function and Optimization Algorithm<\/h3>\n<p>To train GAN, we will define the loss function and optimization algorithm. Typically, the generator and discriminator use different loss functions. We will use a simple binary cross-entropy loss.<\/p>\n<pre><code>criterion = nn.BCELoss()\noptimizer_G = torch.optim.Adam(Generator.parameters(), lr=0.0002, betas=(0.5, 0.999))\noptimizer_D = torch.optim.Adam(Discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999))\n<\/code><\/pre>\n<h3>3.5 GAN Training Loop<\/h3>\n<p>Now let&#8217;s implement the loop for training GAN. Here, we alternate training the generator and discriminator for a specified number of epochs.<\/p>\n<pre><code>def train_gan(generator, discriminator, train_loader, num_epochs=100):\n    for epoch in range(num_epochs):\n        for i, (real_images, _) in enumerate(train_loader):\n            batch_size = real_images.size(0)\n\n            # Define real and fake labels\n            real_labels = torch.ones(batch_size, 1)\n            fake_labels = torch.zeros(batch_size, 1)\n\n            # Train the discriminator\n            discriminator.zero_grad()\n            outputs = discriminator(real_images)\n            d_loss_real = criterion(outputs, real_labels)\n            d_loss_real.backward()\n\n            z = torch.randn(batch_size, 100)\n            fake_images = generator(z)\n            outputs = discriminator(fake_images.detach())\n            d_loss_fake = criterion(outputs, fake_labels)\n            d_loss_fake.backward()\n\n            optimizer_D.step()\n\n            # Train the generator\n            generator.zero_grad()\n            outputs = discriminator(fake_images)\n            g_loss = criterion(outputs, real_labels)\n            g_loss.backward()\n            optimizer_G.step()\n\n        print(f'Epoch [{epoch+1}\/{num_epochs}], d_loss: {d_loss_real.item() + d_loss_fake.item()}, g_loss: {g_loss.item()}')\n\n# Start GAN training\ngenerator = Generator()\ndiscriminator = Discriminator()\ntrain_gan(generator, discriminator, train_loader)\n<\/code><\/pre>\n<h2>4. Visualizing Results<\/h2>\n<p>After training, we can visualize the generated images to check the results.<\/p>\n<pre><code>import matplotlib.pyplot as plt\n\ndef show_generated_images(generator, num_images=25):\n    z = torch.randn(num_images, 100)\n    generated_images = generator(z).detach().numpy()\n    \n    plt.figure(figsize=(10, 10))\n    for i in range(num_images):\n        plt.subplot(5, 5, i + 1)\n        plt.imshow(generated_images[i][0], cmap='gray')\n        plt.axis('off')\n    plt.show()\n\n# Show generated images\nshow_generated_images(generator)\n<\/code><\/pre>\n<h2>5. Conclusion<\/h2>\n<p>In this post, we explored the basic concepts of GANs and a simple implementation using PyTorch. GANs are powerful generative models that can be applied to various data generation problems. However, training GANs can be unstable and may require various techniques and hyperparameter tuning. Exploring more complex GAN architectures (e.g., DCGAN, WGAN) can yield interesting results.<\/p>\n<p>Now you are aware of the basic operation of GANs and how to implement them using PyTorch. Based on this knowledge, I encourage you to try out various examples!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generative Adversarial Network (GAN) is a neural network architecture introduced by Ian Goodfellow and colleagues in 2014, consisting of two competing neural networks: the Generator and the Discriminator. GANs are primarily used in fields such as image generation, transformation, and reconstruction, and are particularly popular for creating high-resolution photographs and artworks. In this article, we &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36415\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning with GAN Using PyTorch, Training Process&#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-36415","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, Training Process - \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\/36415\/\" \/>\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, Training Process - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Generative Adversarial Network (GAN) is a neural network architecture introduced by Ian Goodfellow and colleagues in 2014, consisting of two competing neural networks: the Generator and the Discriminator. GANs are primarily used in fields such as image generation, transformation, and reconstruction, and are particularly popular for creating high-resolution photographs and artworks. In this article, we &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning with GAN Using PyTorch, Training Process&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36415\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:48:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:00:00+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\/36415\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36415\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning with GAN Using PyTorch, Training Process\",\"datePublished\":\"2024-11-01T09:48:18+00:00\",\"dateModified\":\"2024-11-01T11:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36415\/\"},\"wordCount\":534,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"GAN deep learning course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36415\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36415\/\",\"name\":\"Deep Learning with GAN Using PyTorch, Training Process - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:48:18+00:00\",\"dateModified\":\"2024-11-01T11:00:00+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36415\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36415\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36415\/#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, Training Process\"}]},{\"@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, Training Process - \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\/36415\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning with GAN Using PyTorch, Training Process - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Generative Adversarial Network (GAN) is a neural network architecture introduced by Ian Goodfellow and colleagues in 2014, consisting of two competing neural networks: the Generator and the Discriminator. GANs are primarily used in fields such as image generation, transformation, and reconstruction, and are particularly popular for creating high-resolution photographs and artworks. In this article, we &hellip; \ub354 \ubcf4\uae30 \"Deep Learning with GAN Using PyTorch, Training Process\"","og_url":"https:\/\/atmokpo.com\/w\/36415\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:48:18+00:00","article_modified_time":"2024-11-01T11:00:00+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\/36415\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36415\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning with GAN Using PyTorch, Training Process","datePublished":"2024-11-01T09:48:18+00:00","dateModified":"2024-11-01T11:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36415\/"},"wordCount":534,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["GAN deep learning course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36415\/","url":"https:\/\/atmokpo.com\/w\/36415\/","name":"Deep Learning with GAN Using PyTorch, Training Process - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:48:18+00:00","dateModified":"2024-11-01T11:00:00+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36415\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36415\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36415\/#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, Training Process"}]},{"@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\/36415","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=36415"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36415\/revisions"}],"predecessor-version":[{"id":36416,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36415\/revisions\/36416"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}