{"id":36181,"date":"2024-11-01T09:46:26","date_gmt":"2024-11-01T09:46:26","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36181"},"modified":"2024-11-01T09:46:26","modified_gmt":"2024-11-01T09:46:26","slug":"transformers-course-by-hugging-face-sentiment-analysis","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36181\/","title":{"rendered":"Transformers Course by Hugging Face, Sentiment Analysis"},"content":{"rendered":"<p><body><\/p>\n<p>In this article, we will learn how to perform sentiment analysis using the <strong>Hugging Face Transformers<\/strong> library, which is frequently used in Natural Language Processing (NLP). Sentiment analysis is a technique for extracting emotions or sentiments from text data and is widely used in various fields.<\/p>\n<h2>1. What are Hugging Face Transformers?<\/h2>\n<p>The Hugging Face Transformers library is a Python library that allows easy access to various pre-trained Natural Language Processing models. It supports multiple types of models, including BERT, GPT-2, T5, and is especially easy to fine-tune, enabling the adjustment of models for various tasks.<\/p>\n<h2>2. Overview of Sentiment Analysis<\/h2>\n<p>Sentiment analysis primarily includes tasks such as:<\/p>\n<ul>\n<li>The overall emotional state of a document (positive, negative, neutral)<\/li>\n<li>Detailed sentiments of product reviews<\/li>\n<li>Tracking emotions in social media posts<\/li>\n<\/ul>\n<p>Sentiment analysis can be implemented using machine learning and deep learning techniques, and the quality and quantity of training data greatly influence the results.<\/p>\n<h2>3. Setting Up the Environment<\/h2>\n<p>We will install the necessary libraries to proceed with this tutorial. Use the following command to install Hugging Face Transformers and the tokenization libraries <code>transformers<\/code> and <code>torch<\/code>.<\/p>\n<pre><code>pip install transformers torch<\/code><\/pre>\n<h2>4. Preparing the Dataset<\/h2>\n<p>We will use the famous <strong>IMDb Movie Review Dataset<\/strong> as our dataset for sentiment analysis. This dataset contains positive and negative reviews about movies.<\/p>\n<pre><code>from sklearn.datasets import fetch_openml\n\ndata = fetch_openml('IMDb', version=1)\ntexts, labels = data['data'], data['target']\n<\/code><\/pre>\n<h2>5. Data Preprocessing<\/h2>\n<p>We will preprocess the data to prepare it for input into the model. This involves cleaning the text and converting the labels into numbers.<\/p>\n<pre><code>import pandas as pd\n\ndf = pd.DataFrame({'text': texts, 'label': labels})\ndf['label'] = df['label'].apply(lambda x: 1 if x == 'pos' else 0)\ntexts = df['text'].tolist()\nlabels = df['label'].tolist()\n<\/code><\/pre>\n<h2>6. Loading the Model<\/h2>\n<p>We will load a pre-trained BERT model for sentiment analysis. Additionally, we will tokenize the text and convert it into a format suitable for model input.<\/p>\n<pre><code>from transformers import AutoTokenizer, AutoModelForSequenceClassification\n\nmodel_name = 'nlptown\/bert-base-multilingual-uncased-sentiment'\ntokenizer = AutoTokenizer.from_pretrained(model_name)\nmodel = AutoModelForSequenceClassification.from_pretrained(model_name)\n<\/code><\/pre>\n<h2>7. Text Tokenization<\/h2>\n<p>We tokenize the text so that it can be input into the model. This process involves transforming each review into an appropriate format for the model.<\/p>\n<pre><code>encodings = tokenizer(texts, truncation=True, padding=True, max_length=128, return_tensors=\"pt\")\n<\/code><\/pre>\n<h2>8. Model Training<\/h2>\n<p>To train the model, we need to perform fine-tuning on the given data. Now, we will set up the dataset using PyTorch&#8217;s data loader.<\/p>\n<pre><code>import torch\nfrom torch.utils.data import DataLoader, Dataset\n\nclass SentimentDataset(Dataset):\n    def __init__(self, encodings, labels):\n        self.encodings = encodings\n        self.labels = labels\n\n    def __getitem__(self, idx):\n        item = {key: val[idx] for key, val in self.encodings.items()}\n        item['labels'] = torch.tensor(self.labels[idx])\n        return item\n\n    def __len__(self):\n        return len(self.labels)\n\ndataset = SentimentDataset(encodings, labels)\ntrain_loader = DataLoader(dataset, batch_size=16, shuffle=True)\n<\/code><\/pre>\n<h2>9. Model Training<\/h2>\n<p>We define the loss function and optimizer to train the model, proceeding with training over multiple epochs.<\/p>\n<pre><code>from transformers import AdamW\n\noptimizer = AdamW(model.parameters(), lr=5e-5)\n\nmodel.train()\nfor epoch in range(3):\n    for batch in train_loader:\n        optimizer.zero_grad()\n        outputs = model(**batch)\n        loss = outputs.loss\n        loss.backward()\n        optimizer.step()\n        print(f'Epoch: {epoch}, Loss: {loss.item()}')\n<\/code><\/pre>\n<h2>10. Model Evaluation<\/h2>\n<p>We will evaluate the model to check its performance. We will measure accuracy and loss using the validation dataset.<\/p>\n<pre><code>model.eval()\ncorrect = 0\ntotal = 0\n\nwith torch.no_grad():\n    for batch in train_loader:\n        outputs = model(**batch)\n        predictions = outputs.logits.argmax(dim=-1)\n        correct += (predictions == batch['labels']).sum().item()\n        total += batch['labels'].size(0)\n\naccuracy = correct \/ total\nprint(f'Accuracy: {accuracy}')\n<\/code><\/pre>\n<h2>11. Making Predictions<\/h2>\n<p>Once the model is trained, we can make predictions on new data. Below is an example code to make actual predictions.<\/p>\n<pre><code>def predict_sentiment(text):\n    inputs = tokenizer(text, return_tensors='pt', truncation=True, padding=True, max_length=128)\n    with torch.no_grad():\n        outputs = model(**inputs)\n    prediction = outputs.logits.argmax(dim=-1)\n    return 'Positive' if prediction.item() == 1 else 'Negative'\n\ntest_text = \"This movie was really enjoyable!\"\nprint(f'Prediction: {predict_sentiment(test_text)}')\n<\/code><\/pre>\n<h2>12. Conclusion<\/h2>\n<p>In this article, we explored the entire process of performing sentiment analysis using the Hugging Face Transformers library. Through fine-tuning the model and predicting real data, we were able to verify the potential applications of deep learning models. We can expect to apply Hugging Face Transformers to various Natural Language Processing tasks in the future.<\/p>\n<h2>13. References<\/h2>\n<ul>\n<li>Hugging Face Documentation: <a href=\"https:\/\/huggingface.co\/docs\/transformers\" target=\"_blank\" rel=\"noopener\">https:\/\/huggingface.co\/docs\/transformers<\/a><\/li>\n<li>IMDb Dataset: <a href=\"https:\/\/www.imdb.com\/interfaces\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.imdb.com\/interfaces\/<\/a><\/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<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn how to perform sentiment analysis using the Hugging Face Transformers library, which is frequently used in Natural Language Processing (NLP). Sentiment analysis is a technique for extracting emotions or sentiments from text data and is widely used in various fields. 1. What are Hugging Face Transformers? The Hugging Face &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36181\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Transformers Course by Hugging Face, Sentiment Analysis&#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-36181","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>Transformers Course by Hugging Face, Sentiment Analysis - \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\/36181\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Transformers Course by Hugging Face, Sentiment Analysis - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"In this article, we will learn how to perform sentiment analysis using the Hugging Face Transformers library, which is frequently used in Natural Language Processing (NLP). Sentiment analysis is a technique for extracting emotions or sentiments from text data and is widely used in various fields. 1. What are Hugging Face Transformers? The Hugging Face &hellip; \ub354 \ubcf4\uae30 &quot;Transformers Course by Hugging Face, Sentiment Analysis&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36181\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:46: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\/36181\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36181\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Transformers Course by Hugging Face, Sentiment Analysis\",\"datePublished\":\"2024-11-01T09:46:26+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36181\/\"},\"wordCount\":468,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Using Hugging Face\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36181\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36181\/\",\"name\":\"Transformers Course by Hugging Face, Sentiment Analysis - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:46:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36181\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36181\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36181\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Transformers Course by Hugging Face, Sentiment Analysis\"}]},{\"@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":"Transformers Course by Hugging Face, Sentiment Analysis - \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\/36181\/","og_locale":"ko_KR","og_type":"article","og_title":"Transformers Course by Hugging Face, Sentiment Analysis - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"In this article, we will learn how to perform sentiment analysis using the Hugging Face Transformers library, which is frequently used in Natural Language Processing (NLP). Sentiment analysis is a technique for extracting emotions or sentiments from text data and is widely used in various fields. 1. What are Hugging Face Transformers? The Hugging Face &hellip; \ub354 \ubcf4\uae30 \"Transformers Course by Hugging Face, Sentiment Analysis\"","og_url":"https:\/\/atmokpo.com\/w\/36181\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:46: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\/36181\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36181\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Transformers Course by Hugging Face, Sentiment Analysis","datePublished":"2024-11-01T09:46:26+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36181\/"},"wordCount":468,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Using Hugging Face"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36181\/","url":"https:\/\/atmokpo.com\/w\/36181\/","name":"Transformers Course by Hugging Face, Sentiment Analysis - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:46:26+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36181\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36181\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36181\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Transformers Course by Hugging Face, Sentiment Analysis"}]},{"@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\/36181","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=36181"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36181\/revisions"}],"predecessor-version":[{"id":36182,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36181\/revisions\/36182"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}