{"id":36169,"date":"2024-11-01T09:46:20","date_gmt":"2024-11-01T09:46:20","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36169"},"modified":"2024-11-01T09:46:20","modified_gmt":"2024-11-01T09:46:20","slug":"using-hugging-face-transformers-pre-training-the-trainer-class","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36169\/","title":{"rendered":"Using Hugging Face Transformers, Pre-training the Trainer Class"},"content":{"rendered":"<p><body><\/p>\n<p>Deep learning is currently being utilized in various fields, among which natural language processing (NLP) is a particularly rapidly developing area. Hugging Face is well-known as a platform that provides various libraries to easily handle these deep learning models. In this course, we will explain in detail how to use the pre-training and Trainer classes of Hugging Face&#8217;s Transformers library, and provide practical Python code examples.<\/p>\n<h2>1. Introduction to Hugging Face Transformers<\/h2>\n<p>Hugging Face Transformers is a library that allows easy access to various natural language processing models based on the transformer architecture. This library provides various pre-trained models such as BERT, GPT-2, RoBERTa, and T5. This way, we can perform natural language processing tasks conveniently without the need for complex model implementations.<\/p>\n<h2>2. Overview of the Trainer Class<\/h2>\n<p>The Trainer class is a high-level API provided by the Hugging Face Transformers library that helps easily perform model training and evaluation. By using the Trainer class, you can train a model without writing a custom training loop. When using the Trainer class, you need to specify the dataset, model, training arguments, and evaluation arguments.<\/p>\n<h3>2.1. Installing Required Libraries<\/h3>\n<p>First, you need to install the libraries. You can run the following command to install the necessary libraries along with Transformers.<\/p>\n<pre><code>!pip install transformers datasets<\/code><\/pre>\n<h3>2.2. Preparing to Use the Trainer Class<\/h3>\n<p>The preparations needed to use the Trainer class are as follows:<\/p>\n<ul>\n<li>Loading the Model: Load the desired model from Hugging Face&#8217;s model hub.<\/li>\n<li>Setting Up the Tokenizer: Set up the tokenizer to convert input data into vectors.<\/li>\n<li>Preparing the Dataset: Prepare the dataset for training and evaluation purposes.<\/li>\n<li>Setting Training Arguments: Set various arguments to be used during the training process.<\/li>\n<\/ul>\n<h2>3. Preparing the Dataset<\/h2>\n<p>We will use the IMDb movie review dataset to train a model that classifies positive and negative reviews. For this purpose, we will download the IMDb dataset using Hugging Face&#8217;s datasets library.<\/p>\n<pre><code>from datasets import load_dataset\n\ndataset = load_dataset(\"imdb\")\ntrain_dataset = dataset[\"train\"]\ntest_dataset = dataset[\"test\"]<\/code><\/pre>\n<h2>4. Setting Up the Model and Tokenizer<\/h2>\n<p>We will be using the BERT model and will load the &#8216;bert-base-uncased&#8217; model provided by Hugging Face. At the same time, we need to set up the tokenizer appropriate for that model.<\/p>\n<pre><code>from transformers import BertTokenizer, BertForSequenceClassification\n\nmodel_name = \"bert-base-uncased\"\ntokenizer = BertTokenizer.from_pretrained(model_name)\nmodel = BertForSequenceClassification.from_pretrained(model_name)<\/code><\/pre>\n<h2>5. Data Preprocessing<\/h2>\n<p>We need to preprocess the dataset to fit the model. We will tokenize the text data and, if necessary, add padding to adjust to a fixed length.<\/p>\n<pre><code>def preprocess_function(examples):\n    return tokenizer(examples[\"text\"], truncation=True, padding=\"max_length\", max_length=512)\n\ntrain_tokenized = train_dataset.map(preprocess_function, batched=True)\ntest_tokenized = test_dataset.map(preprocess_function, batched=True)<\/code><\/pre>\n<h2>6. Setting Up the Trainer Class<\/h2>\n<p>Now we need to define the training arguments to set up the Trainer class. Training arguments define the hyperparameters of the training process.<\/p>\n<pre><code>from transformers import Trainer, TrainingArguments\n\ntraining_args = TrainingArguments(\n    output_dir=\".\/results\",\n    evaluation_strategy=\"epoch\",\n    learning_rate=2e-5,\n    per_device_train_batch_size=8,\n    per_device_eval_batch_size=8,\n    num_train_epochs=3,\n    weight_decay=0.01,\n)\n\ntrainer = Trainer(\n    model=model,\n    args=training_args,\n    train_dataset=train_tokenized,\n    eval_dataset=test_tokenized,\n)<\/code><\/pre>\n<h2>7. Training the Model<\/h2>\n<p>Start model training. You can perform the training with the code below.<\/p>\n<pre><code>trainer.train()<\/code><\/pre>\n<h2>8. Evaluating the Model<\/h2>\n<p>After training, you can evaluate the model&#8217;s performance. Let&#8217;s check the evaluation metrics to see how well the model works.<\/p>\n<pre><code>trainer.evaluate()<\/code><\/pre>\n<h2>9. Predicting with the Model<\/h2>\n<p>Now, you can use the trained model to make predictions on new data.<\/p>\n<pre><code>def predict(texts):\n    inputs = tokenizer(texts, padding=True, truncation=True, return_tensors=\"pt\")\n    with torch.no_grad():\n        outputs = model(**inputs)\n    predictions = torch.argmax(outputs.logits, dim=-1)\n    return predictions\n\nsample_texts = [\"I love this movie!\", \"This is the worst film ever.\"]\npredictions = predict(sample_texts)\nprint(predictions)<\/code><\/pre>\n<h2>10. Conclusion<\/h2>\n<p>In this course, we learned about pre-training models using the Trainer class in the Hugging Face Transformers library. Hugging Face provides various pre-trained models to facilitate various tasks in natural language processing. We hope you have learned how to easily train and evaluate models through this example. We encourage you to continue exploring the various possibilities of Hugging Face and deep learning in the future.<\/p>\n<p>Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Deep learning is currently being utilized in various fields, among which natural language processing (NLP) is a particularly rapidly developing area. Hugging Face is well-known as a platform that provides various libraries to easily handle these deep learning models. In this course, we will explain in detail how to use the pre-training and Trainer classes &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36169\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Using Hugging Face Transformers, Pre-training the Trainer Class&#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-36169","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, Pre-training the Trainer Class - \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\/36169\/\" \/>\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, Pre-training the Trainer Class - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Deep learning is currently being utilized in various fields, among which natural language processing (NLP) is a particularly rapidly developing area. Hugging Face is well-known as a platform that provides various libraries to easily handle these deep learning models. In this course, we will explain in detail how to use the pre-training and Trainer classes &hellip; \ub354 \ubcf4\uae30 &quot;Using Hugging Face Transformers, Pre-training the Trainer Class&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36169\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:46: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\/36169\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36169\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Using Hugging Face Transformers, Pre-training the Trainer Class\",\"datePublished\":\"2024-11-01T09:46:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36169\/\"},\"wordCount\":539,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Using Hugging Face\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36169\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36169\/\",\"name\":\"Using Hugging Face Transformers, Pre-training the Trainer Class - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:46:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36169\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36169\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36169\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Hugging Face Transformers, Pre-training the Trainer Class\"}]},{\"@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, Pre-training the Trainer Class - \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\/36169\/","og_locale":"ko_KR","og_type":"article","og_title":"Using Hugging Face Transformers, Pre-training the Trainer Class - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Deep learning is currently being utilized in various fields, among which natural language processing (NLP) is a particularly rapidly developing area. Hugging Face is well-known as a platform that provides various libraries to easily handle these deep learning models. In this course, we will explain in detail how to use the pre-training and Trainer classes &hellip; \ub354 \ubcf4\uae30 \"Using Hugging Face Transformers, Pre-training the Trainer Class\"","og_url":"https:\/\/atmokpo.com\/w\/36169\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:46: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\/36169\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36169\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Using Hugging Face Transformers, Pre-training the Trainer Class","datePublished":"2024-11-01T09:46:20+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36169\/"},"wordCount":539,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Using Hugging Face"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36169\/","url":"https:\/\/atmokpo.com\/w\/36169\/","name":"Using Hugging Face Transformers, Pre-training the Trainer Class - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:46:20+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36169\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36169\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36169\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Using Hugging Face Transformers, Pre-training the Trainer Class"}]},{"@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\/36169","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=36169"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36169\/revisions"}],"predecessor-version":[{"id":36170,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36169\/revisions\/36170"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}