{"id":36383,"date":"2024-11-01T09:48:02","date_gmt":"2024-11-01T09:48:02","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36383"},"modified":"2024-11-01T11:00:08","modified_gmt":"2024-11-01T11:00:08","slug":"deep-learning-with-gans-using-pytorch-world-model-structure","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36383\/","title":{"rendered":"Deep Learning with GANs Using PyTorch, World Model Structure"},"content":{"rendered":"<p><body><\/p>\n<p>Generative Adversarial Networks (GANs) are a deep learning framework in which two neural networks compete to improve the quality of generated data. The basic structure of GAN consists of a generator and a discriminator. The generator tries to create data that is similar to real data, while the discriminator distinguishes whether the generated data is real or fake. These two networks compete to enhance each other&#8217;s performance, thereby progressively generating more realistic data.<\/p>\n<h2>1. Structure of GAN<\/h2>\n<p>The structure of GAN is composed as follows:<\/p>\n<ul>\n<li><strong>Generator<\/strong>: Takes random noise as input, learns the distribution of real data, and generates new data.<\/li>\n<li><strong>Discriminator<\/strong>: Takes real and generated data as input and determines which one it is. This network solves a binary classification problem.<\/li>\n<\/ul>\n<h3>1.1 Training Process of GAN<\/h3>\n<p>GAN undergoes a two-step training process as follows:<\/p>\n<ol>\n<li>The generator generates data to deceive the discriminator, and the discriminator evaluates the generated data.<\/li>\n<li>The generator updates itself based on the discriminator&#8217;s feedback to generate better data, while the discriminator evaluates the quality of the generated data and updates itself.<\/li>\n<\/ol>\n<h2>2. PyTorch Implementation of GAN<\/h2>\n<p>In this section, we will implement a simple GAN using PyTorch.<\/p>\n<h3>2.1 Installing and Importing Required Libraries<\/h3>\n<pre><code>python\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision\nimport torchvision.transforms as transforms\nimport matplotlib.pyplot as plt\n<\/code><\/pre>\n<h3>2.2 Defining the Generator and Discriminator<\/h3>\n<p>We define the structure of the generator and discriminator in GAN.<\/p>\n<pre><code>python\nclass Generator(nn.Module):\n    def __init__(self):\n        super(Generator, self).__init__()\n        self.model = nn.Sequential(\n            nn.Dense(128, input_size=100),\n            nn.ReLU(),\n            nn.Dense(256),\n            nn.ReLU(),\n            nn.Dense(512),\n            nn.ReLU(),\n            nn.Dense(1, activation='tanh')  # Assume output is 1D data\n        )\n    \n    def forward(self, z):\n        return self.model(z)\n\nclass Discriminator(nn.Module):\n    def __init__(self):\n        super(Discriminator, self).__init__()\n        self.model = nn.Sequential(\n            nn.Dense(512, input_size=1),  # 1D data input\n            nn.LeakyReLU(0.2),\n            nn.Dense(256),\n            nn.LeakyReLU(0.2),\n            nn.Dense(1, activation='sigmoid')  # Binary output\n        )\n    \n    def forward(self, x):\n        return self.model(x)\n<\/code><\/pre>\n<h3>2.3 Training Process of GAN<\/h3>\n<p>Now, let&#8217;s look at the process of training the GAN.<\/p>\n<pre><code>python\ndef train_gan(num_epochs=10000, batch_size=64, learning_rate=0.0002):\n    transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])\n    dataset = torchvision.datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\n    dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True)\n\n    generator = Generator()\n    discriminator = Discriminator()\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\n    for epoch in range(num_epochs):\n        for real_data, _ in dataloader:\n            real_data = real_data.view(-1, 1).to(torch.float32)\n            batch_size = real_data.size(0)\n\n            # Train Discriminator\n            optimizer_d.zero_grad()\n            z = torch.randn(batch_size, 100)\n            fake_data = generator(z).detach()\n            real_label = torch.ones(batch_size, 1)\n            fake_label = torch.zeros(batch_size, 1)\n            output_real = discriminator(real_data)\n            output_fake = discriminator(fake_data)\n            loss_d = criterion(output_real, real_label) + criterion(output_fake, fake_label)\n            loss_d.backward()\n            optimizer_d.step()\n\n            # Train Generator\n            optimizer_g.zero_grad()\n            z = torch.randn(batch_size, 100)\n            fake_data = generator(z)\n            output = discriminator(fake_data)\n            loss_g = criterion(output, real_label)\n            loss_g.backward()\n            optimizer_g.step()\n\n        if epoch % 1000 == 0:\n            print(f'Epoch [{epoch}\/{num_epochs}], Loss D: {loss_d.item()}, Loss G: {loss_g.item()}')\n<\/code><\/pre>\n<h2>3. World Model Structure<\/h2>\n<p>The world model is a structure used to learn a model of the environment and utilize that model to simulate various scenarios to learn optimal actions. This can be seen as a combination of reinforcement learning and generative models.<\/p>\n<h3>3.1 Components of the World Model<\/h3>\n<p>The world model consists of three basic components:<\/p>\n<ul>\n<li><strong>Visual Model<\/strong>: Models the visual state of the environment.<\/li>\n<li><strong>Dynamic Model<\/strong>: Models the transition from state to state.<\/li>\n<li><strong>Policy<\/strong>: Determines the optimal actions based on simulation results.<\/li>\n<\/ul>\n<h3>3.2 PyTorch Implementation of the World Model<\/h3>\n<p>Next, we will implement a simple example of the world model.<\/p>\n<pre><code>python\nclass VisualModel(nn.Module):\n    def __init__(self):\n        super(VisualModel, self).__init__()\n        self.model = nn.Sequential(\n            nn.Linear(784, 128),\n            nn.ReLU(),\n            nn.Linear(128, 64),\n            nn.ReLU(),\n            nn.Linear(64, 32)\n        )\n\n    def forward(self, x):\n        return self.model(x)\n\nclass DynamicModel(nn.Module):\n    def __init__(self):\n        super(DynamicModel, self).__init__()\n        self.model = nn.Sequential(\n            nn.Linear(32 + 10, 64),  # State + Action\n            nn.ReLU(),\n            nn.Linear(64, 32)\n        )\n\n    def forward(self, state, action):\n        return self.model(torch.cat([state, action], dim=1))\n\nclass Policy(nn.Module):\n    def __init__(self):\n        super(Policy, self).__init__()\n        self.model = nn.Sequential(\n            nn.Linear(32, 64),\n            nn.ReLU(),\n            nn.Linear(64, 10)  # 10 actions\n        )\n\n    def forward(self, state):\n        return self.model(state)\n<\/code><\/pre>\n<h3>3.3 Training the World Model<\/h3>\n<p>We train each model to learn the relationship between states and actions. This allows for learning a policy through various simulations.<\/p>\n<h2>4. Conclusion<\/h2>\n<p>Here, we explained the fundamental principles of GANs and world models, and how to implement them using PyTorch. These components play significant roles in various machine learning and deep learning applications. GANs are suitable for image generation, while world models are apt for simulation and policy learning. These techniques enable more sophisticated modeling and data generation.<\/p>\n<h2>5. References<\/h2>\n<ul>\n<li>Ian Goodfellow et al., &#8216;Generative Adversarial Nets&#8217;<\/li>\n<li>David Ha and J\u00fcrgen Schmidhuber, &#8216;World Models&#8217;<\/li>\n<li>Refer to the official PyTorch documentation for proper use of deep learning.<\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generative Adversarial Networks (GANs) are a deep learning framework in which two neural networks compete to improve the quality of generated data. The basic structure of GAN consists of a generator and a discriminator. The generator tries to create data that is similar to real data, while the discriminator distinguishes whether the generated data is &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36383\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning with GANs Using PyTorch, World Model Structure&#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-36383","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, World Model Structure - \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\/36383\/\" \/>\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, World Model Structure - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Generative Adversarial Networks (GANs) are a deep learning framework in which two neural networks compete to improve the quality of generated data. The basic structure of GAN consists of a generator and a discriminator. The generator tries to create data that is similar to real data, while the discriminator distinguishes whether the generated data is &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning with GANs Using PyTorch, World Model Structure&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36383\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:48:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:00: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\/36383\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36383\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning with GANs Using PyTorch, World Model Structure\",\"datePublished\":\"2024-11-01T09:48:02+00:00\",\"dateModified\":\"2024-11-01T11:00:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36383\/\"},\"wordCount\":445,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"GAN deep learning course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36383\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36383\/\",\"name\":\"Deep Learning with GANs Using PyTorch, World Model Structure - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:48:02+00:00\",\"dateModified\":\"2024-11-01T11:00:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36383\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36383\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36383\/#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, World Model Structure\"}]},{\"@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, World Model Structure - \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\/36383\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning with GANs Using PyTorch, World Model Structure - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Generative Adversarial Networks (GANs) are a deep learning framework in which two neural networks compete to improve the quality of generated data. The basic structure of GAN consists of a generator and a discriminator. The generator tries to create data that is similar to real data, while the discriminator distinguishes whether the generated data is &hellip; \ub354 \ubcf4\uae30 \"Deep Learning with GANs Using PyTorch, World Model Structure\"","og_url":"https:\/\/atmokpo.com\/w\/36383\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:48:02+00:00","article_modified_time":"2024-11-01T11:00: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\/36383\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36383\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning with GANs Using PyTorch, World Model Structure","datePublished":"2024-11-01T09:48:02+00:00","dateModified":"2024-11-01T11:00:08+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36383\/"},"wordCount":445,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["GAN deep learning course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36383\/","url":"https:\/\/atmokpo.com\/w\/36383\/","name":"Deep Learning with GANs Using PyTorch, World Model Structure - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:48:02+00:00","dateModified":"2024-11-01T11:00:08+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36383\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36383\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36383\/#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, World Model Structure"}]},{"@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\/36383","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=36383"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36383\/revisions"}],"predecessor-version":[{"id":36384,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36383\/revisions\/36384"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}