{"id":8729,"date":"2024-01-11T06:00:30","date_gmt":"2024-01-11T14:00:30","guid":{"rendered":"https:\/\/live-cometml.pantheonsite.io\/?p=8729"},"modified":"2025-04-24T17:03:34","modified_gmt":"2025-04-24T17:03:34","slug":"how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning","status":"publish","type":"post","link":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning\/","title":{"rendered":"How I Leveraged the Alpaca Dataset to Fine-Tune the Llama2 Model Based On Contrastive\/Few-Shot Learning"},"content":{"rendered":"\n<p class=\"graf graf--p\">With the arrival of the Llama-2 model, several articles have been published to describe the nuances of fine-tuning it in a consumer-grade GPU. In many of those tutorials, the authors would have touched upon how critical the dataset&#8217;s quality is for fine-tuning. But only some of them would have delved deeper into how we can curate the high-quality, focused dataset for the fine-tuning purpose of specialized LLMs. In this article, I will explain how I leveraged the industry standard Alpaca dataset towards building a quality dataset for fine-tuning food-based LLMs.<\/p>\n\n\n\n<p class=\"graf graf--p\">Let&#8217;s get started\u2026<\/p>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*O7-1QEy4cKvSpKlHkaKP8Q.png\" alt=\"llama head over the meta logo and the number 2\"\/><figcaption class=\"wp-element-caption\">Image <a class=\"markup--anchor markup--figure-anchor\" href=\"https:\/\/aibusiness.com\/nlp\/meta-offers-companies-free-use-of-llama-2-language-model\" target=\"_blank\" rel=\"noopener ugc nofollow\" data-href=\"https:\/\/aibusiness.com\/nlp\/meta-offers-companies-free-use-of-llama-2-language-model\">Src<\/a><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Alpaca Dataset\u200a\u2014\u200aOverview<\/h3>\n\n\n\n<p class=\"graf graf--p\">Alpaca is a dataset of 52,000 instructions generated by OpenAI&#8217;s <code class=\"markup--code markup--p-code\">text-davinci-003<\/code> engine. These 52K instructions span different domains such as text summarization, fashion, maths, food, etc., and they are widely employed to fine-tune LLM models.<\/p>\n\n\n\n<p class=\"graf graf--p\">You can find more details about the Alpaca dataset <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/github.com\/gururise\/AlpacaDataCleaned\/tree\/main\" target=\"_blank\" rel=\"noopener ugc nofollow\" data-href=\"https:\/\/github.com\/gururise\/AlpacaDataCleaned\/tree\/main\">here<\/a>.<\/p>\n\n\n\n<p class=\"graf graf--p\">The Alpaca dataset has been created with the consideration of having diverse datasets covering various kinds of activities that LLMs could be used for. The image below summarizes how diverse the Alpaca instructions are, and using such a diverse dataset for fine-tuning any specific purpose LLMs may not be helpful.<\/p>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*bAEt6mM8lkmxVjZ5pREKzg.png\" alt=\"wheel of alpaca dataset\"\/><figcaption class=\"wp-element-caption\">src: <a class=\"markup--anchor markup--figure-anchor\" href=\"https:\/\/github.com\/gururise\/AlpacaDataCleaned\/blob\/main\/assets\/parse_analysis.png\" target=\"_blank\" rel=\"noopener ugc nofollow\" data-href=\"https:\/\/github.com\/gururise\/AlpacaDataCleaned\/blob\/main\/assets\/parse_analysis.png\">https:\/\/github.com\/gururise\/AlpacaDataCleaned\/blob\/main\/assets\/parse_analysis.png<\/a><\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">So, to build any specific LLMs, we could extract the questions related to that particular domain from the base set. And I wanted to build an LLM about the &#8216;food&#8217; domain. Hence, I considered making a text classification model to identify food-related instructions from the base alpaca dataset.<\/p>\n\n\n\n<p class=\"graf graf--p\">Rather than building a text classification model from scratch, I considered exploring &#8216;few shot learning&#8217; models, and the results were outstanding. Let&#8217;s learn more about &#8216;few-shot learning&#8217; and &#8216;contrastive learning.&#8217;<\/p>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Contrastive Learning &amp; Few Shot&nbsp;Learning<\/h3>\n\n\n\n<p class=\"graf graf--p\">Contrastive learning is a branch of machine learning where we fine-tune an already trained model with a few samples of a specific problem with positive and negative samples\u200a\u2014\u200asuch that the fine-tuned model can perform classification tasks more accurately.<\/p>\n\n\n\n<p class=\"graf graf--p\">I have employed this approach towards extracting food-related datasets from Alpaca&#8217;s diverse dataset by leveraging the SetFit model. You can refer to how the &#8216;SetFit&#8217; model helps with few shot learning in this detailed <a href=\"https:\/\/huggingface.co\/blog\/setfit\">HuggingFace blog post<\/a>.<\/p>\n\n\n\n<p class=\"graf graf--p\">As mentioned in the above blog, SetFit helps train the text classification models with a handful of data. It leverages sentence transformers to embed the text data and fine-tunes the head layer to perform the classification task.<\/p>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/0*obGzb1A0ceN503wD.png\" alt=\"SetFit's two-stage training process\"\/><figcaption class=\"wp-element-caption\">SetFit&#8217;s two-stage training process\u200a\u2014\u200a<a class=\"markup--anchor markup--figure-anchor\" href=\"https:\/\/huggingface.co\/blog\/setfit\" target=\"_blank\" rel=\"noopener ugc nofollow\" data-href=\"https:\/\/huggingface.co\/blog\/setfit\">src<\/a><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Few-Shot Training\u200a\u2014\u200aData Preparation<\/h3>\n\n\n\n<p class=\"graf graf--p\">As explained, we are all set to train the SetFit model with a handful of data. So, I have curated the dataset with exactly 8 counts of +ve and -ve classes.<\/p>\n\n\n\n<p class=\"graf graf--p\">Here, the objective is to extract the food-related data from the Alpaca dataset. So, the +ve class is food-related data, and the -ve class is all other data. I have prepared 8 counts of data with food information and another 8 counts with diverse topics such as quantum computers, psychology, fashion, etc.<\/p>\n\n\n\n<p class=\"graf graf--p\">I have used the hyper-parameter config as below, with 20 iterations of training and &#8216;CosineSimilarityLoss&#8217; as a loss function:<\/p>\n\n\n\n<figure class=\"wp-block-image graf graf--figure\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*yYO8j675P9-lEb0XYK6maA.png\" alt=\"code of hyperparameter config for few-shot\u00a0training\"\/><figcaption class=\"wp-element-caption\">Hyperparameter config for few-shot&nbsp;training<\/figcaption><\/figure>\n\n\n\n<p>&nbsp;<\/p>\n\n\n\n<p class=\"graf graf--p\">Post-training, the model can identify the food-related data from the rest and predict the Alpaca dataset. I was able to extract 1000+ records related to food, and most of the classifications were spot on.<\/p>\n\n\n\n<p class=\"graf graf--p\">With the extracted data, I have fine-tuned the LLM, and the results of the LLM model are decent, considering the number of epochs that I have trained.<\/p>\n\n\n\n<p class=\"graf graf--p\">You can refer to <a href=\"https:\/\/github.com\/databricks\/databricks-ml-examples\/blob\/master\/llm-models\/llamav2\/llamav2-7b\/05_fine_tune_deepspeed.py\">this code<\/a> from the official &#8216;databricks&#8217; repository about fine-tuning Llama models with custom datasets in your environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading graf graf--h3\">Summary<\/h3>\n\n\n\n<p class=\"graf graf--p\">Thus, at the end of the article, we summarize how I leveraged the few-shot learning approach to extract the &#8216;food&#8217; related instruction dataset from Alpaca and how it helped me build a custom LLM model for my food-related needs.<\/p>\n\n\n\n<p class=\"graf graf--p\">Please follow my handle for more insightful articles about LLMs and their applications. Happy Reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the arrival of the Llama-2 model, several articles have been published to describe the nuances of fine-tuning it in a consumer-grade GPU. In many of those tutorials, the authors would have touched upon how critical the dataset&#8217;s quality is for fine-tuning. But only some of them would have delved deeper into how we can [&hellip;]<\/p>\n","protected":false},"author":118,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"customer_name":"","customer_description":"","customer_industry":"","customer_technologies":"","customer_logo":"","footnotes":""},"categories":[7],"tags":[],"coauthors":[215],"class_list":["post-8729","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v25.9 (Yoast SEO v25.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Leverage the Alpaca Dataset to Fine-Tune the Llama2 Model<\/title>\n<meta name=\"description\" content=\"Learn how to leverage the industry standard Alpaca dataset towards building a quality dataset for fine-tuning food-based LLMs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How I Leveraged the Alpaca Dataset to Fine-Tune the Llama2 Model Based On Contrastive\/Few-Shot Learning\" \/>\n<meta property=\"og:description\" content=\"Learn how to leverage the industry standard Alpaca dataset towards building a quality dataset for fine-tuning food-based LLMs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning\" \/>\n<meta property=\"og:site_name\" content=\"Comet\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cometdotml\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-11T14:00:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-24T17:03:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*O7-1QEy4cKvSpKlHkaKP8Q.png\" \/>\n<meta name=\"author\" content=\"Vasanthkumar Velayudham\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Cometml\" \/>\n<meta name=\"twitter:site\" content=\"@Cometml\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vasanthkumar Velayudham\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Leverage the Alpaca Dataset to Fine-Tune the Llama2 Model","description":"Learn how to leverage the industry standard Alpaca dataset towards building a quality dataset for fine-tuning food-based LLMs.","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:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning","og_locale":"en_US","og_type":"article","og_title":"How I Leveraged the Alpaca Dataset to Fine-Tune the Llama2 Model Based On Contrastive\/Few-Shot Learning","og_description":"Learn how to leverage the industry standard Alpaca dataset towards building a quality dataset for fine-tuning food-based LLMs.","og_url":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning","og_site_name":"Comet","article_publisher":"https:\/\/www.facebook.com\/cometdotml","article_published_time":"2024-01-11T14:00:30+00:00","article_modified_time":"2025-04-24T17:03:34+00:00","og_image":[{"url":"https:\/\/cdn-images-1.medium.com\/max\/800\/1*O7-1QEy4cKvSpKlHkaKP8Q.png","type":"","width":"","height":""}],"author":"Vasanthkumar Velayudham","twitter_card":"summary_large_image","twitter_creator":"@Cometml","twitter_site":"@Cometml","twitter_misc":{"Written by":"Vasanthkumar Velayudham","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning#article","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning\/"},"author":{"name":"Vasanthkumar Velayudham","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/eafd525d6dca0813f547b678598af186"},"headline":"How I Leveraged the Alpaca Dataset to Fine-Tune the Llama2 Model Based On Contrastive\/Few-Shot Learning","datePublished":"2024-01-11T14:00:30+00:00","dateModified":"2025-04-24T17:03:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning\/"},"wordCount":674,"publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning#primaryimage"},"thumbnailUrl":"https:\/\/cdn-images-1.medium.com\/max\/800\/1*O7-1QEy4cKvSpKlHkaKP8Q.png","articleSection":["Tutorials"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning\/","url":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning","name":"Leverage the Alpaca Dataset to Fine-Tune the Llama2 Model","isPartOf":{"@id":"https:\/\/www.comet.com\/site\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning#primaryimage"},"image":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning#primaryimage"},"thumbnailUrl":"https:\/\/cdn-images-1.medium.com\/max\/800\/1*O7-1QEy4cKvSpKlHkaKP8Q.png","datePublished":"2024-01-11T14:00:30+00:00","dateModified":"2025-04-24T17:03:34+00:00","description":"Learn how to leverage the industry standard Alpaca dataset towards building a quality dataset for fine-tuning food-based LLMs.","breadcrumb":{"@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning#primaryimage","url":"https:\/\/cdn-images-1.medium.com\/max\/800\/1*O7-1QEy4cKvSpKlHkaKP8Q.png","contentUrl":"https:\/\/cdn-images-1.medium.com\/max\/800\/1*O7-1QEy4cKvSpKlHkaKP8Q.png"},{"@type":"BreadcrumbList","@id":"https:\/\/www.comet.com\/site\/blog\/how-i-leveraged-the-alpaca-dataset-to-fine-tune-the-llama2-model-based-on-contrastive-few-shot-learning#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.comet.com\/site\/"},{"@type":"ListItem","position":2,"name":"How I Leveraged the Alpaca Dataset to Fine-Tune the Llama2 Model Based On Contrastive\/Few-Shot Learning"}]},{"@type":"WebSite","@id":"https:\/\/www.comet.com\/site\/#website","url":"https:\/\/www.comet.com\/site\/","name":"Comet","description":"Build Better Models Faster","publisher":{"@id":"https:\/\/www.comet.com\/site\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.comet.com\/site\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.comet.com\/site\/#organization","name":"Comet ML, Inc.","alternateName":"Comet","url":"https:\/\/www.comet.com\/site\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/","url":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","contentUrl":"https:\/\/www.comet.com\/site\/wp-content\/uploads\/2025\/01\/logo_comet_square.png","width":310,"height":310,"caption":"Comet ML, Inc."},"image":{"@id":"https:\/\/www.comet.com\/site\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/cometdotml","https:\/\/x.com\/Cometml","https:\/\/www.youtube.com\/channel\/UCmN63HKvfXSCS-UwVwmK8Hw"]},{"@type":"Person","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/eafd525d6dca0813f547b678598af186","name":"Vasanthkumar Velayudham","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.comet.com\/site\/#\/schema\/person\/image\/0785408c3140acca85d8c056c56593fd","url":"https:\/\/secure.gravatar.com\/avatar\/2fbaa1f3b0905bc155c13a020f05668f43a66aa55a401d4e335355c501bdccb1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2fbaa1f3b0905bc155c13a020f05668f43a66aa55a401d4e335355c501bdccb1?s=96&d=mm&r=g","caption":"Vasanthkumar Velayudham"},"url":"https:\/\/www.comet.com\/site\/blog\/author\/vvk-victorygmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/users\/118"}],"replies":[{"embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/comments?post=8729"}],"version-history":[{"count":1,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8729\/revisions"}],"predecessor-version":[{"id":15406,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/posts\/8729\/revisions\/15406"}],"wp:attachment":[{"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/media?parent=8729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/categories?post=8729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/tags?post=8729"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.comet.com\/site\/wp-json\/wp\/v2\/coauthors?post=8729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}