{"id":36633,"date":"2024-11-01T09:50:09","date_gmt":"2024-11-01T09:50:09","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36633"},"modified":"2024-11-01T11:52:26","modified_gmt":"2024-11-01T11:52:26","slug":"deep-learning-pytorch-course-performance-optimization-using-early-stopping","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36633\/","title":{"rendered":"Deep Learning PyTorch Course, Performance Optimization Using Early Stopping"},"content":{"rendered":"<p><body><\/p>\n<p>\n        Overfitting is one of the common problems that occur during the training of deep learning models. Overfitting refers to the phenomenon where a model is too closely fitted to the training data, leading to a decreased ability to generalize to new data. Therefore, many researchers and engineers strive to prevent overfitting through various methods. One of these methods is &#8216;Early Stopping.&#8217;\n    <\/p>\n<h2>What is Early Stopping?<\/h2>\n<p>\n        Early stopping is a technique that monitors the training process of a model and stops the training when the performance on validation data does not improve. This method prevents overfitting by stopping the training when the model performs poorly on validation data, even if it has learned successfully from the training data.\n    <\/p>\n<h2>How Early Stopping Works<\/h2>\n<p>\n        Early stopping fundamentally observes the validation loss or validation accuracy during model training and stops the training if there is no performance improvement for a certain number of epochs. At this point, the optimal model parameters are saved, allowing the use of this model after training is completed.\n    <\/p>\n<h2>Implementing Early Stopping<\/h2>\n<p>\n        Here, we will implement early stopping through a simple example of training an image classification model using PyTorch. In this example, we will use the MNIST dataset to train a model that recognizes handwritten digits.\n    <\/p>\n<h3>Installing Required Libraries<\/h3>\n<pre><code>pip install torch torchvision matplotlib numpy<\/code><\/pre>\n<h3>Code Example<\/h3>\n<p>\n        Below is a PyTorch code example with early stopping applied.\n    <\/p>\n<pre><code>import torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision\nimport torchvision.transforms as transforms\nfrom torch.utils.data import DataLoader\n\n# Hyperparameter settings\ninput_size = 28 * 28  # MNIST image size\nnum_classes = 10  # Number of classes to classify\nnum_epochs = 20  # Total number of training epochs\nbatch_size = 100  # Batch size\nlearning_rate = 0.001  # Learning rate\n\n# Load MNIST dataset\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize((0.5,), (0.5,))\n])\n\ntrain_dataset = torchvision.datasets.MNIST(root='.\/data', train=True, transform=transform, download=True)\ntrain_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)\n\ntest_dataset = torchvision.datasets.MNIST(root='.\/data', train=False, transform=transform, download=True)\ntest_loader = DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)\n\n# Define a simple neural network model\nclass SimpleNN(nn.Module):\n    def __init__(self):\n        super(SimpleNN, self).__init__()\n        self.fc1 = nn.Linear(input_size, 128)\n        self.fc2 = nn.Linear(128, num_classes)\n\n    def forward(self, x):\n        x = x.view(-1, input_size)  # Reshape image dimensions\n        x = torch.relu(self.fc1(x))  # Activation function\n        x = self.fc2(x)\n        return x\n\n# Initialize model, loss function, and optimizer\nmodel = SimpleNN()\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=learning_rate)\n\n# Initialize variables for early stopping\nbest_loss = float('inf')\npatience, trials = 5, 0  # Stop training if no performance improvement for 5 trials\ntrain_losses, val_losses = [], []\n\n# Training loop\nfor epoch in range(num_epochs):\n    model.train()  # Switch model to training mode\n    running_loss = 0.0\n\n    for images, labels in train_loader:\n        optimizer.zero_grad()  # Reset gradients\n        outputs = model(images)  # Model predictions\n        loss = criterion(outputs, labels)  # Calculate loss\n        loss.backward()  # Compute gradients\n        optimizer.step()  # Update weights\n\n        running_loss += loss.item()\n\n    avg_train_loss = running_loss \/ len(train_loader)\n    train_losses.append(avg_train_loss)\n\n    # Validation step\n    model.eval()  # Switch model to evaluation mode\n    val_loss = 0.0\n\n    with torch.no_grad():  # Disable gradient computation\n        for images, labels in test_loader:\n            outputs = model(images)\n            loss = criterion(outputs, labels)\n            val_loss += loss.item()\n\n    avg_val_loss = val_loss \/ len(test_loader)\n    val_losses.append(avg_val_loss)\n\n    print(f'Epoch [{epoch+1}\/{num_epochs}], Train Loss: {avg_train_loss:.4f}, Valid Loss: {avg_val_loss:.4f}')\n\n    # Early stopping logic\n    if avg_val_loss < best_loss:\n        best_loss = avg_val_loss\n        trials = 0  # Reset performance improvement record\n        torch.save(model.state_dict(), 'best_model.pth')  # Save best model\n    else:\n        trials += 1\n        if trials >= patience:  # Stop training if no improvement for patience\n            print(\"Early stopping...\")\n            break\n\n# Evaluate performance on test data\nmodel.load_state_dict(torch.load('best_model.pth'))  # Load best model\nmodel.eval()  # Switch model to evaluation mode\ncorrect, total = 0, 0\n\nwith torch.no_grad():\n    for images, labels in test_loader:\n        outputs = model(images)\n        _, predicted = torch.max(outputs.data, 1)  # Select class with maximum probability\n        total += labels.size(0)\n        correct += (predicted == labels).sum().item()\n\nprint(f'Accuracy of the model on the test images: {100 * correct \/ total:.2f}%')<\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n        The above code represents the process of training a simple neural network model using the MNIST dataset. First, we import the necessary libraries and load the MNIST dataset. Then, we define a simple neural network composed of two fully connected layers.\n    <\/p>\n<p>\n        After that, at each epoch, we calculate the training loss and validation loss and stop the training if there is no improvement in the validation loss through the early stopping logic. Finally, we evaluate the model&#8217;s performance by calculating the accuracy on the test data.\n    <\/p>\n<h2>Conclusion<\/h2>\n<p>\n        Early stopping is a useful technique for optimizing the performance of deep learning models. It helps prevent overfitting and leads to the generation of an optimal model. In this tutorial, we demonstrated how to implement early stopping using PyTorch to solve the MNIST classification problem. We encourage you to apply early stopping techniques to various deep learning problems based on this.\n    <\/p>\n<h2>References<\/h2>\n<ul>\n<li>Deep Learning, Ian Goodfellow, Yoshua Bengio, Aaron Courville.<\/li>\n<li>PyTorch Documentation: <a href=\"https:\/\/pytorch.org\/docs\/stable\/index.html\" target=\"_blank\" rel=\"noopener\">https:\/\/pytorch.org\/docs\/stable\/index.html<\/a><\/li>\n<li>MNIST Dataset: <a href=\"http:\/\/yann.lecun.com\/exdb\/mnist\/\" target=\"_blank\" rel=\"noopener\">http:\/\/yann.lecun.com\/exdb\/mnist\/<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overfitting is one of the common problems that occur during the training of deep learning models. Overfitting refers to the phenomenon where a model is too closely fitted to the training data, leading to a decreased ability to generalize to new data. Therefore, many researchers and engineers strive to prevent overfitting through various methods. One &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36633\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, Performance Optimization Using Early Stopping&#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-36633","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, Performance Optimization Using Early Stopping - \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\/36633\/\" \/>\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, Performance Optimization Using Early Stopping - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Overfitting is one of the common problems that occur during the training of deep learning models. Overfitting refers to the phenomenon where a model is too closely fitted to the training data, leading to a decreased ability to generalize to new data. Therefore, many researchers and engineers strive to prevent overfitting through various methods. One &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, Performance Optimization Using Early Stopping&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36633\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:50:09+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\/36633\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36633\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, Performance Optimization Using Early Stopping\",\"datePublished\":\"2024-11-01T09:50:09+00:00\",\"dateModified\":\"2024-11-01T11:52:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36633\/\"},\"wordCount\":408,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36633\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36633\/\",\"name\":\"Deep Learning PyTorch Course, Performance Optimization Using Early Stopping - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:50:09+00:00\",\"dateModified\":\"2024-11-01T11:52:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36633\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36633\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36633\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, Performance Optimization Using Early Stopping\"}]},{\"@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, Performance Optimization Using Early Stopping - \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\/36633\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, Performance Optimization Using Early Stopping - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Overfitting is one of the common problems that occur during the training of deep learning models. Overfitting refers to the phenomenon where a model is too closely fitted to the training data, leading to a decreased ability to generalize to new data. Therefore, many researchers and engineers strive to prevent overfitting through various methods. One &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, Performance Optimization Using Early Stopping\"","og_url":"https:\/\/atmokpo.com\/w\/36633\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:50:09+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\/36633\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36633\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, Performance Optimization Using Early Stopping","datePublished":"2024-11-01T09:50:09+00:00","dateModified":"2024-11-01T11:52:26+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36633\/"},"wordCount":408,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36633\/","url":"https:\/\/atmokpo.com\/w\/36633\/","name":"Deep Learning PyTorch Course, Performance Optimization Using Early Stopping - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:50:09+00:00","dateModified":"2024-11-01T11:52:26+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36633\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36633\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36633\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, Performance Optimization Using Early Stopping"}]},{"@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\/36633","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=36633"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36633\/revisions"}],"predecessor-version":[{"id":36634,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36633\/revisions\/36634"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}