{"id":36531,"date":"2024-11-01T09:49:18","date_gmt":"2024-11-01T09:49:18","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36531"},"modified":"2024-11-01T11:52:49","modified_gmt":"2024-11-01T11:52:49","slug":"deep-learning-pytorch-course-what-is-a-graph","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36531\/","title":{"rendered":"Deep Learning PyTorch Course, What is a Graph"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Concept of Graphs<\/h2>\n<p>A graph is a collection of points and lines, where points are represented as nodes and lines as edges. This structure is a powerful tool for visually representing various data. In deep learning, it is primarily used to analyze the relationships between data or to define the structure of neural networks.<\/p>\n<h2>2. Use of Graphs in Deep Learning<\/h2>\n<p>In deep learning, graphs are used to model the computation process. Each node represents data or variables, and each edge represents the transformation (e.g., operations) between them. Once the data is inputted, it undergoes various operations to produce a result. This process is divided into two main stages:<\/p>\n<ol>\n<li><strong>Forward Pass<\/strong>: The process of passing input data through the neural network to generate results.<\/li>\n<li><strong>Backward Pass<\/strong>: The process of updating weights according to the loss function, also known as Backpropagation.<\/li>\n<\/ol>\n<h2>3. Graphs in PyTorch<\/h2>\n<p>PyTorch supports a Dynamic Computation Graph. This means the graph is created at execution time, allowing for a dynamic flow of data. Therefore, defining and training models is carried out intuitively and flexibly.<\/p>\n<h3>3.1. Static vs Dynamic<\/h3>\n<p>In the traditionally used Static Computation Graph, calculations proceeded in a fixed form after the graph was built. In contrast, PyTorch&#8217;s dynamic computation graph allows for creating and modifying the graph as needed, providing greater flexibility.<\/p>\n<h2>4. PyTorch Example<\/h2>\n<h3>4.1. Creating a Basic Computation Graph<\/h3>\n<p>In this section, we will create a basic computation graph to perform simple tensor operations.<\/p>\n<div class=\"code-block\">\n<pre><code>import torch\n\n# Create tensor\nx = torch.tensor([2.0], requires_grad=True)  # Set requires_grad=True to make it differentiable.\ny = x**2 + 3*x + 1  # Perform operation\n\n# Forward pass\nprint(f'y: {y.item()}')  # Print result\n\n# Backward pass\ny.backward()  # Compute the derivative of y with respect to x\n\n# Print gradient\nprint(f'Gradient: {x.grad.item()}')  # Print gradient with respect to x\n        <\/code><\/pre>\n<\/div>\n<p>The above code defines a simple polynomial <code>y = x^2 + 3x + 1<\/code> and provides an example of calculating the gradient through its derivative. The tensor <code>x<\/code> is set with <code>requires_grad=True<\/code>, creating a computation graph, and the gradients are computed by invoking the <code>backward()<\/code> method.<\/p>\n<h3>4.2. Neural Network Graph Example<\/h3>\n<p>Now, let&#8217;s look at an example of constructing a neural network to learn from the MNIST handwritten digit recognition dataset.<\/p>\n<div class=\"code-block\">\n<pre><code>import torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torchvision import datasets, transforms\n\n# Define neural network model\nclass SimpleNN(nn.Module):\n    def __init__(self):\n        super(SimpleNN, self).__init__()\n        self.fc1 = nn.Linear(28 * 28, 128)  # First layer\n        self.fc2 = nn.Linear(128, 64)  # Second layer\n        self.fc3 = nn.Linear(64, 10)  # Output layer\n\n    def forward(self, x):\n        x = x.view(-1, 28 * 28)  # Transform 2D image to 1D\n        x = torch.relu(self.fc1(x))  # ReLU activation function\n        x = torch.relu(self.fc2(x))\n        x = self.fc3(x)  # Final output\n        return x\n\n# Load and preprocess data\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize((0.5,), (0.5,))\n])\ntrain_data = datasets.MNIST(root='.\/data', train=True, download=True, transform=transform)\ntrain_loader = torch.utils.data.DataLoader(dataset=train_data, batch_size=64, shuffle=True)\n\n# Define model, loss function, and optimizer\nmodel = SimpleNN()\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Training\nnum_epochs = 5\nfor epoch in range(num_epochs):\n    for images, labels in train_loader:\n        optimizer.zero_grad()  # Initialize gradients\n        outputs = model(images)  # Input image data to the model\n        loss = criterion(outputs, labels)  # Compute loss\n        loss.backward()  # Backpropagation\n        optimizer.step()  # Update parameters\n\n    print(f'Epoch [{epoch+1}\/{num_epochs}], Loss: {loss.item():.4f}')\n        <\/code><\/pre>\n<\/div>\n<p>This code constructs a simple neural network and provides an example of training it on the MNIST dataset. Input image data is passed through the defined network structure via the <code>forward()<\/code> method to produce predictions. Similarly, weights are updated through the backpropagation process.<\/p>\n<h2>5. Conclusion<\/h2>\n<p>Graphs are an important element in deep learning for processing data and defining the structure of models. PyTorch allows for the dynamic handling of such graph-based approaches, making it useful for training large and complex models.<\/p>\n<p>Through the above examples, we hope to enhance the understanding of the concept of graphs and its applications in PyTorch. We wish you continued success in the upcoming advanced topics of the deep learning course.<\/p>\n<footer>\n<p>Author: Deep Learning Course Team | Date: October 2023<\/p>\n<\/footer>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Concept of Graphs A graph is a collection of points and lines, where points are represented as nodes and lines as edges. This structure is a powerful tool for visually representing various data. In deep learning, it is primarily used to analyze the relationships between data or to define the structure of neural networks. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36531\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, What is a Graph&#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-36531","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, What is a Graph - \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\/36531\/\" \/>\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, What is a Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Concept of Graphs A graph is a collection of points and lines, where points are represented as nodes and lines as edges. This structure is a powerful tool for visually representing various data. In deep learning, it is primarily used to analyze the relationships between data or to define the structure of neural networks. &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, What is a Graph&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36531\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:49:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:52:49+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\/36531\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36531\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, What is a Graph\",\"datePublished\":\"2024-11-01T09:49:18+00:00\",\"dateModified\":\"2024-11-01T11:52:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36531\/\"},\"wordCount\":428,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36531\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36531\/\",\"name\":\"Deep Learning PyTorch Course, What is a Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:49:18+00:00\",\"dateModified\":\"2024-11-01T11:52:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36531\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36531\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36531\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, What is a Graph\"}]},{\"@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, What is a Graph - \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\/36531\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, What is a Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Concept of Graphs A graph is a collection of points and lines, where points are represented as nodes and lines as edges. This structure is a powerful tool for visually representing various data. In deep learning, it is primarily used to analyze the relationships between data or to define the structure of neural networks. &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, What is a Graph\"","og_url":"https:\/\/atmokpo.com\/w\/36531\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:49:18+00:00","article_modified_time":"2024-11-01T11:52:49+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\/36531\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36531\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, What is a Graph","datePublished":"2024-11-01T09:49:18+00:00","dateModified":"2024-11-01T11:52:49+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36531\/"},"wordCount":428,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36531\/","url":"https:\/\/atmokpo.com\/w\/36531\/","name":"Deep Learning PyTorch Course, What is a Graph - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:49:18+00:00","dateModified":"2024-11-01T11:52:49+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36531\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36531\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36531\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, What is a Graph"}]},{"@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\/36531","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=36531"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36531\/revisions"}],"predecessor-version":[{"id":36532,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36531\/revisions\/36532"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}