{"id":36379,"date":"2024-11-01T09:48:00","date_gmt":"2024-11-01T09:48:00","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36379"},"modified":"2024-11-01T11:00:09","modified_gmt":"2024-11-01T11:00:09","slug":"deep-learning-with-gans-using-pytorch-deep-neural-networks","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36379\/","title":{"rendered":"Deep Learning with GANs using PyTorch, Deep Neural Networks"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Overview of GAN<\/h2>\n<p>\n        GAN (Generative Adversarial Networks) is a deep learning model proposed by Ian Goodfellow in 2014. GAN has the ability to generate new data by learning the distribution of a given dataset.<br \/>\n        The main components of GAN are two neural networks: the Generator and the Discriminator. The Generator creates fake data that resembles real data, while the Discriminator determines whether the generated data is real or fake.\n    <\/p>\n<h2>2. Structure of GAN<\/h2>\n<p>\n        GAN consists of the following structure:\n    <\/p>\n<ul>\n<li><strong>Generator (G)<\/strong>: Takes random noise as input and generates fake data from it.<\/li>\n<li><strong>Discriminator (D)<\/strong>: Functions to distinguish between real data and generated fake data.<\/li>\n<\/ul>\n<h3>2.1. Loss Function<\/h3>\n<p>\n        During the training process of GAN, both the Generator and the Discriminator learn competitively by optimizing their respective loss functions. The goal of the Discriminator is to accurately distinguish real data from fake data, while the goal of the Generator is to fool the Discriminator. This can be expressed mathematically as follows:\n    <\/p>\n<pre><code>\n    min_G max_D V(D, G) = E[log(D(x))] + E[log(1 - D(G(z)))]\n    <\/code><\/pre>\n<h2>3. Implementing GAN using PyTorch<\/h2>\n<p>\n        In this section, we will implement a simple GAN using PyTorch. We will create a GAN that generates digit images using the MNIST dataset as a simple example.\n    <\/p>\n<h3>3.1. Importing Libraries<\/h3>\n<pre><code>\n    import torch\n    import torch.nn as nn\n    import torch.optim as optim\n    from torchvision import datasets, transforms\n    from torch.utils.data import DataLoader\n    import matplotlib.pyplot as plt\n    <\/code><\/pre>\n<h3>3.2. Setting Hyperparameters<\/h3>\n<pre><code>\n    # Setting hyperparameters\n    latent_size = 64\n    batch_size = 128\n    learning_rate = 0.0002\n    num_epochs = 50\n    <\/code><\/pre>\n<h3>3.3. Loading the Dataset<\/h3>\n<pre><code>\n    # Loading MNIST dataset\n    transform = transforms.Compose([\n        transforms.ToTensor(),\n        transforms.Normalize((0.5,), (0.5,))\n    ])\n\n    mnist = datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\n    dataloader = DataLoader(mnist, batch_size=batch_size, shuffle=True)\n    <\/code><\/pre>\n<h3>3.4. Defining the Generator and Discriminator<\/h3>\n<pre><code>\n    class Generator(nn.Module):\n        def __init__(self):\n            super(Generator, self).__init__()\n            self.model = nn.Sequential(\n                nn.Linear(latent_size, 128),\n                nn.ReLU(),\n                nn.Linear(128, 256),\n                nn.ReLU(),\n                nn.Linear(256, 512),\n                nn.ReLU(),\n                nn.Linear(512, 784),\n                nn.Tanh()\n            )\n\n        def forward(self, z):\n            return self.model(z).reshape(-1, 1, 28, 28)\n\n    class Discriminator(nn.Module):\n        def __init__(self):\n            super(Discriminator, self).__init__()\n            self.model = nn.Sequential(\n                nn.Flatten(),\n                nn.Linear(784, 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, img):\n            return self.model(img)\n    <\/code><\/pre>\n<h3>3.5. Setting up the Model, Loss Function, and Optimization Techniques<\/h3>\n<pre><code>\n    generator = Generator()\n    discriminator = Discriminator()\n\n    criterion = nn.BCELoss()\n    optimizer_G = optim.Adam(generator.parameters(), lr=learning_rate)\n    optimizer_D = optim.Adam(discriminator.parameters(), lr=learning_rate)\n    <\/code><\/pre>\n<h3>3.6. GAN Training Loop<\/h3>\n<pre><code>\n    for epoch in range(num_epochs):\n        for i, (imgs, _) in enumerate(dataloader):\n            # Define real images and labels.\n            real_imgs = imgs\n            real_labels = torch.ones(batch_size, 1)\n            fake_labels = torch.zeros(batch_size, 1)\n\n            # Training the Discriminator\n            optimizer_D.zero_grad()\n            outputs = discriminator(real_imgs)\n            d_loss_real = criterion(outputs, real_labels)\n            d_loss_real.backward()\n\n            z = torch.randn(batch_size, latent_size)\n            fake_imgs = generator(z)\n            outputs = discriminator(fake_imgs.detach())\n            d_loss_fake = criterion(outputs, fake_labels)\n            d_loss_fake.backward()\n            optimizer_D.step()\n\n            # Training the Generator\n            optimizer_G.zero_grad()\n            outputs = discriminator(fake_imgs)\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    <\/code><\/pre>\n<h3>3.7. Visualization of Results<\/h3>\n<p>\n        After training, we will visualize the generated images to evaluate the performance of the GAN.\n    <\/p>\n<pre><code>\n    z = torch.randn(64, latent_size)\n    generated_images = generator(z).detach().numpy()\n    generated_images = (generated_images + 1) \/ 2  # Normalize to 0-1\n\n    fig, axs = plt.subplots(8, 8, figsize=(10,10))\n    for i in range(8):\n        for j in range(8):\n            axs[i,j].imshow(generated_images[i*8 + j][0], cmap='gray')\n            axs[i,j].axis('off')\n    plt.show()\n    <\/code><\/pre>\n<h2>4. Conclusion<\/h2>\n<p>\n        In this article, we explored the basic concepts of GAN and how to implement a simple GAN using PyTorch. GAN demonstrates excellent performance in the field of data generation and is utilized across various application domains.\n    <\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Overview of GAN GAN (Generative Adversarial Networks) is a deep learning model proposed by Ian Goodfellow in 2014. GAN has the ability to generate new data by learning the distribution of a given dataset. The main components of GAN are two neural networks: the Generator and the Discriminator. The Generator creates fake data that &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36379\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning with GANs using PyTorch, Deep Neural Networks&#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-36379","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 GANs using PyTorch, Deep Neural Networks - \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\/36379\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deep Learning with GANs using PyTorch, Deep Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Overview of GAN GAN (Generative Adversarial Networks) is a deep learning model proposed by Ian Goodfellow in 2014. GAN has the ability to generate new data by learning the distribution of a given dataset. The main components of GAN are two neural networks: the Generator and the Discriminator. The Generator creates fake data that &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning with GANs using PyTorch, Deep Neural Networks&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36379\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:48:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:00:09+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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/36379\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36379\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning with GANs using PyTorch, Deep Neural Networks\",\"datePublished\":\"2024-11-01T09:48:00+00:00\",\"dateModified\":\"2024-11-01T11:00:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36379\/\"},\"wordCount\":276,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"GAN deep learning course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36379\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36379\/\",\"name\":\"Deep Learning with GANs using PyTorch, Deep Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:48:00+00:00\",\"dateModified\":\"2024-11-01T11:00:09+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36379\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36379\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36379\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning with GANs using PyTorch, Deep Neural Networks\"}]},{\"@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 GANs using PyTorch, Deep Neural Networks - \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\/36379\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning with GANs using PyTorch, Deep Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Overview of GAN GAN (Generative Adversarial Networks) is a deep learning model proposed by Ian Goodfellow in 2014. GAN has the ability to generate new data by learning the distribution of a given dataset. The main components of GAN are two neural networks: the Generator and the Discriminator. The Generator creates fake data that &hellip; \ub354 \ubcf4\uae30 \"Deep Learning with GANs using PyTorch, Deep Neural Networks\"","og_url":"https:\/\/atmokpo.com\/w\/36379\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:48:00+00:00","article_modified_time":"2024-11-01T11:00:09+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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/36379\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36379\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning with GANs using PyTorch, Deep Neural Networks","datePublished":"2024-11-01T09:48:00+00:00","dateModified":"2024-11-01T11:00:09+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36379\/"},"wordCount":276,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["GAN deep learning course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36379\/","url":"https:\/\/atmokpo.com\/w\/36379\/","name":"Deep Learning with GANs using PyTorch, Deep Neural Networks - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:48:00+00:00","dateModified":"2024-11-01T11:00:09+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36379\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36379\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36379\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning with GANs using PyTorch, Deep Neural Networks"}]},{"@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\/36379","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=36379"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36379\/revisions"}],"predecessor-version":[{"id":36380,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36379\/revisions\/36380"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}