{"id":36209,"date":"2024-11-01T09:46:40","date_gmt":"2024-11-01T09:46:40","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36209"},"modified":"2024-11-01T09:46:40","modified_gmt":"2024-11-01T09:46:40","slug":"hugging-face-transformers-tutorial-loading-pre-trained-bert-for-ensemble-training","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36209\/","title":{"rendered":"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble Training"},"content":{"rendered":"<p><body><\/p>\n<p>Today, we will learn how to apply the most commonly used <strong>BERT (Bidirectional Encoder Representations from Transformers)<\/strong> model in ensemble training. In this process, we will explain how to load a pre-trained BERT model using the <strong>Hugging Face Transformers library<\/strong> and build an ensemble model based on it.<\/p>\n<h2>How does BERT work?<\/h2>\n<p>The BERT model is a model that performs bidirectional transfer learning to understand context. This means that it considers how the words on the left and right of the input sentence form the context simultaneously. Through this, it can understand the meaning of words in a deeper way. BERT is pre-trained in an unsupervised manner on large amounts of text data and can be fine-tuned for various downstream tasks.<\/p>\n<h2>Installing the Hugging Face Library<\/h2>\n<p>The Hugging Face Transformers library makes it easy to use various pre-trained models like BERT. To install the library, you can run the command below:<\/p>\n<pre><code>pip install transformers torch<\/code><\/pre>\n<h2>Loading a Pre-trained BERT Model<\/h2>\n<p>Now we will load a pre-trained BERT model using the Hugging Face Transformers library. The code below is a simple example that loads the BERT model and its tokenizer.<\/p>\n<pre><code>\nfrom transformers import BertTokenizer, BertModel\n\n# Loading BERT tokenizer and model\ntokenizer = BertTokenizer.from_pretrained('bert-base-uncased')\nmodel = BertModel.from_pretrained('bert-base-uncased')\n\n# Test sentence\ntext = \"Hello, how are you?\"\ninputs = tokenizer(text, return_tensors='pt')\n\n# Inputting into BERT model and getting output\noutputs = model(**inputs)\nprint(outputs)\n    <\/code><\/pre>\n<h3>Code Explanation<\/h3>\n<ul>\n<li><code>from transformers import BertTokenizer, BertModel<\/code>: Imports BERT tokenizer and model from Hugging Face&#8217;s Transformers.<\/li>\n<li><code>tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')<\/code>: Loads a pre-trained BERT tokenizer.<\/li>\n<li><code>model = BertModel.from_pretrained('bert-base-uncased')<\/code>: Loads a pre-trained BERT model.<\/li>\n<li><code>inputs = tokenizer(text, return_tensors='pt')<\/code>: Tokenizes the input sentence and converts it to a PyTorch tensor.<\/li>\n<li><code>outputs = model(**inputs)<\/code>: Inputs it to the model and gets the output.<\/li>\n<\/ul>\n<h2>Overview of Ensemble Training<\/h2>\n<p>Ensemble learning is a method to improve final prediction performance by combining the prediction results of multiple models. It allows for more reliable prediction results by merging the strengths of various learning models. Generally, there can be several techniques in ensemble learning, with Bagging and Boosting being widely used.<\/p>\n<h2>Building Ensemble Models with BERT<\/h2>\n<p>Now let&#8217;s see how to construct an ensemble model using the BERT model. We will train multiple BERT models and combine their predictions to derive the final prediction.<\/p>\n<h3>Model Overview<\/h3>\n<p>We will construct the ensemble model with the following structure:<\/p>\n<ul>\n<li>Creating and training multiple BERT models<\/li>\n<li>Collecting the prediction results of each model<\/li>\n<li>Combining the prediction results to generate the final prediction<\/li>\n<\/ul>\n<h3>Preparing the Dataset<\/h3>\n<p>We will use a simple text classification problem. For example, we can assume problems like email spam filtering. First, we will prepare a convenient dataset as follows.<\/p>\n<pre><code>\nimport pandas as pd\n\n# Creating an example dataset\ndata = {'text': [\"Free money now\", \"Hello friend, how are you?\", \"Limited time offer\", \"Nice to see you\"],\n        'label': [1, 0, 1, 0]}  # 1: Spam, 0: Regular mail\ndf = pd.DataFrame(data)\n    <\/code><\/pre>\n<h3>Training the Model and Performing Ensemble<\/h3>\n<p>Now let&#8217;s proceed to train each BERT model. The trained models will be saved for ensemble purposes.<\/p>\n<pre><code>\nfrom sklearn.model_selection import train_test_split\nimport torch\n\n# Splitting the data\ntrain_texts, test_texts, train_labels, test_labels = train_test_split(df['text'], df['label'], test_size=0.2, random_state=42)\n\n# Preparing data for BERT model\ntrain_encodings = tokenizer(list(train_texts), truncation=True, padding=True, return_tensors='pt')\ntest_encodings = tokenizer(list(test_texts), truncation=True, padding=True, return_tensors='pt')\n\nclass BERTClassifier(torch.nn.Module):\n    def __init__(self):\n        super(BERTClassifier, self).__init__()\n        self.bert = BertModel.from_pretrained('bert-base-uncased')\n        self.classifier = torch.nn.Linear(self.bert.config.hidden_size, 2)  # 2 classes (spam, non-spam)\n\n    def forward(self, input_ids, attention_mask):\n        output = self.bert(input_ids, attention_mask=attention_mask)[1]\n        return self.classifier(output)\n\n# Declaring model and optimizer\nmodel1 = BERTClassifier()\nmodel2 = BERTClassifier()  # Example for the second model\noptimizer = torch.optim.Adam(model1.parameters(), lr=5e-5)\n\n# Simple training loop\nmodel1.train()\nfor epoch in range(3):  # 3 epochs\n    optimizer.zero_grad()\n    outputs = model1(input_ids=train_encodings['input_ids'], attention_mask=train_encodings['attention_mask'])\n    loss = torch.nn.CrossEntropyLoss()(outputs, torch.tensor(train_labels.values))\n    loss.backward()\n    optimizer.step()\n    print(f'Epoch {epoch + 1}, Loss: {loss.item()}')\n    # Train model2 in the same way\n\n# Saving the model\ntorch.save(model1.state_dict(), 'bert_model1.pth')\ntorch.save(model2.state_dict(), 'bert_model2.pth')\n    <\/code><\/pre>\n<h2>Performing Predictions and Ensemble Results<\/h2>\n<p>Once model training is complete, we can combine the prediction results of each model to generate the final prediction values.<\/p>\n<pre><code>\n# Defining the prediction function\ndef predict(model, encodings):\n    model.eval()\n    with torch.no_grad():\n        outputs = model(input_ids=encodings['input_ids'], attention_mask=encodings['attention_mask'])\n    return torch.argmax(outputs, dim=1)\n\n# Loading models\nmodel1.load_state_dict(torch.load('bert_model1.pth'))\nmodel2.load_state_dict(torch.load('bert_model2.pth'))\n\n# Individual model predictions\npreds_model1 = predict(model1, test_encodings)\npreds_model2 = predict(model2, test_encodings)\n\n# Ensemble prediction\nfinal_preds = (preds_model1 + preds_model2) \/ 2\nfinal_preds = (final_preds > 0.5).int()  # Using 0.5 as the threshold for binary prediction\nprint(f'Final Prediction: {final_preds}')\n    <\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Today, we explored how to load a pre-trained BERT model using the Hugging Face Transformers library and examined a simple ensemble training method based on it. BERT shows excellent performance in complex natural language processing tasks, and we can expect even better performance when utilizing ensemble techniques. In the future, consider applying these techniques to various tasks to achieve better results.<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/huggingface.co\/docs\/transformers\/index\">Hugging Face Transformers Documentation<\/a><\/li>\n<li><a href=\"https:\/\/arxiv.org\/pdf\/1810.04805.pdf\">BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today, we will learn how to apply the most commonly used BERT (Bidirectional Encoder Representations from Transformers) model in ensemble training. In this process, we will explain how to load a pre-trained BERT model using the Hugging Face Transformers library and build an ensemble model based on it. How does BERT work? The BERT model &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36209\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble 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-36209","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>Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble 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\/36209\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble Training - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Today, we will learn how to apply the most commonly used BERT (Bidirectional Encoder Representations from Transformers) model in ensemble training. In this process, we will explain how to load a pre-trained BERT model using the Hugging Face Transformers library and build an ensemble model based on it. How does BERT work? The BERT model &hellip; \ub354 \ubcf4\uae30 &quot;Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble Training&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36209\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:46:40+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=\"5\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/36209\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36209\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble Training\",\"datePublished\":\"2024-11-01T09:46:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36209\/\"},\"wordCount\":513,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Using Hugging Face\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36209\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36209\/\",\"name\":\"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble Training - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:46:40+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36209\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36209\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36209\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble 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":"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble 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\/36209\/","og_locale":"ko_KR","og_type":"article","og_title":"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble Training - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Today, we will learn how to apply the most commonly used BERT (Bidirectional Encoder Representations from Transformers) model in ensemble training. In this process, we will explain how to load a pre-trained BERT model using the Hugging Face Transformers library and build an ensemble model based on it. How does BERT work? The BERT model &hellip; \ub354 \ubcf4\uae30 \"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble Training\"","og_url":"https:\/\/atmokpo.com\/w\/36209\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:46:40+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":"5\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/36209\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36209\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble Training","datePublished":"2024-11-01T09:46:40+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36209\/"},"wordCount":513,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Using Hugging Face"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36209\/","url":"https:\/\/atmokpo.com\/w\/36209\/","name":"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble Training - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:46:40+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36209\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36209\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36209\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Hugging Face Transformers Tutorial, Loading Pre-trained BERT for Ensemble 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\/36209","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=36209"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36209\/revisions"}],"predecessor-version":[{"id":36210,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36209\/revisions\/36210"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}