{"id":2550,"date":"2025-03-21T07:02:25","date_gmt":"2025-03-21T07:02:25","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2025\/03\/21\/r-e-d-scaling-text-classification-with-expert-delegation\/"},"modified":"2025-03-21T07:02:25","modified_gmt":"2025-03-21T07:02:25","slug":"r-e-d-scaling-text-classification-with-expert-delegation","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2025\/03\/21\/r-e-d-scaling-text-classification-with-expert-delegation\/","title":{"rendered":"R.E.D.: Scaling Text Classification with Expert Delegation"},"content":{"rendered":"<p>    R.E.D.: Scaling Text Classification with Expert Delegation<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<p class=\"wp-block-paragraph\" id=\"dd12\">With the new age of problem-solving augmented by Large Language Models (LLMs), only a handful of problems remain that have subpar solutions. Most classification problems (at a PoC level) can be solved by leveraging LLMs at 70\u201390% Precision\/F1 with just good prompt engineering techniques, as well as adaptive in-context-learning (ICL) examples.<\/p>\n<p class=\"wp-block-paragraph\" id=\"a9c3\">What happens when you want to consistently achieve performance\u00a0<strong>higher<\/strong> than that \u2014 when prompt engineering no longer suffices?<\/p>\n<h2 class=\"wp-block-heading\" id=\"e92c\">The classification conundrum<\/h2>\n<p class=\"wp-block-paragraph\" id=\"7669\">Text classification is one of the oldest and most well-understood examples of supervised learning. Given this premise, it should\u00a0really\u00a0not be hard to build robust, well-performing classifiers that handle a large number of input classes, right\u2026?<\/p>\n<p class=\"wp-block-paragraph\" id=\"6a88\">Welp. It is.<\/p>\n<p class=\"wp-block-paragraph\" id=\"971d\">It actually has to do a lot more with the \u2018constraints\u2019 that the algorithm is generally expected to work under:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">low amount of training data per class<\/li>\n<li class=\"wp-block-list-item\">high classification accuracy (that plummets as you add more classes)<\/li>\n<li class=\"wp-block-list-item\">possible addition of\u00a0<strong>new classes<\/strong>\u00a0to an existing subset of classes<\/li>\n<li class=\"wp-block-list-item\">quick training\/inference<\/li>\n<li class=\"wp-block-list-item\">cost-effectiveness<\/li>\n<li class=\"wp-block-list-item\">(potentially) really large number of training classes<\/li>\n<li class=\"wp-block-list-item\">(potentially) endless\u00a0<strong>required\u00a0<\/strong>retraining of\u00a0<em>some<\/em>\u00a0classes due to data drift, etc.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\" id=\"4076\">Ever tried building a classifier beyond a few dozen classes under these conditions? (I mean, even GPT could probably do a great job up to ~30 text classes with just a few samples\u2026)<\/p>\n<p class=\"wp-block-paragraph\" id=\"9a91\"><span style=\"margin: 0px; padding: 0px;\">Considering you take the GPT route \u2014 If you have more than a couple dozen classes or a sizeable amount of data to be classified, you are gonna have to reach deep into your pockets with the system prompt, user prompt, few shot example tokens that you will need to classify\u00a0<strong>one sample.<\/strong><\/span><strong>\u00a0<\/strong>That is after making peace with the throughput of the API, even if you are running async queries.<\/p>\n<p class=\"wp-block-paragraph\" id=\"56ca\">In applied ML, problems like these are generally tricky to solve since they don\u2019t fully satisfy the requirements of supervised learning or aren\u2019t cheap\/fast enough to be run via an LLM. This particular pain point is what the R.E.D algorithm addresses: semi-supervised learning, when the training data per class is not enough to build (quasi)traditional classifiers.<\/p>\n<h2 class=\"wp-block-heading\" id=\"4542\">The R.E.D. algorithm<\/h2>\n<p class=\"wp-block-paragraph\" id=\"d277\"><strong>R.E.D: <a href=\"https:\/\/towardsdatascience.com\/tag\/recursive\/\" title=\"Recursive\">Recursive<\/a> Expert Delegation<\/strong>\u00a0is a novel framework that changes how we approach text classification. This is an applied ML paradigm \u2014 i.e., there is no\u00a0<em>fundamentally different<\/em>\u00a0architecture to what exists, but its a highlight reel of ideas that work best to build something that is practical and scalable.<\/p>\n<p class=\"wp-block-paragraph\" id=\"7e07\">In this post, we will be working through a specific example where we have a large number of text classes (100\u20131000), each class only has few samples (30\u2013100), and there are a non-trivial number of samples to classify (10,000\u2013100,000). We approach this as a\u00a0<strong>semi-supervised learning<\/strong>\u00a0problem via R.E.D.<\/p>\n<p class=\"wp-block-paragraph\" id=\"cb11\">Let\u2019s dive in.<\/p>\n<h2 class=\"wp-block-heading\" id=\"0831\">How it works<\/h2>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/03\/RED-1.png?ssl=1\" alt=\"\" class=\"wp-image-599767\"><figcaption class=\"wp-element-caption\">simple representation of what R.E.D. does<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"c672\">Instead of having a single classifier classify between a large number of classes, R.E.D. intelligently:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<strong>Divides and conquers\u00a0<\/strong>\u2014 Break the label space (large number of input labels) into multiple subsets of labels. This is a greedy label subset formation approach.<\/li>\n<li class=\"wp-block-list-item\">\n<strong>Learns efficiently<\/strong>\u00a0\u2014 Trains specialized classifiers for each subset. This step focuses on building a classifier that oversamples on noise, where noise is intelligently modeled as data from\u00a0<em>other subsets.<\/em>\n<\/li>\n<li class=\"wp-block-list-item\">\n<strong>Delegates to an expert<\/strong>\u00a0\u2014 Employes LLMs as expert oracles for specific label validation and correction only, similar to having a team of domain experts. Using an LLM as a proxy, it empirically \u2018mimics\u2019\u00a0<strong>how\u00a0<\/strong>a human expert validates an output.<\/li>\n<li class=\"wp-block-list-item\">\n<strong>Recursive retraining\u00a0<\/strong>\u2014 Continuously retrains with fresh samples added back from the expert until there are no more samples to be added\/a saturation from information gain is achieved<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\" id=\"eabb\">The intuition behind it is not very hard to grasp:\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Active_learning_(machine_learning)\" target=\"_blank\" rel=\"noreferrer noopener\">Active Learning<\/a>\u00a0employs humans as domain experts to consistently \u2018correct\u2019 or \u2018validate\u2019 the outputs from an ML model, with continuous training. This stops when the model achieves acceptable performance. We intuit and rebrand the same, with a few clever innovations that will be detailed in a research pre-print later.<\/p>\n<p class=\"wp-block-paragraph\" id=\"239e\">Let\u2019s take a deeper look\u2026<\/p>\n<h3 class=\"wp-block-heading\" id=\"d592\">Greedy subset selection with least similar elements<\/h3>\n<p class=\"wp-block-paragraph\" id=\"6315\">When the number of input labels (classes) is high, the complexity of learning a linear decision boundary between classes increases. As such, the quality of the classifier deteriorates as the number of classes increases. This is especially true when the classifier does not have enough\u00a0<strong>samples<\/strong>\u00a0to learn from \u2014 i.e. each of the training classes has only a few samples.<\/p>\n<p class=\"wp-block-paragraph\" id=\"ab12\">This is very reflective of a real-world scenario, and the primary motivation behind the creation of R.E.D.<\/p>\n<p class=\"wp-block-paragraph\" id=\"8bc7\">Some ways of improving a classifier\u2019s performance under these constraints:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<strong>Restrict\u00a0<\/strong>the number of classes a classifier needs to classify between<\/li>\n<li class=\"wp-block-list-item\">Make the decision boundary between classes clearer, i.e., train the classifier on\u00a0<strong>highly dissimilar classes<\/strong>\n<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\" id=\"1fd5\">Greedy Subset Selection does exactly this \u2014 since the scope of the problem is <a href=\"https:\/\/towardsdatascience.com\/tag\/text-classification\/\" title=\"Text Classification\">Text Classification<\/a>, we form embeddings of the training labels, reduce their dimensionality via UMAP, then form\u00a0<strong><em>S<\/em><\/strong>\u00a0subsets from them. Each of the\u00a0<strong><em>S\u00a0<\/em><\/strong>subsets has elements as\u00a0<strong><em>n\u00a0<\/em><\/strong>training labels. We pick training labels greedily, ensuring that every label we pick for the subset is the most dissimilar label w.r.t. the other labels that exist in the subset:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">import numpy as np\nfrom sklearn.metrics.pairwise import cosine_similarity\n\n\ndef avg_embedding(candidate_embeddings):\n    return np.mean(candidate_embeddings, axis=0)\n\ndef get_least_similar_embedding(target_embedding, candidate_embeddings):\n    similarities = cosine_similarity(target_embedding, candidate_embeddings)\n    least_similar_index = np.argmin(similarities)  # Use argmin to find the index of the minimum\n    least_similar_element = candidate_embeddings[least_similar_index]\n    return least_similar_element\n\n\ndef get_embedding_class(embedding, embedding_map):\n    reverse_embedding_map = {value: key for key, value in embedding_map.items()}\n    return reverse_embedding_map.get(embedding)  # Use .get() to handle missing keys gracefully\n\n\ndef select_subsets(embeddings, n):\n    visited = {cls: False for cls in embeddings.keys()}\n    subsets = []\n    current_subset = []\n\n    while any(not visited[cls] for cls in visited):\n        for cls, average_embedding in embeddings.items():\n            if not current_subset:\n                current_subset.append(average_embedding)\n                visited[cls] = True\n            elif len(current_subset) &gt;= n:\n                subsets.append(current_subset.copy())\n                current_subset = []\n            else:\n                subset_average = avg_embedding(current_subset)\n                remaining_embeddings = [emb for cls_, emb in embeddings.items() if not visited[cls_]]\n                if not remaining_embeddings:\n                    break # handle edge case\n                \n                least_similar = get_least_similar_embedding(target_embedding=subset_average, candidate_embeddings=remaining_embeddings)\n\n                visited_class = get_embedding_class(least_similar, embeddings)\n\n                \n                if visited_class is not None:\n                  visited[visited_class] = True\n\n\n                current_subset.append(least_similar)\n    \n    if current_subset:  # Add any remaining elements in current_subset\n        subsets.append(current_subset)\n        \n\n    return subsets<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"ef93\">the result of this greedy subset sampling is all the training labels clearly boxed into subsets, where each subset has at most only\u00a0<strong><em>n\u00a0<\/em><\/strong>classes. This inherently makes the job of a classifier easier, compared to the original\u00a0<strong><em>S\u00a0<\/em><\/strong>classes it would have to classify between otherwise!<\/p>\n<h3 class=\"wp-block-heading\" id=\"a338\">Semi-supervised classification with noise oversampling<\/h3>\n<p class=\"wp-block-paragraph\" id=\"d663\">Cascade this after the initial label subset formation \u2014 i.e., this classifier is only classifying between a given\u00a0<strong>subset\u00a0<\/strong>of classes.<\/p>\n<p class=\"wp-block-paragraph\" id=\"acf4\">Picture this: when you have low amounts of training data, you absolutely cannot create a hold-out set that is meaningful for evaluation. Should you do it at all? How do you know if your classifier is working well?<\/p>\n<p class=\"wp-block-paragraph\" id=\"2c77\">We approached this problem slightly differently \u2014 we defined the fundamental job of a semi-supervised classifier to be\u00a0<strong>pre-emptive<\/strong> classification of a sample. This means that regardless of what a sample gets classified as it will be \u2018verified\u2019 and \u2018corrected\u2019 at a later stage: this classifier only needs to identify what needs to be verified.<\/p>\n<p class=\"wp-block-paragraph\" id=\"bceb\">As such, we created a design for how it would treat its data:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<strong><em>n+1<\/em><\/strong><em>\u00a0<\/em>classes, where the last class is\u00a0<strong>noise<\/strong>\n<\/li>\n<li class=\"wp-block-list-item\">\n<strong>noise:\u00a0<\/strong>data from classes that are NOT in the current classifier\u2019s purview. The noise class is oversampled to be 2x the average size of the data for the classifier\u2019s labels<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\" id=\"9292\">Oversampling on noise is a faux-safety measure, to ensure that adjacent data that belongs to another class is most likely predicted as noise instead of slipping through for verification.<\/p>\n<p class=\"wp-block-paragraph\" id=\"9cf0\">How do you check if this classifier is working well \u2014 in our experiments, we define this as the number of \u2018uncertain\u2019 samples in a classifier\u2019s prediction. Using uncertainty sampling and information gain principles, we were effectively able to gauge if a classifier is \u2018learning\u2019 or not, which acts as a pointer towards classification performance. This classifier is consistently retrained unless there is an inflection point in the number of uncertain samples predicted, or there is only a delta of information being added iteratively by new samples.<\/p>\n<h3 class=\"wp-block-heading\" id=\"a175\">Proxy active learning via an LLM agent<\/h3>\n<p class=\"wp-block-paragraph\" id=\"9545\">This is the heart of the approach \u2014 using an LLM as a proxy for a human validator. The human validator approach we are talking about is Active Labelling<\/p>\n<p class=\"wp-block-paragraph\" id=\"e581\">Let\u2019s get an intuitive understanding of Active Labelling:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Use an ML model to learn on a sample input dataset, predict on a large set of datapoints<\/li>\n<li class=\"wp-block-list-item\">For the predictions given on the datapoints, a subject-matter expert (SME) evaluates \u2018validity\u2019 of predictions<\/li>\n<li class=\"wp-block-list-item\">Recursively, new \u2018corrected\u2019 samples are added as training data to the ML model<\/li>\n<li class=\"wp-block-list-item\">The ML model consistently learns\/retrains, and makes predictions until the SME is satisfied by the quality of predictions<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\" id=\"6e03\">For Active Labelling to work, there are expectations involved for an SME:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">when we expect a human expert to \u2018validate\u2019 an output sample, the expert understands what the task is<\/li>\n<li class=\"wp-block-list-item\">a human expert will use judgement to evaluate \u2018what else\u2019 definitely belongs to a label\u00a0<strong>L<\/strong>\u00a0when deciding if a new sample should belong to\u00a0<strong>L<\/strong>\n<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\" id=\"cc42\">Given these expectations and intuitions, we can \u2018mimic\u2019 these using an LLM:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<strong>give the LLM an \u2018understanding\u2019 of what each label means<\/strong>. This can be done by using a larger model to\u00a0<strong>critically evaluate the relationship<\/strong> between {label: data mapped to label} for all labels. In our experiments, this was done using a\u00a0<strong>32B variant of DeepSeek<\/strong>\u00a0that was self-hosted.<\/li>\n<\/ul>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" height=\"232\" width=\"1024\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/03\/RED-2-1024x232.webp?resize=1024%2C232&#038;ssl=1\" alt=\"\" class=\"wp-image-599769\"><figcaption class=\"wp-element-caption\">Giving an LLM the capability to understand \u2018why, what, and how\u2019<\/figcaption><\/figure>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\"><span style=\"margin: 0px; padding: 0px;\">Instead of predicting what is the correct label,\u00a0<strong>leverage the LLM to identify if a prediction is \u2018valid\u2019 or \u2018invalid\u2019 only<\/strong>\u00a0(i.e., LLM only has to answer a binary query).<\/span><\/li>\n<li class=\"wp-block-list-item\">\n<strong>Reinforce the idea of what other valid samples for the label look like,<\/strong>\u00a0i.e., for every pre-emptively predicted label for a sample, dynamically source\u00a0<strong><em>c<\/em><\/strong>\u00a0closest samples in its training (guaranteed valid) set when prompting for validation.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\" id=\"5162\">The result? A cost-effective framework that relies on a fast, cheap classifier to make pre-emptive classifications, and an LLM that verifies these using (meaning of the label + dynamically sourced training samples that are similar to the current classification):<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">import math\n\ndef calculate_uncertainty(clf, sample):\n    predicted_probabilities = clf.predict_proba(sample.reshape(1, -1))[0]  # Reshape sample for predict_proba\n    uncertainty = -sum(p * math.log(p, 2) for p in predicted_probabilities)\n    return uncertainty\n\n\ndef select_informative_samples(clf, data, k):\n    informative_samples = []\n    uncertainties = [calculate_uncertainty(clf, sample) for sample in data]\n\n    # Sort data by descending order of uncertainty\n    sorted_data = sorted(zip(data, uncertainties), key=lambda x: x[1], reverse=True)\n\n    # Get top k samples with highest uncertainty\n    for sample, uncertainty in sorted_data[:k]:\n        informative_samples.append(sample)\n\n    return informative_samples\n\n\ndef proxy_label(clf, llm_judge, k, testing_data):\n    #llm_judge - any LLM with a system prompt tuned for verifying if a sample belongs to a class. Expected output is a bool : True or False. True verifies the original classification, False refutes it\n    predicted_classes = clf.predict(testing_data)\n\n    # Select k most informative samples using uncertainty sampling\n    informative_samples = select_informative_samples(clf, testing_data, k)\n\n    # List to store correct samples\n    voted_data = []\n\n    # Evaluate informative samples with the LLM judge\n    for sample in informative_samples:\n        sample_index = testing_data.tolist().index(sample.tolist()) # changed from testing_data.index(sample) because of numpy array type issue\n        predicted_class = predicted_classes[sample_index]\n\n        # Check if LLM judge agrees with the prediction\n        if llm_judge(sample, predicted_class):\n            # If correct, add the sample to voted data\n            voted_data.append(sample)\n\n    # Return the list of correct samples with proxy labels\n    return voted_data<\/code><\/pre>\n<p class=\"wp-block-paragraph\" id=\"6f89\">By feeding the valid samples (voted_data) to our classifier under controlled parameters, we achieve the \u2018recursive\u2019 part of our algorithm:<\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/contributor.insightmediagroup.io\/wp-content\/uploads\/2025\/03\/RED-3.png?ssl=1\" alt=\"\" class=\"wp-image-599770\"><figcaption class=\"wp-element-caption\">Recursive Expert Delegation: R.E.D.<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"9cf8\">By doing this, we were able to achieve close-to-human-expert validation numbers on controlled multi-class datasets. Experimentally, R.E.D. scales up to\u00a0<strong>1,000 classes while maintaining a competent degree of accuracy<\/strong>\u00a0almost on par with human experts (90%+ agreement).<\/p>\n<p class=\"wp-block-paragraph\" id=\"2b12\">I believe this is a significant achievement in applied ML, and has real-world uses for production-grade expectations of cost, speed, scale, and adaptability. The technical report, publishing later this year, highlights relevant code samples as well as experimental setups used to achieve given results.<\/p>\n<p class=\"wp-block-paragraph\" id=\"cc1d\"><em>All images, unless otherwise noted, are by the author<\/em><\/p>\n<p class=\"wp-block-paragraph\" id=\"4d85\">Interested in more details? Reach out to me over <a href=\"https:\/\/medium.com\/@aamirsyed2801\" target=\"_blank\" rel=\"noreferrer noopener\">Medium<\/a> or email for a chat!<\/p>\n<p class=\"wp-block-paragraph\">\n<p>The post <a href=\"https:\/\/towardsdatascience.com\/r-e-d-scaling-text-classification-with-expert-delegation\/\">R.E.D.: Scaling Text Classification with Expert Delegation<\/a> appeared first on <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Aamir Syed<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/towardsdatascience.com\/r-e-d-scaling-text-classification-with-expert-delegation\/\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>R.E.D.: Scaling Text Classification with Expert Delegation With the new age of problem-solving augmented by Large Language Models (LLMs), only a handful of problems remain that have subpar solutions. Most classification problems (at a PoC level) can be solved by leveraging LLMs at 70\u201390% Precision\/F1 with just good prompt engineering techniques, as well as adaptive [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,240,71,70,2081,2082,1419],"tags":[2083,1245,834],"class_list":["post-2550","post","type-post","status-publish","format-standard","hentry","category-aimldsaimlds","category-editors-pick","category-large-language-models","category-machine-learning","category-recursive","category-semi-supervised-learning","category-text-classification","tag-classes","tag-classification","tag-text"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/2550"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=2550"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/2550\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=2550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=2550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=2550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}