<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Farnaz Ghassemi Toudeshki on Medium]]></title>
        <description><![CDATA[Stories by Farnaz Ghassemi Toudeshki on Medium]]></description>
        <link>https://medium.com/@farnazgh73?source=rss-5385b1c333f0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2LaTqgIL5fEvXQV3AsM23Q.jpeg</url>
            <title>Stories by Farnaz Ghassemi Toudeshki on Medium</title>
            <link>https://medium.com/@farnazgh73?source=rss-5385b1c333f0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 15:22:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@farnazgh73/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[What I learned about Agentic AI]]></title>
            <link>https://medium.com/@farnazgh73/what-i-learned-about-agentic-ai-0d9ce71e095b?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/0d9ce71e095b</guid>
            <category><![CDATA[llm-agent]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Fri, 29 Aug 2025 15:44:46 GMT</pubDate>
            <atom:updated>2025-09-22T13:04:01.966Z</atom:updated>
            <content:encoded><![CDATA[<p>[This post is a collection of info from different sources, reorganized to make learning easier. I don’t claim it as original work.]</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NbjIep8PAOrJyEeU" /></figure><h4>What are agents?</h4><p>Any efficient system using AI will need to provide LLMs some kind of access to the real world: for instance the possibility to call a search tool to get external information, or to act on certain programs in order to solve a task. In other words, LLMs should have <strong><em>agency</em></strong>. Agentic programs are the gateway to the outside world for LLMs.</p><blockquote>AI Agents are <strong>programs where LLM outputs control the workflow</strong>.</blockquote><p>Any system leveraging LLMs will integrate the LLM outputs into code. The influence of the LLM’s input on the code workflow is the level of agency of LLMs in the system.</p><p>Note that with this definition, “agent” is not a discrete, 0 or 1 definition: instead, “agency” evolves on a continuous spectrum, as you give more or less power to the LLM on your workflow.</p><p>The table below illustrates how agency varies across systems:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/917/1*i5pX8gIKZiWtDHLweBXgPw.png" /><figcaption><a href="https://huggingface.co/blog/smolagents">https://huggingface.co/blog/smolagents</a></figcaption></figure><p>The multi-step agent has this code structure:</p><pre>memory = [user_defined_task]<br>while llm_should_continue(memory): # this loop is the multi-step part<br>    action = llm_get_next_action(memory) # this is the tool-calling part<br>    observations = execute_action(action)<br>    memory += [action, observations]</pre><p>So this system runs in a <strong>loop</strong>, executing a new action at each step (the action can involve calling some pre-determined <em>tools</em> that are just functions), until its observations make it apparent that a satisfactory state has been reached to solve the given task. Here’s an example of how a multi-step agent can solve a simple math question:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gF24Ddi1Az2hVoT4.gif" /><figcaption><a href="https://huggingface.co/blog/smolagents">https://huggingface.co/blog/smolagents</a></figcaption></figure><h4>Stop here — do we really need Agents ?</h4><p><strong>Agents</strong> are useful when you need an LLM to determine the workflow of an app.</p><p>The question is: do I really need flexibility in the workflow to efficiently solve the task at hand? If the pre-determined workflow falls short too often, that means you need more flexibility. Let’s take an example: say you’re making an app that handles customer requests on a surfing trip website.</p><p>You could know in advance that the requests will belong to either of 2 buckets (based on user choice), and you have a predefined workflow for each of these 2 cases.</p><ol><li>Want some knowledge on the trips? ⇒ give them access to a search bar to search your knowledge base</li><li>Wants to talk to sales? ⇒ let them type in a contact form.</li></ol><p>If that deterministic workflow fits all queries, by all means just code everything! This will give you a 100% reliable system with no risk of error introduced by letting unpredictable LLMs meddle in your workflow. For the sake of simplicity and robustness, it’s advised to regularize towards not using any agentic behaviour. ❎</p><p>But what if the workflow can’t be determined that well in advance?</p><p>For instance, a user wants to ask : &quot;I can come on Monday, but I forgot my passport so risk being delayed to Wednesday, is it possible to take me and my stuff to surf on Tuesday morning, with a cancellation insurance?&quot; This question hinges on many factors, and probably none of the predetermined criteria above will suffice for this request.</p><p>If the pre-determined workflow falls short too often, that means you need more flexibility.</p><p>That is where an agentic setup helps. ✅</p><p>In the above example, you could just make a <strong>multi-step agent</strong> that has access to a weather API for weather forecasts, Google Maps API to compute travel distance, an employee availability dashboard and a RAG system on your knowledge base.</p><p>Until recently, computer programs were restricted to pre-determined workflows, trying to handle complexity by piling up if/else switches. They focused on extremely narrow tasks, like “compute the sum of these numbers” or “find the shortest path in this graph”. But actually, most real-life tasks, like our trip example above, do not fit in pre-determined workflows. Agentic systems open up the vast world of real-world tasks to programs!</p><h4>Multi-step agent</h4><p>In a multi-step agent, at each step, the LLM can write an action, in the form of some calls to external tools. A common format (used by Anthropic, OpenAI, and many others) for writing these actions is generally different shades of “writing actions as a JSON of tools names and arguments to use, which you then parse to know which tool to execute and with which arguments”.</p><p>read this: <a href="https://www.anthropic.com/engineering/building-effective-agents">https://www.anthropic.com/engineering/building-effective-agents</a></p><h4>Code agents</h4><p><a href="https://huggingface.co/papers/2402.01030">Multiple</a> <a href="https://huggingface.co/papers/2411.01747">research</a> <a href="https://huggingface.co/papers/2401.00812">papers</a> have shown that having the tool calling LLMs in code is much better.</p><p>The reason for this is simply that <em>we crafted our code languages specifically to be the best possible way to express actions performed by a computer</em>. If JSON snippets were a better expression, JSON would be the top programming language and programming would be hell on earth.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rGgWLilq1c7Zs_y3" /></figure><h4><strong>References:</strong></h4><ul><li><a href="https://github.com/huggingface/blog/blob/main/smolagents.md">https://github.com/huggingface/blog/blob/main/smolagents.md</a></li><li><a href="https://huggingface.co/blog/smolagents">https://huggingface.co/blog/smolagents</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0d9ce71e095b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ensemble approaches in ML]]></title>
            <link>https://medium.com/@farnazgh73/ensemble-approaches-in-ml-726c52742bad?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/726c52742bad</guid>
            <category><![CDATA[ensemble-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[bagging]]></category>
            <category><![CDATA[boosting]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Sun, 15 Sep 2024 15:18:35 GMT</pubDate>
            <atom:updated>2025-09-22T12:54:43.972Z</atom:updated>
            <content:encoded><![CDATA[<p>Ensemble learning combines multiple learners to improve predictive performance. It has been adopted in response to issues resulting from limited datasets. An ensemble model combines several individual models to produce more accurate predictions than a single model alone.</p><h3>Ensemble learning techniques</h3><p>There are in general 3 ensemble approaches:</p><ul><li>Bagging</li><li>Boosting</li><li>Stacking</li></ul><h4>Bagging</h4><p>This process involves creating several subsets of the training data through random sampling with replacement. These subsets are used to train multiple base models, such as decision trees, neural networks, or other algorithms.</p><p>When making predictions, the outputs of these base models are combined, typically by averaging or through voting, to generate the final prediction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*I1Jsovb1XD7DZ2NI.jpeg" /></figure><h4>Boosting</h4><p>Boosting is a sequential ensemble technique where each base model is trained to address the errors made by its predecessors. In this approach, each subsequent model gives more attention to the instances that were misclassified by the earlier models. Common boosting algorithms include AdaBoost, Gradient Boosting Machines (GBM), and XGBoost.</p><h4>Stacking</h4><p>Stacking merges the predictions of several base models using a separate model, commonly called a meta-learner or blender. Rather than just averaging or voting, the meta-learner is trained on the outputs of the base models, learning the optimal way to combine their predictions to generate the final output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/0*N5Z4Kcl_SWk2ZlRo.PNG" /></figure><p>References:</p><ul><li><a href="https://www.ibm.com/topics/ensemble-learning#:~:text=Ensemble%20learning%20is%20a%20machine,than%20a%20single%20model%20alone.">https://www.ibm.com/topics/ensemble-learning#:~:text=Ensemble%20learning%20is%20a%20machine,than%20a%20single%20model%20alone.</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=726c52742bad" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Few-shot Text Classification on a Multilabel Dataset with SetFit]]></title>
            <link>https://medium.com/@farnazgh73/few-shot-text-classification-on-a-multilabel-dataset-with-setfit-e89504f5fb75?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e89504f5fb75</guid>
            <category><![CDATA[setfit]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[multilabel-classification]]></category>
            <category><![CDATA[text-classification]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Mon, 03 Jun 2024 17:20:40 GMT</pubDate>
            <atom:updated>2025-09-22T12:55:20.184Z</atom:updated>
            <content:encoded><![CDATA[<h3>Few-Shot Multi-label Text Classification Using SetFit</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ijv0wPQfISHuFokd.jpg" /></figure><h3>SetFit (Few-shot model)</h3><p><a href="https://github.com/huggingface/setfit/tree/main"><strong>SetFit</strong></a> is an efficient and prompt-free framework for<strong> few-shot fine-tuning</strong> of <a href="https://sbert.net/">Sentence Transformers</a>. It achieves <strong>high accuracy</strong> with<strong> little labeled data</strong> — for instance, with only 8 labeled examples per class.</p><p>SetFit has several unique features:</p><ul><li>🗣 No prompts or verbalizers</li><li>🏎 Fast to train</li><li>🌎 Multilingual support</li></ul><h3>Multilabel Classification</h3><p>SetFit <strong>supports multilabel classification</strong>, allowing multiple labels to be assigned to each instance.</p><h4>Multilabel strategies</h4><p>SetFit will initialise a multilabel classification head from sklearn - the following options are available for multi_target_strategy [1]:</p><ul><li>&quot;one-vs-rest&quot;: uses a <a href="https://scikit-learn.org/stable/modules/generated/sklearn.multiclass.OneVsRestClassifier.html">OneVsRestClassifier</a> head.</li><li>&quot;multi-output&quot;: uses a <a href="https://scikit-learn.org/stable/modules/generated/sklearn.multioutput.MultiOutputClassifier.html">MultiOutputClassifier</a> head.</li><li>&quot;classifier-chain&quot;: uses a <a href="https://scikit-learn.org/stable/modules/generated/sklearn.multioutput.ClassifierChain.html">ClassifierChain</a> head.</li></ul><h3>SetFit Training</h3><p>Install SetFit library</p><pre>!pip install setfit</pre><p>Load dataset</p><pre>from datasets import load_dataset<br><br>model_id = &quot;sentence-transformers/paraphrase-mpnet-base-v2&quot;<br>dataset = load_dataset(&quot;ethos&quot;, &quot;multilabel&quot;)</pre><p>Features</p><pre>import numpy as np<br><br>features = dataset[&quot;train&quot;].column_names<br>features.remove(&quot;text&quot;)<br>features<br><br># output<br>[&#39;violence&#39;,<br> &#39;directed_vs_generalized&#39;,<br> &#39;gender&#39;,<br> &#39;race&#39;,<br> &#39;national_origin&#39;,<br> &#39;disability&#39;,<br> &#39;religion&#39;,<br> &#39;sexual_orientation&#39;]</pre><p>Encode features into a single label</p><pre>def encode_labels(record):<br>    return {&quot;labels&quot;: [record[feature] for feature in features]}<br><br><br>dataset = dataset.map(encode_labels)</pre><p>Sample 8 data points for each feature</p><pre>num_samples = 8<br>samples = np.concatenate(<br>    [np.random.choice(np.where(dataset[&quot;train&quot;][f])[0], num_samples) for f in features]<br>)</pre><p>Define train and test sets</p><pre>train_dataset = dataset[&quot;train&quot;].select(samples)<br>eval_dataset = dataset[&quot;train&quot;].select(<br>    np.setdiff1d(np.arange(len(dataset[&quot;train&quot;])), samples)<br>)</pre><p>Load model</p><pre>from setfit import SetFitModel<br><br>model = SetFitModel.from_pretrained(model_id, multi_target_strategy=&quot;one-vs-rest&quot;)</pre><p>Set trainer</p><p>The main arguments to notice in the trainer is the following:</p><ul><li>loss_class: The loss function to use for contrastive learning with the Sentence Transformer body</li><li>num_iterations: The number of text pairs to generate for contrastive learning</li><li>column_mapping: The SetFitTrainer expects the inputs to be found in a text and label column. This mapping automatically formats the training and evaluation datasets for us.</li></ul><pre>from sentence_transformers.losses import CosineSimilarityLoss<br>from setfit import SetFitTrainer<br><br>trainer = SetFitTrainer(<br>    model=model,<br>    train_dataset=train_dataset,<br>    eval_dataset=eval_dataset,<br>    loss_class=CosineSimilarityLoss,<br>    num_iterations=20,<br>    column_mapping={&quot;text&quot;: &quot;text&quot;, &quot;labels&quot;: &quot;label&quot;},<br>)</pre><p>Start training</p><pre>trainer.train()</pre><p>Save model on HuggingFace</p><pre>trainer.push_to_hub(f&quot;setfit-ethos-multilabel-example&quot;)</pre><p>Load model for inference</p><pre>from setfit import SetFitModel<br><br>model = SetFitModel.from_pretrained(&quot;lewtun/setfit-ethos-multilabel-example&quot;)</pre><p>Inference</p><pre>preds = model(<br>    [<br>        &quot;Jewish people often don&#39;t eat pork.&quot;,<br>        &quot;Is this lipstick suitable for people with dark skin?&quot;<br>    ]<br>)<br>[[f for f, p in zip(features, ps) if p] for ps in preds]<br><br># output<br>[[&#39;religion&#39;], [&#39;directed_vs_generalized&#39;]]</pre><p>[1] <a href="https://huggingface.co/docs/setfit/en/how_to/multilabel">https://huggingface.co/docs/setfit/en/how_to/multilabel</a></p><p>[2] <a href="https://github.com/huggingface/setfit/blob/main/notebooks/text-classification_multilabel.ipynb">https://github.com/huggingface/setfit/blob/main/notebooks/text-classification_multilabel.ipynb</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e89504f5fb75" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Stanford CoreNLP Library]]></title>
            <link>https://medium.com/@farnazgh73/stanford-corenlp-library-fc36b4297db9?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc36b4297db9</guid>
            <category><![CDATA[text-preprocessing]]></category>
            <category><![CDATA[pos-tagging]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[stanford-corenlp]]></category>
            <category><![CDATA[named-entity-recognition]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Thu, 30 May 2024 22:02:31 GMT</pubDate>
            <atom:updated>2025-09-22T12:55:47.715Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*B0va-Vg2b1Qnp7lV.jpg" /></figure><p><a href="https://stanfordnlp.github.io/CoreNLP/"><strong><em>Stanford CoreNLP</em></strong></a></p><ul><li>Java library</li><li>part-of-speech (POS) tagger</li><li>named entity recognizer (NER)</li><li>coreference resolution system</li></ul><p><strong>1-Constituent tree</strong></p><pre># set java path<br>import os<br>java_path = r&#39;/usr/lib/jvm/java-8-oracle/jre/bin/java&#39;<br>os.environ[&#39;JAVAHOME&#39;] = java_path<br><br># import NLTK wrapper<br>from nltk.parse.stanford import StanfordParser<br><br># Create Parser instance<br># scp = Stanford Constituency Parser<br>scp = StanfordParser(path_to_jar=&#39;path_to_jar&#39;,<br>path_to_models_jar=&#39;path_to_models_jar&#39;)<br><br>sentence = &quot;Mr. Dursley was the director of a firm called Grunnings, which made drills.&quot;<br><br># Apply the parser<br>parse_trees = list(scp.raw_parse(sentence))<br>print(parse_trees[0])<br><br>#pretty printing<br>from IPython.display import display<br>display(resultparse_trees[0])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*HVCS5dqWFddLSo0FLzVxYQ.png" /></figure><pre># (stanfordCoreNLP)<br># first run the server<br># java -mx4g -cp &quot;*&quot; edu.stanford.nlp.pipeline.StanfordCoreNLPServer -port 9000 -timeout 80000<br><br>from pycorenlp import StanfordCoreNLP<br>nlp = StanfordCoreNLP(&#39;http://localhost:9000&#39;)<br><br>output = nlp.annotate(sent, properties={<br>        &#39;annotators&#39;: &#39;parse&#39;,<br>        &#39;outputFormat&#39;: &#39;json&#39;<br>        })<br><br>a = [s[&#39;parse&#39;] for s in output[&#39;sentences&#39;]]<br><br>tree = Tree.fromstring(a[0])<br>print(tree)</pre><p><strong>2- Dependency tree</strong></p><pre>from nltk.parse.stanford import StanfordDependencyParser<br><br># Create instance of Stanford dependency parser<br>sdp = StanfordDependencyParser(path_to_jar=&#39;path_to_jar&#39;, path_to_models_jar= &#39;path_to_models_jar&#39;<br><br>sentence = &quot;Mr. Dursley was the director of a firm called Grunnings, which made drills.&quot;<br><br># Apply parser<br>result = list(sdp.raw_parse(sentence))<br><br># Get first parse tree<br>dep_tree = [parse.tree() for parse in result][0]<br><br># Print in bracketted format<br>print(dep_tree)<br><br># Pretty printing showing the tree structure<br>from IPython.display import display<br>display(dep_tree)</pre><p><strong>3- NER</strong></p><pre>from nltk.tag import StanfordNERTagger<br>import os<br>import pandas as pd<br><br>java_path = r&#39;/usr/lib/jvm/java-8-oracle/jre/bin/java&#39;<br>os.environ[&#39;JAVAHOME&#39;] = java_path<br><br>sner = StanfordNERTagger(path_to_jar=&#39;path_to_jar&#39;)<br><br>sentence = &quot;On Oct. 24, trading volume in China&#39;s onshoreyuan also spiked Reuters reported.&quot;<br><br>ner_tagged_sentence = sner.tag(sentence.split())<br>named_entities = [ne for ne in ner_tagged_sentence if ne[1] != &#39;O&#39;]<br>print(named_entities)<br><br>#--&gt; [(&#39;Reuters&#39;, &#39;ORGANIZATION&#39;)]</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc36b4297db9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SpaCy Library Cheatsheet]]></title>
            <link>https://medium.com/@farnazgh73/spacy-library-cheatsheet-0a0534240372?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/0a0534240372</guid>
            <category><![CDATA[text-preprocessing]]></category>
            <category><![CDATA[named-entity-recognition]]></category>
            <category><![CDATA[lemmatization]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[spacy]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Thu, 30 May 2024 21:51:34 GMT</pubDate>
            <atom:updated>2025-09-22T12:56:36.629Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZpYLSLIRVyONBKGX.jpg" /></figure><p><a href="https://spacy.io/"><strong>SpaCy</strong></a></p><ul><li>Library for Natural Language Processing</li><li><strong>pre-trained statistical models</strong> and <strong>word vectors</strong></li><li><strong>Convolutional neural network </strong>models for <strong>tagging, parsing and named<br>entity recognition</strong></li><li>Interacts well with <strong>Deep Learning Libraries</strong></li></ul><p><strong>1-Sentence Segmentation</strong></p><pre>import spacy<br><br># Load English Model<br>nlp = spacy.load(&#39;en&#39;)<br>text = &quot;Twenty-two years after the original Jurassic Park failed, the new park,also known as Jurassic World, is open for business. After years of studying genetics, the scientists on the park genetically engineer a new breed of dinosaur, the Indominus Rex.&quot;<br><br># Run SPaCy pipeline<br>sp_text = nlp(text)<br><br># Segment into sentences<br>for sentence in sp_text.sents:<br>  print(sentence)</pre><p><strong>2- Tokenizing</strong></p><pre>import spacy<br><br># Load English Model<br>nlp = spacy.load(&#39;en&#39;)<br>text = &quot;Twenty-two years after the original Jurassic Park failed, the new park,also known as Jurassic World, is open for business. After years of studying genetics, the scientists on the park genetically engineer a new breed of dinosaur, the Indominus Rex.&quot;</pre><pre># Run SpaCy pipeline<br>sp_text = nlp(text)</pre><pre># Get tokens<br>for word in sp_text:<br>  print(word.text)</pre><p><strong>3- Removing punctuation and stop words</strong></p><pre>import spacy<br><br># Load English Model<br>nlp = spacy.load(&#39;en&#39;)<br>text = &quot;Twenty-two years after the original Jurassic Park failed, the new park,also known as Jurassic World, is open for business. After years of studying genetics, the scientists on the park genetically engineer a new breed of dinosaur, the Indominus Rex.&quot;<br><br># Run SpaCy pipeline<br>sp_text = nlp(text)<br><br># Remove stopwords and punctuations<br>words = [token for token in sp_text if token.is_stop != True<br>and token.is_punct != True]</pre><p><strong>4- POS tagging</strong></p><pre>nlp = spacy.load(&#39;en&#39;)<br>sentence = &quot;Mr. Dursley was the director of a firm called<br>Grunnings, which made drills.&quot;<br><br># Run SpaCy Pipeline<br>sp_sentence = nlp(sentence)<br># Get POS tags for each token<br>spacy_pos_tagged = [(w, w.tag_, w.pos_) for w in sp_sentence]</pre><p><strong>5- Lemmatizing</strong></p><pre>import spacy<br>nlp = spacy.load(&#39;en&#39;)<br>sentence = &quot;Mr. Dursley was the director of a firm called Grunnings which made drills.&quot;<br>sp_text = nlp(text)<br>text = &#39; &#39;.join([word.lemma_ for word in sp_text])<br>print(text)</pre><p><strong>6- Named Entity Recognition</strong></p><pre>import spacy<br>from spacy import displacy<br><br># Load SpaCy NER<br>ner = spacy.load(&#39;en&#39;, entity=True)<br><br># Apply SpaCy NER<br>sp_sentence = ner(&quot;TSA employees received their last paycheck on Dec. 28, giving them money that would typically<br>last through the next pay period ― but which will now have to stretch much further. Many TSA workers live paycheck<br>to paycheck, with the starting salary for officers between $25,000 to $30,000 a year. &quot;)<br><br># Jupyter display<br>displacy.render(sp_sentence, style=&#39;ent&#39;, jupyter=True)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*z3n-SUvszbVfKxf8.png" /></figure><p><strong>7- Dependency Parsing</strong></p><pre>import spacy<br>import en_core_web_sm<br>nlp = en_core_web_sm.load()<br><br># nlp = spacy.load(&quot;en_core_web_sm&quot;)<br>doc = nlp(u&quot;Apple bought U.K. startup for $1 billion&quot;)<br>for token in doc:<br>    print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_, token.head)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0a0534240372" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NLTK Cheatsheet]]></title>
            <link>https://medium.com/@farnazgh73/nltk-cheatsheet-4b5875f32704?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/4b5875f32704</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[stemming]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[text-preprocessing]]></category>
            <category><![CDATA[nltk]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Thu, 30 May 2024 21:42:14 GMT</pubDate>
            <atom:updated>2025-09-22T12:56:58.475Z</atom:updated>
            <content:encoded><![CDATA[<h3>NLTK Library Cheatsheet</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*u94icAeJi3i_GmjZ.jpg" /></figure><p><a href="https://www.nltk.org/"><strong>NLTK</strong></a></p><ul><li>Very wide range of <strong>tools and resources for NLP</strong></li><li>interfaces to over <strong>50 corpora and lexical resources</strong> such as <strong>WordNet</strong></li><li>text processing libraries for <strong>classification</strong>, <strong>tokenization</strong>, <strong>stemming</strong>,<br><strong>tagging</strong>, <strong>parsing</strong>, and<strong> semantic reasoning</strong></li></ul><p><strong>1-Sentence Segmentation</strong></p><pre>text = &quot;This ice-cream is from the U.K.. This isn&#39;t a cake. &quot;<br>sentences = nltk.sent_tokenize(text)</pre><pre>#output<br>[&#39;This ice-cream is from the U.K..&#39;,<br>&quot;This ice cream isn&#39;t a cake.&quot;]</pre><p><strong>2- Tokenizing</strong></p><pre>import nltk<br>text = &quot;This ice-cream is from the U.K.. This isn&#39;t a cake. &quot;<br>tokens = nltk.word_tokenize(text)</pre><pre>#output<br>[&#39;This&#39;, &#39;ice-cream&#39;, &#39;is&#39;, &#39;from&#39;, &#39;the&#39;, &#39;U.K..&#39;, &#39;This&#39;,<br>&#39;is&#39;, &quot;n&#39;t&quot;, &#39;a&#39;, &#39;cake&#39;, &#39;.&#39;]</pre><p><strong>3- Removing punctuation</strong></p><pre>#Get a list of punctuation signs<br>import string<br>print(string.punctuation)<br>#Translate punctuation signs to the empty string<br>translator = str.maketrans(&#39;&#39;, &#39;&#39;, string.punctuation)<br>s = &#39;string with &quot;punctuation&quot; inside of it! Does this work?&#39;<br>print(s.translate(translator))</pre><p><strong>4- POS tagging</strong></p><pre>import nltk<br>text = &quot;This ice-cream is from the U.K.. This isn&#39;t a cake. &quot;<br>tagged_tokens = nltk.pos_tag(tokens)<br>#output<br>[(&#39;This&#39;, &#39;DT&#39;), (&#39;ice-cream&#39;, &#39;NN&#39;), (&#39;is&#39;, &#39;VBZ&#39;),<br>(&#39;from&#39;, &#39;IN&#39;), (&#39;the&#39;, &#39;DT&#39;), (&#39;U.K..&#39;, &#39;NNP&#39;),<br>(&#39;This&#39;, &#39;DT&#39;), (&#39;ice&#39;, &#39;NN&#39;), (&#39;cream&#39;, &#39;NN&#39;),<br>(&#39;is&#39;, &#39;VBZ&#39;), (&quot;n&#39;t&quot;, &#39;RB&#39;), (&#39;a&#39;, &#39;DT&#39;),<br>(&#39;cake&#39;, &#39;NN&#39;), (&#39;.&#39;, &#39;.&#39;)]</pre><p><strong>5- Stemming</strong></p><pre>from nltk.stem.porter import PorterStemmer<br><br># Create a stemmer instance<br>porter = PorterStemmer()<br>sentence = &quot;Mr. and Mrs. Dursley, of number four, Privet Drive<br>in Stansted, were proud to say that they were perfectly normal,<br>thank you very much.&quot;<br><br># Tokenize<br>tokens = nltk.word_tokenize(sentence)<br><br># Apply the stemmer and collect the stems<br>porterlemmas = []<br>for word in tokens:<br>  porterlemmas.append(porter.stem(word))</pre><p><strong>6- Lemmatizing</strong></p><pre>#NLTK lemmatizer needs to know the WordNet POS tag of the tokens<br>sentence = &quot;Mr. Dursley was the director of a firm which made drills.&quot;</pre><pre># Tokenize and POS tag the sentence<br>tokens = nltk.word_tokenize(sentence)<br>tagged_tokens = nltk.pos_tag(tokens)</pre><pre># Store all verbs from the input sentence into a list<br>verbs = []<br>for token, tag in tagged_tokens:<br>if tag in [&quot;VBD&quot;, &quot;VBG&quot;, &quot;VBN&quot;, &quot;VBP&quot;, &quot;VBZ&quot;]:<br>verbs.append(token)</pre><pre># Create a lemmatizer instance<br>lemmatizer = nltk.stem.wordnet.WordNetLemmatizer()</pre><pre>#Apply lemmatizer to verb list<br>verb_lemmas = []<br>for word_form in verbs:<br>  lemma = lemmatizer.lemmatize(word_form, &quot;v&quot;)<br>  verb_lemmas.append((word_form,lemma))</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4b5875f32704" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Active Learning Tools and Strategies — part 2]]></title>
            <link>https://medium.com/@farnazgh73/active-learning-tools-and-strategies-part-2-325c2be2ca00?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/325c2be2ca00</guid>
            <category><![CDATA[transformers]]></category>
            <category><![CDATA[annotations]]></category>
            <category><![CDATA[active-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Thu, 30 May 2024 21:20:46 GMT</pubDate>
            <atom:updated>2025-09-22T13:05:06.806Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*HIKxEldoXD2GHcX8.jpg" /></figure><p>In <a href="https://medium.com/@farnazgh73/ultimate-guide-for-active-learning-main-approaches-3cf53ce207f0"><strong>Part 1</strong></a>, we explained what is active learning and gave a good explanation on different types of strategies and main advantages of active learning. In this post, we continue with explaining more precisely the <strong>strategies</strong> and the <strong>main popular tools</strong> in this domain. Lets get in 💛</p><h3>Active learning: strategies for subsampling</h3><p>Selecting the best query strategy is very important as they decide which data points are informative and should be sent for labeling and used for further training. The efficiency of the active learning pipeline can be determined by how quickly the query strategy can select the most effective sample from the pool of unlabeled data.</p><ul><li>Uncertainty-based Sampling</li><li>Committee-based Sampling</li><li>Diversity Weighted Methods</li><li>Expected Model-change-based Sampling</li><li>Expected Error Reduction</li><li>Stream-based Sampling</li><li>Active Regression</li><li>Bayesian optimization</li><li>Multilabel Strategies</li></ul><h4>Uncertainty-based Sampling</h4><p>Uncertainty sampling is a query strategy that selects samples that are expected to reduce the uncertainty of the model the most. The uncertainty of the model is typically measured using a measure of uncertainty, such as entropy or margin-based uncertainty. Samples with high uncertainty are selected for labeling, as they are expected to provide the most significant improvements to the model’s performance.</p><p>Uncertainty-based strategies are the most convenient way for querying labels. Despite the simple nature of these methods, they have shown high performance in all sorts of tasks (even higher performance in<br>some tasks compared to other complicated approaches).</p><h4>Committee-based Sampling</h4><p>Query-by committee is a query strategy that involves training multiple models on different subsets of the labeled dataset and selecting samples based on the disagreement among the models.</p><p>This strategy is useful when the model tends to make errors on specific samples or classes. By selecting samples on which the committee of models disagrees, the model can learn to recognize sample patterns and improve its performance in those classes.</p><h4>Diversity Weighted Methods</h4><p>Diversity-weighted methods select examples for labeling based on their diversity in the current training set. It involves ranking the pool of unlabeled examples based on a diversity measure, such as the dissimilarity between examples or the uncertainty of the model’s predictions.</p><h4>Expected Model-change-based Sampling</h4><p>Expected model-change-based sampling is an active learning method that selects examples for labeling based on the expected change in the model’s predictions. This approach aims to select examples likely to cause the most significant changes in the model’s predictions when labeled to improve the model’s performance on new, unseen data.</p><p>In expected model-change-based sampling, the unlabeled examples are first ranked based on estimating the expected change in the model’s predictions when each example is labeled.</p><p>This estimation can be based on various measures such as expected model output variance, expected gradient magnitude or by measuring the euclidean distance between the current model parameters and the expected model parameters after labeling.</p><p>Using this approach, the examples that are expected to cause the most significant changes in the model’s predictions are then selected for labeling, with the idea that these examples will provide the most informative training data for the model. These samples are then added to the training data to update the model.</p><h4>Expected Error Reduction</h4><p>Expected error reduction is an active learning method that selects examples for labeling based on the expected reduction in the model’s prediction error. The idea behind this approach is to select examples likely to reduce the model’s prediction error the most when labeled, to improve the model’s performance on new, unseen data.</p><p>In expected error reduction, the unlabeled examples are first ranked based on estimating the expected reduction in the model’s prediction error when each example is labeled. This estimation can be based on various measures, such as the distance to the decision boundary, the margin between the predicted labels, or the expected entropy reduction.</p><h3>Active Learning for NLP</h3><p>Active learning is quite popular in the world of NLP. Specifically, information extraction for POS tagging, Named Entity Recognition (NER), etc., require lots of training (labeled) data, and the cost of labeling data for these kinds of use cases is really high. Most of the advanced language models are based on deep neural networks and trained on large datasets. However, under typical training scenarios, the upper hand deep learning usually commands will diminish if the datasets are too small. Hence to make deep learning broadly useful, it is crucial to find an effective solution for the above problem.</p><p><a href="https://www.inovex.de/de/blog/active-learning-in-context-of-natural-language-processing-hands-on/">&gt;&gt; Active Learning for Text Classification example</a></p><h3>Tools for Active Learning</h3><ul><li><a href="https://cleanlab.ai/">Cleanlab</a> : Proposing a novel active learning method, called <a href="https://cleanlab.ai/blog/active-learning/">ActiveLab</a>, that results showed that under a fixed total number of allowed annotations, training datasets constructed with ActiveLab lead to <em>much better</em> ML models vs. other active learning methods.</li><li><a href="https://modal-python.readthedocs.io/en/latest/">modAL</a> (A modular active learning framework for Python3): modAL is an active learning framework for Python3, designed with modularity, flexibility and extensibility in mind. Built on top of scikit-learn, it allows you to rapidly create active learning workflows with nearly complete freedom. modAL supports many of the active learning strategies discussed in the previous sections, such as probability/uncertainty-based algorithms, committee-based algorithms, error reduction, and so on.</li><li><a href="https://libact.readthedocs.io/en/latest/">libact</a> (Pool-based Active Learning in Python): libact is a python package designed to make active learning easier for real-world users. The package not only implements several popular active learning strategies but also features active learning by learning meta-strategy that allows the machine to automatically learn the best strategy on the fly.</li><li><a href="https://github.com/INK-USC/AlpacaTag">AlpacaTag</a> : AlpacaTag is an active learning-based crowd annotation framework for sequence tagging, such as named-entity recognition (NER). Intelligent recommendation , Automatic crowd consolidation and Real-time model deployment are some of the distinctive advantages of AlpacaTag.</li></ul><h3>When to stop Active Learning?</h3><p>When adopting AL in practice, it would be desirable to know the time to stop AL when the model performance is already near the upper limits, before running out of all the budgets. For this purpose, a stopping criterion is needed, which checks certain metrics satisfying certain conditions.</p><p>For the design of a general stopping criterion, there are three main<br>aspects to consider: Metric, Dataset, and Condition.</p><p>For the <strong>metric</strong>, measuring performance on a development set seems a natural option. However, the results would be unstable if this set is too small and it would be impractical to assume a large development set. Cross-validation on the training set also has problems since the labeled data by AL is usually biased.</p><p>The <strong>dataset</strong> to calculate the stopping metric requires careful choosing. The results could be unstable if not adopting a proper set.</p><p>The <strong>condition</strong> to stop AL is usually comparing the metrics to a pre-defined threshold. Earlier works only look at the metric at the current iteration, for example, stopping if the uncertainty or the error is less than the threshold</p><h3>Active Learning in production</h3><h4>Active learning pipelines — semi-automatic</h4><p>In a semi-automatic or semi-active learning approach, each cycle runs automatically, but it needs to be triggered manually. Key areas where manual intervention is required are at –</p><p>1. selecting the data for annotation — we need to choose the next images in an informed way.</p><p>2. Selecting the best model out of the ensemble of models created among each cycle</p><p>A semi-automatic pipeline needs to be closely monitored for its performance indicators, such as the model performance metrics after each active learning cycle. Inherently this technique is prone to errors, especially when it requires a number of learning cycles.</p><p><a href="https://huggingface.co/blog/autonlp-prodigy">&gt;&gt; An example of semi-automatic or semi-active Learning with AutoTrain and Prodigy</a></p><h4>Active learning pipelines — automatic</h4><p>So what happens in an automatic pipeline is that we leave the two manual intervention steps to a smart algorithm. Selecting the data for annotation could be through any one of the sampling techniques discussed in the previous section, i.e, Querying, Pool based sampling, etc. Selecting the best model could be again based on any of the performance metrics of our choice or their weighted values.</p><h3>References:</h3><p>[1] <a href="https://aclanthology.org/2020.lrec-1.348">Optimizing Annotation Effort Using Active Learning Strategies: A Sentiment Analysis Case Study in Persian</a> (Ashrafi Asli et al., LREC 2020)</p><p>[2] <a href="https://encord.com/blog/top-active-learning-tools-for-machine-learning/">Top 6 Tools for Active Learning in Machine Learning</a></p><p>[3] <a href="https://aclanthology.org/2022.emnlp-main.414">A Survey of Active Learning for Natural Language Processing</a> (Zhang et al., EMNLP 2022)</p><p>[4] <a href="https://neptune.ai/blog/active-learning-strategies-tools-use-cases">Active Learning: Strategies, Tools, and Real-World Use Cases</a></p><p>[5] <a href="https://huggingface.co/blog/autonlp-prodigy">Active Learning with AutoNLP and Prodigy</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=325c2be2ca00" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unlocking the Black Box: How to Make Your AI Model Explainable]]></title>
            <link>https://medium.com/@farnazgh73/unlocking-the-black-box-how-to-make-your-ai-model-explainable-9fa6f9ac2c71?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/9fa6f9ac2c71</guid>
            <category><![CDATA[explainable-ai]]></category>
            <category><![CDATA[machie-learning]]></category>
            <category><![CDATA[shap]]></category>
            <category><![CDATA[lime]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Thu, 30 May 2024 20:49:00 GMT</pubDate>
            <atom:updated>2025-09-22T12:57:52.471Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*32DUlsx3Nfzl-QQM.jpg" /></figure><h3>Explainable AI</h3><ul><li>Better model understanding <strong>of why the model classifies data the way it does</strong></li><li>Gives us <strong>an idea of where to improve the training data</strong>.</li><li><strong>Detecting biases</strong> and building fair(er) models</li><li>A <strong>consistent and systematic explainable AI</strong> check in <strong>our model evaluation</strong></li></ul><p>ML models are often thought of as <strong>black boxes</strong> that are impossible to interpret [1]. <strong>Explainable AI</strong> (xAI) is a set of tools and frameworks that allows human users to <strong>understand and interpret predictions</strong> made by machine learning models [2].</p><p>It helps characterize model <strong>accuracy</strong>, <strong>fairness</strong>, <strong>transparency</strong> in decision making. Explainable AI is crucial for an organization in <strong>building trust and confidence</strong> when putting AI models into production.</p><h3>Explainable AI for NLP</h3><p>In the past, NLP systems have traditionally relied on <strong>white-box techniques</strong>. These techniques — rules, decision trees, hidden Markov models, and logistic regression — are inherently explainable [8].</p><p>The recent developments in deep learning have contributed to the emergence of <strong>black-box architectures</strong> that improve task performance at the expense of model explainability. Such black-box predictions make understanding how a model arrives at a decision challenging. This lack of explainability is a significant cause of concern for critical applications. For example, directly applying natural language generation methods to automatically generate radiology reports from chest X-ray images only guarantees that the produced reports will look natural rather than contain correct anatomically-aware information.</p><p>Considering the utilization of NLP in healthcare, finance, and law domains, all of which can directly affect human lives, it can be dangerous to blindly follow machine predictions without fully understanding them. Systems employing Machine Learning (ML), such as most current NLP methods, are prone to adversarial attacks where small, carefully crafted local perturbations can maximally alter model predictions, essentially misguiding the model to predict incorrectly but with high confidence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*58iTKxCY9WYN-bj6.png" /></figure><p>In this survey, the focus is on explainability and mainly on the <strong>outcome explanation problem</strong> where “the perspective of an end-user whose goal is to understand how a model arrives at its result”.</p><h3>Explainable AI Techniques</h3><p>In NLP, there exist various explainable techniques such as <strong>LIME</strong> (Local Interpretable Model-Agnostic Explanations) (<a href="https://www.frontiersin.org/articles/10.3389/frai.2023.1225093/full#B70">Ribeiro et al., 2016</a>), <strong>Integrated Gradients </strong>(<a href="https://www.frontiersin.org/articles/10.3389/frai.2023.1225093/full#B80">Sundararajan et al., 2017</a>), <strong>SHAP</strong> (Shapley Additive Explanations) (<a href="https://www.frontiersin.org/articles/10.3389/frai.2023.1225093/full#B48">Lundberg and Lee, 2017a</a>), and <strong>Rationalization</strong> (<a href="https://www.frontiersin.org/articles/10.3389/frai.2023.1225093/full#B7">Atanasova et al., 2020</a>).</p><p><strong>Rationalization</strong> provides explanations in natural language to justify a model’s prediction. These explanations are rationales, which present the input features influencing the model’s prediction. The reasoning behind the prediction could be understood simply by reading the explanation/rationale, thereby revealing the model’s decision-making process. Rationalization can be an attractive technique because it is human-comprehensible and allows individuals without domain knowledge to understand how a model arrived at a prediction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/0*ZSqTAIpVX8nqDtG2.jpg" /><figcaption>Rationalization types. (A) An example of the Extractive Rationalization approch (<a href="https://www.frontiersin.org/articles/10.3389/frai.2023.1225093/full#B39">Lei et al., 2016</a>). (B) An example of the Abstractive Rationalization approach (<a href="https://www.frontiersin.org/articles/10.3389/frai.2023.1225093/full#B68">Rajani et al., 2019</a>).</figcaption></figure><blockquote><em>To read more about Rationalization for NLP [</em><a href="https://www.frontiersin.org/articles/10.3389/frai.2023.1225093/full"><em>Link</em></a><em>]</em></blockquote><h3>Explainable AI available tools</h3><p><strong>(1) Shap Library</strong></p><p><a href="https://github.com/shap/shap"><strong>SHAP</strong></a> (SHapley Additive exPlanations) is a game theoretic approach to <strong>explain the output of any machine learning model</strong>. SHAP has specific support for<strong> natural language models</strong> like those in the <strong>Hugging Face</strong>transformers library. Below, we can see an example of an explanation from SHAP documentation presenting why the model predicted the sentiment of a movie review to be positive:</p><pre>import transformers<br>import shap<br><br># load a transformers pipeline model<br>model = transformers.pipeline(&#39;sentiment-analysis&#39;, return_all_scores=True)<br><br># explain the model on two sample inputs<br>explainer = shap.Explainer(model)<br>shap_values = explainer([&quot;What a great movie! ...if you have no taste.&quot;])<br><br># visualize the first prediction&#39;s explanation for the POSITIVE output class<br>shap.plots.text(shap_values[0, :, &quot;POSITIVE&quot;])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gQqAGz1GblMxIUqk.png" /></figure><p><strong>(2) LIT</strong></p><p>The <a href="https://pair-code.github.io/lit/"><strong>LIT</strong></a><strong> — Learning Interpretability</strong> Tool is for researchers and practitioners looking to understand<strong> NLP model behavior</strong> through a visual, interactive, and extensible tool.</p><p>Use LIT to ask and answer questions like:</p><ul><li>What kind of examples does my model perform poorly on?</li><li>Why did my model make this prediction? Can it attribute it to adversarial behavior, or undesirable priors from the training set?</li><li>Does my model behave consistently if I change things like textual style, verb tense, or pronoun gender?</li></ul><p>It works as a standalone web service, which can also be run in Jupyter Notebook. Below, you can see the script that loads the dataset together with a pretrained model and runs the service in Jupyter Notebook:</p><pre>from lit_nlp import notebook from lit_nlp. examples.datasets import glue<br>from lit_nlp.examples.models import glue_models<br>datasets = {&#39;sst_dev&#39;: glue.SST2Data(&#39;validation&#39;)}<br>models = {&#39;sst_tiny&#39;: glue_models.SST2Model(&#39;•/&#39;)}<br>widget = notebook.LitWidget(models, datasets, height=800)<br>widget.render()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hUU0UQx8KukY4zao.png" /></figure><p>The result clearly shows that the key phrases in the prediction were ‘great’ and ‘no taste’.</p><blockquote><em>Exploring a sentiment classifier with LIT (python code) [</em><a href="https://pair-code.github.io/lit/tutorials/sentiment/"><em>Link</em></a><em>]</em></blockquote><p><strong>(3) LIME</strong></p><p><a href="https://github.com/marcotcr/lime?tab=readme-ov-file">LIME</a> — Local Interpretable Model-agnostic Explanations is an algorithm that can explain the predictions of any classifier or regressor in a faithful way, by approximating it locally with an interpretable model. Instead of providing a global understanding of the model on the entire dataset, LIME focuses on explaining the model’s prediction for individual instances.</p><p>Here is a code snippet on how to use LIME for text classification. LIME identifies key words that can predict the sentiment of a given text [7]:</p><pre>import lime<br>from lime import LimeTextExplainer <br><br># Class names used for interpretation<br>class_names=[&#39;negative&#39;,&#39;positive&#39;]<br># Initialize LIME explaniner<br>explainer= LimeTextExplainer(class_names=class_names)<br><br># Function to predict probabilities of an input text<br>def predict_proba(statements):<br>    # Store processed strings<br>    processed_str = []<br>    # Iterate through each sentiment and clean data<br>    for statment in statements:<br>        # data_cleaner returns the lemmed statement and label, if specified<br>        cleaned_str, _ = data_cleaner(statment, labeled=False, spell_check=False)<br>        # Append cleaned data <br>        processed_str.append(cleaned_str)<br>    # Tokenize word using tokenizer for the final model <br>    tokenized = tokenizer.texts_to_sequences(processed_str)<br>    # Apply padding <br>    input_data = pad_sequences(tokenized, <br>                       maxlen=test_dataset_dict[&#39;tokenized-no-weight&#39;].shape[1])<br>    # Predict the sentiment of the statement <br>    pred = model.predict(input_data)<br>    # Return the binary probabilities<br>    return np.array([[1-i[0],i[0]] for i in pred])<br>  <br># Randomly choose comment from the test dataset <br>idx = np.random.choice(X_test.shape[0], 1)<br>print(&#39;Actual sentiment for this review is&#39;, class_names[y_test[idx]])<br>explainer.explain_instance(X_test[0], predict_proba)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pUqraS-oAJUluEJU.png" /></figure><p><strong>(4) Captum</strong></p><p><a href="https://captum.ai/">Captum</a> (“comprehension” in Latin) is an open source, extensible library for model interpretability built on PyTorch.</p><ul><li>A framework that <a href="https://captum.ai/docs/captum_insights">can be used for any PyTorch-based model</a>.</li><li>Captum is multi modal and works with text, computer vision, and tabular data.</li><li>Three kinds of attributions are available in Captum: <strong>Feature Attribution, Layer Attribution, and Neuron Attribution </strong>[9].</li><li><a href="https://captum.ai/docs/captum_insights">Captum Insights</a> is an interpretability visualization widget built on top of Captum to facilitate model understanding.</li></ul><p>Below is an example of how we can visualize attributions of a sentiment analyzer for the text tokens, using Captum.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/0*2RAGZq0JqFJIDao0.png" /></figure><blockquote>Interpreting sentiment analyzer using Captum (python code) [<a href="https://captum.ai/tutorials/IMDB_TorchText_Interpret">Link</a>]</blockquote><p><strong>(5) Transformers-interpret</strong></p><p><a href="https://github.com/cdpierse/transformers-interpret">Transformers-Interpret</a> is a model explainability tool designed to work exclusively with 🤗 transformers.</p><ul><li>It allows any transformers model to be explained <strong>in just two lines</strong>.</li><li>It allows you to get word attributions and visualizations for those attributions.</li><li>Right now the package supports all transformer models with a sequence classification head.</li><li>Explainers are available for both text and computer vision models.</li><li>Visualizations are also available in notebooks and as savable png and html files.</li></ul><p>Here is a code snippet on how to get word attributions and visualizations for the sentiment analyzer model’s output in just <strong>2 lines of code</strong>. [10]</p><pre>from transformers import AutoModelForSequenceClassification, AutoTokenizer<br>from transformers_interpret import SequenceClassificationExplainer<br><br><br>model = AutoModelForSequenceClassification.from_pretrained(&quot;distilbert-base-uncased-finetuned-sst-2-english&quot;)<br>tokenizer = AutoTokenizer.from_pretrained(&quot;distilbert-base-uncased-finetuned-sst-2-english&quot;)<br><br>cls_explainer = SequenceClassificationExplainer(&quot;I love you, I like you&quot;, model, tokenizer)<br>attributions = cls_explainer()<br><br>print(attributions.word_attributions)<br>cls_explainer.visualize(&quot;distilbert_example.html&quot;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eXdn1-T2eB8VGY6B.png" /></figure><p><strong>(6) Exbert</strong></p><p><a href="https://github.com/bhoov/exbert?tab=readme-ov-file">Exbert</a> is a tool that enables users to explore the learned attention weights and contextual representations of different <strong>hugging face Transformer models</strong>.</p><ul><li>ExBERT renders visualizations which provide insight into both the attention and the token embeddings for the model and corpus.</li><li>Support for many of the common Transformer models.</li><li>Discover linguistic features (e.g., Part of Speech, Dependencies, Entities) learned by particular heads.</li><li>A bit old though 👵</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gb1MSNneGPejIBbh.png" /></figure><h3>The necessity of Explainable AI</h3><p>Here are four common reasons why we need to go for XAI [6]:</p><ul><li><strong><em>Explain to Justify</em></strong><em>:</em> To justify the result, explainable AI is used to deliver the information, mainly when it comes to real-world critical situations with the name of instances called “Right to Explanation” guidelines involved in GDPR (General Data Protection Regulation).</li><li><strong><em>Explain to Control</em></strong><em>:</em> Apart from justification, it is also necessary to control the explanation from going wrong and used to identify the vulnerabilities and flaws.</li><li><strong><em>Explain to Improve</em></strong><em>: Another needs to build an explainable model is to improve them continuously by knowing the flaws. This can be achieved by using the explainable model.</em></li><li><strong><em>Explain to Discover</em></strong><em>: It is used to discover the new facts for the problem, thus gaining knowledge by gathering the information, which will create a new insight.</em></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*thZyHu8BoJQemUu9.jpg" /></figure><p><strong>References:</strong></p><p>[1] <a href="https://towardsdatascience.com/explainable-artificial-intelligence-14944563cc79">https://towardsdatascience.com/explainable-artificial-intelligence-14944563cc79</a></p><p>[2] <a href="https://cloud.google.com/explainable-ai">https://cloud.google.com/explainable-ai</a></p><p>[3] <a href="https://www.ibm.com/topics/explainable-ai">https://www.ibm.com/topics/explainable-ai</a></p><p>[4] <a href="https://github.com/shap/shap">https://github.com/shap/shap</a></p><p>[5] <a href="https://pair-code.github.io/lit/">https://pair-code.github.io/lit/</a></p><p>[6] <a href="https://www.sciencedirect.com/science/article/pii/S277266222300070X">https://www.sciencedirect.com/science/article/pii/S277266222300070X</a></p><p>[7] <a href="https://medium.com/@afanta/lime-vs-shap-a92623e95c4">https://medium.com/@afanta/lime-vs-shap-a92623e95c4</a></p><p>[8] Danilevsky, Marina, et al. “A survey of the state of explainable AI for natural language processing.” <em>arXiv preprint arXiv:2010.00711</em> (2020).</p><p>[9] <a href="https://pytorch.org/tutorials/beginner/introyt/captumyt.html#:~:text=Captum%20(%E2%80%9Ccomprehension%E2%80%9D%20in%20Latin,model%20interpretability%20built%20on%20PyTorch.">https://pytorch.org/tutorials/beginner/introyt/captumyt.html#:~:text=Captum%20(%E2%80%9Ccomprehension%E2%80%9D%20in%20Latin,model%20interpretability%20built%20on%20PyTorch.</a></p><p>[10] <a href="https://towardsdatascience.com/introducing-transformers-interpret-explainable-ai-for-transformers-890a403a9470">https://towardsdatascience.com/introducing-transformers-interpret-explainable-ai-for-transformers-890a403a9470</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9fa6f9ac2c71" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[All You Need to Know about Active Learning — Part 1]]></title>
            <link>https://medium.com/@farnazgh73/ultimate-guide-for-active-learning-main-approaches-3cf53ce207f0?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3cf53ce207f0</guid>
            <category><![CDATA[active-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[annotations]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Mon, 26 Jun 2023 15:15:42 GMT</pubDate>
            <atom:updated>2024-06-03T14:25:56.528Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CsiFGmHcQOS_-ubJ.png" /></figure><p>With the advent and explosion of deep-learning based ML-models, and more recently foundation models, active learning is becoming ever-more important to bridge the gap between prototype and production AI models. Active learning is even heralded as the future of generative AI. The main goal of active learning is to intelligently curate training data and improve model performance by surfacing the most informative data samples for labeling to reduce uncertainty in your model and reduce the cost of manually labeled data.</p><p>Active learning’s idea is simple: let the model choose the samples for annotation instead of labeling the whole dataset. This method leads to a more efficient annotation process. Active Learning models can achieve the baseline performance (the accuracy of the model trained on the whole dataset), with a considerably lower amount of labeled data.</p><blockquote><strong>Active learning is a powerful approach for improving the performance of machine learning models by reducing labeling costs and improving accuracy and generalization.</strong></blockquote><h3>What is Active Learning?</h3><p>Active learning is a supervised machine learning approach that aims to optimize annotation using a few small training samples. During the training stage and even into the production stage, a supervised machine learning process involves a continuous feedback loop whereby annotators and data scientists provide more data points to keep improving the model’s performance and accuracy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/0*VYi0iMVOjBF_aPhy.png" /></figure><h3>Difference Between Active Learning and Passive Learning</h3><p><a href="https://towardsdatascience.com/explaining-reinforcement-learning-active-vs-passive-a389f41e7195">Passive learning</a> and active learning are two different approaches to machine learning. In passive learning, the model is trained on a predefined labeled dataset, and the learning process is complete once the model is trained.</p><p>In contrast, in active learning, the informative data points are selected using query strategies instead of a pre-defined labeled dataset. These are then passed to be labeled by an annotator before being used to train the model. By iterating this process of using informative samples, we constantly work on improving the performance of a predictive model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*30OcVUcDs7ED4mxU.png" /></figure><h3>Advantages of Active Learning</h3><p>Unfortunately, data annotation can be a costly and time-consuming endeavor, especially when outsourcing this work to large teams of human annotators. Many teams don’t have the time, money, or manpower to label and review each piece of data in these vast datasets.</p><p>Active learning is a powerful technique that can help overcome these challenges by allowing a machine learning model to selectively query a human annotator for the most informative data points to label in image or video-based datasets. By iteratively selecting the most informative samples to label, active learning can help improve the accuracy of machine learning models while reducing the amount of labeled data required.</p><ul><li><strong>Reduced Labeling Costs</strong>: Labeling large datasets is time-consuming and expensive. Active learning helps to reduce labeling costs by selecting the most informative samples that require labeling.</li><li><strong>Improved Accuracy:</strong> Active learning improves the accuracy of machine learning models by selecting the most informative samples for labeling.</li><li><strong>Faster Convergence</strong>: The model can learn more quickly and converge faster by focusing on the most relevant samples.</li><li><strong>Improved Generalization</strong>: Active learning helps machine learning models to generalize better to new data by selecting the most diverse samples for labeling.</li><li><strong>Robustness to Noise:</strong> By selecting the most informative samples, active learning algorithms are trained on the samples that best represent the entire dataset. Hence, the models trained on these samples will perform well on the best data points and the outliers.</li></ul><h3><strong>Two main type of Active Learning Strategies:</strong></h3><ul><li><strong>Informativeness</strong><br>Informativeness-based query strategies mostly assign an informative measure to each unlabeled instance individually. The instance(s) with the highest measure will be selected.</li><li><strong>Representativeness</strong><br>Only considering the informativeness of individual instances may have the drawback of sampling bias and the selection of outliers. Therefore, representativeness, which measures how instances correlate with each other, is another major factor to consider when designing AL query strategies.</li><li><strong>Hybrid</strong><br>There is no surprise that informativeness and representativeness can be combined for instance querying, leading to hybrid strategies. A simple combination can be used to merge multiple criteria into one.</li></ul><h3>Categorizing Active Learning Strategies</h3><p>This step of selecting the data points can be categorized into three methods:</p><ul><li><strong>Selective Sampling</strong>: The sampling strategy measures the informativeness of the samples and determines which samples the model should request labels for to improve its performance. For example, uncertainty sampling selects the samples the model is most uncertain about, while diversity sampling selects the samples most dissimilar to the samples already seen.</li><li><strong>Pool-based Sampling</strong>: In this approach, a pool of unlabeled data is created, and the model selects the most informative examples from this pool to be labeled by an expert or a human annotator.Pool-based sampling can be further categorized into uncertainty sampling, query-by-committee, and density-weighted sampling.</li><li><strong>Synthesis Methods</strong>: Query synthesis methods are a group of active learning strategies that generate new samples for labeling by synthesizing them from the existing labeled data. The methods are useful when your labeled dataset is small, and the cost of obtaining new labeled samples is high.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*TUiRt9voYmhGFFXZ.png" /></figure><h3>Active Learning Tools and Strategies</h3><p>In <strong>Next section</strong>, we will continue the talk about <strong>(a) active learning (b) strategies</strong>, <strong>active learning for NLP</strong>, <strong>(c) Popular active learning tools</strong> and <strong>(d) active learning in production</strong> and when we need to stop the process.</p><p><a href="https://medium.com/@farnazgh73/active-learning-tools-and-strategies-part-2-325c2be2ca00">Active Learning Tools and Strategies</a></p><h3>References:</h3><p>[1] <a href="https://aclanthology.org/2020.lrec-1.348">Optimizing Annotation Effort Using Active Learning Strategies: A Sentiment Analysis Case Study in Persian</a> (Ashrafi Asli et al., LREC 2020)</p><p>[2] <a href="https://encord.com/blog/top-active-learning-tools-for-machine-learning/">Top 6 Tools for Active Learning in Machine Learning</a></p><p>[3] <a href="https://aclanthology.org/2022.emnlp-main.414">A Survey of Active Learning for Natural Language Processing</a> (Zhang et al., EMNLP 2022)</p><p>[4] <a href="https://neptune.ai/blog/active-learning-strategies-tools-use-cases">Active Learning: Strategies, Tools, and Real-World Use Cases</a></p><p>[5] <a href="https://huggingface.co/blog/autonlp-prodigy">Active Learning with AutoNLP and Prodigy</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3cf53ce207f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Main Libraries for Text Pre-processing]]></title>
            <link>https://medium.com/@farnazgh73/main-libraries-for-text-pre-processing-1d9bd2ba92db?source=rss-5385b1c333f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d9bd2ba92db</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[spacy]]></category>
            <category><![CDATA[nltk]]></category>
            <category><![CDATA[text-preprocessing]]></category>
            <dc:creator><![CDATA[Farnaz Ghassemi Toudeshki]]></dc:creator>
            <pubDate>Fri, 23 Jun 2023 07:53:57 GMT</pubDate>
            <atom:updated>2024-05-30T22:05:13.964Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*ArfYQnnxXc-BkXLr.jpg" /></figure><h3>Motivations for Text Pre-processing</h3><ul><li><strong>Converting</strong> text to a format amenable for <strong>data analysis </strong>and ML</li><li><strong>Enriching</strong> text with l<strong>inguistic annotations</strong> useful for data analysis and ML</li></ul><h3>Main Libraries:</h3><p>NLTK, SpaCy, StanfordCoreNLP, tokenization, lowercasing, pos tagging,..</p><h3>Common Pre-processing Steps</h3><p>1- <strong>Tokenization </strong>and Sentence segmentation</p><p>2- Removing <strong>Punctuation Signs</strong> and <strong>Stop Words</strong></p><p>3- <strong>Lower casing</strong></p><p>4- <strong>Lemmatization</strong> and <strong>Stemming</strong></p><p>5- <strong>POS</strong> tagging (To disambiguate / To group together words of the same type)</p><p>6- <strong>Named Entity Recognition</strong></p><p>7- <strong>Syntactic</strong> and <strong>Semantic</strong> Parsing (To identify relations between sentence chunks (constituents))</p><h3>Python Libraries for Text Pre-processing</h3><p><strong>(1) </strong><a href="https://www.nltk.org/"><strong>NLTK</strong></a></p><ul><li>Very wide range of <strong>tools and resources for NLP</strong></li><li>interfaces to over <strong>50 corpora and lexical resources</strong> such as <strong>WordNet</strong></li><li>text processing libraries for <strong>classification</strong>, <strong>tokenization</strong>, <strong>stemming</strong>,<br><strong>tagging</strong>, <strong>parsing</strong>, and<strong> semantic reasoning</strong></li></ul><p><a href="https://medium.com/@farnazgh73/nltk-cheatsheet-4b5875f32704">NLTK Cheatsheet</a></p><p><strong>(2) </strong><a href="https://spacy.io/"><strong>SpaCy</strong></a></p><ul><li>Library for Natural Language Processing</li><li><strong>pre-trained statistical models</strong> and <strong>word vectors</strong></li><li><strong>Convolutional neural network </strong>models for <strong>tagging, parsing and named<br>entity recognition</strong></li><li>Interacts well with <strong>Deep Learning Libraries</strong></li></ul><p><a href="https://medium.com/@farnazgh73/spacy-library-cheatsheet-0a0534240372">SpaCy Library Cheatsheet</a></p><p><strong>(3)</strong><a href="https://stanfordnlp.github.io/CoreNLP/"><strong> Stanford CoreNLP</strong></a></p><ul><li>Java library</li><li>part-of-speech (POS) tagger</li><li>named entity recognizer (NER)</li><li>coreference resolution system</li></ul><p><a href="https://medium.com/@farnazgh73/stanford-corenlp-library-fc36b4297db9">Stanford CoreNLP Library</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d9bd2ba92db" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>