{"id":36631,"date":"2024-11-01T09:50:08","date_gmt":"2024-11-01T09:50:08","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36631"},"modified":"2024-11-01T11:52:26","modified_gmt":"2024-11-01T11:52:26","slug":"deep-learning-pytorch-course-restricted-boltzmann-machine","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36631\/","title":{"rendered":"Deep Learning PyTorch Course, Restricted Boltzmann Machine"},"content":{"rendered":"<p><body><\/p>\n<p>The Restricted Boltzmann Machine (RBM) is a type of unsupervised learning algorithm, also known as a generative model. RBMs can effectively learn from large amounts of input data and are utilized in various fields. This document aims to provide a deep understanding of the fundamental principles of RBMs, how to implement them in Python, and examples using the PyTorch framework.<\/p>\n<h2>1. Understanding Restricted Boltzmann Machines (RBM)<\/h2>\n<p>RBM is a model that originated from statistical physics, based on the concept of &#8216;Boltzmann Machines&#8217;. An RBM consists of two types of nodes: visible nodes and hidden nodes. There are connections between these two nodes, but there are no connections between the hidden nodes, resulting in a restricted structure. This structure allows for more efficient learning in RBMs.<\/p>\n<h3>1.1 Structure of RBM<\/h3>\n<p>RBM consists of the following components:<\/p>\n<ul>\n<li><strong>Visible Units<\/strong>: Represents the characteristics of the input data.<\/li>\n<li><strong>Hidden Units<\/strong>: Learns the underlying characteristics of the data.<\/li>\n<li><strong>Weights<\/strong>: Represents the strength of the connections between visible and hidden nodes.<\/li>\n<li><strong>Bias<\/strong>: Represents the bias values for each node.<\/li>\n<\/ul>\n<h3>1.2 Energy Function<\/h3>\n<p>The learning of RBM occurs through the process of minimizing the Energy Function. The energy function is defined based on the states of the visible and hidden nodes as follows:<\/p>\n<pre><code>E(v, h) = -\u2211 v<sub>i<\/sub> * b<sub>i<\/sub> - \u2211 h<sub>j<\/sub> * c<sub>j<\/sub> - \u2211 v<sub>i<\/sub> * h<sub>j<\/sub> * w<sub>ij<\/sub><\/code><\/pre>\n<p>Here, \\( v \\) represents the visible node, \\( h \\) represents the hidden node, \\( b \\) is the bias of the visible node, \\( c \\) is the bias of the hidden node, and \\( w \\) is the weight.<\/p>\n<h2>2. Learning Process of Restricted Boltzmann Machines<\/h2>\n<p>The learning process of RBM proceeds as follows:<\/p>\n<ul>\n<li>Initialize the visible nodes from the dataset.<\/li>\n<li>Calculate the probabilities of the hidden nodes.<\/li>\n<li>Sample the hidden nodes.<\/li>\n<li>Calculate the probabilities of the new visible nodes through the reconstruction of visible nodes.<\/li>\n<li>Calculate the probabilities of the new hidden nodes through the reconstruction of hidden nodes.<\/li>\n<li>Update weights and biases.<\/li>\n<\/ul>\n<h3>2.1 Contrastive Divergence Algorithm<\/h3>\n<p>The learning of RBM occurs through the Contrastive Divergence (CD) algorithm. CD consists of two main phases:<\/p>\n<ol>\n<li><strong>Positive Phase<\/strong>: Identify the activations of the hidden nodes from the input data and update the weights based on this value.<\/li>\n<li><strong>Pseudo Negative Phase<\/strong>: Reconstruct visible nodes from the sampled hidden nodes and then sample hidden nodes again to update weights in a way that reduces similarity.<\/li>\n<\/ol>\n<h2>3. Implementing RBM with PyTorch<\/h2>\n<p>This section explains how to implement RBM using PyTorch. First, let&#8217;s install the required libraries and prepare the dataset.<\/p>\n<h3>3.1 Install Libraries and Prepare Dataset<\/h3>\n<pre><code>!pip install torch torchvision<\/code><\/pre>\n<p>We will use the MNIST dataset to train the RBM. This dataset consists of handwritten digit images.<\/p>\n<pre><code>import torch\nfrom torchvision import datasets, transforms\n\n# Downloading and transforming MNIST dataset\ntransform = transforms.Compose([transforms.ToTensor(), transforms.Lambda(lambda x: x.view(-1))])\nmnist = datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\ntrain_loader = torch.utils.data.DataLoader(dataset=mnist, batch_size=64, shuffle=True)<\/code><\/pre>\n<h3>3.2 Define RBM Class<\/h3>\n<p>Now let&#8217;s define the RBM class. The class should include methods for weight initialization, weight updates, and training.<\/p>\n<pre><code>class RBM:\n    def __init__(self, visible_units, hidden_units, learning_rate=0.1):\n        self.visible_units = visible_units\n        self.hidden_units = hidden_units\n        self.learning_rate = learning_rate\n        self.weights = torch.randn(visible_units, hidden_units) * 0.1\n        self.visible_bias = torch.zeros(visible_units)\n        self.hidden_bias = torch.zeros(hidden_units)\n\n    def sample_hidden(self, visible):\n        activation = torch.mm(visible, self.weights) + self.hidden_bias\n        probabilities = torch.sigmoid(activation)\n        return probabilities, torch.bernoulli(probabilities)\n\n    def sample_visible(self, hidden):\n        activation = torch.mm(hidden, self.weights.t()) + self.visible_bias\n        probabilities = torch.sigmoid(activation)\n        return probabilities, torch.bernoulli(probabilities)\n\n    def train(self, train_loader, num_epochs=10):\n        for epoch in range(num_epochs):\n            for data, _ in train_loader:\n                # Sample visible nodes\n                v0 = data\n                h0, h0_sample = self.sample_hidden(v0)\n\n                # Negative phase\n                v1, v1_sample = self.sample_visible(h0_sample)\n                h1, _ = self.sample_hidden(v1_sample)\n\n                # Update weights\n                self.weights += self.learning_rate * (torch.mm(v0.t(), h0) - torch.mm(v1.t(), h1)) \/ v0.size(0)\n                self.visible_bias += self.learning_rate * (v0 - v1).mean(0)\n                self.hidden_bias += self.learning_rate * (h0 - h1).mean(0)\n\n                print('Epoch: {} - Loss: {:.4f}'.format(epoch, torch.mean((v0 - v1) ** 2).item()))<\/code><\/pre>\n<h3>3.3 Perform RBM Training<\/h3>\n<p>Now let&#8217;s train the model using the defined RBM class.<\/p>\n<pre><code>visible_units = 784  # For MNIST, 28x28 pixels\nhidden_units = 256    # Number of hidden nodes\nrbm = RBM(visible_units, hidden_units)\nrbm.train(train_loader, num_epochs=10)<\/code><\/pre>\n<h2>4. Results and Interpretation<\/h2>\n<p>As training progresses, the loss value is printed for each epoch. The loss value indicates how similar the reconstruction of visible nodes is to the hidden state, so a decrease in the loss value signifies an improvement in model performance. Notably, the Boltzmann Machine forms the basis of many other algorithms and is combined with various deep learning models.<\/p>\n<h2>5. Conclusion<\/h2>\n<p>In this post, we addressed the concept of restricted Boltzmann machines, the learning process, and a practical implementation example using PyTorch. RBM is a highly effective tool for learning the underlying structure of data. Nevertheless, it is primarily used for pre-training or in combination with other architectures in current deep learning frameworks. Further research on various generative models is expected in the future.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Restricted Boltzmann Machine (RBM) is a type of unsupervised learning algorithm, also known as a generative model. RBMs can effectively learn from large amounts of input data and are utilized in various fields. This document aims to provide a deep understanding of the fundamental principles of RBMs, how to implement them in Python, and &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36631\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, Restricted Boltzmann Machine&#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-36631","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, Restricted Boltzmann Machine - \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\/36631\/\" \/>\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, Restricted Boltzmann Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"The Restricted Boltzmann Machine (RBM) is a type of unsupervised learning algorithm, also known as a generative model. RBMs can effectively learn from large amounts of input data and are utilized in various fields. This document aims to provide a deep understanding of the fundamental principles of RBMs, how to implement them in Python, and &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, Restricted Boltzmann Machine&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36631\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:50:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:52:26+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\/36631\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36631\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, Restricted Boltzmann Machine\",\"datePublished\":\"2024-11-01T09:50:08+00:00\",\"dateModified\":\"2024-11-01T11:52:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36631\/\"},\"wordCount\":581,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36631\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36631\/\",\"name\":\"Deep Learning PyTorch Course, Restricted Boltzmann Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:50:08+00:00\",\"dateModified\":\"2024-11-01T11:52:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36631\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36631\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36631\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, Restricted Boltzmann Machine\"}]},{\"@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, Restricted Boltzmann Machine - \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\/36631\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, Restricted Boltzmann Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"The Restricted Boltzmann Machine (RBM) is a type of unsupervised learning algorithm, also known as a generative model. RBMs can effectively learn from large amounts of input data and are utilized in various fields. This document aims to provide a deep understanding of the fundamental principles of RBMs, how to implement them in Python, and &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, Restricted Boltzmann Machine\"","og_url":"https:\/\/atmokpo.com\/w\/36631\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:50:08+00:00","article_modified_time":"2024-11-01T11:52:26+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\/36631\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36631\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, Restricted Boltzmann Machine","datePublished":"2024-11-01T09:50:08+00:00","dateModified":"2024-11-01T11:52:26+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36631\/"},"wordCount":581,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36631\/","url":"https:\/\/atmokpo.com\/w\/36631\/","name":"Deep Learning PyTorch Course, Restricted Boltzmann Machine - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:50:08+00:00","dateModified":"2024-11-01T11:52:26+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36631\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36631\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36631\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, Restricted Boltzmann Machine"}]},{"@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\/36631","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=36631"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36631\/revisions"}],"predecessor-version":[{"id":36632,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36631\/revisions\/36632"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}