{"id":36233,"date":"2024-11-01T09:46:52","date_gmt":"2024-11-01T09:46:52","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36233"},"modified":"2024-11-01T09:46:52","modified_gmt":"2024-11-01T09:46:52","slug":"using-hugging-face-transformers-pytorch-pre-training","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36233\/","title":{"rendered":"Using Hugging Face Transformers, PyTorch Pre-training"},"content":{"rendered":"<p><body><\/p>\n<p>With the advancement of deep learning, significant innovations are also occurring in the field of Natural Language Processing (NLP). Among them, the Transformers library provided by Hugging Face makes it easy to utilize pre-trained language models. In this article, we will introduce the basic concepts and usage of Hugging Face Transformers, and provide a detailed explanation of how to leverage pre-trained models based on PyTorch.<\/p>\n<h2>1. What is the Hugging Face Transformers Library?<\/h2>\n<p>The Hugging Face Transformers library includes various pre-trained models for different natural language processing tasks. These models have been trained to perform a wide range of NLP tasks, such as translation, text classification, summarization, and question answering. The transformer architecture demonstrates excellent performance in understanding context by calculating the importance of each word in the input sequence through a self-attention mechanism.<\/p>\n<h3>1.1 Installation<\/h3>\n<p>To install the Hugging Face Transformers library, you first need to set up a Python environment. You can do this with the following command.<\/p>\n<pre><code>pip install transformers torch<\/code><\/pre>\n<h2>2. Understanding Pre-trained Models<\/h2>\n<p>Pre-trained models are trained on large datasets and can be fine-tuned to improve performance for specific tasks. Examples include models such as BERT, GPT-2, and RoBERTa. These models are designed for basic language understanding and have undergone pre-training on various datasets.<\/p>\n<h3>2.1 BERT Model<\/h3>\n<p>The BERT (Bidirectional Encoder Representations from Transformers) model is a bidirectional sequence encoder that captures the meaning of words by reflecting on context. This model, which shows outstanding performance across numerous natural language processing tasks, has the ability to understand the context surrounding words in a sentence simultaneously.<\/p>\n<h3>2.2 GPT-2 Model<\/h3>\n<p>GPT-2 (Generative Pre-trained Transformer 2) is a model primarily used for text generation tasks, excelling in understanding and generating context through sequential data processing. This model is widely used to generate documents on specific topics or styles.<\/p>\n<h2>3. How to Use with PyTorch<\/h2>\n<p>PyTorch is a very useful framework for building and training deep learning models. The Hugging Face Transformers library is designed to work with PyTorch, making it easy to load and utilize models.<\/p>\n<h3>3.1 Text Classification Example<\/h3>\n<p>In this section, I will show you an example of performing a simple text classification task using the BERT model. We will use the IMDB movie review dataset for text classification.<\/p>\n<h4>3.1.1 Preparing the Dataset<\/h4>\n<p>First, we will download and preprocess the IMDB dataset. We will use the pandas library for this.<\/p>\n<pre><code>import pandas as pd\n\n# Download IMDB movie review data\nurl = \"https:\/\/ai.stanford.edu\/~amaas\/data\/sentiment\/aclImdb_v1.tar.gz\"\nimdb_data = pd.read_csv(url)\n\n# Data preprocessing\ndf = pd.DataFrame(imdb_data)\ndf['label'] = df['label'].map({'pos': 1, 'neg': 0})\ndf.head()<\/code><\/pre>\n<h4>3.1.2 Loading and Splitting the Dataset<\/h4>\n<p>After loading the dataset, we will split it into training and validation sets.<\/p>\n<pre><code>from sklearn.model_selection import train_test_split\n\n# train\/test split\ntrain_data, test_data = train_test_split(df, test_size=0.2, random_state=42)\n\n# Check the split data\nprint(f'Train data size: {len(train_data)}, Test data size: {len(test_data)}')<\/code><\/pre>\n<h4>3.1.3 Setting Up Hugging Face Dataset<\/h4>\n<p>Now we will load the data using Hugging Face&#8217;s <code>Dataset<\/code> class.<\/p>\n<pre><code>from transformers import Dataset\n\n# Convert to Hugging Face Dataset format\ntrain_dataset = Dataset.from_pandas(train_data)\ntest_dataset = Dataset.from_pandas(test_data)<\/code><\/pre>\n<h4>3.1.4 Loading the Model<\/h4>\n<p>We will load the pre-trained BERT model. These models can be called using the <code>AutoModelForSequenceClassification<\/code> class.<\/p>\n<pre><code>from transformers import AutoTokenizer, AutoModelForSequenceClassification\n\n# Load pre-trained BERT model and tokenizer\nmodel_name = \"bert-base-uncased\"\ntokenizer = AutoTokenizer.from_pretrained(model_name)\nmodel = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=2)<\/code><\/pre>\n<h4>3.1.5 Data Preprocessing<\/h4>\n<p>We will convert the text data into a format that the BERT model can process.<\/p>\n<pre><code>def preprocess_function(examples):\n    return tokenizer(examples['text'], truncation=True, padding='max_length', max_length=512)\n\ntokenized_train_data = train_dataset.map(preprocess_function, batched=True)\ntokenized_test_data = test_dataset.map(preprocess_function, batched=True)<\/code><\/pre>\n<h4>3.1.6 Training the Model<\/h4>\n<p>We will use the <code>Trainer<\/code> class to train the model.<\/p>\n<pre><code>from transformers import Trainer, TrainingArguments\n\n# Set training arguments\ntraining_args = TrainingArguments(\n    output_dir='.\/results',\n    evaluation_strategy='epoch',\n    learning_rate=2e-5,\n    per_device_train_batch_size=16,\n    per_device_eval_batch_size=16,\n    num_train_epochs=3,\n    weight_decay=0.01,\n)\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    train_dataset=tokenized_train_data,\n    eval_dataset=tokenized_test_data\n)\n\n# Train the model\ntrainer.train()<\/code><\/pre>\n<h4>3.1.7 Evaluating the Model<\/h4>\n<p>We will evaluate the performance of the trained model.<\/p>\n<pre><code>metrics = trainer.evaluate()\nprint(metrics)<\/code><\/pre>\n<h2>4. Conclusion<\/h2>\n<p>By utilizing the Hugging Face Transformers library, you can easily handle pre-trained models. Models like BERT can be used for various natural language processing tasks beyond text classification and play a significant role in research and industry. In this blog, we have explored the process from installation to training and evaluation of the model. I hope you have acquired useful knowledge that you can apply in practice.<\/p>\n<h2>5. References<\/h2>\n<ul>\n<li><a href=\"https:\/\/huggingface.co\/transformers\/\" target=\"_blank\" rel=\"noopener\">Hugging Face Transformers Documentation<\/a><\/li>\n<li><a href=\"https:\/\/pytorch.org\/docs\/stable\/index.html\" target=\"_blank\" rel=\"noopener\">PyTorch Documentation<\/a><\/li>\n<li><a href=\"https:\/\/huggingface.co\/models\" target=\"_blank\" rel=\"noopener\">Pre-trained Models on Hugging Face<\/a><\/li>\n<li><a href=\"https:\/\/scikit-learn.org\/stable\/\" target=\"_blank\" rel=\"noopener\">Scikit-learn Documentation<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the advancement of deep learning, significant innovations are also occurring in the field of Natural Language Processing (NLP). Among them, the Transformers library provided by Hugging Face makes it easy to utilize pre-trained language models. In this article, we will introduce the basic concepts and usage of Hugging Face Transformers, and provide a detailed &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36233\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Using Hugging Face Transformers, PyTorch Pre-training&#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":[108],"tags":[],"class_list":["post-36233","post","type-post","status-publish","format-standard","hentry","category---en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using Hugging Face Transformers, PyTorch Pre-training - \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\/36233\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Hugging Face Transformers, PyTorch Pre-training - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"With the advancement of deep learning, significant innovations are also occurring in the field of Natural Language Processing (NLP). Among them, the Transformers library provided by Hugging Face makes it easy to utilize pre-trained language models. In this article, we will introduce the basic concepts and usage of Hugging Face Transformers, and provide a detailed &hellip; \ub354 \ubcf4\uae30 &quot;Using Hugging Face Transformers, PyTorch Pre-training&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36233\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:46:52+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\/36233\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36233\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Using Hugging Face Transformers, PyTorch Pre-training\",\"datePublished\":\"2024-11-01T09:46:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36233\/\"},\"wordCount\":562,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Using Hugging Face\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36233\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36233\/\",\"name\":\"Using Hugging Face Transformers, PyTorch Pre-training - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:46:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36233\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36233\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36233\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Hugging Face Transformers, PyTorch Pre-training\"}]},{\"@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":"Using Hugging Face Transformers, PyTorch Pre-training - \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\/36233\/","og_locale":"ko_KR","og_type":"article","og_title":"Using Hugging Face Transformers, PyTorch Pre-training - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"With the advancement of deep learning, significant innovations are also occurring in the field of Natural Language Processing (NLP). Among them, the Transformers library provided by Hugging Face makes it easy to utilize pre-trained language models. In this article, we will introduce the basic concepts and usage of Hugging Face Transformers, and provide a detailed &hellip; \ub354 \ubcf4\uae30 \"Using Hugging Face Transformers, PyTorch Pre-training\"","og_url":"https:\/\/atmokpo.com\/w\/36233\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:46:52+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\/36233\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36233\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Using Hugging Face Transformers, PyTorch Pre-training","datePublished":"2024-11-01T09:46:52+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36233\/"},"wordCount":562,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Using Hugging Face"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36233\/","url":"https:\/\/atmokpo.com\/w\/36233\/","name":"Using Hugging Face Transformers, PyTorch Pre-training - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:46:52+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36233\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36233\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36233\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Using Hugging Face Transformers, PyTorch Pre-training"}]},{"@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\/36233","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=36233"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36233\/revisions"}],"predecessor-version":[{"id":36234,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36233\/revisions\/36234"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}