<?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 Varun Sampath Kumar on Medium]]></title>
        <description><![CDATA[Stories by Varun Sampath Kumar on Medium]]></description>
        <link>https://medium.com/@varun_54675?source=rss-7d2a6db4ea90------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*GGHTBrc4wd3zSWxk</url>
            <title>Stories by Varun Sampath Kumar on Medium</title>
            <link>https://medium.com/@varun_54675?source=rss-7d2a6db4ea90------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 14:20:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@varun_54675/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[Building a Tiny CLIP: Contrastive Image-Text Learning from Scratch]]></title>
            <link>https://medium.com/@varun_54675/building-a-tiny-clip-contrastive-image-text-learning-from-scratch-0ca31bf40c65?source=rss-7d2a6db4ea90------2</link>
            <guid isPermaLink="false">https://medium.com/p/0ca31bf40c65</guid>
            <category><![CDATA[retrieval-augmented-gen]]></category>
            <category><![CDATA[multimodal-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[genarative-ai]]></category>
            <dc:creator><![CDATA[Varun Sampath Kumar]]></dc:creator>
            <pubDate>Sat, 07 Jun 2025 17:42:02 GMT</pubDate>
            <atom:updated>2025-06-07T17:42:02.515Z</atom:updated>
            <content:encoded><![CDATA[<p>In recent years, multimodal learning — where models understand and relate information across different data types such as images and text — has revolutionized how machines interpret the world. One of the landmark advances in this field is <strong>CLIP (Contrastive Language–Image Pre-training)</strong>, introduced by OpenAI in 2021 in the paper “Learning Transferable Visual Models From Natural Language Supervision”. CLIP is a neural network trained on a vast dataset of <strong>400 million image-text pairs</strong> collected from the internet. This massive scale of diverse data enables CLIP to learn rich, generalized visual and textual representations that can connect images to natural language descriptions effectively.</p><p>Unlike traditional supervised models trained for specific tasks or limited categories, CLIP’s <strong>contrastive learning approach</strong> aligns image embeddings with their corresponding text embeddings in a shared latent space. This enables impressive zero-shot capabilities, meaning CLIP can recognize and classify images based on arbitrary textual prompts, without explicit task-specific training. For example, it can correctly identify objects, actions, and even styles in images simply by matching them to descriptive phrases it has never seen during training.</p><p>Due to its powerful zero-shot and transfer learning abilities, CLIP has become a foundational model for numerous applications such as:</p><ul><li>Zero-shot image classification without labeled training data</li><li>Image search and retrieval based on text queries</li><li>Multimodal content understanding for captioning, generation, and editing</li><li>Assisting creative tools with natural language control over visual content</li></ul><p>In this blog, we explore how to build a simplified CLIP-like model trained on the <strong>Oxford Flowers102 dataset</strong>, showcasing how contrastive learning between images and text can be applied even on smaller, domain-specific datasets. We’ll walk through data preparation, model architecture and training to demonstrate the core principles of CLIP in practice.</p><h3>CLIP Architecture Explained</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fRohsFl-QBcH0BGD.jpg" /><figcaption>Figure 1: CLIP Architecture</figcaption></figure><h4>At its core, <strong>CLIP (Contrastive Language–Image Pre-training)</strong> consists of two powerful components working together:</h4><ul><li>An <strong>Image Encoder</strong></li><li>A <strong>Text Encoder</strong></li></ul><h4>Both of these modules process their respective modalities independently and then project their outputs into a shared embedding space where their similarities can be measured. Let’s break down each piece:</h4><h4>🔍 Image Encoder</h4><p>CLIP typically uses a <strong>Vision Transformer (ViT)</strong> or a <strong>ResNet</strong>-based architecture to process images. The image is transformed into a fixed-dimensional vector representation (embedding). In our simplified implementation, we use <strong>ResNet-18</strong> pretrained on ImageNet as the backbone.</p><p><strong>Key steps in image encoding:</strong></p><ol><li><strong>Preprocess</strong> the image (resize, normalize).</li><li>Pass it through a CNN backbone (like ResNet-18).</li><li>Extract the final feature representation before the classification head.</li><li>Project this feature vector into a lower-dimensional <strong>shared embedding space</strong> using a learnable linear layer.</li><li>Normalize the embedding vector (unit norm) to prepare it for cosine similarity-based comparison.</li></ol><h4>🔍 Text Encoder</h4><p>On the text side, CLIP originally uses a <strong>Transformer-based language model</strong> like a GPT variant or a Transformer encoder. In our implementation, we use <strong>DistilBERT</strong>, a lightweight, efficient transformer pretrained on English text.</p><p><strong>Key steps in text encoding:</strong></p><ol><li><strong>Tokenize</strong> the input text using the appropriate tokenizer.</li><li>Pass tokens through the Transformer encoder to obtain contextualized embeddings.</li><li>Extract the embedding.</li><li>Project this embedding into the shared embedding space with a learnable linear layer.</li><li>Normalize the embedding to unit norm.️</li></ol><h4>📊 Contrastive Learning Objective</h4><p>Once we have both image and text embeddings, CLIP computes the <strong>cosine similarity</strong> between each image-text pair in a batch. The <strong>contrastive loss</strong> then encourages matched image-text pairs to have high similarity, while unrelated pairs are pushed apart.</p><p><strong>Loss Function:</strong><br> For a batch of N image-text pairs:</p><ul><li>Compute a similarity matrix between all image and text embeddings.</li><li>Use <strong>cross-entropy loss</strong> with temperature scaling, treating each image-text pair as a positive example and others as negatives.</li></ul><p>This training strategy aligns the two modalities in a shared space, enabling zero-shot classification: for any given image, we can compute its similarity with any set of textual descriptions and pick the most similar one as the predicted label.</p><h3>🖥️ CLIP Zero-Shot Image Classification — Code Implementation</h3><h4>📦 Install &amp; Import Dependencies</h4><pre>import torch<br>import torch.nn as nn<br>from torch.utils.data import DataLoader<br>from torchvision import transforms<br>from torchvision.models import resnet18<br>from transformers import AutoTokenizer, AutoModel<br>import torch.nn.functional as F<br>from torch.optim import Adam<br>from torch.optim.lr_scheduler import CosineAnnealingLR<br>from torch.cuda import amp<br>from tqdm import tqdm<br>import requests<br>from scipy.io import loadmat</pre><h4>📚 Dataset Preparation — Oxford Flowers-102 🌸</h4><pre>from torchvision.datasets import Flowers102<br><br>transform = transforms.Compose([<br>    transforms.Resize(256),<br>    transforms.CenterCrop(224),<br>    transforms.ToTensor(),<br>    transforms.Normalize([0.485, 0.456, 0.406],<br>                         [0.229, 0.224, 0.225])<br>])<br><br>train_ds = Flowers102(&quot;.&quot;, split=&quot;train&quot;, download=True, transform=transform)<br>test_ds  = Flowers102(&quot;.&quot;, split=&quot;test&quot;, download=True, transform=transform)<br><br>train_dl = DataLoader(train_ds, batch_size=256, shuffle=True)<br>test_dl  = DataLoader(test_ds, batch_size=256, shuffle=False)<br><br>prompt_templates = [<br>    &quot;a photo of a {}&quot;,<br>    &quot;a picture of a {}&quot;,<br>    &quot;a close-up of a {}&quot;,<br>    &quot;a detailed photo of a {}&quot;,<br>    &quot;a bright photo of a {}&quot;,<br>    &quot;a cropped photo of a {}&quot;,<br>    &quot;an artistic photo of a {}&quot;<br>]<br><br>url = &quot;https://gist.githubusercontent.com/JosephKJ/94c7728ed1a8e0cd87fe6a029769cde1/raw/403325f5110cb0f3099734c5edb9f457539c77e9/Oxford-102_Flower_dataset_labels.txt&quot;<br>response = requests.get(url)<br>class_names = response.text.strip().split(&#39;\n&#39;)<br>class_names = [name.strip().strip(&quot;&#39;&quot;) for name in class_names]<br><br>labels_mat = loadmat(&#39;/path/to/imagelabels.mat&#39;)<br>labels_array = labels_mat[&#39;labels&#39;][0]<br>idx_to_classname = {idx: class_names[label-1] for idx, label in enumerate(labels_array)}</pre><h4>🎨 Define Image Encoder</h4><pre>class CLIPImageEncoder(nn.Module):<br>    def __init__(self, embed_size=512):<br>        super().__init__()<br>        resnet = resnet18(pretrained=True)<br>        modules = list(resnet.children())[:-1]<br>        self.resnet_backbone = nn.Sequential(*modules)<br>        self.projection = nn.Linear(resnet.fc.in_features, embed_size)<br><br>    def forward(self, images):<br>        x = self.resnet_backbone(images)<br>        x = x.view(x.size(0), -1)<br>        x = self.projection(x)<br>        return F.normalize(x, dim=-1)</pre><h4>✍️ Define Text Encoder</h4><pre>class CLIPTextEncoder(nn.Module):<br>    def __init__(self, model_name=&#39;distilbert-base-uncased&#39;, embed_size=512):<br>        super().__init__()<br>        self.tokenizer = AutoTokenizer.from_pretrained(model_name)<br>        self.text_encoder = AutoModel.from_pretrained(model_name)<br>        self.projection = nn.Linear(self.text_encoder.config.hidden_size, embed_size)<br><br>    def forward(self, texts):<br>        x = self.tokenizer(texts, return_tensors=&#39;pt&#39;, padding=True, truncation=True)<br>        x = {key: val.to(next(self.parameters()).device) for key, val in x.items()}<br>        output = self.text_encoder(**x)<br>        cls_embedding = output.last_hidden_state[:, 0, :]<br>        x = self.projection(cls_embedding)<br>        return F.normalize(x, dim=-1)</pre><h4>📊 Define Contrastive Loss</h4><pre>def contrastive_loss(image_embedding, text_embedding, temperature=0.05):<br>    logits = image_embedding @ text_embedding.T / temperature<br>    labels = torch.arange(len(logits)).to(logits.device)<br>    loss_img = F.cross_entropy(logits, labels)<br>    loss_txt = F.cross_entropy(logits.T, labels)<br>    return (loss_img + loss_txt) / 2</pre><h4>📈 Training Loop</h4><pre>def train(model, train_dl, epochs, device):<br>    model = model.to(device)<br>    optimizer = Adam(model.parameters(), lr=0.001)<br>    scheduler = CosineAnnealingLR(optimizer, T_max=epochs, eta_min=1e-6)<br>    scaler = amp.GradScaler()<br><br>    model.train()<br>    for epoch in range(epochs):<br>        running_loss = []<br>        for images, label_idxs in tqdm(train_dl):<br>            images = images.to(device)<br>            captions = [random.choice(prompt_templates).format(idx_to_classname[idx.item()]) for idx in label_idxs]<br><br>            with amp.autocast():<br>                image_embed, text_embed = model(images, captions)<br>                loss = contrastive_loss(image_embed, text_embed)<br><br>            scaler.scale(loss).backward()<br>            scaler.step(optimizer)<br>            scaler.update()<br>            optimizer.zero_grad()<br><br>            running_loss.append(loss.item())<br>        scheduler.step()<br>        print(f&quot;Epoch {epoch+1}, Loss: {sum(running_loss)/len(running_loss):.4f}&quot;)</pre><h4>📝 Zero-Shot Evaluation</h4><blockquote><em>Once trained, instead of predicting class indices directly, we compute cosine similarities between the image embedding and class name text embeddings generated via prompt templates. The class with highest similarity score becomes the prediction.</em></blockquote><pre>def evaluate(model, test_dl, idx_to_classname, prompt_templates, device):<br>    model.eval()<br>    all_class_prompts = [[template.format(class_name) for template in prompt_templates]<br>                         for class_name in idx_to_classname.values()]<br><br>    flattened_prompts = [prompt for prompts in all_class_prompts for prompt in prompts]<br>    with torch.no_grad():<br>        text_embeds = []<br>        for i in range(0, len(flattened_prompts), 256):<br>            batch_prompts = flattened_prompts[i:i+256]<br>            emb = model.text_encoder(batch_prompts).cpu()<br>            text_embeds.append(emb)<br>        text_embeds = torch.cat(text_embeds)<br>        num_templates = len(prompt_templates)<br>        text_embeds = text_embeds.view(len(idx_to_classname), num_templates, -1).mean(dim=1)<br>        text_embeds = F.normalize(text_embeds, dim=-1)<br><br>    correct, total = 0, 0<br>    for images, label_idxs in tqdm(test_dl):<br>        images = images.to(device)<br>        label_idxs = label_idxs.to(device)<br>        with torch.no_grad():<br>            image_embeds = model.image_encoder(images)<br>        logits = image_embeds @ text_embeds.T.to(device)<br>        preds = logits.argmax(dim=1)<br>        correct += (preds == label_idxs).sum().item()<br>        total += images.size(0)<br><br>    accuracy = correct / total<br>    print(f&quot;Zero-shot Test Accuracy: {accuracy*100:.2f}%&quot;)</pre><h3>📌 Conclusion and Next Steps</h3><p>In this blog, we built a simplified version of the CLIP model — a powerful image-text model capable of zero-shot classification by learning a shared embedding space for both images and text. While OpenAI&#39;s original CLIP was trained on a massive <strong>400 million (image, text)</strong> pairs scraped from the web, here we demonstrated how the same idea can be prototyped quickly using smaller datasets like <strong>Oxford 102 Flowers</strong> for experimentation and learning purposes.</p><p>The beauty of this setup is its flexibility:</p><ul><li>You can easily swap out the <strong>dataset</strong> — using more diverse or domain-specific image-text datasets (like <strong>MS-COCO</strong>, <strong>Flickr30k</strong>, or even your own custom collection) to build models better suited for your applications.</li><li>The <strong>encoders</strong> are modular. We used <strong>ResNet-18</strong> for images and <strong>DistilBERT</strong> for text, but you could replace them with other architectures.</li><li>The <strong>training and evaluation code flow remains largely the same</strong> — you just need to adjust your encoders, projection heads, and dataset preparation.</li></ul><h3>References</h3><ol><li>Radford, A., et al. (2021). <em>Learning Transferable Visual Models From Natural Language Supervision</em>. arXiv:2103.00020.</li><li><a href="https://www.youtube.com/watch?v=T9XSU0pKX2E">Paper walkthrough by Yannik Kilcher</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0ca31bf40c65" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Unlearning Tutorial 3: Approximate Unlearning via Negative Gradients]]></title>
            <link>https://medium.com/@varun_54675/machine-unlearning-tutorial-3-approximate-unlearning-via-negative-gradients-266835a7b4c4?source=rss-7d2a6db4ea90------2</link>
            <guid isPermaLink="false">https://medium.com/p/266835a7b4c4</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[genai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[machine-unlearning]]></category>
            <dc:creator><![CDATA[Varun Sampath Kumar]]></dc:creator>
            <pubDate>Wed, 30 Apr 2025 16:13:54 GMT</pubDate>
            <atom:updated>2025-04-30T16:13:54.582Z</atom:updated>
            <content:encoded><![CDATA[<p>In this blog series, I’ve been exploring the landscape of Machine Unlearning techniques — methods that allow trained machine learning models to selectively forget specific data points without retraining from scratch. In <a href="https://medium.com/@varun_54675/machine-unlearning-tutorial-1-introduction-1227471fc169"><strong>Part 1</strong></a>, we introduced the fundamentals of Machine Unlearning, its motivations, and categorized the various existing techniques into exact and approximate approaches. <a href="https://medium.com/@varun_54675/machine-unlearning-tutorial-2-exact-unlearning-31851bb4a3fe"><strong>Part 2</strong></a> dove deeper into practical unlearning strategies, covering <strong>retraining-based methods</strong> and <strong>SISA (Sharded, Isolated, Sliced Aggregation)</strong>, a clever technique that structurally supports forgetting by design.</p><p>In this third installment, we’re going to focus on an approximate unlearning method based on <strong>negative gradients</strong> — also known as <strong>gradient ascent-based approximate unlearning</strong>. The core idea is intuitive: if training a model involves moving in the direction of minimizing loss for a given data point, then “forgetting” it can be approximated by taking a small step in the <em>opposite</em> direction. This technique offers a fast, resource-efficient way to mitigate the influence of a data point on a trained model, making it highly valuable in time-critical or resource-constrained scenarios.</p><p>In this post, we’ll break down the intuition, mathematical formulation, algorithm, and limitations of this approach, and discuss where it fits into the broader Machine Unlearning toolkit.</p><h3>📌 Why Do We Use Gradient Descent During Training?</h3><p>At the core of training any machine learning model lies the goal of <strong>minimizing a loss function</strong> — a mathematical expression that quantifies how far off the model’s predictions are from the actual values. The lower the loss, the better the model is at making accurate predictions.</p><p>To minimize this loss, we use an optimization technique called <strong>gradient descent</strong>.</p><p>Here’s how it works: during each iteration of training, a <strong>batch of data points</strong> is passed through the model to compute predictions. These predictions are compared against the true labels to calculate the <strong>loss for the batch</strong>. The model then computes the <strong>gradient of this loss with respect to its parameters</strong> — essentially measuring how much and in which direction each parameter should change to reduce the loss.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*FqokSkpZDg20HxfLp7Ry5Q.png" /><figcaption>Figure 1: Illustration of how weight are moved based on gradient</figcaption></figure><p>Once these gradients are computed, the parameters are updated by taking a small step in the <strong>opposite direction of the gradient</strong> (since we want to minimize, not increase, the loss). This process is repeated over many iterations and batches until the model parameters settle at values that yield minimal prediction error across the dataset. In short — we use gradient descent because it incrementally moves our model towards becoming better at making predictions on the data it sees.</p><h3>📌 What is Gradient Ascent?</h3><p>While <strong>Gradient Descent</strong> is used to find the minimum of a function by moving in the direction of steepest decrease, <strong>Gradient Ascent</strong> does the exact opposite — it’s used to find the <strong>maximum of a function</strong> by moving in the direction of steepest increase.</p><p>Just like in gradient descent, at each iteration, we calculate the <strong>gradient</strong> of the function with respect to its parameters. But instead of moving against the gradient (to go downhill), we move <strong>with the gradient (uphill)</strong>, since our goal is to maximize the function.</p><p>Formally, if f(θ) is the function we want to maximize and θ is the learnable parameter , then parameter update rule is: θ ← θ + η*∇​f(θ)</p><p>Where:</p><ul><li>θ is the set of parameters</li><li>η is the learning rate or step size, and</li><li>∇f(θ) is the gradient of the function with respect to those parameters</li></ul><h3>🔍 What happens when you remove a datapoint?</h3><p>When a model is trained, it updates its parameters to minimize a <strong>loss function</strong> based on all the data points it sees. Each data point contributes to the overall direction and magnitude of these updates through its gradient — essentially nudging the model parameters to better predict that specific instance.</p><p>If you were to <em>remove</em> a data point from the training set, ideally, you would want to undo the influence it had during training. The most accurate way to do this is by <strong>retraining the model from scratch without that data point</strong>. However, this is computationally expensive and often impractical for large models and frequent deletion requests.</p><p>This is where <strong>approximate unlearning</strong> comes in — rather than retraining, we attempt to reverse the effect of the data point on the model parameters through a lightweight adjustment.</p><h3>💡 Intuition: Reverse the Learning Effect</h3><p>Think of the training process as taking small steps downhill on a loss landscape (via <strong>gradient descent</strong>) to reach the lowest possible point (minimum loss). Each step moves in the direction of the negative gradient of the loss with respect to the model parameters.</p><p>Now, if you wanted to “forget” the contribution of a specific data point, a simple intuition is:<br> <strong>“What if we just moved a small step back in the opposite direction for that point?”</strong></p><p>In other words — if training on a data point involved updating the model by moving <strong>against the gradient of its loss</strong>, we could attempt to undo this by taking a <strong>small step along the gradient</strong>. This is essentially a <strong>gradient ascent</strong> step for that data point alone.</p><h3>⚙️ Negative Gradient (Gradient Ascent) Step</h3><p>Formally, during training, for a data point xi​ with a loss function L(θ,xi), we update the model parameters using <strong>gradient descent</strong>: θ ← θ − η*∇L(θ,xi)</p><p>To approximately forget xi where xi belongs to forget set​, we perform a <strong>negative gradient (gradient ascent) step</strong>: θ ← θ + η*∇L(θ,xi)</p><p>The idea is that this step would <strong>partially negate the influence</strong> of the data point on the model. Since it’s typically infeasible to exactly cancel out its historical impact (especially in non-convex models), this technique serves as a fast, approximate alternative.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uVKerc8jDm8VKCCt.gif" /><figcaption>Figure 2: Illustration Learning vs Unlearning: A Gradient-Based Perspective</figcaption></figure><h3>Experimental Results on MNIST</h3><p>Let’s put these ideas to the test with a classic dataset — <strong>MNIST</strong>. In this experiment, I aim to <strong>simulate unlearning</strong> by removing an entire <strong>class</strong> from the dataset — specifically, all instances of the digit <strong>7</strong> in MNIST.</p><p>🧠 While this setup focuses on unlearning a class for <strong>demonstration purposes</strong>, the same theories and approaches can be extended to <strong>sample-level deletions</strong> as well!</p><h4>✅ Setup:</h4><ul><li><strong>Dataset</strong>: MNIST (28x28 grayscale digits 0–9)</li><li><strong>Class to Forget</strong>: Digit 7</li><li><strong>Goal</strong>: Ensure models “forget” the digit 7 post-unlearning, while preserving performance on the remaining digits.</li><li><strong>Models Used</strong>: A simple 2-layer Convolutional Neural Network built with PyTorch.</li></ul><h4><strong>Metrics Evaluated</strong>:</h4><ul><li><strong>Overall Accuracy</strong> (on the full test set)</li><li><strong>Accuracy on Deleted Class (7)</strong> (should drop to near 0)</li><li><strong>Unlearning Time</strong> (how long each method takes to forget)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*CFYgEbQLdJtm4e9VV1TerA.png" /><figcaption>Results from CNN</figcaption></figure><p>Link to Code: <a href="https://github.com/Lordvarun23/Code-for-my-blogs/blob/main/Machine%20Unlearning/Negative%20Gradient(Gradient%20Ascent).ipynb">link to github</a></p><p>In this experiment, we explored the effects of <strong>gradient ascent-based unlearning</strong> on the model’s ability to forget class 7 (digit 7) from the MNIST dataset. The results, however, reveal some important nuances:</p><h4>1. Rapid Forgetting:</h4><p>Gradient ascent, which is used in unlearning methods, effectively removes the influence of specific data by adjusting the model parameters in the opposite direction of the gradient. This leads to a <strong>quick reduction in the model’s confidence</strong> in the forgotten data. In our experiment, after performing unlearning for 3 epochs, the <strong>Class 7 accuracy</strong> dropped significantly to <strong>93.29%</strong> while overall accuracy remained relatively stable at <strong>98.16%</strong>. However, with unlearning for <strong>10 epochs</strong>, we observed a dramatic decline, with <strong>Class 7 accuracy dropping to 0%</strong>, but the overall accuracy plummeted to a mere <strong>9.80%</strong>. This highlights how fast and aggressive unlearning can disrupt the model’s performance.</p><h4>2. Model Utility Degradation:</h4><p>While gradient ascent can help with forgetting, the <strong>overall performance</strong> of the model suffers as a result. In our case, the model’s ability to classify the other digits (except class 7) deteriorated significantly with prolonged unlearning. This can be attributed to the fact that unlearning methods, like gradient ascent, aim to disrupt the model’s learned weights in ways that can <strong>negatively affect its ability to generalize</strong>, even on the remaining data.</p><h4>3. Catastrophic Collapse:</h4><p>One of the key challenges we observed was <strong>catastrophic collapse</strong> — a situation where the model’s utility on retained data significantly degrades. This often occurs because <strong>gradient ascent</strong> can inadvertently change model parameters in ways that are detrimental to the learned knowledge on other classes. In our case, unlearning for 10 epochs resulted in a drastic drop in overall accuracy (down to 9.80%), showcasing how <strong>catastrophic forgetting</strong> can occur during unlearning.</p><h4>4. Impact on Related Knowledge:</h4><p>Another consequence of aggressive unlearning is that the model might lose its ability to leverage <strong>related knowledge</strong>. Since class 7 (digit ‘7’) shares certain visual similarities with other digits, particularly digits like ‘1’, ‘4’, and ‘9’, unlearning may inadvertently impact the model’s understanding of these related digits. This highlights the importance of considering <strong>inter-class relationships</strong> during unlearning procedures.</p><h3>Challenges and Solutions:</h3><p>The unlearning process, while effective in certain cases, often leads to these challenges. Research is actively working on methods to mitigate these issues. For instance:</p><ul><li><strong>Negative Preference Optimization (NPO)</strong> has shown promise in helping to stabilize the unlearning process, by targeting specific data distributions without disrupting the model’s overall functionality.</li><li><strong>Regularization techniques</strong> can be applied during the unlearning process to <strong>prevent overfitting</strong> to forgotten data and mitigate the degradation of model utility on other tasks.</li></ul><p>By adopting these techniques, we can aim for a <strong>more stable and controlled unlearning process</strong>, potentially making it safer for practical use in systems where model integrity and performance are critical.</p><p>🔜 <strong>What’s Next?</strong></p><p>In the next part of this blog series, we’ll explore <strong>Negative Preference Optimization (NPO)</strong> — a technique designed to make models forget specific data without catastrophic forgetting. We’ll see how NPO can provide a more controlled and efficient approach to unlearning!</p><p>🔥 <strong>NPO Overview</strong> — Safely reducing model preference for forgotten data 🔍 <strong>Impact on Generalization</strong> — Preserving model performance on remaining tasks</p><p>We’ll evaluate NPO on the MNIST dataset and compare its performance using:</p><p>🎯 <strong>Accuracy</strong><br> 💭 <strong>Model Preference</strong><br> ⏱️ <strong>Unlearning Time</strong></p><p>Stay tuned to see how we can make models forget more effectively, without losing everything in the process!</p><h3>References:</h3><ol><li>Golatkar, A.; Achille, A.; and Soatto, S. 2020a. Eternal sunshine of the spotless net: Selective forgetting in deep networks. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition, 9304–9312.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=266835a7b4c4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Unlearning Tutorial 2: Exact Unlearning]]></title>
            <link>https://medium.com/@varun_54675/machine-unlearning-tutorial-2-exact-unlearning-31851bb4a3fe?source=rss-7d2a6db4ea90------2</link>
            <guid isPermaLink="false">https://medium.com/p/31851bb4a3fe</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[responsible-ai]]></category>
            <category><![CDATA[machine-unlearning]]></category>
            <dc:creator><![CDATA[Varun Sampath Kumar]]></dc:creator>
            <pubDate>Mon, 14 Apr 2025 07:00:07 GMT</pubDate>
            <atom:updated>2025-04-14T07:00:07.629Z</atom:updated>
            <content:encoded><![CDATA[<p>As regulations like <strong>GDPR</strong> give users the “right to be forgotten,” it’s not enough to just delete data from databases. We now need to ensure that ML models also <strong>forget what they’ve learned</strong> from that data. Welcome back to the <em>Unlearning Saga</em>! If you’re scratching your head wondering what machine unlearning even is, take a detour to my previous blog post:<br> 👉 “<a href="https://medium.com/@varun_54675/machine-unlearning-tutorial-1-introduction-1227471fc169">Machine Unlearning Tutorial 1: Introduction</a>”</p><p>This blog dives into how we can teach our models to “forget responsibly,” using techniques like <strong>SISA (Sharded, Isolated, Sliced, and Aggregated)</strong> training and compares it with traditional full retraining. We’ll be experimenting on MNIST with Decision Trees and CNNs, specifically focusing on what happens when we ask our model to forget the digit <strong>7</strong>.</p><h3>Exact Unlearning: Forget it Like You Mean it</h3><p>Exact unlearning refers to the process of <strong>completely removing the influence</strong> of a specific data point or set of data points from a trained model — as if they were never seen. Unlike approximate methods that <em>hope</em> to unlearn, exact unlearning provides <strong>verifiable deletion</strong>. More formally, Given a learning algorithm 𝐴(.), a dataset 𝐷, and a forget set 𝐷𝑓 ⊆ 𝐷, we say the process 𝑈 (.) is an exact unlearning process iff: 𝐴(𝐷 \ 𝐷𝑓 ) = 𝑈 (𝐷, 𝐷𝑓 , 𝐴(𝐷))</p><p>You can think of it like Ctrl+Z for machine learning — but without keeping the original file in the recycle bin.</p><p>There are two primary methods we’ll explore today:</p><ul><li><strong>Retraining from scratch</strong></li><li><strong>SISA Training (Sharded, Isolated, Sliced, Aggregated)</strong></li></ul><h3>Retraining from Scratch: The Brutal but Pure Way</h3><p>The most foolproof way to unlearn something? <strong>Burn the books and start over.</strong></p><p>That’s what retraining from scratch is. You remove the offending data points from the training set and retrain the model <strong>from the beginning</strong>. This guarantees that the model never saw the data, but it comes with <em>a few</em> downsides:</p><ul><li><strong>Computationally expensive</strong></li><li><strong>Time-consuming</strong>, especially for large-scale deep models</li><li>Not practical for frequent deletion requests</li></ul><p>But hey, it works. And sometimes, purity is worth the price.</p><h3>SISA: A More Elegant Weapon</h3><p><strong>SISA training</strong> — proposed by Bourtoule et al. (2021). This method says, “Why retrain <em>everything</em> when you can just fix what’s broken?”</p><h4>✂️ How it Works (The SISA Breakdown):</h4><ul><li><strong>Shards</strong>: Randomly divide the dataset into k non-overlapping parts.</li><li><strong>Slices</strong>: Each shard is sliced into m parts.</li><li><strong>Isolation</strong>: For a given shard, the model is:</li><li>Trained on slice₁ for e1 epochs</li><li>Then slice₁ + slice₂ for e2 epochs</li><li>… until all slices are seen cumulatively.</li><li><strong>Aggregation </strong>: Predict using <strong>majority vote</strong> from all shard models.</li></ul><h4>🔁 Unlearning with SISA:</h4><p>When you get a deletion/Unlearning request:</p><ol><li>Identify which shard contains the data.</li><li>Retrain only that shard’s model from the slice which containes the data to be unlearned to the end slice subtracting the data to be unlearned.</li><li>Re-aggregate predictions.</li></ol><h4>📜 SISA Training Pseudocode (Simplified):</h4><pre>for shard in data_shards:<br>    model = init_model()<br>    for slice_i in range(0,len(shard)):<br>        for slice_j in range(0,slice_i+1):<br>          data_slice_i += shard[slice_j]<br>        model.train(data_slice_i)<br>    shard_models.append(model)<br><br># Aggregation for inference<br>def predict(x):<br>    return aggregate([model.predict(x) for model in shard_models])</pre><h4>This setup gives us a sweet trade-off:</h4><ul><li>Faster unlearning vs full retraining ✅</li><li>Approximate model equivalence ❗ (but often still accurate)</li></ul><h3>Retraining vs. SISA</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*9eVV1YoyiGzym8GSnKVmKA.png" /><figcaption>Comparing Retraining vs SISA</figcaption></figure><h3>Experimental Results on MNIST</h3><p>Let’s put these ideas to the test with a classic dataset — <strong>MNIST</strong>. In this experiment, I aim to <strong>simulate unlearning</strong> by removing an entire <strong>class</strong> from the dataset — specifically, all instances of the digit <strong>7</strong> in MNIST.</p><p>🧠 While this setup focuses on unlearning a class for <strong>demonstration purposes</strong>, the same theories and approaches can be extended to <strong>sample-level deletions</strong> as well!</p><h4>✅ Setup:</h4><ul><li><strong>Dataset</strong>: MNIST (28x28 grayscale digits 0–9)</li><li><strong>Class to Forget</strong>: Digit 7</li><li><strong>Goal</strong>: Ensure models “forget” the digit 7 post-unlearning, while preserving performance on the remaining digits.</li></ul><p><strong>Models Used</strong>:</p><ul><li>🧩 <strong>Decision Tree</strong>: From sklearn</li><li>🧠 <strong>CNN</strong>: A simple 2-layer Convolutional Neural Network built with PyTorch</li></ul><p><strong>Unlearning Methods Compared</strong>:</p><ul><li>Full retraining after deletion</li><li>SISA retraining</li></ul><p><strong>Metrics Evaluated</strong>:</p><ul><li><strong>Overall Accuracy</strong> (on the full test set)</li><li><strong>Accuracy on Deleted Class (7)</strong> (should drop to near 0)</li><li><strong>Unlearning Time</strong> (how long each method takes to forget)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*NxzNZlQnNfLkMld5t9eAPw.png" /><figcaption>Results from Decision Tree</figcaption></figure><p>You might notice that <strong>SISA unlearning took slightly more time (50.31s) than full retraining (37.57s)</strong> in this experiment. This is because <strong>Class 7 was present in nearly all shards</strong>, which forced the unlearning process to revisit and retrain a significant portion of the slices.</p><p>🧠 <strong>However, in real-world scenarios involving large-scale data</strong>, it’s rare for a class or a sample to be uniformly distributed across all shards. In such cases, <strong>unlearning would typically affect only a few shards</strong>, leading to <strong>substantial time savings</strong> compared to full retraining.</p><p>This controlled experiment was intentionally constructed to illustrate a <strong>worst-case scenario for SISA</strong>. It highlights that:</p><ul><li>SISA is <strong>not always the optimal choice</strong>, especially when the data to be forgotten is spread across all shards.</li><li>Careful consideration of <strong>data distribution, sharding strategies</strong>, and <strong>unlearning goals</strong> is essential.</li><li>For many practical cases, <strong>SISA can offer both computational efficiency and effective unlearning</strong>.</li></ul><p>In short, <strong>there’s no one-size-fits-all</strong> — but understanding the trade-offs helps in designing better, privacy-aware ML pipelines.</p><p>Link to Code: <a href="https://github.com/Lordvarun23/Code-for-my-blogs/tree/main/Machine%20Unlearning">https://github.com/Lordvarun23/Code-for-my-blogs/tree/main/Machine%20Unlearning</a></p><h4>🔜 <strong>What’s Next?</strong></h4><p><strong>In the next part of this blog series, we’ll shift gears and explore Approximate Unlearning techniques — methods that aim to make models forget without full retraining, trading off a bit of performance for a lot of speed.</strong></p><p>🔥 <strong>Knowledge Distillation</strong> — transferring “clean” knowledge to a new model<br> 🔍 <strong>Influence Functions</strong> — estimating the impact of specific samples on the model and negating it.</p><p>We’ll benchmark these techniques on the MNIST dataset across model types and compare their performance using:</p><ul><li>🎯 Accuracy</li><li>💭 Weight space distance [Verification Error]</li><li>⏱️ Unlearning Time</li></ul><p>So stay tuned — we’re about to find out how to forget <em>smartly</em> without starting over!!</p><h3>References:</h3><ol><li>L. Bourtoule <em>et al</em>., “Machine Unlearning,” <em>2021 IEEE Symposium on Security and Privacy (SP)</em>, San Francisco, CA, USA, 2021, pp. 141–159, doi: 10.1109/SP40001.2021.00019.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31851bb4a3fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Unlearning Tutorial 1: Introduction]]></title>
            <link>https://medium.com/@varun_54675/machine-unlearning-tutorial-1-introduction-1227471fc169?source=rss-7d2a6db4ea90------2</link>
            <guid isPermaLink="false">https://medium.com/p/1227471fc169</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[machine-unlearning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Varun Sampath Kumar]]></dc:creator>
            <pubDate>Sun, 06 Apr 2025 15:28:40 GMT</pubDate>
            <atom:updated>2025-04-06T15:28:40.855Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/720/0*5nkt3XiwfwWUAfco.png" /></figure><p><strong>What if forgetting was a feature, not a flaw?</strong> In an era where AI systems remember everything — from your old selfies to your deleted shopping history — <em>machine unlearning</em> emerges as a radical, necessary shift. It challenges the core of traditional machine learning by asking a simple yet profound question: <em>Can machines forget as efficiently as they learn?</em> Whether it’s for user privacy, compliance with laws like GDPR, or correcting biased or poisoned data, unlearning isn’t just a technical novelty — it’s a moral imperative. In this series of blogs, we dive deep into the algorithms, challenges, and the state of the art methods.</p><h3>Motivation</h3><p>Machine unlearning plays a crucial role in making AI systems more <strong><em>responsible</em></strong>, <strong><em>secure</em></strong>, and <strong><em>adaptable</em></strong>. From a privacy perspective, it empowers users with control over their data by enabling models to “forget” personal information, in line with laws like GDPR and CCPA. This not only honors users’ right to be forgotten but also helps defend against privacy breaches like membership inference attacks. On the security front, unlearning acts as a defense mechanism against data poisoning — where attackers sneak malicious data into training sets to misguide the model — by allowing swift removal of such harmful entries. Beyond privacy and security, unlearning also ensures models stay relevant in a constantly evolving world. As data changes over time, outdated or biased information can be unlearned, helping models stay accurate and aligned with current trends and realities.</p><h3><strong>🔍 What Exactly Is Machine Unlearning?</strong></h3><p>Machine unlearning refers to the process of removing the influence of specific training data on an already trained model. Formally, given a model with parameters w∗ trained on dataset D using learning algorithm A, and a subset D’ ⊆ D to be removed, the machine unlearning algorithm U(A(D), D, D’ ) aims to obtain new model with parameters w⁰ by removing the effects of D’ while preserving performance on D \ D’ .</p><h3>⚙️ How Do Machines Forget? (Techniques &amp; Approaches)</h3><p>Broadly, these techniques fall into <strong>exact</strong> and <strong>approximate unlearning</strong>, depending on how faithfully they replicate the effect of full retraining. Let’s dive into the key approaches:</p><h4>1. Exact Unlearning</h4><p><strong>Goal:</strong> Remove the data point completely, <em>as if it was never seen by the model</em>.<br> <strong>Example Techniques:</strong></p><ul><li><strong>Retraining from scratch</strong>: The most straightforward (but expensive) method — retrain the model without the data to be forgotten. It’s precise but not scalable.</li><li><strong>SISA (Sharded, Isolated, Sliced, Aggregated)</strong>: This approach breaks training data into independent shards, trains submodels on them, and combines them. When forgetting is needed, only the affected submodels are retrained — offering a balance between exactness and efficiency.</li></ul><p>🧠 <em>Best for situations where high assurance of forgetting is required and retraining cost can be managed.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*4HG1A_3X3jpGsvCQh688Ow.png" /><figcaption>Figure 1: Illustration of Naive Retraining and Machine Unlearning</figcaption></figure><h4>2. Approximate Unlearning</h4><p><strong>Goal:</strong> Approximate unlearning aims to minimize the influence of unlearned data to an acceptable level while achieving an efficient unlearning process. It has several advantages over exact unlearning techniques, including better computational efficiency, less storage cost, and higher flexibility.<br> <strong>Example Techniques:</strong></p><ul><li><strong>Influence Functions</strong>: Estimate how much a data point influenced the model’s output and then adjust weights to reverse this effect. Fast, but less accurate for large or deep models.</li><li><strong>Knowledge Distillation</strong>: Train a new model to imitate the original one — <em>excluding</em> the data to be forgotten. It’s like teaching a “clean slate” student everything except the bad memory.</li><li><strong>Gradient Reversal or Fine-Tuning</strong>: Subtly unlearn using targeted gradient steps to reduce the model’s dependence on specific data.</li></ul><p>⚡ <em>Best for real-time unlearning where speed and scalability are key, even if some approximation is acceptable.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*J8tSkTQiJhbdILOX.gif" /><figcaption>Figure 2: Illustration Learning vs Unlearning: A Gradient-Based Perspective</figcaption></figure><h3>✅ Wrapping Up:</h3><p>As machine learning models become more intertwined with our lives and data privacy becomes a growing concern, <strong>machine unlearning</strong> steps in as a powerful and necessary capability. Whether it’s ensuring compliance with privacy laws like GDPR, defending against adversarial attacks, or simply keeping models up-to-date and relevant, teaching machines to forget is no longer optional — it’s essential.</p><p>While unlearning might sound simple, the <strong>“how”</strong> behind it spans a spectrum — from exact to approximate, from model-agnostic to model-intrinsic methods. Each approach comes with its own trade-offs in terms of efficiency, accuracy, and guarantees, making it a rich and evolving area of research.</p><h3>🔜 What’s Next?</h3><p>In the next part of this blog series, we’ll <strong>dive into Exact Unlearning methods</strong> — specifically:</p><ul><li><strong>Naive Retraining</strong></li><li><strong>SISA (Sharded, Isolated, Sliced, Aggregated)</strong></li></ul><p>We’ll also <strong>compare their performance across different model architectures on MNIST Dataset</strong>:</p><ul><li>🧮 <strong>Linear Models</strong></li><li>🌳 <strong>Tree-based Models</strong></li><li>🧠 <strong>Convolutional Neural Networks (CNNs)</strong></li></ul><p>We’ll evaluate them based on <strong>Accuracy, Completeness, Unlearn time &amp; Layer wise distance</strong>.</p><p>So stay tuned — because in the next chapter, we’re going hands-on with forgetting!</p><h3>References:</h3><ol><li>Survey On Machine Unlearning by Thanh Tam Nguyen, Thanh Trung Huynh etc. Link: <a href="https://arxiv.org/pdf/2209.02299">https://arxiv.org/pdf/2209.02299</a></li><li>A More practical approach to Machine Unlearning by David Zagardo. Link: <a href="https://arxiv.org/pdf/2406.09391">https://arxiv.org/pdf/2406.09391</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1227471fc169" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[To Preprocess or Not To Preprocess]]></title>
            <link>https://medium.com/@varun_54675/to-preprocess-or-not-to-preprocess-7daae10404a3?source=rss-7d2a6db4ea90------2</link>
            <guid isPermaLink="false">https://medium.com/p/7daae10404a3</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[text-preprocessing]]></category>
            <dc:creator><![CDATA[Varun Sampath Kumar]]></dc:creator>
            <pubDate>Wed, 10 Apr 2024 15:03:09 GMT</pubDate>
            <atom:updated>2024-04-23T08:37:29.996Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*mE9nch_OPfX5KenERiEuWQ.jpeg" /></figure><p>Have you ever wondered while taking up a Natural Language course or project many insisting on performing various text preprocessing such as Spell Correction, Lemmetization etc really needed?</p><p>Answer to that is Yes and No, Seems confusing lets dive deep to understand when to say yes no in this article.</p><p><strong>What are Text Preprocessing steps?</strong></p><p>Text preprocessing typically involves these steps:</p><ol><li>Lowercasing</li><li>Removing special characters</li><li>Stop Words Removal</li><li>Stemming &amp; Lemmatization</li><li>Tokenization</li></ol><p><strong>Is all steps mandatory?</strong></p><p>Some or all of these text preprocessing techniques are commanly used by NLP Projects. When to use these techniques vary depending on the needs of the project. We need to really understand the downstream task and do the processing steps.</p><p>Let’s jump on to a case study to understand a scenerio when not to use text preprocessing.</p><p><strong>Task: Given a text we need to predict whether it is written by human or AI.</strong></p><p>This may seem a striaght forward binary classification task. The dataset for this task is collected from a Kaggle Competetion and datasets.</p><p><strong>Dataset: Contains 27,000 Human written and 17,000 AI written texts.</strong></p><p>Now, lets evaluate each preprocessing step whether it is needed or not?</p><p><strong>Spell Correction:</strong></p><p>If you think bit deep, probability of making spell mistakes when written by human is generally higher when compared to almost none in case of AI written.</p><blockquote>According to a Survey by Cambridge reveals, 345 errors in 11,730 words written. Therefore, 97.1% of words were correctly spelled.</blockquote><p>So, if we do spell correction we might loose a important correlating factor to the target.</p><p><strong>Removing Special Characters:</strong></p><p>Most of the times when a human write the special characters and punctutations are tend to be forgotten. So these could also be an important attribute in classifying the target.</p><p><strong>Stemming and Lemmetization:</strong></p><p>Same logic as spelling mistake. So Stemming and lemmitization is also not needed for this case study.</p><p>So finally, We generated features using simple Bag of Words technique for simple demonstration and trained a Logistic Regression Model. Model test accuracy on the test dataset is as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*wyH6f3sc2_tatgwRGVWsnw.png" /></figure><blockquote><strong>Github of codebase: </strong><a href="https://github.com/AnruthaKamal/Detect-AI-generated-Text">https://github.com/AnruthaKamal/Detect-AI-generated-Text</a></blockquote><blockquote><strong>Streamlit Demo: </strong><a href="https://genuineverify.streamlit.app/">https://genuineverify.streamlit.app/</a></blockquote><p><strong>Conclusion:</strong></p><p>In this article, The main aim is to make audience aware that not all projects dealing with texts actually needs all the preprocessing steps mentioned as a flow in books and courses. The core lies in analysing the problem at hand carefully to evaluate each preprocessing step is needed or not.</p><p>I hope it will be a useful article for you. Happy reading!!</p><p><strong>Authors: Varun S , Anrutha J K, Gowri Thangam J [PSG College Of Technology]</strong></p><p>Contact Account: <a href="https://www.linkedin.com/in/varun-sampath-kumar/">Varun LinkedIn</a>, <a href="https://www.linkedin.com/in/anrutha-kamalanathan/">Anrutha LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7daae10404a3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>