{"id":36611,"date":"2024-11-01T09:49:59","date_gmt":"2024-11-01T09:49:59","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36611"},"modified":"2024-11-01T11:52:30","modified_gmt":"2024-11-01T11:52:30","slug":"deep-learning-pytorch-course-self-organizing-map","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36611\/","title":{"rendered":"Deep Learning PyTorch Course, Self-Organizing Map"},"content":{"rendered":"<p><body><\/p>\n<p>The Self-Organizing Map (SOM) is an unsupervised learning algorithm used for nonlinear dimensionality reduction and data clustering. In this lecture, we will explain the basic concepts of SOM, how it works, and how to implement it using <strong>Pytorch<\/strong>.<\/p>\n<h2>What is a Self-Organizing Map (SOM)?<\/h2>\n<p>The Self-Organizing Map is a neural network originally developed by <strong>Teuvo Kohonen<\/strong>. SOM is used to map high-dimensional data into a lower-dimensional space (usually a 2D grid). In this process, data is organized into a map consisting of neighboring nodes that have similar characteristics.<\/p>\n<h3>Main Features of SOM<\/h3>\n<ul>\n<li><strong>Unsupervised Learning<\/strong>: It can handle unlabeled data.<\/li>\n<li><strong>Dimensionality Reduction<\/strong>: Reduces high-dimensional data to lower dimensions while preserving important features of the data.<\/li>\n<li><strong>Clustering<\/strong>: Similar data points are grouped in the same region.<\/li>\n<\/ul>\n<h2>How SOM Works<\/h2>\n<p>SOM learns by calculating the distance between the input vector and the node vectors. Here are the typical learning steps of SOM:<\/p>\n<h3>1. Initialization<\/h3>\n<p>All nodes are initialized randomly. Each node has a weight vector with the same dimension as the input data.<\/p>\n<h3>2. Input Data Selection<\/h3>\n<p>Randomly select a training sample. Each sample becomes an input to the SOM.<\/p>\n<h3>3. Finding the Nearest Node<\/h3>\n<p>Find the node that is most similar to the selected input data. This node is called the <strong>Best Matching Unit (BMU)<\/strong>.<\/p>\n<h3>4. Weight Update<\/h3>\n<p>Update the weights of the BMU and its neighboring nodes to move closer to the input data. The process is as follows:<\/p>\n<pre><code>\nw_{i}(t+1) = w_{i}(t) + \u03b1(t) * h_{i,j}(t) * (x(t) - w_{i}(t))\n<\/code><\/pre>\n<p>Where:<\/p>\n<ul>\n<li><code>w_{i}<\/code>: Weight vector of the node<\/li>\n<li><code>\u03b1(t)<\/code>: Learning rate<\/li>\n<li><code>h_{i,j}(t)<\/code>: Neighbor function of node <code>i<\/code> regarding the BMU<\/li>\n<li><code>x(t)<\/code>: Input vector<\/li>\n<\/ul>\n<h3>5. Iteration<\/h3>\n<p>Repeat steps 2-4 for a sufficient number of epochs to gradually update the weights.<\/p>\n<h2>Implementing SOM with Pytorch<\/h2>\n<p>Now let&#8217;s implement SOM using Pytorch. Here we will show you how to build and visualize a basic SOM.<\/p>\n<h3>Installing Required Libraries<\/h3>\n<p>First, install the required libraries.<\/p>\n<pre><code>!pip install torch numpy matplotlib<\/code><\/pre>\n<h3>Defining the Model Class<\/h3>\n<p>Next, we define the SOM class. This class includes functions for weight initialization, finding the BMU, and updating weights.<\/p>\n<pre><code>\nimport numpy as np\nimport torch\n\nclass SelfOrganizingMap:\n    def __init__(self, m, n, input_dim, learning_rate=0.5, sigma=None):\n        self.m = m  # grid rows\n        self.n = n  # grid columns\n        self.input_dim = input_dim\n        self.learning_rate = learning_rate\n        self.sigma = sigma if sigma else max(m, n) \/ 2\n\n        # Initialize weight vectors\n        self.weights = torch.rand(m, n, input_dim)\n\n    def find_bmu(self, x):\n        distances = torch.sqrt(torch.sum((self.weights - x) ** 2, dim=2))\n        bmu_index = torch.argmin(distances)\n        return bmu_index \/\/ self.n, bmu_index % self.n  # return row, column\n\n    def update_weights(self, x, bmu, iteration):\n        learning_rate = self.learning_rate * np.exp(-iteration \/ 100)\n        sigma = self.sigma * np.exp(-iteration \/ 100)\n\n        for i in range(self.m):\n            for j in range(self.n):\n                h = self.neighbourhood(bmu, (i, j), sigma)\n                self.weights[i, j] += learning_rate * h * (x - self.weights[i, j])\n\n    def neighbourhood(self, bmu, point, sigma):\n        distance = np.sqrt((bmu[0] - point[0]) ** 2 + (bmu[1] - point[1]) ** 2)\n        return np.exp(-distance ** 2 \/ (2 * sigma ** 2))\n\n    def train(self, data, num_iterations):\n        for i in range(num_iterations):\n            for x in data:\n                bmu = self.find_bmu(x)\n                self.update_weights(x, bmu, i)\n<\/code><\/pre>\n<h3>Preparing Data and Training the Model<\/h3>\n<p>We will prepare appropriate data and train the SOM model. Here we will use randomly generated data.<\/p>\n<pre><code>\n# Generate random data\ndata = torch.rand(200, 3)  # 200 samples, 3 dimensions\n\n# Create and train SOM\nsom = SelfOrganizingMap(10, 10, 3)\nsom.train(data, 100)\n<\/code><\/pre>\n<h3>Visualizing the Results<\/h3>\n<p>We will visualize the weights of the trained SOM to check the distribution of the data.<\/p>\n<pre><code>\nimport matplotlib.pyplot as plt\n\ndef plot_som(som):\n    plt.figure(figsize=(8, 8))\n    for i in range(som.m):\n        for j in range(som.n):\n            plt.scatter(som.weights[i, j, 0].item(), som.weights[i, j, 1].item(), c='blue')\n    plt.title('Self Organizing Map')\n    plt.xlabel('Dimension 1')\n    plt.ylabel('Dimension 2')\n    plt.show()\n\nplot_som(som)\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this lecture, we explored the basic principles of Self-Organizing Maps (SOM) and how to implement SOM using Pytorch. SOM is an effective unsupervised learning technique that is useful for identifying patterns in data and performing clustering. In the future, we can experiment with SOM&#8217;s application on more complex datasets or apply optimization techniques to enhance learning performance.<\/p>\n<p>I hope this article has helped you explore the world of deep learning! If you have any questions or feedback, please leave a comment.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Self-Organizing Map (SOM) is an unsupervised learning algorithm used for nonlinear dimensionality reduction and data clustering. In this lecture, we will explain the basic concepts of SOM, how it works, and how to implement it using Pytorch. What is a Self-Organizing Map (SOM)? The Self-Organizing Map is a neural network originally developed by Teuvo &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36611\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, Self-Organizing Map&#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-36611","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, Self-Organizing Map - \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\/36611\/\" \/>\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, Self-Organizing Map - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"The Self-Organizing Map (SOM) is an unsupervised learning algorithm used for nonlinear dimensionality reduction and data clustering. In this lecture, we will explain the basic concepts of SOM, how it works, and how to implement it using Pytorch. What is a Self-Organizing Map (SOM)? The Self-Organizing Map is a neural network originally developed by Teuvo &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, Self-Organizing Map&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36611\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:49:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:52:30+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\/36611\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36611\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, Self-Organizing Map\",\"datePublished\":\"2024-11-01T09:49:59+00:00\",\"dateModified\":\"2024-11-01T11:52:30+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36611\/\"},\"wordCount\":453,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36611\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36611\/\",\"name\":\"Deep Learning PyTorch Course, Self-Organizing Map - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:49:59+00:00\",\"dateModified\":\"2024-11-01T11:52:30+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36611\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36611\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36611\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, Self-Organizing Map\"}]},{\"@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, Self-Organizing Map - \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\/36611\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, Self-Organizing Map - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"The Self-Organizing Map (SOM) is an unsupervised learning algorithm used for nonlinear dimensionality reduction and data clustering. In this lecture, we will explain the basic concepts of SOM, how it works, and how to implement it using Pytorch. What is a Self-Organizing Map (SOM)? The Self-Organizing Map is a neural network originally developed by Teuvo &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, Self-Organizing Map\"","og_url":"https:\/\/atmokpo.com\/w\/36611\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:49:59+00:00","article_modified_time":"2024-11-01T11:52:30+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\/36611\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36611\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, Self-Organizing Map","datePublished":"2024-11-01T09:49:59+00:00","dateModified":"2024-11-01T11:52:30+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36611\/"},"wordCount":453,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36611\/","url":"https:\/\/atmokpo.com\/w\/36611\/","name":"Deep Learning PyTorch Course, Self-Organizing Map - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:49:59+00:00","dateModified":"2024-11-01T11:52:30+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36611\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36611\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36611\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, Self-Organizing Map"}]},{"@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\/36611","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=36611"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36611\/revisions"}],"predecessor-version":[{"id":36612,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36611\/revisions\/36612"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}