{"id":36657,"date":"2024-11-01T09:50:21","date_gmt":"2024-11-01T09:50:21","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36657"},"modified":"2024-11-01T11:52:20","modified_gmt":"2024-11-01T11:52:20","slug":"deep-learning-pytorch-course-feature-extraction-techniques","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36657\/","title":{"rendered":"Deep Learning PyTorch Course, Feature Extraction Techniques"},"content":{"rendered":"<p><body><\/p>\n<p>Deep learning is a powerful technology that enables problem-solving by automatically learning useful features from various data. Today, we will address feature extraction techniques using the PyTorch library. This plays a crucial role in extracting attributes from various forms of data such as images, text, and audio to enhance the performance of machine learning models.<\/p>\n<h2>What is Feature Extraction?<\/h2>\n<p>Feature extraction refers to the process of transforming original data into a lower dimension to extract useful information. This process helps reduce noise in the data and alleviates the difficulties encountered by the model during learning. For example, in an image classification problem, instead of directly using the pixel values of the images, we can utilize CNN (Convolutional Neural Network) to extract only the important features.<\/p>\n<h2>1. Extracting Features from Image Data<\/h2>\n<p>We will look at an example of using CNN to extract features in the field of image processing. CNN is structured favorably to capture local information in images.<\/p>\n<h3>1.1 Data Preparation<\/h3>\n<pre><code>import torchvision.transforms as transforms\nimport torchvision.datasets as datasets\nimport torch\nimport torch.nn as nn\nimport torchvision.models as models\n\n# Download and preprocess dataset\ntransform = transforms.Compose([\n    transforms.Resize((224, 224)),\n    transforms.ToTensor(),\n])\n\n# Example of CIFAR10 dataset\ntrain_dataset = datasets.CIFAR10(root='.\/data', train=True, download=True, transform=transform)\ntrain_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=32, shuffle=True)\n<\/code><\/pre>\n<h3>1.2 Define CNN Model<\/h3>\n<p>We will use a model based on ResNet to extract features.<\/p>\n<pre><code># Load ResNet model\nmodel = models.resnet18(pretrained=True)  # Pre-trained model\nmodel.fc = nn.Identity()  # Remove the last layer to output features only\n\n# Set GPU usage\ndevice = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nmodel = model.to(device)\n<\/code><\/pre>\n<h3>1.3 Extract Features<\/h3>\n<p>To check the shape of the extracted features, let&#8217;s pass the data through the model.<\/p>\n<pre><code>def extract_features(data_loader):\n    features = []\n    \n    model.eval()  # Switch to evaluation mode\n\n    with torch.no_grad():  # Disable gradient calculation\n        for images, labels in data_loader:\n            images = images.to(device)\n            feature = model(images)\n            features.append(feature.cpu())\n\n    return torch.cat(features)\n\n# Execute feature extraction\nfeatures = extract_features(train_loader)\nprint(\"Size of extracted features:\", features.size())\n<\/code><\/pre>\n<h2>2. Extracting Features from Text Data<\/h2>\n<p>To handle text data, we will explore how to extract features using RNN (Recurrent Neural Network). This is commonly used in natural language processing (NLP).<\/p>\n<h3>2.1 Data Preparation<\/h3>\n<pre><code>from torchtext.datasets import AG_NEWS\nfrom torchtext.data import Field, BucketIterator\n\nTEXT = Field(tokenize='spacy', lower=True)\nLABEL = Field(sequential=False)\n\n# Load AG News dataset\ntrain_data, test_data = AG_NEWS splits=(TEXT, LABEL))\nTEXT.build_vocab(train_data)\nLABEL.build_vocab(train_data)\n\n# Build data loaders\ntrain_iterator, test_iterator = BucketIterator.splits((train_data, test_data), batch_size=64, device=device)\n<\/code><\/pre>\n<h3>2.2 Define RNN Model<\/h3>\n<pre><code>class RNN(nn.Module):\n    def __init__(self, input_dim, embed_dim, hidden_dim, output_dim):\n        super().__init__()\n        self.embedding = nn.Embedding(input_dim, embed_dim)\n        self.rnn = nn.RNN(embed_dim, hidden_dim)\n        self.fc = nn.Linear(hidden_dim, output_dim)\n\n    def forward(self, text):\n        embedded = self.embedding(text)\n        output, hidden = self.rnn(embedded)\n        return hidden\n\n# Instantiate the model\ninput_dim = len(TEXT.vocab)\nembed_dim = 100\nhidden_dim = 256\noutput_dim = len(LABEL.vocab)\n\nmodel = RNN(input_dim, embed_dim, hidden_dim, output_dim).to(device)\n<\/code><\/pre>\n<h3>2.3 Extract Features<\/h3>\n<p>Now we will use the RNN model to extract features from the text data.<\/p>\n<pre><code>def extract_text_features(data_loader):\n    text_features = []\n\n    model.eval()\n\n    with torch.no_grad():\n        for batch in data_loader:\n            text, labels = batch.text\n            text = text.to(device)\n            hidden = model(text)\n            text_features.append(hidden.cpu())\n\n    return torch.cat(text_features)\n\n# Execute feature extraction\ntext_features = extract_text_features(train_iterator)\nprint(\"Size of extracted text features:\", text_features.size())\n<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>In this post, we explored how to extract features from image and text data using PyTorch. We confirmed that we can implement feature extraction methods suitable for each data type using structures such as CNN and RNN. Feature extraction is an essential step in improving the performance of machine learning models and enabling smooth data analysis. We encourage further exploration of various models and techniques!<\/p>\n<p>If you have any questions related to feature extraction or need more information, please leave a comment. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Deep learning is a powerful technology that enables problem-solving by automatically learning useful features from various data. Today, we will address feature extraction techniques using the PyTorch library. This plays a crucial role in extracting attributes from various forms of data such as images, text, and audio to enhance the performance of machine learning models. &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36657\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Deep Learning PyTorch Course, Feature Extraction Techniques&#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-36657","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, Feature Extraction Techniques - \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\/36657\/\" \/>\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, Feature Extraction Techniques - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Deep learning is a powerful technology that enables problem-solving by automatically learning useful features from various data. Today, we will address feature extraction techniques using the PyTorch library. This plays a crucial role in extracting attributes from various forms of data such as images, text, and audio to enhance the performance of machine learning models. &hellip; \ub354 \ubcf4\uae30 &quot;Deep Learning PyTorch Course, Feature Extraction Techniques&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36657\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:50:21+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:52:20+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\/36657\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36657\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Deep Learning PyTorch Course, Feature Extraction Techniques\",\"datePublished\":\"2024-11-01T09:50:21+00:00\",\"dateModified\":\"2024-11-01T11:52:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36657\/\"},\"wordCount\":335,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"PyTorch Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36657\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36657\/\",\"name\":\"Deep Learning PyTorch Course, Feature Extraction Techniques - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:50:21+00:00\",\"dateModified\":\"2024-11-01T11:52:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36657\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36657\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36657\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Deep Learning PyTorch Course, Feature Extraction Techniques\"}]},{\"@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, Feature Extraction Techniques - \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\/36657\/","og_locale":"ko_KR","og_type":"article","og_title":"Deep Learning PyTorch Course, Feature Extraction Techniques - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Deep learning is a powerful technology that enables problem-solving by automatically learning useful features from various data. Today, we will address feature extraction techniques using the PyTorch library. This plays a crucial role in extracting attributes from various forms of data such as images, text, and audio to enhance the performance of machine learning models. &hellip; \ub354 \ubcf4\uae30 \"Deep Learning PyTorch Course, Feature Extraction Techniques\"","og_url":"https:\/\/atmokpo.com\/w\/36657\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:50:21+00:00","article_modified_time":"2024-11-01T11:52:20+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\/36657\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36657\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Deep Learning PyTorch Course, Feature Extraction Techniques","datePublished":"2024-11-01T09:50:21+00:00","dateModified":"2024-11-01T11:52:20+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36657\/"},"wordCount":335,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["PyTorch Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36657\/","url":"https:\/\/atmokpo.com\/w\/36657\/","name":"Deep Learning PyTorch Course, Feature Extraction Techniques - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:50:21+00:00","dateModified":"2024-11-01T11:52:20+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36657\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36657\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36657\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Deep Learning PyTorch Course, Feature Extraction Techniques"}]},{"@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\/36657","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=36657"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36657\/revisions"}],"predecessor-version":[{"id":36658,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36657\/revisions\/36658"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}