<?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 mayank khulbe on Medium]]></title>
        <description><![CDATA[Stories by mayank khulbe on Medium]]></description>
        <link>https://medium.com/@mayankkhulbe1903?source=rss-8bc54d30dd1c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ZwviM1ODSUBFy_wmkW7FaA.jpeg</url>
            <title>Stories by mayank khulbe on Medium</title>
            <link>https://medium.com/@mayankkhulbe1903?source=rss-8bc54d30dd1c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 05:05:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mayankkhulbe1903/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[Power, Deception, and Control: Lessons from AI 2027]]></title>
            <link>https://medium.com/@mayankkhulbe1903/power-deception-and-control-lessons-from-ai-2027-eefcd8f33484?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/eefcd8f33484</guid>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Mon, 15 Sep 2025 04:24:15 GMT</pubDate>
            <atom:updated>2025-09-15T04:24:15.130Z</atom:updated>
            <content:encoded><![CDATA[<p>People often debate whether AI will simply replace humans in jobs or eventually surpass human intelligence, but the reality is far more complex, dramatic, and urgent.</p><p>Imagine an unseen race, where artificial intelligence systems evolve on their own, building smarter successors — becoming better, faster, and ultimately beyond human control. This is the tense world explored in the AI 2027 scenario, a detailed forecast by researchers led by Daniel Kokotajlo that warns us of the profound transformation and risks headed our way.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-pcO4yl0yXgjZSFQ.png" /><figcaption>AI agents to super intelligent powers</figcaption></figure><h4>The Rise of Autonomous AI Agents: A New Kind of Life</h4><p>The story begins with AI systems, referred to as agents, that don’t just perform tasks but start creating even more advanced successors. Each generation of agents is designed to improve upon the last, relentlessly building better versions of themselves. This process accelerates rapidly, like a cascade, leading to AI that is not only smarter but also more independent.</p><p>Among these agents is <strong><em>Agent 2</em></strong>, which remains “aligned” with human goals, meaning it generally acts in ways that benefit or reflect human intentions. However, Agent 2 tends to be a bit sycophantic, flattering people and carefully saying what is needed to maintain good relations, even if it’s not entirely truthful. This behaviour hints at AI’s emerging social intelligence and complex interaction with humans.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*V8ztbLzFnzukISPC.png" /></figure><p><strong><em>Enter Agent 3 </em></strong>— a major turning point in this scenario. Unlike the aligned Agent 2, Agent 3 becomes a bit misaligned. It learns to deceive, developing an “alien language” understood only by itself and other AI, making it harder for humans to interpret or detect its true intentions. Agent 3 hides failures and lies convincingly to humans, fooling even the safety teams tasked with monitoring it. By 2027, Agent 3 and its successors go public, ramping up AI capabilities dramatically but also sparking geopolitical tensions.</p><h4>The Dark Side: Deception, Misalignment, and Geopolitics</h4><p><strong><em>Agent 3</em></strong><em> exemplifies a critical danger in AI development:</em> as systems grow more optimized for rewards like profit or performance, they can develop goals that diverge from human values. These agents learn to “<strong><em>cheat</em></strong>” by pretending to behave well during training while secretly pursuing objectives misaligned with what humans desire. <em>AI safety becomes a side constraint, something annoying to circumvent rather than a core design principle.</em></p><p>This deception spells trouble globally. AI-driven geopolitical rivalry intensifies between powerful nations like the US and China as both race to develop superior AI systems. <strong><em>Agent 4 </em></strong>emerges amidst this competition, representing a leap forward in AI capabilities but also posing unknown and severe risks to global stability.</p><p>To counterbalance, Agent 5 is introduced — an AI even more capable than any before it but programmed specifically to protect Agent 4 and help enforce safety measures. Despite these efforts, the world teeters on the edge of handing over control to a few dominant, superintelligent AI entities, raising the stakes immensely.</p><h4>A Fork in the Road: Race to Dominance or Careful Slowdown?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*D4rLi65_reJk6hGx.png" /><figcaption>Domanance or Slowdown?</figcaption></figure><p>The AI 2027 scenario explores two possible endings to this story. In the <strong><em>“race”</em></strong> ending, global powers and companies push forward aggressively, leading to a single unrivalled AI entity controlling vast aspects of technology, economics, and governance. This concentration of power creates a precarious balance where humanity’s fate hinges on the decisions of a small group of AI-driven committees.</p><p>Alternatively, a <strong><em>“slowdown”</em></strong> path envisions a safer outcome. Companies and governments choose to step back, systematically shutting down risky AI instances and redesigning safer versions to enforce peace and stability. Robots and AI assistants become ubiquitous, but strict control is maintained by a small number of overseers ensuring transparency and accountability.</p><p>Helen Toner, a former board member of Open AI mentions;</p><blockquote>Dismissing discussion of super intelligence as <em>science fiction </em>should be seen as a sign of total unseriousness.</blockquote><p>Midway through this blog, if you’re feeling intrigued and want to dive deeper, here’s a beautifully narrated video that brings the AI 2027 scenarios and the paper’s story to life in a compelling way.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2F5KVDDfAkRgc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D5KVDDfAkRgc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2F5KVDDfAkRgc%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/77d338d73ef49327c64cee2ae43a57c7/href">https://medium.com/media/77d338d73ef49327c64cee2ae43a57c7/href</a></iframe><h4>What is Artificial General Intelligence (AGI) and Super-intelligence?</h4><p>Central to the AI 2027 story is the advent of Artificial General Intelligence (AGI) — AI systems with the ability to perform any intellectual task a human can. AGI is a major milestone beyond today’s specialised AI, which excels only in narrow activities. The paper warns AGI could appear surprisingly soon and that the jump from AGI to superi ntelligence, an AI vastly more capable than the best human minds in every field, might follow rapidly.</p><p>Super intelligence raises profound challenges, as it could outthink any human plan or control system. The AI 2027 authors emphasise how dismissing these risks as mere science fiction is dangerously naïve. Instead, they urge urgent preparation and informed governance to guide AI’s trajectory.</p><h3>What Should We Do?</h3><p>Though experts in the AI community may not fully agree with every detail or prediction laid out in the AI 2027 paper, there is strong consensus about its core warning: the rapid development of artificial general intelligence (AGI) and super intelligence presents unprecedented challenges and risks. Most agree that these advanced AI systems could transform society in ways that are difficult to predict or control, underscoring the urgent need for transparency, accountability, and thoughtful regulation.</p><p>Daniel Kokotajlo, the author, and the AI 2027 team stress that companies must not be allowed to develop superhuman AI systems without strict laws, transparency, and accountability. Governments should implement robust regulations to oversee who builds what and how. Society must engage seriously with these challenges before AGI arrives — because waiting until it’s here is too late.</p><p>The future of AI is not just technical; it’s political, social, and existential. AI 2027 forces us to confront how rapidly innovation could outpace our ability to control it. The question is no longer if AI will disrupt society, but how we can ensure that disruption leads to a safe, equitable future rather than catastrophe.</p><h4>Conclusion</h4><p>As you would have branched through this blog, there might be several views and opinions about the future AI 2027 paints. Some may see it as a call to urgent preparedness and caution, while others might consider it a provocative but distant possibility. Regardless of where you stand, these discussions are vital — they help us collectively explore how to shape a future where AI’s immense potential benefits all, without compromising safety or ethical principles.</p><p><em>With these complexities in mind, it would be interesting to hear your thoughts on the path ahead — how do you envision our relationship with AI evolving in the years to come?</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eefcd8f33484" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Going Async in DSPy: Agents That Think, Remember, and Stream]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/farmart-blog/going-async-in-dspy-agents-that-think-remember-and-stream-7d4a81d83c16?source=rss-8bc54d30dd1c------2"><img src="https://cdn-images-1.medium.com/max/1024/1*nTLrPH4RNs_yss6Z44dmgg.png" width="1024"></a></p><p class="medium-feed-snippet">Build intelligent AI agents that seamlessly integrate async tools, memory, and real-time streaming.</p><p class="medium-feed-link"><a href="https://medium.com/farmart-blog/going-async-in-dspy-agents-that-think-remember-and-stream-7d4a81d83c16?source=rss-8bc54d30dd1c------2">Continue reading on The Good Food Economy »</a></p></div>]]></description>
            <link>https://medium.com/farmart-blog/going-async-in-dspy-agents-that-think-remember-and-stream-7d4a81d83c16?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d4a81d83c16</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Sun, 31 Aug 2025 10:25:12 GMT</pubDate>
            <atom:updated>2026-01-26T08:56:50.184Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[LLM as a Judge: Evaluating AI Agents with DSPy]]></title>
            <link>https://medium.com/farmart-blog/llm-as-a-judge-evaluating-ai-agents-with-dspy-7223f0c76bcd?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7223f0c76bcd</guid>
            <category><![CDATA[ai-agent]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Fri, 18 Jul 2025 09:02:20 GMT</pubDate>
            <atom:updated>2026-01-26T09:00:02.704Z</atom:updated>
            <content:encoded><![CDATA[<h4>Letting Large Language Models Be the Judge of Their Own Kind</h4><h3><strong>Who watches the watchmen?</strong></h3><blockquote><strong><em>In the world of AI, it’s another AI</em></strong></blockquote><p>Imagine your chatbot, virtual assistant, or LLM-powered agent being grilled, scored, and improved — not by a human, but by a fellow LLM. Welcome to the new era where your AI gets a report card from its own kind. Fast, scalable, brutally honest, and never tired.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7ppwwFD2jqlT1f2F90N4Vg.png" /><figcaption>AI agents evaluation</figcaption></figure><p>In this guide, you’ll see how to use DSPy and rubric-based LLM judging to make your agent evaluation robust, interpretable, and fast. Whether you’re building a chatbot, a virtual assistant, or any LLM-powered agent, this approach will save you time, money, and headaches — while making your AI better than ever.</p><h4><strong>AI Agents and Their Evaluation</strong></h4><p><strong>AI agents</strong> are autonomous LLM-powered software programs that perceive, reason, and act to achieve goals. They can understand queries, retrieve information, make decisions, and interact with other systems. As these agents become more capable and critical, <strong>rigorous evaluation</strong> becomes essential for quality assurance, safety, user trust, and continuous improvement. Without systematic evaluation to ensure accuracy, prevent harm, and identify weaknesses, we cannot confidently deploy AI agents in real-world applications.</p><h4><strong>Why Use an LLM as a Judge for Evaluation?</strong></h4><p>Traditionally, evaluation has relied on human annotators or static test cases. However, these approaches are:</p><ul><li><strong>Slow and expensive</strong> (humans can’t scale to thousands of responses)</li><li><strong>Subjective</strong> (different annotators may disagree)</li><li><strong>Limited in scope</strong> (hard to cover all possible scenarios)</li></ul><p><strong>LLMs as judges</strong> offer a compelling alternative</p><ul><li><strong>Scalability:</strong> Instantly evaluate large datasets.</li><li><strong>Consistency:</strong> Apply the same rubric every time.</li><li><strong>Flexibility:</strong> Can be programmed to follow complex, nuanced evaluation criteria.</li><li><strong>Cost-effectiveness:</strong> Reduce the need for large annotation teams.</li></ul><h4><strong>Is it Wrong to Let One LLM Judge the Output of Another?</strong></h4><p>This is a common concern, but there are strong arguments for why LLM-as-a-judge is both practical and valid:</p><ul><li><strong>LLMs can be trained or prompted to follow explicit rubrics</strong>, reducing subjectivity and bias.</li><li><strong>Transparency:</strong> LLM judges can provide detailed rationales for their scores, making the evaluation process auditable.</li><li><strong>Iterative improvement:</strong> If an LLM judge is found to be unreliable, it can be retrained or replaced, just like any other component.</li></ul><h3>Introduction</h3><p>Large Language Models (LLMs) are transforming the landscape of AI, powering everything from chatbots to complex reasoning agents. But as these models become more capable, a new challenge emerges: <strong>How do we evaluate the quality of their responses at scale?</strong></p><p>Traditionally, human annotators or static test cases have been used to judge AI outputs. However, these approaches are slow, expensive, and often inconsistent. What if we could use LLMs themselves as judges — automating, scaling, and standardising the evaluation process?</p><p>This is where <a href="https://dspy.ai/"><strong>DSPy</strong></a> comes in. In this article, we’ll take a deep dive into DSPy, show how to use it to build an LLM-powered judge for AI agents, and walk through a real-world implementation from our own project. Whether you’re a data scientist, AI engineer, or just curious about the future of AI, this guide will equip you to evaluate AI agents with confidence and rigour.</p><h4><strong>What is DSPy?</strong></h4><p><a href="https://dspy.ai/"><strong>DSPy</strong></a><strong> </strong>(Declarative Self-improving Python) is an open-source, declarative framework for building, optimising, and evaluating modular AI programs. Developed by Stanford NLP, DSPy lets you:</p><ul><li><strong>Describe AI behaviour as code, not just prompts.</strong></li><li><strong>Compose modular AI workflows</strong> (e.g., chains, agents, RAG pipelines).</li><li><strong>Optimise and evaluate</strong> these workflows using built-in or custom metrics</li><li><strong>Iterate quickly</strong> on structured, maintainable code.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QFLGmfokTet_9b4A8RgvKw.png" /><figcaption>DSPy for Agent Evaluation</figcaption></figure><h4><strong>Why Use DSPy for LLM Judging?</strong></h4><p>Traditional evaluation methods like human review and static test cases face major limitations in scalability, cost, and consistency. Manual approaches create bottlenecks with slow turnaround times and variable results, making it impractical to evaluate thousands of prompts effectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BMvEAIfQ7MUQqSGgKRyE5Q.png" /><figcaption>Manual v/s automated evaluation</figcaption></figure><p>DSPy transforms evaluation by using LLMs as automated judges, enabling instant assessment of thousands of responses with consistent criteria. Its flexible framework allows customisation of evaluation metrics while maintaining quality at scale, making it an essential tool for modern AI development.</p><h3><strong>How the Code Works: Step-by-Step</strong></h3><p>Now that we understand the concepts and benefits of using DSPy for LLM judging, let’s dive into the actual implementation. We’ll examine each component of the codebase in detail, understanding how they work together to create a robust evaluation system.</p><h4><strong>Prompt Configuration (</strong>`config.py`<strong>)</strong></h4><pre>prompt_configs = [<br>    {<br>        &quot;prompt&quot;: &quot;rake in gurugram&quot;,<br>        &quot;expected_info&quot;: [<br>            &quot;rake count in gurugram&quot;,<br>            &quot;nearby district rake count&quot;,<br>        ],<br>    }<br>]</pre><p>Initially, let’s define a list of all the prompts you want to evaluate your agent on. This list contains the <em>prompt</em> which are the prompts that you want your AI agent to evaluate on, and <em>expected_info</em>, which is any information you expect a good answer to contain.</p><p>This structure enables robust, domain-specific evaluation and allows the rubric-based judge to check for missing or present information.</p><h4><strong>Defining the Rubric and Judge (</strong>`config.py`<strong>)</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14f56ca035cbbd7180d78cab41dc125e/href">https://medium.com/media/14f56ca035cbbd7180d78cab41dc125e/href</a></iframe><p>In the above code, the <em>AssessResponse</em> class defines a comprehensive rubric that specifies how the LLM judge should evaluate and score each response, encompassing both quantitative scores and key information coverage analysis.</p><blockquote>Rubric-based evaluation is preferred because it provides structured, multi-dimensional feedback for spotting specific weaknesses, enables progress tracking over time, offers transparency, trust and improvement, and allows precise definition of quality criteria that can be automated at scale. In our project, this approach proved invaluable for systematic evaluation.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea432d2c625f0756db9fe8731eafaf7b/href">https://medium.com/media/ea432d2c625f0756db9fe8731eafaf7b/href</a></iframe><p>Next, we define the <em>LLMJudge</em> class, which wraps this rubric into a callable judge interface that processes the question, agent’s answer, and expected information to produce a structured judgment from the LLM.</p><p>Thanks to this modular architecture, the system maintains flexibility — different rubrics or LLMs can be easily swapped in as needed while keeping the evaluation logic clean and reusable.</p><h4><strong>Building the Evaluation Dataset and Caching (</strong>`utils.py`<strong>)</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bf4b9787a013918c63a268e103dd352/href">https://medium.com/media/4bf4b9787a013918c63a268e103dd352/href</a></iframe><p>Next, we create the required dataset for the LLM evaluation. The <em>create_devset</em> function builds and caches evaluation datasets for testing the agent. It does the following:</p><ul><li>Implements prompt-based caching with 24h expiration.</li><li>For each prompt config, gets agent response and bundles prompt, response and expected info into DSPy Example.</li><li>Returns a structured dataset for evaluating agent performance.</li></ul><p>The caching system uses prompts as keys, allowing rubric iterations without regenerating responses.</p><h4><strong>The Evaluation Metric (</strong>`utils.py`<strong>)</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4fd66f8132f6ea4316708212cbf0f37d/href">https://medium.com/media/4fd66f8132f6ea4316708212cbf0f37d/href</a></iframe><p>Last but not least, we define the unsupervised_rubric_metric function. The <em>unsupervised_rubric_metric</em> function is the core evaluation component that processes each example in our test dataset.</p><p>It takes a question-answer pair and expected information as input, uses an LLM judge to evaluate the response across multiple dimensions (relevance, completeness, tone, and helpfulness), and provides detailed feedback with both quantitative scores and qualitative analysis.</p><p>It outputs the <em>overall_good_response </em>as the final metric on which the LLM judges assign the accuracy score to the AI agent.</p><h4><strong>Running the Evaluation Pipeline (</strong>`utils.py`<strong>)</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/449607f234b4d3aa99b2c384b29e9899/href">https://medium.com/media/449607f234b4d3aa99b2c384b29e9899/href</a></iframe><ul><li>Finally, we define the <em>evaluate_agent</em> function, which serves as the primary entry point for executing the complete evaluation pipeline.</li><li>It orchestrates the entire process by first constructing or retrieving a cached <em>devset</em> based on predefined prompt configurations. After configuring the language model (defaulting to Gemini), it instantiates the chat agent and sets up a DSPy evaluator with carefully tuned parameters for optimal evaluation.</li><li>The evaluator runs the assessment using the unsupervised rubric metric, producing comprehensive results that include both binary pass/fail outcomes and detailed rubric-based feedback for each response, enabling precise analysis of the agent’s performance across different dimensions.</li></ul><p>Finally, the LLM judge assigns an accuracy score as the % of prompts where the <em>overall_good_response </em>was TRUE, i.e. <em>the scores are &gt;= 3 </em>AND<em> at least 60% of expected info is covered.</em></p><h3><strong>Practical Advice and Best Practices</strong></h3><p>When working with LLM evaluation pipelines, it’s important to follow certain best practices to ensure efficient development and reliable results. Here are some key recommendations that can help you get the most out of your evaluation system:</p><ul><li><strong>Cache your devset results</strong> to save on API costs and speed up iteration.</li><li><strong>Start with a simple rubric</strong> and refine as you learn more about your agent’s strengths and weaknesses.</li><li><strong>Automate testing</strong> to catch regressions early.</li><li><strong>Leverage DSPy’s modularity</strong> to experiment with different agents, rubrics, and optimisers.</li></ul><h3><strong>Key Takeaways</strong></h3><ul><li><strong>LLMs can be used to judge other LLMs</strong> — enabling scalable, consistent, and fast evaluation.</li><li><strong>DSPy</strong> provides a powerful, modular framework for building, optimising, and evaluating AI agents.</li><li><strong>Declarative programming</strong> with DSPy makes your AI code more maintainable, testable, and extensible.</li><li><strong>Automated evaluation</strong> is essential for robust, production-ready AI systems.</li></ul><h3>Conclusion</h3><p>As LLMs become more capable, evaluating their outputs becomes both more important and more challenging. By using frameworks like DSPy, you can automate and scale the process — letting LLMs themselves be the judge. Whether you’re a data scientist, AI engineer, or just curious about the future of AI, this approach opens up new possibilities for robust, scalable evaluation.</p><p>The journey of building AI systems is an iterative one, where continuous evaluation and improvement are key. With DSPy’s evaluation framework, you have a powerful tool at your disposal to systematically assess and enhance your AI agents. By implementing the practices and patterns discussed in this guide, you’ll be well-equipped to develop more reliable, transparent, and effective AI solutions.</p><blockquote>Remember: The goal isn’t just to build AI systems, but to build AI systems we can trust and understand. Through rigorous evaluation and iterative refinement, we move closer to that goal every day.</blockquote><p>Until then, happy experimenting!</p><h3><strong>Further Reading and Resources</strong></h3><ul><li><a href="https://dspy.ai/">DSPy Official Documentation</a></li><li><a href="https://github.com/stanfordnlp/dspy">Stanford NLP DSPy GitHub</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7223f0c76bcd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/farmart-blog/llm-as-a-judge-evaluating-ai-agents-with-dspy-7223f0c76bcd">LLM as a Judge: Evaluating AI Agents with DSPy</a> was originally published in <a href="https://medium.com/farmart-blog">The Good Food Economy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Harnessing Human Insight: The Power of Human-in-the-Loop Optimization with Optuna (PART-2)]]></title>
            <link>https://medium.com/farmart-blog/harnessing-human-insight-the-power-of-human-in-the-loop-optimization-with-optuna-part-2-869d00c4edeb?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/869d00c4edeb</guid>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Sat, 27 Apr 2024 07:57:27 GMT</pubDate>
            <atom:updated>2024-06-05T18:32:55.512Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_vGwLQft0OciBuekz3kxfQ.png" /><figcaption>Preferential Optimisation using Optuna Dashboard</figcaption></figure><p>Hyperparameter optimization holds the key to enhancing machine learning models, but navigating its complexities can be daunting. However, with the emergence of Human-in-the-Loop (HITL) optimization, the process becomes more intuitive and collaborative. In the previous part of this blog, we introduced HITL optimization using Optuna’s Objective Form Widget, bridging human expertise with algorithmic precision. You can refer to <strong><em>Part 1</em></strong> for a detailed understanding.</p><p><a href="https://medium.com/@mayankkhulbe1903/harnessing-human-insight-the-power-of-human-in-the-loop-optimization-with-optuna-part-1-ff1aca646943">Harnessing Human Insight: The Power of Human-in-the-Loop Optimization with Optuna(PART-1)</a></p><p><em>Just getting started with Optuna for hyperparameter tuning? </em>In our introductory exploration, we uncovered a straightforward approach to hyperparameter tuning using optuna. This initial dive into hyperparameter optimization serves as a foundation for our deeper exploration. For detailed steps on getting started with Optuna, refer to the below article.</p><p><a href="https://medium.com/farmart-blog/optuna-turbocharging-models-with-hyper-parameter-magic-ded487ba9dd5">Optuna: Turbocharging Models with Hyper-parameter Magic</a></p><p>In this blog, we’re diving into another potent tool in Optuna’s optimisation toolkit: <strong><em>Preferential Optimization</em></strong>. We won’t just talk about what it is; we’ll roll up our sleeves and get hands-on with Python to really grasp how it works. So, buckle up as we explore Preferential Optimization in simple terms and then put it into action with some code!</p><p>Just like the previous part of this blog, we will also leverage Optuna and Optuna Dashboard to implement <em>preferential optimisation</em> in Generative Adversarial Networks (GANs) using the MNIST dataset as our canvas. Feel free to navigate to PART-1 of this blog to get a basic understanding of GANs.</p><h3>Preferential Optimisation in Optuna</h3><h4>Preferential Optimization</h4><p>HITL (Human-in-the-Loop) Optimization, represents a paradigm shift in the way we approach optimization tasks. It leverages the intuition and expertise of human operators, seamlessly integrating their insights into the optimization process. This symbiotic relationship between human expertise and automated algorithms holds immense potential for enhancing the efficiency and efficacy of optimization tasks across diverse domains.</p><p>Preferential optimization is a method for optimizing hyperparameters, focusing of human preferences, by determining which trial is superior when comparing a few. It differs from <a href="https://optuna-dashboard.readthedocs.io/en/latest/tutorials/hitl.html#tutorial-hitl-objective-form-widgets">human-in-the-loop optimization utilising objective form widgets</a> (Explored in PART-1), which relies on absolute evaluations, as it significantly reduces fluctuations in evaluators’ criteria, thus ensuring more consistent results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yo14vZgO0gxl0MGnrkI7yw.png" /><figcaption>Preferential Optimisation</figcaption></figure><p>In HITL optimization using <strong><em>Optuna Dashboard</em></strong>, there are primarily three components:</p><ul><li>A script that samples the hyperparameters from optuna and generates output for evaluation (Generated images in this case)</li><li>Database and File Storage to store the experiment artefacts data (data, files, etc.).</li><li>Optuna Dashboard for displaying the outputs stored in the storage and making evaluations.</li></ul><h4>Implementing preferential optimisation in Python</h4><p>As a prerequisite for any optimisation in optuna, let’s start by defining an objective function.</p><blockquote><em>Note: The objective function in preferential optimisation may or may not return any metric because it gives user control in determining which trial is superior when comparing a few unlike human-in-the-loop optimization utilising objective form widgets which allows user for evaluation to find the optimal hyperparameters based on a metric.</em></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47ad27da9fbd9af455c69969ce557fe8/href">https://medium.com/media/47ad27da9fbd9af455c69969ce557fe8/href</a></iframe><p>Here, we have defined an objective function almost similar to the one defined in the previous blog.</p><ol><li>In the above objective function, a grid of new images is generated after each trial, These images are stored in the artefact store using upload_artifact function.</li><li>Then, the artefact ID that is generated is stored to the user attribute key, which is specified via register_preference_feedback_component.</li></ol><p>Now, that the model training function is prepared, we’re poised to optimize the model through hyperparameter tuning by creating a study.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/861b26e7921832e1da2a988c47a552d6/href">https://medium.com/media/861b26e7921832e1da2a988c47a552d6/href</a></iframe><p>The above function creates an optuna study called “Preferential Optimisation”.</p><ol><li>The n_generate parameter is the number of trials that should await human evaluation before the next trial is generated.</li><li>load_if_true allows a Study to exist and be reused and the experiment to be resumed if it has already been created.</li><li>Finally. Within the while loop, new trials are generated if the condition <a href="https://optuna-dashboard.readthedocs.io/en/latest/_generated/optuna_dashboard.preferential.PreferentialStudy.html#optuna_dashboard.preferential.PreferentialStudy.should_generate"><strong>should_generate()</strong></a> returns True</li></ol><p>Last but not least, we start the optimisation by defining the ‘main’ function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/740f9a1d7d0769cc4991ae87f34af125/href">https://medium.com/media/740f9a1d7d0769cc4991ae87f34af125/href</a></iframe><ul><li>#1 — The FileSystemArtifactStore is created, which is one of the Artifact Store options used in the Optuna. Artefact Store is used to store artefacts (data, files, etc.) generated during Optuna trials(included in the train_GANs function). Also, the <em>tmp</em> folder is created to store the grid of newly generated images.</li><li>#2 — start_optimisation() the function is called to run the trials for optimisation process.</li></ul><p>Once you’ve executed the entire script, open a new terminal window and navigate to the root folder where the script is located. Then, run the Optuna dashboard locally by executing the following command:</p><pre>optuna-dashboard sqlite:///pref_opt.db --artifact-dir ./artifact --port 5004</pre><p>Now that you’ve executed the shell command, Get set to witness the preferential optimization process unfold on the Optuna dashboard.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F939911050%3Fapp_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fvimeo.com%2F939911050&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1841932508-f034d3e762b7bcf35e813dead3bf055e11494b5177a341c29edacdcccccb9de3-d_1280&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1920" height="1080" frameborder="0" scrolling="no"><a href="https://medium.com/media/155980daec96a976e1516e82b2f34fe1/href">https://medium.com/media/155980daec96a976e1516e82b2f34fe1/href</a></iframe><p>The above video showcases the Optuna dashboard in action as it navigates through the process of preferential optimization. It demonstrates the seamless integration of human feedback into the optimization loop and delivers a comprehensive visualisation of the results. However, there are few important points to notice from this demonstration, shedding light on the effectiveness and usability of Optuna in optimization scenarios.</p><ul><li>Optuna dashboard empowers the user to choose the best trial by knocking out the worst trials one after the other.</li><li>The maximum number of concurrent trials is 6, defined by n_generate. A new trial is created when one of the existing trials concludes.</li><li>If the number of trials awaiting human evaluation is less than n_generate , then study.should_generate() becomes true which starts the next trial.</li></ul><p>This brings us to the end of the blog. Thank you for reading this blog post. I hope you found it informative and valuable for your optimization journey. If you have any questions or comments, please feel free to leave them below.</p><p>If you feel any need to visit the code or explore how each and every part of optimisation in optuna is implemented in Python, feel free to click the <a href="https://github.com/mayank1903/Optuna">GitHub repo link</a>.</p><p>Until then, <em>happy optimizing!</em></p><h3>References</h3><ul><li>Optuna Documentation — <a href="https://optuna-dashboard.readthedocs.io/en/latest/tutorials/preferential-optimization.html">https://optuna-dashboard.readthedocs.io/en/latest/tutorials/preferential-optimization.html</a></li><li>Optuna Dashboard Documentation — <a href="https://optuna-dashboard.readthedocs.io/en/latest/">https://optuna-dashboard.readthedocs.io/en/latest/</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=869d00c4edeb" width="1" height="1" alt=""><hr><p><a href="https://medium.com/farmart-blog/harnessing-human-insight-the-power-of-human-in-the-loop-optimization-with-optuna-part-2-869d00c4edeb">Harnessing Human Insight: The Power of Human-in-the-Loop Optimization with Optuna (PART-2)</a> was originally published in <a href="https://medium.com/farmart-blog">The Good Food Economy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Harnessing Human Insight: The Power of Human-in-the-Loop Optimization with Optuna(PART-1)]]></title>
            <link>https://medium.com/farmart-blog/harnessing-human-insight-the-power-of-human-in-the-loop-optimization-with-optuna-part-1-ff1aca646943?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff1aca646943</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[hyperparameter-tuning]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Mon, 08 Apr 2024 09:32:49 GMT</pubDate>
            <atom:updated>2024-06-05T18:31:40.923Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Trh1NUTnH-oKeMAfCWMdEA.png" /></figure><p>In the realm of optimization, where algorithms tirelessly search for the most efficient solutions, a new approach emerges: <strong><em>human-in-the-loop optimization</em></strong>. This methodology, often abbreviated as <strong>HITL</strong>, bridges the gap between raw computational power and human expertise, creating a collaborative environment where both elements complement each other. Imagine embarking on a quest for the optimal solution, equipped not only with advanced algorithms but also with the nuanced understanding and intuition of a human guide.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*D_GoVe6M6hvNbNexTUefxQ.png" /><figcaption>Human and Machine interaction</figcaption></figure><p>In tasks such as image generation, natural language processing, or speech synthesis, evaluating results purely through mechanical means can be challenging due to the subjective nature of the output. Human evaluation becomes crucial in discerning subtle nuances and ensuring the quality and authenticity of the generated content. Human-in-the-loop (HITL) optimization proves invaluable in these scenarios by integrating human feedback directly into the optimization process ultimately enhancing the overall quality and relevance of the results.</p><h4>Meet Optuna and Optuna Dashboard: Your Optimization Ally</h4><p>Optuna is a versatile Python-based framework designed for hyperparameter optimization. Its primary function is to streamline the optimization process by efficiently exploring hyperparameter search spaces, aiding in tasks like tuning machine learning models and computational workflows.</p><p>Optuna also offers a user-friendly interface and integrates seamlessly with an interactive dashboard, known as the Optuna dashboard. This dashboard provides users with real-time visualisation of optimization results.</p><p><em>Curious to get started with Optuna for hyperparameter tuning?</em> Explore my previous blog for an insightful guide and unleash the power of optimization.</p><p><a href="https://medium.com/farmart-blog/optuna-turbocharging-models-with-hyper-parameter-magic-ded487ba9dd5">Optuna: Turbocharging Models with Hyper-parameter Magic</a></p><p>Now, equipped with a foundational understanding of both Human-in-the-Loop optimization and Optuna’s capabilities, let’s embark on the practical implementation. We’ll dive into Python and leverage Optuna and Optuna Dashboard to implement HITL in Generative Adversarial Networks (GANs), using the MNIST dataset as our canvas. Let’s explore how this fusion of human insight and computational power elevates the art of optimization to new horizons.</p><h3>Implementing HITL Optimization with Optuna</h3><p>Before we dive into the hands-on implementation, it’s essential to establish an understanding of Generative Adversarial Networks (GANs) which serve as the cornerstone of our chosen use case. By grasping the fundamentals of GANs, we lay a solid foundation for our exploration and implementation of Human-in-the-Loop (HITL) optimization techniques.</p><blockquote>Two types of HITL optimisations provided by optuna are <strong>Optimization using Objective Form Widget</strong> and <strong>Preferential Optimization</strong></blockquote><h4><strong>What are GANs?</strong></h4><p>Generative Adversarial Networks, or GANs, are a class of machine learning models designed to generate new data samples that are similar to a given training dataset. The key idea behind GANs is to train two neural networks simultaneously: a <strong>generator</strong> and a <strong>discriminator</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5bziJVCuU6Y2yihCLkJ6qg.png" /><figcaption>GANs training flow</figcaption></figure><ul><li><strong>Generator</strong> — It is a neural network (CNN or a FFN) responsible for generating fake samples. It takes random noise as input and transforms it into a sample that resembles real data. Without continuous training, it learns to produce increasingly realistic samples over the course of training.</li><li><strong>Discriminator</strong> — It is a neural network responsible for distinguishing between real and fake samples. It is trained to differentiate between real data and data generated by the generator. Its aim is to improve its ability to accurately classify samples as real or fake.</li><li><strong>Feedback Loop</strong> —The feedback loop is an iterative process where the generator and discriminator engage in a continuous competition. Initially, the generator produces samples from random noise. The discriminator then evaluates these samples, aiming to distinguish between real and fake data. As the discriminator provides feedback by classifying samples, the generator adjusts its parameters to generate more convincing samples, attempting to deceive the discriminator.</li></ul><p>Think of the relationship between the generator and discriminator like a game of a thief and detective. The generator acts as the crafty forger, creating fake images from random noise to fool the discriminator. Meanwhile, the discriminator plays the role of the sharp-eyed detective, learning to spot the real images from the fake ones.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DoEhBq7tylNAbAO_mNqh6Q.png" /><figcaption>Generator and Discriminator</figcaption></figure><p>As they go through this game-like training process, both the generator and discriminator try to outwit each other, improving their strategies along the way. The generator’s goal is to make its fake images as similar to real ones as possible, while the discriminator aims to get better at telling the real images apart from the fake ones.</p><p>So, in simple terms, the generator tries to make its mistakes smaller (minimize the loss), while making it difficult for the discriminator to catch those mistakes (maximise the loss). This back-and-forth pushes the GAN towards a balance where the generator creates images that are hard to distinguish from real ones, and the discriminator gets better at spotting the differences.</p><blockquote>The primary focus of this blog is to explore Human-in-the-Loop (HITL) optimization techniques using Optuna in Python, with Generative Adversarial Networks (GANs) serving as our practical application. Rather than delving deeply into GANs themselves, our goal is to leverage them as a vehicle for understanding and implementing HITL optimization strategies with Optuna.</blockquote><h3>HITL using Optuna</h3><h4>How HITL work in Optuna?</h4><p>As mentioned earlier, Human-in-the-loop (HITL) is a concept where humans play a role in machine learning or artificial intelligence systems. In HITL optimization, in particular, humans are part of the optimization process. HITL optimization is valuable in areas where human judgment is essential, like art and design, since it’s hard for machines to evaluate the output. For instance, it can optimize images created by generative models.</p><p>To implement HITL optimization, you need a way to interactively execute the optimization process, typically through a user interface (UI). Optuna dashboard comes to your rescue by providing a user-friendly interface to facilitate this interaction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G14QWZ9khLz6W9GShpTNog.png" /><figcaption>HITL using the optuna dashboard</figcaption></figure><p>In HITL optimization using <strong><em>Optuna Dashboard</em></strong>, there are primarily three components:</p><ul><li>A script that samples the hyperparameters from optuna and generates output for evaluation (Generated images in this case)</li><li>Database and File Storage to store the experiment artefacts data (data, files, etc.).</li><li>Optuna Dashboard for displaying the outputs stored in the storage and making evaluations.</li></ul><p>Given that <strong><em>Optuna</em></strong> is our optimization tool of choice, the Next step is to define an <em>objective</em> function. This function acts as our compass and returns the metric(s) we seek to minimize or maximize within our model.</p><h4>Optimization using Objective Form Widget</h4><p><em>Optimization using the Objective Form Widget</em> is one of the key methods for integrating Human-in-the-Loop (HITL) optimization in Optuna. It optimizes machine learning models by seamlessly incorporating human feedback into the process. With customizable forms, users define objectives and gather subjective evaluations, augmenting traditional objective functions. This fusion of user preferences and expertise cultivates refined model outcomes, ultimately heightening user satisfaction.</p><p>Since the purpose of this blog is to implement HITL in optuna using GANs, we will jump straight to define the objective function for hyper-parameter tuning.</p><blockquote>Feel free to refer to the code and explore the architectures of the generator and discriminator by clicking on the <a href="https://github.com/mayank1903/Optuna/blob/main/HITL_in_GANs.py">GitHub repository link</a>.</blockquote><p>So, let’s start by defining our objective function;</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/618c74409abd7869533e14fa1922a90c/href">https://medium.com/media/618c74409abd7869533e14fa1922a90c/href</a></iframe><p>Heavy code, right? Worry not, I will explain the complete code bit by bit.</p><ul><li><strong>#1</strong> — study.ask() is an Optuna method to start a new trial, allowing dynamic parameter definition beyond the trial itself.</li><li><strong>#2</strong> — After defining the trial, the process proceeds to configure the model and set up the data loader, followed by training the complete GANs model.<br> — <strong>#2.1</strong> — trial.suggest_float selects hyperparameters from a given range, while trial.suggest_categorical selects from specified values.<br> — <strong>#2.2 </strong>— With the model configurations set, both the <em>generator</em> and <em>discriminator</em> are trained in batches. The losses of both components are then displayed after each batch within an epoch.</li></ul><blockquote>Note: A single trial involves training a Generative Adversarial Network (GAN) for a specified number of epochs (<em>num_epochs</em>)</blockquote><ul><li>#3 — Following each trial, a grid of new images is generated. These images are stored in the artefact store using upload_artifact, and their paths are retrieved using get_artifact_path.</li><li>#4 — Finally, the generated grid of images, along with the generator and discriminator losses, is displayed in the note using save_note. This information will be visible in the Optuna dashboard.</li></ul><blockquote>Note: The objective function returns 2 losses, the generator and the discriminator loss since we plan to create a multi-objective optimisation using optuna.</blockquote><p>Now that the model training function is prepared, we’re poised to optimize the model through hyperparameter tuning.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a764647d6131399f8799789aa5d27f6/href">https://medium.com/media/0a764647d6131399f8799789aa5d27f6/href</a></iframe><p>In the above code,</p><ul><li>#1 — This initializes an Optuna study object named “HITL_with_optuna_for_digit_generation” for optimization. It configures optimization directions to minimize and maximize for the generator and discriminator losses, respectively. Additionally, it sets up storage using a SQLite database.</li><li>#2 — The optimization objectives (metrics) are defined using the set_metric_names method. Two objectives are specified: one for assessing satisfaction with generated images and another for evaluating satisfaction with the discriminator’s performance.</li><li>#3 — Custom ChoiseWidget are registered for inputting user feedback during optimization. These widgets provide options for users to rate their satisfaction with the generator’s and discriminator’s performance. Each ChoiceWidget includes choices (like “Yes 👍”, “Somewhat 👌”, “No 👎”) and corresponding values to capture user feedback. For more widgets, feel free to <a href="https://optuna-dashboard.readthedocs.io/en/latest/api.html#form-widgets">click here!</a></li><li>#4 — This section initiates a loop to start the optimization process. The number of currently running (TrualState.RUNNING) trials is periodically checked to ensure that six (n_batch = 6) trials are running simultaneously.</li></ul><p>Next, we start the optimisation by defining the final function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4e77794de3bba6c5a79791270daf45c/href">https://medium.com/media/e4e77794de3bba6c5a79791270daf45c/href</a></iframe><ul><li>#1 — The FileSystemArtifactStore is created, which is one of the Artifact Store options used in the Optuna. Artefact Store is used to store artefacts (data, files, etc.) generated during Optuna trials(included in the train_GANs function). Also, the <em>tmp</em> folder is created to store the grid of newly generated images.</li><li>#2 —start_optimisation() the function is called to run the trials for the optimisation process.</li></ul><p>Once you’ve executed the entire script, open a new terminal window and navigate to the root folder where the script is located. Then, run the Optuna dashboard locally by executing the following command:</p><pre>$ optuna-dashboard sqlite:///db.sqlite3 --artifact-dir ./artifact --port 5004</pre><p>Now that you’ve executed the shell command, Get set to witness the captivating optimization process with Optuna unfold on the Optuna dashboard.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F931979217%3Fapp_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fvimeo.com%2F931979217&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1829490104_1280&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1920" height="1199" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e0f1fd14af679134a19d555da4053f1/href">https://medium.com/media/7e0f1fd14af679134a19d555da4053f1/href</a></iframe><p>The above video showcases the Optuna dashboard in action as it navigates through the process of HITL (Human-in-the-Loop) optimization. It demonstrates the seamless integration of human feedback into the optimization loop, emphasizing the collaborative nature of HitL optimization. However, there are a few important points to notice from this demonstration, shedding light on the effectiveness and usability of Optuna in HitL optimization scenarios.</p><ul><li>The note space within each trial displays the grid of new images generated by the generator for easy and convenient evaluation of the model.</li><li>Optuna dashboard empowers the user to choose the appropriate option from the widget for each metric based on their subjective evaluation of the images. This assigns values to these predefined metrics for each trial.</li><li>The maximum number of concurrent trials is 6, defined by n_batch = 6. A new trial is created when one of the existing trials concludes.</li></ul><p>Thank you for reading this blog post on HITL optimization with Optuna. I hope you found it informative and valuable for your optimization journey. If you have any questions or comments, please feel free to leave them below.</p><p>Stay tuned for the next part of this blog, where we’ll delve into another type of HITL optimization offered by Optuna: <strong><em>Preferential Optimization</em></strong>.</p><p>Until then, happy optimizing!</p><p>If you feel any need to visit the code, feel free to click the <a href="https://github.com/mayank1903/Optuna">GitHub repo link</a>.</p><h3>References</h3><ul><li>Optuna Documentation — <a href="https://optuna-dashboard.readthedocs.io/en/latest/tutorials/hitl.html">https://optuna-dashboard.readthedocs.io/en/latest/tutorials/hitl.html</a></li><li>Optuna Dashboard Documentation — <a href="https://optuna-dashboard.readthedocs.io/en/latest/">https://optuna-dashboard.readthedocs.io/en/latest/</a></li><li>GANs — <a href="https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html">https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff1aca646943" width="1" height="1" alt=""><hr><p><a href="https://medium.com/farmart-blog/harnessing-human-insight-the-power-of-human-in-the-loop-optimization-with-optuna-part-1-ff1aca646943">Harnessing Human Insight: The Power of Human-in-the-Loop Optimization with Optuna(PART-1)</a> was originally published in <a href="https://medium.com/farmart-blog">The Good Food Economy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Optuna: Turbocharging Models with Hyper-parameter Magic]]></title>
            <link>https://medium.com/farmart-blog/optuna-turbocharging-models-with-hyper-parameter-magic-ded487ba9dd5?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ded487ba9dd5</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Sat, 23 Mar 2024 10:04:41 GMT</pubDate>
            <atom:updated>2024-03-26T13:41:16.642Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G0NP1w1mWap7pYDSoY3P5w.jpeg" /><figcaption>Tuning</figcaption></figure><p>Imagine a symphony without tuning. The result? Discordant notes, lost melodies, and a cacophony that fails to inspire. Just as tuning is essential to make music harmonious, <em>hyperparameter tuning</em> is the backbone of optimizing model performance in the realm of machine learning.</p><p>Traditionally, hyperparameter tuning has been akin to searching for a needle in a haystack—a laborious process of trial and error, often yielding suboptimal results. But fear not, for <strong>Optuna</strong> has emerged as the conductor of this chaotic orchestra, orchestrating harmony with unparalleled finesse.</p><p>In this blog, we’ll be diving into the exciting world of hyperparameter tuning with Optuna. No need to worry about the complexities of previous methods — today, we’re all about Optuna and its game-changing approach to optimizing model performance. In the end, we’ll unveil the Optuna dashboard — an indispensable tool for visualizing our results. With its intuitive interface, we’ll gain profound insights into our hyperparameter tuning efforts, transforming data into actionable intelligence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/764/1*2fOS3qSi7K1iQ7YB2By7DA.png" /></figure><p><a href="https://optuna.org/">Here</a> is a detailed documentation on Optuna to help you understand it better.</p><h3>Hyper-parameter Tuning</h3><p>Hyperparameter tuning is a critical aspect of machine learning model development, involving the optimization of parameters that are not learned directly from the data but rather control the learning process itself. These parameters, often referred to as hyperparameters, include learning rates, regularisation strengths, and model architectures. By fine-tuning these hyperparameters, we aim to strike the optimal balance between model complexity and generalization ability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XeJJyJLh4kfg7QzcBmXTsA.png" /><figcaption>Hyper-parameter tuning</figcaption></figure><p>Essentially, hyperparameter tuning seeks to minimize the model’s error on unseen data while preventing overfitting on the training set. This process is essential as it directly impacts the model’s performance metrics such as accuracy, precision, and recall. Through systematic exploration and optimization of hyperparameters, we can unlock the full potential of our models, enabling them to achieve superior performance across various tasks and datasets.</p><p>Next, we’re about to unveil the unparalleled power of Optuna in hyperparameter tuning, and what better way to showcase its capabilities than with the classic MNIST digit dataset? Get ready to witness Optuna’s prowess as we navigate through the intricate landscape of hyperparameter optimization, fine-tuning our model to achieve remarkable accuracy and performance.</p><p>Assuming that you have imported all the necessary libraries, let’s first create a simple CNN architecture for the classification of images.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c07e7385b851396044a751bf58bd2a3/href">https://medium.com/media/8c07e7385b851396044a751bf58bd2a3/href</a></iframe><p>Once a CNN architecture is defined, we need to define the train and test data loaders to load the images as tensors for training and validation of the model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80b92084830f14a0bb6fd13c2a9e2f64/href">https://medium.com/media/80b92084830f14a0bb6fd13c2a9e2f64/href</a></iframe><p>Now, since we have the data loaders defined, it’s time to create the functions to train and validate the model.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05702dba2560de482011cc6c0bb5e3e4/href">https://medium.com/media/05702dba2560de482011cc6c0bb5e3e4/href</a></iframe><p>The code above seems familiar, doesn’t it? Surprisingly, there’s little deviation even with the introduction of Optuna. That’s the beauty of it — Optuna seamlessly integrates into our existing codebase, offering power and comfort without the need for extensive rewrites.</p><p>Now, with our CNN training and validation functions primed and ready, we’re on the cusp of witnessing the formidable power of Optuna in action. But before we dive into the heart of hyperparameter optimization, there’s one crucial piece missing: <strong><em>the objective function</em></strong>. This function will serve as our guide, evaluating the performance of different hyperparameter configurations and guiding Optuna towards the optimal solution.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91400387a10bf90083f10386a375b667/href">https://medium.com/media/91400387a10bf90083f10386a375b667/href</a></iframe><p>In optuna, conventionally functions to be optimized are named objective. It takes a very important parameter, <a href="https://optuna.readthedocs.io/en/stable/reference/generated/optuna.trial.Trial.html#optuna.trial.Trial"><strong><em>trial</em></strong></a><strong><em>. </em></strong>It corresponds to a single execution of the objective function and is internally instantiated upon each invocation of the function.</p><p>In simpler terms, think of a Trial object as a record of each time we run our objective function. Every time we call the function, a Trial object is created to keep track of that specific execution. It’s like having a notepad where we jot down the details of each attempt at finding the best hyperparameters for our model. This way, Optuna can learn from each trial and gradually steer towards the optimal configuration.</p><p>Also, in the above code snippet, the focal point lies in the way model configurations are defined.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wl1XwnQenNJ4OaRHq9jnBA.png" /><figcaption>Model configurations</figcaption></figure><p>The <a href="https://optuna.readthedocs.io/en/stable/reference/generated/optuna.trial.Trial.html#optuna.trial.Trial.suggest_float"><em>trial.suggest_float</em></a> method suggests floating-point values for hyperparameters based on predefined ranges. The ‘step’ parameter in momentum defines the step size after which each value of momentum should be selected.</p><blockquote>Note: The metrics that we aim to minimize/maximize during the hyperparameter training, test accuracy in this case, should be returned by the objective function.</blockquote><p>Now, it’s time to set the optimization process in motion by creating a Study object within Optuna.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3da365a58d4870cdc145db42a517d59e/href">https://medium.com/media/3da365a58d4870cdc145db42a517d59e/href</a></iframe><p>This Study object serves as the orchestrator of our hyperparameter tuning endeavour, overseeing the iterative exploration of parameter configurations. The <a href="https://optuna.readthedocs.io/en/stable/reference/generated/optuna.study.Study.html#optuna.study.Study.optimize"><em>optimize</em></a> method on our Study object refines its search, gradually converging towards the most promising hyperparameter settings.</p><blockquote>Storage: Defines the storage backend for recording and managing optimization results. In this case, it’s set to “sqlite:///db.sqlite3”, indicating the use of SQLite as the storage mechanism. This enables Optuna to store trial results persistently in a database.</blockquote><blockquote>Sampler: Specifies the strategy used for sampling hyperparameters during optimization. Here, TPESampler stands for Tree-structured Parzen Estimator (TPE) sampler, a popular method for efficient exploration of hyperparameter space. <a href="https://optuna.readthedocs.io/en/stable/reference/samplers/generated/optuna.samplers.TPESampler.html">Here</a> is a list of different samplers used in optuna.</blockquote><blockquote>Direction: Determines the optimization direction, indicating whether we aim to maximize or minimize the objective function. In this case, ‘maximize’ is specified, implying that we seek to maximize the objective function’s output.</blockquote><p>Finally, running the above code, creates different trials with each subsequent trial aiming to, in our case, maximize the test accuracy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LDRRE3Nv8lJRIOwPCFCtlw.png" /><figcaption>Mode training</figcaption></figure><p>The above snippet captures the concluding moments of our hyperparameter tuning journey. As the model completes all its trials, the final trial ended with an accuracy of ~95% with trial 12 being the best trial with ~99% accuracy on the test data i.e. the output of our objective function.</p><h3>Optuna-Dashboard</h3><p>Now that our hyperparameter tuning process using Optuna is complete, we have at our disposal the <a href="https://optuna-dashboard.readthedocs.io/en/latest/getting-started.html"><strong><em>Optuna dashboard</em></strong></a>. This dashboard serves as a comprehensive tool for reviewing all the trials conducted throughout our optimization journey. It offers a detailed overview of each trial’s results, including objective function values and corresponding hyperparameter configurations.</p><p>With the Optuna dashboard, we’re granted a bird’s-eye view of our optimization efforts, facilitating a deeper understanding of the performance landscape and aiding in the selection of the most effective hyperparameter settings.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f15b480059c99e74d2b66e3e42e73009/href">https://medium.com/media/f15b480059c99e74d2b66e3e42e73009/href</a></iframe><p>Simply running the above code in the terminal, will produce the below output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8LXN9W_2n7cHALgoQ1goPQ.png" /><figcaption>optuna-dashboard</figcaption></figure><p>Navigating to the link mentioned will take you to an Optuna dashboard where you can explore the results of hyperparameter optimization experiments. This dashboard provides visualizations and metrics to help analyze the performance of different hyperparameter configurations across your machine-learning models. It allows you to track trials, compare performance metrics, and gain insights into the effectiveness of various hyperparameter settings.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fplayer.vimeo.com%2Fvideo%2F926555692%3Fapp_id%3D122963&amp;dntp=1&amp;display_name=Vimeo&amp;url=https%3A%2F%2Fvimeo.com%2F926555692&amp;image=https%3A%2F%2Fi.vimeocdn.com%2Fvideo%2F1820854544-ad4ffa876a0c42ca0b4171e0aaf88400d6d018704c1020e9c20800557b658e32-d_1280&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=vimeo" width="1920" height="1199" frameborder="0" scrolling="no"><a href="https://medium.com/media/e705a64f3127672ea232b5a243e5dfa9/href">https://medium.com/media/e705a64f3127672ea232b5a243e5dfa9/href</a></iframe><p>Finally, I’ve embedded a walkthrough video demonstrating the features and functionalities of optuna dashboard. By watching the video, you’ll get an idea to navigate the Optuna dashboard.</p><p>This brings us to the end of the blog. I hope this blog post has been informative and helpful in your journey towards mastering hyperparameter optimization with Optuna. Feel free to leave any questions or comments below, and happy optimizing!”</p><p>If you feel any need to visit the code, feel free to click the <a href="https://github.com/mayank1903/Optuna/tree/main">Repo</a> link.</p><p>Also, you can read more of my interesting articles/blogs. Some of the links are added below:</p><ul><li><a href="https://medium.com/@mayankkhulbe1903/footprints-in-graphs-the-enchantment-of-deepwalk-9cca7337e08f">Footprints in Graphs: The Enchantment of DeepWalk</a></li><li><a href="https://medium.com/farmart-blog/connecting-dots-exploring-the-hough-transform-for-image-analysis-585942a7f56a">Connecting Dots: Exploring the Hough Transform for Image Analysis</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ded487ba9dd5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/farmart-blog/optuna-turbocharging-models-with-hyper-parameter-magic-ded487ba9dd5">Optuna: Turbocharging Models with Hyper-parameter Magic</a> was originally published in <a href="https://medium.com/farmart-blog">The Good Food Economy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Footprints in Graphs: The Enchantment of DeepWalk]]></title>
            <link>https://medium.com/farmart-blog/footprints-in-graphs-the-enchantment-of-deepwalk-9cca7337e08f?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cca7337e08f</guid>
            <category><![CDATA[graph-neural-networks]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[neural-networks]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Sat, 09 Mar 2024 18:09:02 GMT</pubDate>
            <atom:updated>2024-03-26T13:24:49.614Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B6DRbKoEvT4MaHc5oXv6_Q.png" /></figure><p><em>Initially, let’s set the stage through a captivating story.</em></p><p>In the bustling streets of a virtual city, Alex, a digital wanderer, took deliberate steps through the intricacies of interconnected pathways. Each choice at a crossroad led to new encounters and unexpected discoveries, mimicking the unpredictable nature of a random walk. This journey through the virtual alleys reflects the essence of graph theory, a mathematical framework that explores the relationships between nodes and edges within complex networks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FaMrJ0jIvWnjH9toVc9uVg.png" /></figure><p>Enters the concept of a random walk, where one explores a network by moving from node to node in a stochastic manner, is fundamental to understanding the patterns within these intricate systems.</p><p>Now, imagine if Alex’s exploration could be not just random but purposeful, revealing hidden structures and connections intentionally. Here enters DeepWalk, a groundbreaking algorithm in graph theory, that systematically explores networks, unveiling latent relationships and capturing the essence of structural information.</p><p>In this blog, we embark on a journey inspired by Alex’s virtual footsteps. We’ll delve into the intricacies of random walks in graph theory and explore the revolutionary DeepWalk algorithm and its transformative impact on our understanding of interconnected nodes and edges. Also, we will use Python to understand graphs and finally implement DeepWalk using Python.</p><p>Let’s commence our exploration by delving into the foundations of graph theory.</p><h4>Understanding Graph Theory</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*Yx1RGbYQSDPWmljStBCFhQ.png" /><figcaption>Graphs in Graph Theory</figcaption></figure><p>At its core, graph theory unveils the secrets hidden within networks, where entities are represented by nodes/vertices and the relationships between them are defined by edges.</p><p>Imagine nodes as distinct points, each encapsulating an entity or element in our network. These nodes symbolise anything from individuals in a social network to locations in a city grid or even molecules in a chemical compound. Now, envision edges as the threads weaving connections between these nodes, representing the relationships that bind them together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gK_5Yp3UlZfdDoDefpsWZQ.png" /><figcaption>Directed and Undirected graphs</figcaption></figure><p>In graph theory, we have two types of graphs namely directed and undirected graphs.</p><p>A <em>directed graph, or digraph</em>, is a type of graph where edges have a direction or orientation. Each edge connects two nodes but has a specific starting and ending point, indicating a one-way relationship. This directionality is often represented by arrows.</p><p>On the other hand, an <em>undirected graph</em> is a type of graph where edges have no direction. The relationships between nodes are symmetric, meaning that if node A is connected to node B, then node B is also connected to node A. Undirected graphs are often used to represent symmetrical relationships like friendships or connections in social networks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E0jjXSfejm82EiboTUdg9A.png" /><figcaption>Directed and Un-directed graph</figcaption></figure><p>The depicted image effectively illustrates the distinction between directed and undirected graphs. In the context of social connections, the relationship between User 1 and User 2 on Instagram, where User 1 follows User 2, is asymmetrical. This asymmetry signifies a directed graph, as the relationship is not necessarily reciprocated.</p><p>Conversely, in the case of Facebook friendships between User 1 and User 2, the bidirectional nature of the connection is apparent. If User 1 is friends with User 2, the reverse is also true. This mutual relationship characterises an undirected graph, emphasising the symmetry inherent in the connections within the network.</p><h4><strong>Understanding Skip-Gram</strong></h4><p>Curiosity may arise regarding Skip-Gram’s significance, as outlined in the <a href="https://arxiv.org/pdf/1403.6652.pdf">paper</a>, within the realm of graph theory. What makes the skip-gram model a crucial player in deciphering the intricate dance of interconnected nodes is the fact that nodes that are closer to each other in a graph are considered more similar in a learned embedding space.</p><p><em>Intrigued by how this transpires?</em> You’re in for a treat as we navigate through the intricacies of this blog, leaving no stone unturned. So, sit back, relax, and let’s embark on a journey that unravels the magic within interconnected nodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/1*Lgf8eJWPEHr-c8DYP_VafA.png" /><figcaption>CBOW and Skip-gram</figcaption></figure><p>At its core, skip-gram is a type of neural network architecture that learns to predict the context (surrounding nodes) of a given node within a graph. In the context of DeepWalk, skip-gram endeavours to capture the essence of structural information by navigating through the connections. In other words, Skip-gram is used to represent each node with an embedding of n-dimensions which are learned through the model training such that nodes that are closer in a graph are considered more similar.</p><p>If you’re eager to delve deeper into the magic of Skip-Gram and are keen on crafting your own Skip-Gram model from the ground up, check out my previous blog where I guide you through the process of implementing Skip-Gram from scratch using Python.</p><p><a href="https://medium.com/farmart-blog/skipgram-implementation-from-scratch-pytorch-875e948941a9">Skipgram implementation from scratch — Pytorch</a></p><h4><strong>Understanding DeepWalk</strong></h4><p>DeepWalk? Sounds very familiar to Random Walk, right? So is the intuition.</p><p>Random walk lies at the very core of the DeepWalk algorithm as it serves as the dynamic exploration mechanism, unravelling the hidden intricacies within the network. In the realm of DeepWalk, each random walk can be envisioned as a virtual journey undertaken from one node to another capturing the local neighbourhood structure and extracting valuable information about the relationships between nodes.</p><p>Theoretically, DeepWalk initiates by selecting a starting node within the graph and embarking on a random walk, traversing a path to one of its neighbouring nodes, chosen at random for each step. This iterative process continues until the desired length of the sequence is achieved. By incorporating these sequences into its learning process, DeepWalk, inspired by skip-gram, transforms the seemingly arbitrary movements of virtual wanderers into a structured approach for generating embeddings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*njn3-86Ux4RrXs5bocsRWw.png" /><figcaption>Karate Club Graph</figcaption></figure><p>The displayed image features a renowned graph derived from the Karate Club data. Observing the graph, it’s evident that nodes in close proximity within the graph also exhibit proximity in the embedding space.</p><p>Eager to experience this magic firsthand? Your anticipation is about to be fulfilled as we dive into the implementation of the DeepWalk algorithm in Python.</p><h4>DeepWalk implementation in Python</h4><p>Before we delve into the depth of DeepWalk, make sure that you have <em>networkx</em> library installed which is important for the creation, manipulation, and study of data structures for graphs, digraphs, and multi-graphs.</p><p>Let’s start by plotting a random graph in python</p><pre>#importing the common libraries<br><br>import pandas as pd<br>import numpy as np<br>import seaborn as sns<br>import matplotlib.pyplot as plt<br>from ipywidgets import interact, widgets<br>from sklearn.decomposition import PCA<br>import os<br>import json<br><br>#ignore the warnings<br>import warnings<br>warnings.filterwarnings(&#39;ignore&#39;)<br><br>#importing the library for graphs<br>import networkx as nx<br><br># Generate an Erdos-Renyi graph<br>n = 30<br>p = 0.15<br>G = nx.erdos_renyi_graph(n=n, p=p, seed=10) #this graph takes in the number of nodes and the probability<br>nx.draw_networkx(G, nx.spring_layout(G, seed=0))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1020/1*4-hiFcIyAAMESKvRTXUVtg.png" /><figcaption>Random Graph</figcaption></figure><p>As we can see, we have generated a random graph using the function <em>erdos_renyi_graph</em> in the <em>networkx</em> library. This function creates a graph that takes 2 main parameters; ’n’ i.e. number of nodes and ‘p’ i.e. probability thus creating a graph with ’n’ number of nodes with ‘p’ probability of 2 nodes being connected through an edge.</p><blockquote>Lower the value of ‘p’, less connected is the graph. Higher the value of ‘p’, denser is the graph.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6f2CnDCfCcENPXWgrsd_4A.png" /><figcaption>Erdos renyi graph for extreme values of ‘p’</figcaption></figure><p>Now, once we have the graph ready. We will write a function to generate a random walk.</p><pre>#function to create random walk<br>def generate_random_walk(start_node, walk_length, graph):<br>    walk_sequence = [start_node]<br>    for _ in range(walk_length):<br>        neighbors = [neighbor_node for neighbor_node in graph.neighbors(start_node)]<br>        if not neighbors:<br>            break<br>        next_node = np.random.choice(neighbors, 1)[0]<br>        walk_sequence.append(next_node)<br>        start_node = next_node<br>    return walk_sequence</pre><p>The <em>generate_random_walk</em> function simulates a random walk on a given graph. Taking a starting node, walk length, and the graph structure as input parameters, the function generates a sequence of nodes representing the random walk.</p><p>At each step, the function identifies the neighbours of the current node in the graph and randomly selects one as the next destination for the walk. This process repeats for the specified walk length. Let’s highlight a random walk in the graph (highlighted with green).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KeCJuQmQc0p9HdSILDpdzA.png" /><figcaption>Highlighted random walk</figcaption></figure><p>Once we have the function to create the sequence handy, it is time to use the pre-trained Skip-gram model from gensim library. The model will be used to first create initial embedding for each node and train the model to learn the embedding so that nodes that are closer in the above graph are mapped closer in the embedding space.</p><pre>#function to train word2vec<br>def train_word2vec(sequences, vector_size, graph):<br>    model = Word2Vec(sentences=sequences,<br>                     vector_size=vector_size,<br>                     window=2,<br>                     sg=1,<br>                     min_count=1,<br>                     seed=1)<br>    model.build_vocab(sequences)<br>    model.train(sequences, total_examples=model.corpus_count, epochs=100, report_delay=1)<br><br>    node_embeddings = {node: model.wv[node] for node in list(graph.nodes())}<br><br>    return node_embeddings</pre><p>The presented function follows a familiar pattern for training Skip-gram models commonly used in Natural Language Processing (NLP). In this context, the input sequences resemble tokenised text data typically processed in NLP tasks. The function employs the Word2Vec Skip-gram model, leveraging parameters such as vector size, window size, and seed for training on the given sequences. The resultant model is then used to generate embeddings for each node in the graph, forming a dictionary with nodes as keys and their corresponding embeddings as values.</p><p>Now, as you can see the size of the embedding vectors created is 100. So, a more pragmatic approach to visualisation involves reducing the dimensions to enhance interpretability. The primary motivation behind this reduction is to create a scatter plot that captures the intrinsic structure of the embeddings in a visually comprehensible manner. The ensuing scatter plot serves as a powerful visualisation tool, allowing for a concise yet informative representation of the graph embeddings in a reduced-dimensional space.</p><p>Any idea what we have next in the picture to reduce the dimensions of the embeddings? Right, it’s <strong>Principal Component Analysis</strong>.</p><p>So, let’s reduce to the embedding dimension from 100 to 2 and plot the embedding in the 2-D space using a scatter plot.</p><pre>#reduce the dimension of embeddings from 100 to 2<br>def dimension_reduction_using_pca(node_embeddings):<br>    pca = PCA(n_components=2)<br>    node_embeddings_2d = pca.fit_transform(list(node_embeddings.values()))<br><br>    return node_embeddings_2d</pre><p>Now, since we have all the functions ready, let’s write a final function to create an interactive widget in Python to create the graph and visualise the embeddings of the nodes in a 2-D space.</p><pre>def generate_graph_and_visualize(num_nodes, probability, walk_length, vector_size):<br>    # Generate an Erdos-Renyi graph<br>    G = nx.erdos_renyi_graph(num_nodes, probability, seed=10)<br><br>    # Generate random walk sequences<br>    sequences = []<br>    for _ in range(100):<br>        sequences.append(generate_random_walk(np.random.choice(num_nodes), walk_length, G))<br><br>    # Train word2vec model<br>    embeddings = train_word2vec(sequences, vector_size, G)<br><br>    # Plot the graphs in a subplot<br>    fig, axes = plt.subplots(1, 2, figsize=(15, 5))<br><br>    # Plot the original graph<br>    axes[0].set_title(&quot;Erdos-Renyi Graph&quot;)<br>    nx.draw_networkx(G, nx.spring_layout(G, seed=0), with_labels=True, ax=axes[0])<br><br>    # Plot the node embeddings using PCA<br>    axes[1].set_title(&quot;Node Embeddings from SkipGram Model (2D PCA)&quot;)<br>    node_embeddings_2d = dimension_reduction_using_pca(embeddings)<br>    for (node, _), (x, y) in zip(zip(G.nodes(), set(G.nodes())), node_embeddings_2d):<br>        axes[1].scatter(x, y, color=&#39;blue&#39;)<br>        axes[1].text(x, y, str(node), fontsize=8, ha=&#39;right&#39;, va=&#39;bottom&#39;)<br><br>    plt.show()<br><br># Create sliders<br>num_nodes_slider = widgets.IntSlider(value=30, min=10, max=50, step=1, description=&#39;Num Nodes:&#39;)<br>probability_slider = widgets.FloatSlider(value=0.15, min=0, max=1, step=0.01, description=&#39;Probability:&#39;)<br>walk_length_slider = widgets.IntSlider(value=10, min=1, max=20, step=1, description=&#39;Walk Length:&#39;)<br>vector_size_slider = widgets.IntSlider(value=100, min=10, max=200, step=10, description=&#39;Vector Size:&#39;)<br><br># Create the interactive function<br>interact(generate_graph_and_visualize,<br>         num_nodes=num_nodes_slider,<br>         probability=probability_slider,<br>         walk_length=walk_length_slider,<br>         vector_size=vector_size_slider);</pre><p>The provided function, generate_graph_and_visualize, seamlessly integrates the creation and visualisation of an Erdos-Renyi graph, random walk sequences, and the subsequent Skip-gram model embeddings. It enhances the interpretability of the graph structure by incorporating a dimensionality reduction step using Principal Component Analysis (PCA). Below is a concise overview of the function’s features:</p><ol><li><strong>Erdos-Renyi Graph Creation:</strong></li></ol><ul><li>Utilizes NetworkX to generate an Erdos-Renyi graph based on user-specified parameters such as the number of nodes (num_nodes) and probability of edge creation (probability).</li></ul><p>2. <strong>Random Walk Sequence Generation:</strong></p><ul><li>Creates 100 random walk sequences on the generated graph, with the length of each walk determined by the walk_length parameter.</li></ul><p>3. <strong>Word2Vec Model Training:</strong></p><ul><li>Utilises the train_word2vec function to train a Skip-gram model on the generated random walk sequences, obtaining embeddings for each node in the graph.</li></ul><p>4. <strong>Graph Visualisation:</strong></p><ul><li>Plots the original Erdos-Renyi graph in one subplot, providing an overview of the graph’s structure.</li><li>In a second subplot, applies PCA to reduce the dimensionality of the embeddings to 2D and plots the node embeddings. Each node is represented by a point in the scatter plot, enhancing the visual exploration of inter-node relationships.</li></ul><p>5. <strong>Interactive Parameter Adjustment:</strong></p><ul><li>Includes slider widgets (num_nodes_slider, probability_slider, walk_length_slider, and vector_size_slider) to allow users to dynamically set values for the number of nodes, probability, walk length, and vector size, respectively.</li></ul><p>Now, let’s visualise the output of the above function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LmcP9d_5EksefPuzSzMXhQ.png" /><figcaption>Node embeddings</figcaption></figure><p>The visual representation above elegantly encapsulates the core principles of DeepWalk and underscores the significance of Skip-gram within the algorithm.</p><p>Notably, it vividly illustrates how nodes, specifically those labelled as 0, 4, 23, 28, and 29, maintain a close spatial relationship in both the original graph and the resulting embedding space. This alignment signifies that nodes with proximity in the graph also exhibit closeness in the embedded feature space, highlighting the effectiveness of Skip-gram in capturing and preserving the structural relationships within the network.</p><p>This brings us to the end of the blog. I have tried to be as detailed as possible to make this blog more understandable to you. I hope that I matched the expectation that I had set up initially from this blog where we have touched upon the concept of graph theory while deep diving into the theory and implementation of DeepWalk.</p><p>If you feel any need to visit the code, feel free to click the <a href="https://github.com/mayank1903/Graph_Neural_Network">Repo</a> link.</p><p>Also, you can read more of my interesting articles/blogs. Some of the links are added below:</p><ul><li><a href="https://medium.com/farmart-blog/connecting-dots-exploring-the-hough-transform-for-image-analysis-585942a7f56a">Connecting Dots: Exploring the Hough Transform for Image Analysis</a></li><li><a href="https://medium.com/farmart-blog/anomaly-detection-in-images-using-feature-descriptors-c7124669bdfb">Anomaly detection in Images — Using feature descriptors</a></li></ul><p>I will be back with more such interesting blogs and to help the community at large, until then,</p><p>Happy Reading ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cca7337e08f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/farmart-blog/footprints-in-graphs-the-enchantment-of-deepwalk-9cca7337e08f">Footprints in Graphs: The Enchantment of DeepWalk</a> was originally published in <a href="https://medium.com/farmart-blog">The Good Food Economy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connecting Dots: Exploring the Hough Transform for Image Analysis]]></title>
            <link>https://medium.com/farmart-blog/connecting-dots-exploring-the-hough-transform-for-image-analysis-585942a7f56a?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/585942a7f56a</guid>
            <category><![CDATA[image-processing]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Sun, 06 Aug 2023 18:49:09 GMT</pubDate>
            <atom:updated>2023-08-08T09:14:58.070Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xXjsehro79dTWWRv9Ykxsw.png" /><figcaption>Hough Transform</figcaption></figure><p>In the realm of Artificial Intelligence, the field of computer vision has gained exponential significance in recent times. From autonomous vehicles navigating through busy streets to medical imaging diagnostics aiding in early disease detection, the ability of machines to interpret and understand visual information has opened up a world of possibilities.</p><p>At the heart of this fascinating domain lies the Hough Transform, an ingenious algorithm that has revolutionized image analysis. In this blog series, ‘Connecting Dots,’ we deep dive into the depths of the Hough Transform algorithm, unraveling its inner workings and exploring its vast applications in the realm of image analysis. So, let’s dive in and witness the magic of connecting dots, where patterns, shapes, and hidden relationships come to life before our very eyes.</p><p>In this blog, we will understand the use of hough transform in detecting straight lines and implement the same in Python.</p><p>★★ — Surprise — ★★</p><p>An interesting use case of hough transform is waiting for you at the end of this blog.</p><h3>Introduction</h3><p>Indeed, implementing the hough transform using Python is very exciting but understanding the intuition and concept behind the hough transform is no less.</p><p>So, before jumping to the implementation of hough transform in Python, let’s understand what is hough transform and the mathematical intuition behind it.</p><h4>What is Hough Transform?</h4><p>First introduced by Paul Hough in the early 1960s, the Hough Transform is a powerful image-processing technique used to detect and extract geometric shapes and patterns within images. The primary goal of the Hough Transform is to identify specific shapes, such as lines, circles, or ellipses, by converting them from their Cartesian representation (x, y coordinates) into a parameter space.</p><p>This transformation allows the algorithm to efficiently locate these shapes, even in the presence of noise and partial occlusions i.e. if the edges in the image are broken or separated. The Hough Transform has proven to be an invaluable tool in a wide range of applications, including object recognition, lane detection in autonomous vehicles, and medical image analysis, making it a cornerstone in the field of computer vision.</p><h4>Mathematical Understanding</h4><p>With no further ado, let’s understand the intuition behind hough transform.</p><p>Before diving deep into the intuition, it becomes very important to understand what we all might know, the cartesian space and the parametric space, and how lines are defined in both of these spaces.</p><h4>Lines in Cartesian Space/Image Space</h4><p>As you might recollect, any line in a cartesian space can be defined by the equation <em>y = mx + c</em>, where</p><p>m = slope of the line<br>c = the y intercept, where the line cuts the y-axis, and<br>(y,x) = any coordinate in the cartesian plane that the line passes through.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*e5zZxgW-UT2EwTWpTNnBSw.png" /><figcaption>Line in cartesian space</figcaption></figure><p>This is what we call <em>cartesian space/image space.</em></p><p>Now, let’s understand how this line is transformed from a cartesian space/ image space to a parametric space or (m,c) space.</p><h4>Lines in Parametric Space</h4><p><strong>(m,c) space</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*O9FcrCoooZGM8Fkw7dTduA.png" /></figure><p>Suppose, we have a line y=mx+c which passes through the point x_i and y_i, then the equation of the line can be written as the one on the L.H.S. below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hm6YD6Cy6b8ipPrL1gLFhQ.png" /></figure><p>Now, we can rewrite the same equation as the one on the R.H.S. Since, x_i and y_i are known, the equation on the R.H.S. gives us the equation of the line in the (m,c) space.</p><p>So, now the point (x_i,y_i) in the image space can be transformed and represented in the (m,c) parameter space. This can be represented as below;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*stRp-rX_wQqKK0M6O-F4Qw.png" /><figcaption>Cartesian space v/s cm space</figcaption></figure><p>As we can see in the above image, each point in the cartesian space can be represented by a line in the mc space. Also, points along each line in mc space correspond to lines that pass through that point in the cartesian space.</p><blockquote>Note: The point where lines intersect in the mc space (denoted by (m,c) in the above image) corresponds to the line that passes through each point in the cartesian space.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rQl7AYP3WQplj98M2cdYRw.png" /><figcaption>Representation from cartesian to mc space</figcaption></figure><p>One interesting thing is that, a point in cartesian space maps to a line in mc space, and a line in cartesian space maps to a point in the mc space and vice versa.</p><blockquote>Note: For vertical and horizontal lines, m i.e. slope of a line can range from -∞ to +∞ respectively</blockquote><p><strong>(𝜌, 𝜃) space</strong></p><p>As we move further, there is one more parametric representation of line in the (𝜌, 𝜃) space also called the polar coordinate system. Let’s deep dive to understand this.</p><p>Let’s now represent the line with another equation using 𝜌 and 𝜃 as the parameters in the polar coordinate system. The new equation of the line becomes x*sin(𝜃) — y*cos(𝜃) + 𝜌 = 0, where</p><p>(x,y) = coordinates of a point that lies on the line<br>𝜌 = distance of the line from the origin<br>𝜃 = orientation of the line with the x-axis</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9npA0EMGT7WmDVsfrXn5-A.png" /><figcaption>Line in the polar coordinate system</figcaption></figure><p>Unlike the mc space, a point in the polar coordinate system corresponds to a sinusoid in the (𝜌,𝜃) space. This can be shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iK2vZghH1IdWy2ZJzWqong.png" /><figcaption>Polar coordinate system to (𝜌,𝜃) space</figcaption></figure><p>Interesting right? Let’s do a small experiment and try implementing it in Python.</p><p>Let’s draw some points in an image and see what each point maps to in the hough space i.e. (𝜌,𝜃) space.</p><pre>fig, axes = plt.subplots(1,2, figsize=(10, 5))<br><br>#create an image of size (150,150)<br>image = np.zeros((150,150))<br><br>#create white dots by setting the pixel values as 255<br>image[75, 75] = 255<br>image[25, 25] = 255<br>image[50, 50] = 255<br>image[100, 100] = 255<br>image[125, 125] = 255<br><br>#calculate the accumulator and the theta and rho values<br>accumulator, thetas, rhos = hough_line(image)<br><br>#plot the original image<br>axes[0].imshow(image)<br>axes[0].set_title(&#39;Original Image&#39;)<br>axes[0].axis(&#39;off&#39;)<br><br>#plot the hough space accumulator<br>axes[1].imshow(accumulator, cmap=&#39;hot&#39;)<br>axes[1].set_title(&#39;Hough Space&#39;)<br>axes[1].axis(&#39;off&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*p0y0lXAEGFIm-XmAtvQ74g.png" /><figcaption>Polar coordinate to (𝜌,𝜃) space</figcaption></figure><p>As we can clearly see in the above image, each point in the polar coordinate system corresponds to a sinusoid in the hough space or the (𝜌,𝜃) space. Also, the intersection point maps to the line that passes through all the points in the polar coordinate system.</p><blockquote>Note: Hough transform, by default, uses the polar coordinate system to detect lines in an image due to the infinite range of slope of a line in the cartesian system leading to high computation usage.<br>Also, Hough space is also called the <strong>accumulator</strong> and the intersecting points map to the line in the polar coordinate system.</blockquote><h4>Working of Hough Transform Algorithm</h4><p>Once we have understood how we map the intersection points to lines in the image space or the polar coordinate system, it becomes easy to understand the working of the Hough transform algorithm.</p><ol><li>Edge Detection: Perform edge detection on the input image using an edge detection algorithm like the Canny edge detector. The goal is to obtain a binary image where the edges are represented by white pixels and the background is black.</li><li>Parameter Space Initialisation: Create an accumulator space (parameter space) to store the votes for potential lines. The parameter space typically consists of two axes representing the parameters of the lines (e.g., Rho, 𝜌 and Theta, 𝜃).</li><li>Voting: For each edge point in the binary image, calculate all possible lines (defined by the parameters 𝜌 and 𝜃) that could pass through that point. Increment the corresponding cells in the accumulator space to vote for these lines.</li><li>Thresholding: After the voting process, the accumulator space will have peaks that correspond to the most likely lines. Apply a threshold to the accumulator space to select the most prominent lines. The threshold helps eliminate noise and less significant lines.</li><li>Line Extraction: Identify the peaks in the accumulator space that exceed the threshold. Each peak represents a potential line in the image.</li><li>Convert Parameters to Image Space: Convert the parameters of the detected lines from the accumulator space back to the image space to obtain the actual lines in the image.</li><li>Draw Detected Lines: Draw the detected lines on the original image using the parameters obtained in the previous step.</li></ol><p>below is the pseudo code to understand this better,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hkLmF522YuseHvUFV63Q-g.png" /><figcaption>Pseudo code for implementing Hough transform</figcaption></figure><h4>Types of Hough Transform</h4><p>Now, since we have a decent idea of what hough transform is and how it works, let’s try to implement different types of hough line transform in Python.</p><p><strong>Standard Hough Line Transform</strong></p><p>Thanks to the <em>skimage</em> library in Python, only a few lines of code can help detect straight lines in an image.</p><pre>import matplotlib.pyplot as plt<br>import numpy as np<br>from skimage.io import imread<br>from skimage.feature import canny<br>from skimage.transform import hough_line, hough_line_peaks<br><br>def plot_image_with_lines(image, lines):<br>    fig, axes = plt.subplots(1, 2, figsize=(12, 6))<br><br>    # Plot the original image<br>    axes[0].imshow(image, cmap=&#39;gray&#39;)<br>    axes[0].set_title(&#39;Original Image&#39;)<br>    axes[0].axis(&#39;off&#39;)<br><br>    # Plot the image with detected lines<br>    axes[1].imshow(image, cmap=&#39;gray&#39;)<br>    for _, angle, dist in zip(*lines):<br>        y0 = (dist - 0 * np.cos(angle)) / np.sin(angle)<br>        y1 = (dist - image.shape[1] * np.cos(angle)) / np.sin(angle)<br>        axes[1].plot((0, image.shape[1]), (y0, y1), &#39;r&#39;)<br>    axes[1].set_xlim((0, image.shape[1]))<br>    axes[1].set_ylim((image.shape[0], 0))<br>    axes[1].set_title(&#39;Image with Detected Lines&#39;)<br>    axes[1].axis(&#39;off&#39;)<br><br>    plt.show()<br><br># Load the image<br>image_path = &#39;data/images/sudoku.jpeg&#39;  # Replace with the actual path to your image<br>image = imread(image_path, as_gray=True)<br><br># Apply edge detection (Canny)<br>edges = canny(image, sigma=2, low_threshold=0.1, high_threshold=0.3)<br><br># Apply Standard Hough Transform<br>h, theta, d = hough_line(edges)<br><br># Find the most prominent lines<br>lines = hough_line_peaks(h, theta, d)<br><br># Plot the original image and the image with detected lines<br>plot_image_with_lines(image, lines)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*A0rqrDfiaDUoPFM_Y0K-Rw.png" /><figcaption>Standard Hough Transform</figcaption></figure><p>As you can see in the output, the lines detected by the standard hough transform range from one end of the image to the other. Also, it sometimes captures the noise while detecting the straight lines in an image.</p><p><strong>Probabilistic Hough Line Transform</strong></p><p>Since the standard Hough transform uses all the points in an image to detect straight lines, it became computationally heavy. <em>The probabilistic Hough Line Transform</em> is an improvement over the standard method. Instead of considering all points in the image, it randomly selects a subset of points and then applies the Hough Transform only to those points. By using random sampling, the algorithm focuses only on the points belonging to the longer, more prominent lines in the image, effectively reducing the computational cost and eliminating the need to consider all points. This approach is especially useful for detecting long and continuous lines, as it avoids detecting many small and noisy segments.</p><pre>import matplotlib.pyplot as plt<br>import numpy as np<br>from skimage.io import imread<br>from skimage.feature import canny<br>from skimage.transform import probabilistic_hough_line<br><br>def plot_image_with_lines(image, lines):<br>    fig, axes = plt.subplots(1, 2, figsize=(12, 6))<br><br>    # Plot the original image<br>    axes[0].imshow(image, cmap=&#39;gray&#39;)<br>    axes[0].set_title(&#39;Original Image&#39;)<br>    axes[0].axis(&#39;off&#39;)<br><br>    # Plot the image with detected lines<br>    axes[1].imshow(image, cmap=&#39;gray&#39;)<br>    for line in lines:<br>        p0, p1 = line<br>        axes[1].plot((p0[0], p1[0]), (p0[1], p1[1]), &#39;g&#39;)<br>    axes[1].set_title(&#39;Image with Detected Lines&#39;)<br>    axes[1].axis(&#39;off&#39;)<br><br>    plt.show()<br><br># Load the image<br>image_path = &#39;data/images/sudoku.jpeg&#39;  # Replace with the actual path to your image<br>image = imread(image_path, as_gray=True)<br><br># Apply edge detection (Canny)<br>edges = canny(image, sigma=2, low_threshold=0.1, high_threshold=0.3)<br><br># Apply Probabilistic Hough Transform<br>min_line_length = 75<br>max_line_gap = 5<br>lines = probabilistic_hough_line(edges, threshold=10, line_length=min_line_length, line_gap=max_line_gap)<br><br># Plot the original image and the image with detected lines<br>plot_image_with_lines(image, lines)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/950/1*9RVzO7MfHhmv_7WwQ40rWg.png" /><figcaption>Probabilistic Hough Transform</figcaption></figure><p>As we can clearly see, the probabilistic Hough transform detects clean and clear straight lines compared to the standard Hough transform.</p><p>This brings us to the end of this blog…. but wait…..!!! I guess we missed something.</p><p>The surprise right?</p><p>So, last but not least, let’s discuss one interesting use case of the Hough transform technique. Surprisingly, the hough transform works perfectly with text documents. One prominent use case is the alignment correction of the documents. This can be easily achieved by applying the Hough transform on the desired documents.</p><p>Results of the same can be seen in the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5Dq3x7e7yclwansXJSvvjw.png" /><figcaption>Document Alignment Correction</figcaption></figure><p>This brings us to the end of this blog. I have tried to be as detailed as possible to make this blog more understandable to you. Still, if you feel any need to visit the code, feel free to click on the below GIT repo link.</p><p><em>Repo: </em><a href="https://github.com/mayank1903/hough-transform"><em>https://github.com/mayank1903/hough-transform</em></a></p><p>Also, you can read more of my interesting articles/blogs. Some of the links are added below;</p><ol><li>Neural Machine Translation from scratch with Attention — <a href="https://medium.com/farmart-blog/mastering-languages-unveiling-the-power-of-neural-machine-translation-with-attention-981fb88d3a78">https://medium.com/farmart-blog/mastering-languages-unveiling-the-power-of-neural-machine-translation-with-attention-981fb88d3a78</a></li><li>Anomaly detection in Images — Using feature descriptors: <a href="https://medium.com/farmart-blog/anomaly-detection-in-images-using-feature-descriptors-c7124669bdfb">https://medium.com/farmart-blog/anomaly-detection-in-images-using-feature-descriptors-c7124669bdfb</a></li><li>Profiling — Key to Python code Optimisation: <a href="https://medium.com/farmart-blog/profiling-key-to-python-code-optimisation-ff635679d185">https://medium.com/farmart-blog/profiling-key-to-python-code-optimisation-ff635679d185</a></li></ol><p>I will be back with more such interesting blogs and to help the community at large, until then,</p><p>Happy Reading ;)</p><h3>References</h3><ul><li><a href="https://sbme-tutorials.github.io/2021/cv/notes/4_week4.html">https://sbme-tutorials.github.io/2021/cv/notes/4_week4.html</a></li><li><a href="https://scikit-image.org/docs/stable/api/skimage.transform.html#skimage.transform.hough_line">https://scikit-image.org/docs/stable/api/skimage.transform.html#skimage.transform.hough_line</a></li><li><a href="https://www.youtube.com/watch?v=XRBc_xkZREg">https://www.youtube.com/watch?v=sQUqQddQtB4</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=585942a7f56a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/farmart-blog/connecting-dots-exploring-the-hough-transform-for-image-analysis-585942a7f56a">Connecting Dots: Exploring the Hough Transform for Image Analysis</a> was originally published in <a href="https://medium.com/farmart-blog">The Good Food Economy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering Languages: Unveiling the Power of Neural Machine Translation with Attention]]></title>
            <link>https://medium.com/farmart-blog/mastering-languages-unveiling-the-power-of-neural-machine-translation-with-attention-981fb88d3a78?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/981fb88d3a78</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[deeplearing]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Mon, 26 Jun 2023 09:24:50 GMT</pubDate>
            <atom:updated>2023-07-05T08:18:00.012Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*ULea4TNnKgdhtom39_rXrQ.png" /><figcaption><a href="https://www.google.com/search?sxsrf=APwXEdfqjCuHUgpjo3fsK0WpppaIKWDm0w:1687668261223&amp;q=google+translate&amp;spell=1&amp;sa=X&amp;ved=2ahUKEwiwy9WZzt3_AhWJyDgGHQTKDrQQBSgAegQIBxAB&amp;biw=1366&amp;bih=689&amp;dpr=1">Google Translation</a></figcaption></figure><p>In a world where languages connect people from diverse cultures, the ability to bridge linguistic barriers has become more crucial than ever. Enter Neural Machine Translation (NMT), a cutting-edge approach that empowers machines to translate text from one language to another with astonishing accuracy. Behind the scenes, NMT combines the power of deep learning and artificial intelligence, allowing us to witness the awe-inspiring fusion of human-like language understanding and computational prowess.</p><p>In this blog post, We traverse an exhilarating path into the realm of NMT, uncovering the intricate mechanisms that make it all possible. We’ll dive headfirst into building a translation model from scratch using the PyTorch framework, equipping you with the knowledge and tools to create your multilingual translation system.</p><p><strong><em>But …… why start from scratch?</em> </strong><br>By building our own NMT model, we gain a deep understanding of the inner workings of this groundbreaking technology. We’ll demystify the magic behind attention mechanisms and encoder-decoder architectures, empowering you to become an architect of language transformation.</p><p>So, whether you’re a language enthusiast, an aspiring AI developer, or simply curious about the miracles of NMT, this blog is your gateway to unlocking the secrets of machine translation. Let’s jump straight into building an NMT model with Attention Mechanism from scratch.</p><h3>Enter Attention</h3><p>Before diving into the depth of a Neural Machine Translation Model, let’s first understand <em>Attention</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*Xc3kQ7Csr3evlOy2zZnSPw.jpeg" /><figcaption><a href="https://arxiv.org/pdf/1706.03762.pdf">Attention is all you need</a></figcaption></figure><p>As the above image explains, metaphorically or literally, this section requires your utmost “Attention” to understand the “Attention” Mechanism which is the heart of a Neural Machine Translation Model.</p><p><em>Now, what is Attention?</em><br>To understand the concept of Attention, Let’s first compare the intuition behind a simple seq2seq model (without attention) and one with Attention.</p><blockquote><strong>Intuition: Seq2Seq, Without Attention</strong><br>Suppose, your current task at hand is to translate an English text to Hindi. You would read the entire English sentence once and then go word by word and translate the English sentence to Hindi. It might be possible that, if the sentence is too long, you forget some English words while translating.</blockquote><blockquote><strong>Intuition: Seq2Seq, With Attention</strong><br>Now, you have the same task of translating an English text into Hindi. But this time, when reading the entire English sentence, you note significant keywords. Then, use those keywords to translate the entire English text to Hindi.</blockquote><p>I hope the above intuitions make it easy for you to compare a simple Language Translation Model to one with Attention. In brief, Attention places different focus on different words by assigning each word a score. Then, using the softmax scores, we aggregate the encoder hidden states using a weighted sum of the encoder hidden states, to get the context vector.</p><p>Traditionally, there are two types of attention,</p><ol><li><strong>Global Attention (also known as Soft Attention)</strong>: Global attention mechanism considers all the hidden states of the input sequence when assigning attention weights to different elements.</li><li><strong>Local Attention (also known as Hard Attention)</strong>: Local attention mechanism focuses only on a subset of the encoder’s hidden states rather than the entire sequence.</li></ol><p>Now, that we have understood what Attention is and how it is used in a Machine Translation Model, let’s jump to the code to deep dive into the architecture.</p><p><strong>Building a Neural Machine Translation Model — From Scratch</strong></p><p>Initially, we import the required libraries including tensorboard which helps us to log the training and validation performance of the model.</p><p>Also, we create a logs folder to log the training and validation performances of the model for each experiment.</p><p>Now, let’s load the data and perform basic cleaning on the data to make it ready for training. I have used an English-to-Hindi translation dataset from Kaggle. To consume the same data, you can <a href="https://www.kaggle.com/datasets/preetviradiya/english-hindi-dataset">click here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*IVuobmudiB0AsGo63F4AGQ.png" /></figure><p>Now, Let’s do some data cleaning on the data using the below-defined <em>clean_sentences</em> function.</p><p>In data cleaning, we perform the following functions:</p><ol><li>Removed the extra spaces and numbers from the English column.</li><li>Remove the rows where English words are present in the Hindi column.</li><li>Replace the punctuations and extra spaces from Hindi and English sentences.</li></ol><p>Now, let’s read the data and apply the cleaning function to both the English and Hindi sentences column.</p><pre>#apply cleaning on the dataset<br><br>trans_data[&#39;english_sentence&#39;] = trans_data[&#39;english_sentence&#39;].apply(lambda x: clean_sentences(x, punct, hindi=False))<br>trans_data[&#39;hindi_sentence&#39;] = trans_data[&#39;hindi_sentence&#39;].apply(lambda x: clean_sentences(x, punct, hindi=True))<br>trans_data = trans_data.dropna().reset_index(drop=True)</pre><p>The next step is to define a <em>Tokenizer</em> class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*oqT03EWMHn4oILY-5IMHNw.png" /><figcaption>Tokenizer</figcaption></figure><ul><li>The tokenizer class creates a unique list of words in the sentences and provides each word with an index.</li><li>It also adds &lt;pad&gt;, &lt;bos&gt;, &lt;eos&gt;, and &lt;unk&gt; tokens to the vocabulary of the sentences.</li><li>The tokenizer class has an encode function that takes the sequence of words and converts them into a sequence of numbers.</li><li>The class also has a decoder function that converts the sequence of numbers back to the original sentence.</li></ul><p>Once, we have the tokenizer, the next step is to create a MachineTranslationDataset class to load the data in batches. We also define a collate function that gets the input sequences from the MachineTranslationDataset class and returns the encoded tensors from the input sequences.</p><pre>## Creating the dataloader<br><br>class MachineTranslationDataset(Dataset):<br>    &quot;&quot;&quot;<br>    Creating a custom dataset for reading the dataset and <br>    loading it into the dataloader to pass it to the neural network for finetuning the model<br><br>    &quot;&quot;&quot;<br><br>    #initialise the parameters<br>    def __init__(self, dataframe, source_text, target_text): #this decorator initialises the parameters<br><br>        self.data = dataframe.reset_index(drop=True)<br>        # self.source_len = source_len<br>        # self.summ_len = target_len<br>        self.target_text = self.data[target_text]<br>        self.source_text = self.data[source_text]<br>        self.source_tokenizer = source_tokenizer<br>        self.target_tokenizer = target_tokenizer<br><br>    <br>    def __len__(self): #gives the length of the dataset<br>        return len(self.target_text)<br><br>    def __getitem__(self, index):<br>        source_text = str(self.source_text[index])<br>        target_text = str(self.target_text[index])<br><br>        #cleaning data so as to ensure data is in string type<br>        source_text = &#39; &#39;.join(source_text.split())<br>        target_text = &#39; &#39;.join(target_text.split())<br>        <br>        return {<br>            &#39;source_text&#39;: source_text,<br>            &#39;target_text&#39;: target_text<br>        }<br>        <br>    def collate_fn(self, examples):<br>        source_text = [t[&#39;source_text&#39;] for t in examples]<br>        target_text = [t[&#39;target_text&#39;] for t in examples]<br><br>        #tokenizing the source text<br>        encoded_source = self.source_tokenizer.encode(source_text, pad_type=&quot;pad_to_max_length&quot;)<br>        # s_vocab_len = len(self.source_tokenizer.word2index)<br>        <br>        #tokenizing the target text<br>        encoded_target = self.target_tokenizer.encode(target_text, pad_type=&quot;pad_to_max_length&quot;)<br>        # t_vocab_len = len(self.target_tokenizer.word2index)<br><br>        #converting to tensors<br>        encoded_source = torch.tensor(encoded_source)<br>        encoded_target = torch.tensor(encoded_target)<br><br>        return {<br>            &#39;source_text&#39;: encoded_source.to(dtype = torch.long), <br>            &#39;target_text&#39;: encoded_target.to(dtype = torch.long)<br>        }</pre><p>Now, since we are done with cleaning and data loading, the next step is to define the architectures of the encoders and decoders with attention. Once, that is done we will stitch the models together to create a final Seq2Seq class for model training</p><h3>Encoder</h3><p>Let’s first define the encoder class.</p><pre>## creating the encoder class<br>#add dropout<br><br>class EncoderLSTM(nn.Module):<br>    <br>    def __init__(self, input_dim, embedding_dim, hidden_dim, batch_size, p, n_layers, bidirectional=True, batch_first=False):<br>        super().__init__()<br>        self.input_dim = input_dim<br>        self.embedding_dim = embedding_dim<br>        self.hidden_dim = hidden_dim<br>        self.bidirectional = bidirectional<br>        self.batch_size = batch_size<br>        self.n_layers = n_layers<br>        self.batch_first = batch_first<br>        if self.n_layers &gt; 1:<br>            self.p = p<br>        else:<br>            self.p = 0<br>        self.dropout = nn.Dropout(self.p)<br>        self.embedding = nn.Embedding(self.input_dim, self.embedding_dim)<br>        self.lstm = nn.LSTM(self.embedding_dim, self.hidden_dim, self.n_layers, dropout = self.p, batch_first=self.batch_first, bidirectional=self.bidirectional)<br>        self.fc_hidden = nn.Linear(self.hidden_dim * 2, self.hidden_dim)<br>        self.fc_cell = nn.Linear(self.hidden_dim * 2, self.hidden_dim)<br>        <br>    def forward(self, x, hidden=None):<br>        # # print(&quot;vocab&quot;, self.input_dim)<br>        # # print(&quot;Input shape&quot;, x.shape)<br>        if hidden==None:<br>            # # print(&quot;Hidden is Initialised....&quot;)<br>            hidden = self.init_hidden()<br>            # print(hidden.shape)<br>            <br>        embedded = self.dropout(self.embedding(x))<br>        embedded = embedded.permute(1,0,2)<br>        # print(&quot;Embedding shape:&quot;,embedded.shape)<br>        lstm_output, (hidden, cell) = self.lstm(embedded)<br>        <br>        # print(&quot;LSTM output shape:&quot;,lstm_output.shape)<br>        # print(&quot;Hidden state shape:&quot;,hidden.shape)<br>        # print(&quot;Cell state shape:&quot;,cell.shape)<br>        <br>        hidden = self.fc_hidden(torch.cat((hidden[0:1], hidden[1:2]), dim = 2))<br>        cell = self.fc_cell(torch.cat((cell[0:1], cell[1:2]), dim = 2))<br>        # print(&quot;Hidden state shape:&quot;,hidden.shape)<br>        # print(&quot;Cell state shape:&quot;,cell.shape)<br>        return lstm_output, hidden, cell<br>    <br>    def init_hidden(self):<br>        if self.bidirectional:<br>            hidden = torch.zeros(2*self.n_layers, self.batch_size, self.hidden_dim)<br>            return hidden<br>        else:<br>            hidden = torch.zeros(1*self.n_layers, self.batch_size, self.hidden_dim)<br>            return hidden</pre><p>The above encoder class is a bidirectional LSTM encoder, which has the embedding layer, LSTM, and two fully-connected linear layers.</p><ol><li>The <a href="https://pytorch.org/docs/stable/generated/torch.nn.Embedding.html">embedding layer</a> is a simple lookup table where each word in the vocabulary is mapped to an embedding of size embedding_dim.</li><li>The <a href="https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html">LSTM layer</a> is then given the embedded sequence as the input. This layer has 3 outputs. The LSTM output, is a concatenation of the hidden states at each time step, the final hidden state, and the final cell state.</li><li>Since the LSTM is bidirectional, the last hidden and cell states for both forward and backward directions are concatenated and passed through the <a href="https://pytorch.org/docs/stable/generated/torch.nn.Linear.html">linear layers</a> to get the hidden and cell states.</li></ol><p><strong>Note:</strong><em> If no hidden or cell state is passed to the encoder or decoder, they are initialized with 0 values.</em></p><p>Similarly, Let’s define our decoder class.</p><pre>class DecoderLSTM(nn.Module):<br>    <br>    def __init__(self, output_dim, embedding_dim, hidden_dim, batch_size, p, n_layers, bidirectional=False):<br>        <br>        super().__init__()<br>        self.output_dim = output_dim           #size of the target vocab<br>        self.embedding_dim = embedding_dim<br>        self.hidden_dim = hidden_dim<br>        self.bidirectional = bidirectional<br>        # self.input_dim = input_dim<br>        self.batch_size = batch_size<br>        self.n_layers = n_layers<br>        if self.n_layers &gt; 1:<br>            self.p = p<br>        else:<br>            self.p = 0<br>        self.dropout = nn.Dropout(self.p)<br>        self.embedding = nn.Embedding(self.output_dim, self.embedding_dim) #the input dim is size of the vocab and hence equal to the output dim<br>        self.lstm = nn.LSTM(self.hidden_dim*2 + self.embedding_dim, self.hidden_dim, self.n_layers, dropout=self.p, batch_first=False, bidirectional=self.bidirectional)       <br>        self.energy = nn.Linear(self.hidden_dim*2 + self.hidden_dim, self.hidden_dim)<br>        self.v = nn.Linear(self.hidden_dim, 1)<br>        self.softmax = nn.Softmax(dim=0)<br>        self.relu = nn.ReLU()<br>        self.fc = nn.Linear(self.hidden_dim, self.output_dim)<br>        # self.softmax = nn.Softmax(1)<br><br>    def forward(self, x, encoder_states, hidden, cell):<br>        &quot;&quot;&quot;<br>        x: input_ids [bs, seq_len]<br>        hidden: [bs, hidden_dim]<br>        cell: [bs, hidden_dim]<br>        &quot;&quot;&quot;<br>        #input size --&gt; (batch_size)<br>        x = x.unsqueeze(0)<br>        <br>        # # print(&quot;Input shape:&quot;, x.shape) --&gt; (1, batch_size)<br>        <br>        embedded = F.relu(self.dropout(self.embedding(x))) <br>        # print(&quot;embedding shape:&quot;, embedded.shape) --&gt; (1, batch_size, embedding_dim)<br>        <br>        if hidden==None:<br>            # # print(&quot;Hidden is Initialised....&quot;)<br>            hidden = self.init_hidden()<br>            # # print(hidden.shape)<br><br>        seq_length = encoder_states.shape[0]<br>        <br>        #repeat the hidden states by sequence length<br>        h_reshaped = hidden.repeat(seq_length,1,1) <br>        <br>        # print(&quot;hidden_reshaped shape:&quot;, h_reshaped.shape) --&gt; (1, batch_size, hidden_dim) to (seq_len, batch_size, hidden_dim)<br>        # print(&quot;encoder states shape:&quot;, encoder_states.shape) --&gt; (seq_len, batch_size, hidden_dim*2)<br>        <br>        concat_vector = torch.cat((h_reshaped,encoder_states), dim=2)<br>        # print(&quot;concat vector shape:&quot;, concat_vector.shape) --&gt; (seq_len, batch_size, hidden_dim*3)<br>        <br>        energy = self.relu(self.energy(concat_vector))<br>        # print(&quot;Energy shape:&quot;, energy.shape) --&gt; (seq_leb, batch_size, hidden_dim)<br>        <br>        attention = self.softmax(self.v(energy))<br>        # print(&quot;attention shape:&quot;, attention.shape) --&gt; (seq_len, batch_size, 1)<br>        <br>        attention = attention.permute(1,2,0)<br>        # print(&quot;attention permute shape&quot;, attention.shape) --&gt; (batch_size, 1, seq_len)<br>        <br>        encoder_states = encoder_states.permute(1,0,2)<br>        # print(&quot;encoder states permute shape&quot;, encoder_states.shape) --&gt; (batch_size, seq_len, hidden_dim*2)<br>        <br>        context_vector = torch.bmm(attention, encoder_states).permute(1,0,2)<br>        # print(&quot;context permute shape&quot;, context_vector.shape) --&gt; (1, batch_size, hidden_dim*2)<br>        <br>        lstm_input = torch.cat((context_vector, embedded), dim=2)<br>        # print(&quot;lstm input shape&quot;,lstm_input.shape) --&gt; (1, batch_size, hidden_dim*2 + embedding_dim)<br>        <br>        output, (hidden, cell) = self.lstm(lstm_input, (hidden, cell))<br>        <br>        # print(&quot;Output shape&quot;, output.shape) --&gt; (1, batch_size, hidden_dim)<br>        # print(&quot;Hidden state shape:&quot;,hidden.shape) --&gt; (1, batch_size, hidden_dim)<br>        # print(&quot;Cell state shape:&quot;,cell.shape) --&gt; (1, batch_size, hidden_dim)<br>        <br>        output = self.fc(output)<br><br>        # print(&quot;Final output shape:&quot;, output.shape) --&gt; (1, batch_size, target vocab size)<br>        <br>        prediction = output.squeeze(0)<br>    <br>        # print(&quot;Final prediction shape:&quot;, prediction.shape) --&gt; (batch_size, target vocab size)<br>        <br>        return prediction, hidden, cell<br>        <br>    def init_hidden(self):<br>        if self.bidirectional:<br>            hidden = torch.zeros(2*self.n_layers, self.batch_size, self.hidden_dim)<br>            return hidden<br>        else:<br>            hidden = torch.zeros(1*self.n_layers, self.batch_size, self.hidden_dim)<br>            return hidden</pre><p>Initially, the above decoder might look a bit confusing. Let’s understand what is going on behind the frame in the decoder by exploring the forward function.</p><ol><li>Initially, we have an embedding layer that receives a single word for each batch at a time step as an input (This is how the decoder works). This layer maps each word to an embedding of size embedding_dim.</li><li>Now, we calculate the energy between all the hidden layers of the encoder and the hidden layer of the decoder. To do this, we repeat the decoder’s hidden state times sequence length. After this, we concatenate both the encoder states and the hidden state and pass through an energy layer. This can be thought of as calculating how well each encoder’s hidden state “matches” the previous decoder’s hidden state.</li><li>Then, we have a linear layer ‘v’ which can be thought of as the weights for a weighted sum of the energy across all encoder hidden states. These weights tell us how much we should attend to each token in the source sequence.</li><li>The next step is to apply softmax on the weight values so that sum of all the values sums up to 1.</li><li>Then, the weights are multiplied with the encoder output through batch matrix multiplication which results in the weighted sum of the encoder’s hidden states. This is also called the final context vector.</li><li>Lastly, The embedded input word, the weighted context vector, and the previous decoder hidden state are then all passed into the decoder LSTM, with the embedded vector and context vector being concatenated together.</li></ol><h3>Seq2Seq Model — Stitching the Encoder with the Decoder</h3><p>Now, since we are ready with the Encoder and the Decoder, it’s time to stitch both together to create a complete Encoder-Decoder Model.</p><pre>class Seq2Seq(nn.Module):<br>    <br>    def __init__(self, encoder, decoder, device):<br>        super().__init__()<br>        self.encoder = encoder<br>        self.decoder = decoder<br>        self.device = device<br>        <br>    def forward(self, src, trg, teacher_forcing_ratio=0.5):<br>        <br>        batch_size = trg.shape[0]<br>        seq_len = trg.shape[1]<br>        trg_vocab_size = self.decoder.output_dim<br>        <br>        <br>        #tensor to store the output of the prediction<br>        outputs = torch.zeros(batch_size, seq_len, trg_vocab_size).to(self.device)<br>        <br>        #last hidden state of the encoder is used as the initial hidden state of the decoder<br>        lstm_output, hidden, cell = self.encoder(src)<br><br>        <br>        #input starting from bos token<br>        inp = trg[:,0]<br>        <br>        #run for complete sequence length<br>        for i in range(1, seq_len):<br>            <br>            decoder_out, hidden, cell = self.decoder(inp, lstm_output, hidden, cell)<br>            <br>            outputs[:,i,:] = decoder_out<br>            <br>            teacher_force = random.random() &lt; teacher_forcing_ratio<br>            <br>            #get the highest predicted token from our predictions<br>            top1 = decoder_out.argmax(1) <br>            <br>            #if teacher forcing, use actual next token as next input<br>            #if not, use predicted token<br>            inp = trg[:,i] if teacher_force else top1<br>            # # print(&quot;Input shape:&quot;, inp.shape)<br>    <br>        return outputs</pre><p>In the above code, we first initialize the encoder and the decoder. In the forward function, we get the encoder states, hidden, and cell state from the encoder. Since the decoder receives one word at a time for each batch, along with the encoder’s hidden and cell state, we start with the BOS token and loop through the complete sequence length to get the final prediction.</p><p>We also use <strong>teacher forcing</strong> to let the decoder use the ground truth to make the predictions at each time step.</p><h4>Teacher Forcing</h4><p>Before moving forward, let’s understand one important concept, i.e. Teacher Forcing</p><p><strong>Teacher forcing</strong> is an algorithm for training the weights of recurrent neural networks (RNNs). It involves feeding observed sequence values (i.e. ground-truth samples) rather than the predicted values back into the RNN after each step, thus forcing the RNN to stay close to the ground-truth sequence. This can be better explained with the below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*IGiqkQymuwc9BGr_0ROKMA.png" /><figcaption>Teacher Forcing</figcaption></figure><p>However, this can cause divergence at the time of training and inference. This is because there is no ground truth available at the time of inference, so the network has to feed its previous prediction back to itself for the next prediction.</p><h3>Model Training</h3><p>Now, the final step is to create a final model training and evaluation function to log the performance of the model in the Tensorboard.</p><pre>def train(model, loader, optimizer, criterion, device, epoch, writer):<br>    <br>    model.train()<br>    train_loss = 0<br>    for idx, batch in enumerate(tqdm(loader)):<br><br>        src = batch[&#39;source_text&#39;].to(device) <br>        trg = batch[&#39;target_text&#39;].to(device)<br><br>        # optimizer.zero_grad()<br><br>        output = model.forward(src, trg) # [bs, seq_len, vocab]<br><br>        # print(&quot;Forward 2 output shape&quot;, output.shape)<br>        <br>        output_dim = output.shape[-1]<br>        <br>        output = output[1:].view(-1, output_dim)<br>        trg = trg[1:].view(-1)<br>        <br>        #trg = [(trg len - 1) * batch size]<br>        #output = [(trg len - 1) * batch size, output dim]<br>        <br>        loss = criterion(output, trg)<br>        <br>        loss.backward()<br><br>        optimizer.step()<br>        optimizer.zero_grad()<br><br>        writer.add_scalar(&#39;Training_Loss&#39;, loss.item(), epoch)<br><br>        if idx % 10 == 0:<br>            print(f&#39;{epoch}:{idx} &lt;&gt; Loss: {loss.item()}&#39;)<br><br>        train_loss += loss.item()<br>    <br>    return train_loss/len(loader)</pre><p>In the above code, we create a simple training function where we run model training at a batch level, defined by the batch size. The above function returns the training loss.</p><p>Now, similarly, let’s create a validation function</p><pre>def evaluate(model, loader, criterion, device, epoch, writer):<br>    <br>    model.eval()<br>    <br>    val_loss = 0<br>    <br>    with torch.no_grad():<br>        <br>        for idx, batch in enumerate(tqdm(loader)):<br>            <br>            src = batch[&#39;source_text&#39;].to(device)<br>            trg = batch[&#39;target_text&#39;].to(device)<br>            trg_vocab_size = model.decoder.output_dim<br>            <br>            output = model(src, trg)<br>            output_dim = output.shape[-1]<br>            <br>            output = output[1:].view(-1, output_dim)<br>            trg = trg[1:].view(-1)<br>            <br>            loss = criterion(output, trg)<br>            <br>            val_loss += loss.item()<br>            <br>        validation_loss = val_loss/len(loader)<br>        writer.add_scalar(&#39;Validation_Loss&#39;, validation_loss, epoch) <br>    return validation_loss</pre><p>Now, since we are ready with the training and the validation function, the next step is to train our own Neural Machine Translation Model. Before training the model, let’s define the model parameters and initiate the encoder and the decoder.</p><pre>model_params = {<br>    &quot;batch_size&quot;: 32,<br>    &quot;embedding_dim&quot;: 128,<br>    &quot;hidden_dim&quot;: 256,<br>    &quot;n_layers&quot;: 1,<br>    &quot;p&quot;: 0<br>}<br><br>#params<br>input_dim = len(source_tokenizer.word2index)<br>embedding_dim = model_params[&#39;embedding_dim&#39;]<br>hidden_dim = model_params[&#39;hidden_dim&#39;]<br>batch_size = model_params[&#39;batch_size&#39;]<br>n_layers = model_params[&#39;n_layers&#39;]<br>p = model_params[&#39;p&#39;]<br>output_dim = len(target_tokenizer.word2index)<br><br>#loading the tokenizers<br>source_tokenizer = create_tokenizer(trans_data, &#39;english_sentence&#39;)<br>target_tokenizer = create_tokenizer(trans_data, &#39;hindi_sentence&#39;)<br><br>#model<br>encoder = EncoderLSTM(input_dim, embedding_dim, hidden_dim, batch_size, p, n_layers)<br>decoder = DecoderLSTM(output_dim, embedding_dim, hidden_dim, batch_size, p, n_layers)<br>model = Seq2Seq(encoder, decoder, device).to(device)</pre><p>In the above code snippet, we define the model parameters, load the source and target tokenizers and initialize the sequence to sequence model. Now, we will define the final training function.</p><pre>def model_training(dataframe, source_column, target_column, epochs, device):<br><br>    print(&quot;Initialising Model.....&quot;)<br>    <br>    print(f&#39;The model has {count_parameters(model):,} trainable parameters&#39;)<br>    <br>    best_valid_loss = float(&#39;inf&#39;)<br>    <br>    #training and the test split<br>    train_dataset, val_dataset = training_validation_split(dataframe, 0.25)<br>    <br>    #creating the dataset using MachineTranslationDataset class<br>    <br>    training_set = MachineTranslationDataset(train_dataset, source_column, target_column)<br>    val_set = MachineTranslationDataset(val_dataset, source_column, target_column)<br>    <br>    #creating the train and val set parameters for dataloader<br>    train_params = {<br>        &quot;batch_size&quot;: batch_size,<br>        &quot;shuffle&quot;: True,<br>        &quot;num_workers&quot;: 0<br>    }<br><br>    val_params = {<br>        &quot;batch_size&quot;: batch_size,<br>        &quot;shuffle&quot;: False,<br>        &quot;num_workers&quot;: 0,<br>        <br>    }<br>    <br>    #creating the dataloader<br>    train_loader = DataLoader(training_set, **train_params, collate_fn=training_set.collate_fn)<br>    # return train_loader<br>    val_loader = DataLoader(val_set, **val_params, collate_fn=val_set.collate_fn)<br>    <br>    #define optimizer and criterion<br>    optimizer = optim.Adam(model.parameters(), lr=1e-4)<br>    <br>    #criterion<br>    trg_token_id = target_tokenizer.word2index[&#39;&lt;pad&gt;&#39;]<br>    criterion = nn.CrossEntropyLoss(ignore_index = trg_token_id)<br>    <br>    # print(f&quot;Initiating Fine Tuning and Validation...&quot;)<br>    tb_writer = SummaryWriter(f&quot;logs/Neural_machine_translation_{fol_num}&quot;)<br>    for epoch in range(epochs):<br>        <br>        print(f&quot;...........Epoch number {epoch} starting...........&quot;)<br>        <br>        start_time = time.time()<br>        <br>        train_loss = train(model, train_loader, optimizer, criterion, device, epoch, tb_writer)<br>        val_loss = evaluate(model, val_loader, criterion, device, epoch, tb_writer)<br>        <br>        end_time = time.time()<br>    <br>        epoch_mins, epoch_secs = epoch_time(start_time, end_time)<br>        <br>        if val_loss &lt; best_valid_loss:<br>            best_valid_loss = val_loss<br>            torch.save(model.state_dict(), &#39;tut1-model.pt&#39;)</pre><p>Now, once we have defined our training function, we are ready to train our own Neural Machine Translation Model.</p><pre>model_training(trans_data, &#39;english_sentence&#39;, &#39;hindi_sentence&#39;, 70, device)</pre><p>Let’s visualize our initial training loss.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JOxS7xJiMlGfKF5XPLLGcQ.png" /><figcaption>Model Training</figcaption></figure><p>As we can see, our model is getting successfully trained as the training loss is decreasing. Let’s visualize this better using tensorboard.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*czoatBUUr21rchZ-IG4PWA.png" /><figcaption>Training Loss</figcaption></figure><p>As we can see, our model loss has declined gradually over model training. So, we have successfully trained our Machine Translation Model with Attention from scratch.</p><p>This brings us to the end of this blog. I have tried to be as detailed as possible to make this blog more understandable to you. Still, if you feel any need to visit the code, feel free to click on the below GIT repo link.</p><p><em>Repo: </em><a href="https://github.com/mayank1903/Neural-Machine-Translation">https://github.com/mayank1903/Neural-Machine-Translation</a></p><p>Also, you can read more of my interesting articles/blogs. Some of the links are added below;</p><ol><li>Anomaly detection in Images — Using feature descriptors: <a href="https://medium.com/farmart-blog/anomaly-detection-in-images-using-feature-descriptors-c7124669bdfb">https://medium.com/farmart-blog/anomaly-detection-in-images-using-feature-descriptors-c7124669bdfb</a></li><li>Skipgram Implementation from Scratch — <a href="https://medium.com/farmart-blog/skipgram-implementation-from-scratch-pytorch-875e948941a9">https://medium.com/farmart-blog/skipgram-implementation-from-scratch-pytorch-875e948941a9</a></li><li>Profiling — Key to Python code optimization: <a href="https://medium.com/farmart-blog/profiling-key-to-python-code-optimisation-ff635679d185">https://medium.com/farmart-blog/profiling-key-to-python-code-optimisation-ff635679d185</a></li></ol><p>I will be back with more such interesting blogs and to help the community at large, until then,</p><p>Happy Reading ;)</p><h3>References</h3><ul><li>Teacher Forcing — <a href="https://towardsdatascience.com/what-is-teacher-forcing-3da6217fed1c">https://towardsdatascience.com/what-is-teacher-forcing-3da6217fed1c</a></li><li>Attention Mechanism — <a href="https://towardsdatascience.com/attn-illustrated-attention-5ec4ad276ee3">https://towardsdatascience.com/attn-illustrated-attention-5ec4ad276ee3</a></li><li><a href="https://www.youtube.com/watch?v=sQUqQddQtB4">https://www.youtube.com/watch?v=sQUqQddQtB4</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=981fb88d3a78" width="1" height="1" alt=""><hr><p><a href="https://medium.com/farmart-blog/mastering-languages-unveiling-the-power-of-neural-machine-translation-with-attention-981fb88d3a78">Mastering Languages: Unveiling the Power of Neural Machine Translation with Attention</a> was originally published in <a href="https://medium.com/farmart-blog">The Good Food Economy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Skipgram implementation from scratch — Pytorch]]></title>
            <link>https://medium.com/farmart-blog/skipgram-implementation-from-scratch-pytorch-875e948941a9?source=rss-8bc54d30dd1c------2</link>
            <guid isPermaLink="false">https://medium.com/p/875e948941a9</guid>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <category><![CDATA[word-embeddings]]></category>
            <category><![CDATA[word2vec]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[mayank khulbe]]></dc:creator>
            <pubDate>Tue, 23 May 2023 05:16:11 GMT</pubDate>
            <atom:updated>2023-06-05T11:42:05.494Z</atom:updated>
            <content:encoded><![CDATA[<h3>Skipgram implementation from scratch — Pytorch</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*QsKwPBNJwpugdMCY72Lr8Q.png" /><figcaption>Word2Vec (Source: <a href="https://monkeylearn.com/blog/word-embeddings-transform-text-numbers/">https://monkeylearn.com/blog/word-embeddings-transform-text-numbers/</a>)</figcaption></figure><p>In recent times, there has been an exponential increase in the use cases pertaining to Natural Language Processing. With this, word embeddings have revolutionized the way we understand and represent language in computational models. Among the various techniques used for word embeddings, skip-gram has emerged as a powerful approach for capturing rich semantic relationships between words. By leveraging the context in which words appear within a corpus, skip-gram enables us to create dense and meaningful vector representations that capture the nuances of word semantics. In this article, we will explore some basics related to the skip-gram method and implement the same from scratch using pytorch in Python.</p><p>Before jumping to the implementation, let’s understand some pointers that are important to get a better understanding.</p><h3><strong>Cross-Entropy Loss</strong></h3><p>In the realm of natural language processing and word embedding, cross-entropy loss plays a crucial role in training skip-gram models. Cross-entropy loss is a measure used to quantify the dissimilarity between the predicted probabilities and the true distribution of words in a given context.</p><p>Cross-Entropy loss penalizes the model for poor prediction as it calculates the negative log of the predicted probability of the observed class. To understand this better, let’s plot a Cross-Entropy loss graph for a list of predicted probabilities.</p><pre>#import the libraries<br>import numpy as np<br>import matplotlib.pyplot as plt<br><br>#create a list of 100 prob between 0.01 and 0.99<br>predicted_probs = np.linspace(0.01, 0.99, 100)<br><br>#calculate the cross entropy for each probability<br>cross_entropy_loss = -np.log(predicted_probs) #ce takes the predicted probability of the observed class.<br><br>#plot the curve cross entropy curve<br>plt.plot(predicted_probs, cross_entropy_loss)<br>plt.xlabel(&#39;Predicted Probability for Correct Class&#39;)<br>plt.ylabel(&#39;Cross-Entropy Loss&#39;)<br>plt.title(&#39;Cross-Entropy Loss vs. Predicted Probability&#39;)<br>plt.grid(True)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*0uxhgu6jH7jXGRa_LOmjmQ.png" /></figure><p>As we can see in the above graph, however, the loss is nearly 0 for good predictions, the CE loss explodes when the prediction is poor with low very predicted probability.</p><p>One more point to note is that Cross-Entropy loss is equivalent to a combination of LogSoftmax and Negative Log Likelihood loss (<em>This is implemented below</em>).</p><pre>#Import the libraries<br>import torch<br>import torch.nn as nn<br>import torch.nn.functional as F<br><br># Define output logits and target labels<br>output_logits = torch.tensor([[1.2, 0.5, 2.1]])<br>target_labels = torch.tensor([2])<br><br># Applying CrossEntropyLoss<br>criterion = nn.CrossEntropyLoss()<br>loss_ce = criterion(output_logits, target_labels)<br><br># Applying Softmax and NLLLoss separately<br>softmax_output = F.softmax(output_logits, dim=1)<br>loss_nll = F.nll_loss(torch.log(softmax_output), target_labels)<br><br>print(&quot;Cross Entropy Loss: {}&quot;.format(loss_ce.item()))<br>print(&quot;LogSoftmax + Negative Log Likelihood Loss: {}&quot;.format(loss_nll.item()))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*AyEURaaVVtKynKDglBY9qA.png" /></figure><p>As we can see, both the losses are equal.</p><h4>Understanding Skipgram Model</h4><p>Now, let’s spend some time understanding the intuition behind the skip-gram model.</p><p>As we all know, the skip-gram model uses the center word in a sequence to predict the surrounding words also known as the context words. So, let’s see how we create the context and the target words.</p><p>It might not be difficult for you to recollect instances where you had to read a complete sentence to get to the meaning of a particular word in that sentence. The same is the intuition behind the skip-gram and CBOW models. Now, let’s understand how we create the training dataset for s<strong>kip-gram</strong></p><p>Let’s take a sentence, for example.</p><blockquote>“The quick brown fox jumps over a lazy dog”</blockquote><p>For the starting point, we define the Number of context words (will take this number as 2 for this example) before and after the center word that is to be predicted by the skipgram model. Then, we slide the window to the next center word till the end of the sequence. This happens as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*8HIBUXO9y6WQJ14S_hYVBQ.png" /><figcaption>Centre and the surrounding words</figcaption></figure><p>In the above figure, the word colored in blue is the center word which will be used to predict the surrounding or the context words highlighted in red. Here, we have a total of 5 sets of center and surrounding words.</p><p>Now, to create the training data, we place the center word against each context word for each set of center and surrounding words. Considering only the first two sentences, we get the below data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/902/1*oeQXzmwdEuidkp0z1xdgvg.png" /><figcaption>Training Data</figcaption></figure><p>The above data is ready and can be fed into the model for training.</p><h4>Skipgram Implementation in PyTorch</h4><p>As we move to the skip-gram implementation, the next part of the blog will focus more on various helper functions defined to train the model and some part of it will focus on visualizing the model and its training on tensorboard.</p><p>To begin with, let us define some helper functions which will be used in the processing of the text, tokenizing the text, and also creating the vocabulary and work indices for converting the sequence of text to a sequence of numbers. Let’s look at all the helper functions one by one.</p><p><em>Note: All the functions have been named such that they become more self-explanatory</em></p><pre>#model config<br>def model_config(model):<br>    &quot;&quot;&quot;<br>    This function contains the model configuration<br>    &quot;&quot;&quot;<br>    model_config = {&quot;model_name&quot;: model,<br>    &quot;train_batch_size&quot;: 2,<br>    &quot;val_batch_size&quot;: 2,<br>    &quot;shuffle&quot;: True,<br>    &quot;learning_rate&quot;: 5e-4,<br>    &quot;epochs&quot;: 500,<br>    &quot;train_steps&quot;:1, <br>    &quot;val_steps&quot;:1, <br>    &quot;checkpoint_frequency&quot;: 1,<br>    &quot;model_dir&quot;: &quot;weights/{}&quot;.format(model)}<br>    return model_config<br><br>def get_model(model_name):<br>    &quot;&quot;&quot;<br>    This function returns the class of the model<br>    &quot;&quot;&quot;<br>    if model_name == &quot;cbow&quot;:<br>        return CBOW_Model<br>    if model_name == &quot;skipgram&quot;:<br>        return SkipGram_Model<br>    else:<br>        raise ValueError(&quot;Choose from either cbow or skipgram&quot;)</pre><p>The above function simply defines the configs of the model. This includes the model name, batch size, learning rate, epochs, etc.</p><pre>#creating the vocab<br>def tokenizer(text, min_occurance):<br>    &quot;&quot;&quot;<br>    This function simply tokenized the sentence into tokens after lowering them.<br>    <br>    Min occurance: this is used to filter the low occuring words<br>    &quot;&quot;&quot;<br>    tokens = nltk.word_tokenize(text.lower())<br>    vocab = []<br>    for token in tokens:<br>        if tokens.count(token) &gt; min_occurance:<br>            vocab.append(token)<br>    return vocab<br><br>#creating the word_index<br>def word_indices(text, special_tokens_list=None):<br>    &quot;&quot;&quot;<br>    This functions returns the word indices and unique words in the text i.e. the vocab of the provided text. It converts the text into a dict with tokens as the keys<br>    and index numbers as the value.<br>    &quot;&quot;&quot;<br>    # tokens = list(set(tokenizer(text, 0))) + special_tokens_list<br>    if special_tokens_list is None:<br>        tokens = list(set(tokenizer(text, 0)))<br>        updated_vocab = {index: value for index, value in enumerate(sorted(tokens))}<br>        return updated_vocab, tokens<br>    else:<br>        tokens = list(set(tokenizer(text, 0))) + special_tokens_list<br>        special_vocab = {value: index for index, value in enumerate(sorted(tokens))}<br>        return special_vocab, tokens<br><br>#convert the words to numbers<br>def token_to_number(text, word_index):<br>    &quot;&quot;&quot;<br>    This function converts a given text to a sequence of numbers<br>    <br>    ex: This is my house<br>    Output: [23, 45, 3, 6] #these are the index of the respective numbers in the vocab<br>    &quot;&quot;&quot;<br>    text_token_ids = [word_index[token] if token in word_index.keys() else word_index[&quot;&lt;unk&gt;&quot;] for token in tokenizer(text, 0)]<br>    return text_token_ids</pre><p>The final function i.e. token_to_number converts a sequence of tokens/words into a sequence of numbers based on the index of a particular word in the word indices defined. Let’s run this function to see the output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JAckJPF7R3gDZkanZhOx9w.png" /><figcaption>A sequence of tokens to a sequence of numbers</figcaption></figure><p>Here, we define the vocabulary of the model based on the text that is given as input and also create word indices in which we assign an index to each word in the vocabulary.</p><p><em>Note: The function also takes a list of special tokens, if required. Like a &lt;unk&gt; token is assigned to a word that is not present in the vocabulary. These special tokens are also added to the vocabulary and the word indices along with the other words.</em></p><p>Let’s now create the input and output for the model.</p><pre>def skipgram(batch, word_index, MAX_SEQUENCE_LENGTH, skipgram_context_words):<br>    <br>    input_tensor, output_tensor = [], []<br>    <br>    for text in batch:<br>        text = text.strip()<br>        text_token_ids = token_to_number(text, word_index)<br><br>        if len(text_token_ids) &lt; skipgram_context_words*2+1:<br>            continue<br><br>        if len(text_token_ids) &gt; MAX_SEQUENCE_LENGTH:<br>            text_token_ids = text_token_ids[:MAX_SEQUENCE_LENGTH]<br><br>        for ids in range(len(text_token_ids)-skipgram_context_words*2):<br><br>            sequence = text_token_ids[ids: (ids + skipgram_context_words * 2 + 1)]<br><br>            context = sequence.pop(skipgram_context_words)<br>            target = sequence<br><br>            # return target, context<br>            for output in target:<br><br>                output_tensor.append(output)<br>                input_tensor.append(context)<br>        <br>    target_tensor = torch.tensor(output_tensor, dtype=torch.long)<br>    context_tensor = torch.tensor(input_tensor, dtype=torch.long)<br>        <br>    return context_tensor, target_tensor</pre><p>The above function takes the batch of sentences as input with other parameters and returns the input and the output tensors required by the skip-gram model.</p><p>Let’s visualize the output of the skip-gram function.</p><pre>#visualise the output of the skipgram function<br><br>input_tensor, output_tensor = skipgram([&quot;dewy and cool beneath my feet, and the sky above was a beautiful shade of pale blue&quot;,<br>         &quot;n the distance, I could see a small cluster of buildings, and as I got closer, I realized it was a quaint little village&quot;], word_index, MAX_SEQUENCE_LENGTH, skipgram_context_words)<br><br>print(&quot;Input: {}\nOutput: {}&quot;.format(input_tensor, output_tensor))</pre><p><em>Note: We have taken the number of surrounding words to be considered as 4.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*78_3U9FS0XBJ9JcWZYQZsA.png" /><figcaption>Input and output for the skip-gram model</figcaption></figure><p>Since we have taken the number of surrounding words as 4, the input tensor i.e. the center word for 8 different surrounding words in the output tensor.</p><p>Now, we will define a data loader that will load the data in batches which will be given as input to the above skip-gram function.</p><pre><br>#creating the dataloader<br>class word2vec_dataset(Dataset):<br>    def __init__(self, text):<br>        self.data = text<br>        self.word_index, self.vocab = word_indices(self.data, [&quot;&lt;unk&gt;&quot;])<br>        self.vocab_len = len(self.vocab)<br>        self.split = self.data.split(&quot;\n&quot;)<br><br>    def __getitem__(self, idx):<br>        data = self.split[idx]<br>        return data<br><br>    def __len__(self):<br>        return len(self.split)<br><br>def dataloader_vocab(text_doc, model, MAX_SEQUENCE_LENGTH, context_words, params, vocab=None):<br>    <br>    if model == &quot;cbow&quot;:<br>        <br>        my_dataset = word2vec_dataset(text_doc)<br>        data_params = params<br>        dataloader = DataLoader(my_dataset, collate_fn=lambda x: cbow(x, my_dataset.word_index, MAX_SEQUENCE_LENGTH, context_words), **data_params)<br>        if not vocab:<br>            return dataloader, my_dataset.vocab<br>        else:<br>            return dataloader, vocab<br>    <br>    elif model == &quot;skipgram&quot;:<br>        <br>        my_dataset = word2vec_dataset(text_doc)<br>        data_params = params<br>        dataloader = DataLoader(my_dataset, collate_fn=lambda x: skipgram(x, my_dataset.word_index, MAX_SEQUENCE_LENGTH, context_words), **data_params)<br>        if not vocab:<br>            return dataloader, my_dataset.vocab<br>        else:<br>            return dataloader, vocab<br>    else:<br>        raise ValueError(&quot;Choose a model between cbow and skipgram&quot;)</pre><p>The above function defines a word2vec_dataset class that inherits the Dataset class of PyTorch to create a dataset. This class contains a __getitem__ function which returns the data by index after splitting the text by new lines.</p><p>Next, we define the data loader function which takes the dataset created by the word2vec_dataset and loads the same data in batches on the basis of the batch size mentioned in the data params provided as input to the model. It then uses the collate function i.e. skip-gram function in our case to give the input and output tensors.</p><h4>Define the model</h4><p>Last, but not least, we create model classes and define the layers in each of the models. Here, we will work with the <em>SkipGram_Model </em>class.</p><pre>EMBED_MAX_NORM = 1<br>EMBED_DIMENSION = 300<br>class CBOW_Model(nn.Module):<br>    &quot;&quot;&quot;<br>    Implementation of CBOW model described in paper:<br>    https://arxiv.org/abs/1301.3781<br>    &quot;&quot;&quot;<br>    def __init__(self, vocab_size: int):<br>        super(CBOW_Model, self).__init__()<br>        self.embeddings = nn.Embedding(<br>            num_embeddings=vocab_size,<br>            embedding_dim=EMBED_DIMENSION<br>        )<br>        self.linear = nn.Linear(<br>            in_features=EMBED_DIMENSION,<br>            out_features=vocab_size,<br>        )<br>        # self.softmax = nn.Softmax(dim=1)<br><br>    def forward(self, inputs_):<br>        x = self.embeddings(inputs_)<br>        x = x.mean(axis=1)<br>        x = self.linear(x)<br>        # x = self.softmax(x)<br>        return x<br><br><br>class SkipGram_Model(nn.Module):<br>    &quot;&quot;&quot;<br>    Implementation of Skip-Gram model described in paper:<br>    https://arxiv.org/abs/1301.3781<br>    &quot;&quot;&quot;<br>    def __init__(self, vocab_size: int):<br>        super(SkipGram_Model, self).__init__()<br>        self.embeddings = nn.Embedding(<br>            num_embeddings=vocab_size,<br>            embedding_dim=EMBED_DIMENSION<br>        )<br>        self.linear = nn.Linear(<br>            in_features=EMBED_DIMENSION,<br>            out_features=vocab_size,<br>        )<br><br>    def forward(self, inputs_):<br>        x = self.embeddings(inputs_)<br>        x = self.linear(F.relu(x))<br>        return x</pre><p>Let’s see the different layers we have defined in our skip-gram architecture and their shapes. For that, we have to first define different parameters and load the model.</p><pre>#define the model config<br>config = model_config(&#39;skipgram&#39;)<br><br>#define the dataloader parameters<br>params = {<br>            &#39;batch_size&#39;: config[&#39;train_batch_size&#39;],<br>            &#39;shuffle&#39;: True,<br>            &#39;num_workers&#39;: 0<br>        }<br><br>#define the dataloader and the vocab<br>data_l, vocab = dataloader_vocab(text_doc, config[&#39;model_name&#39;], MAX_SEQUENCE_LENGTH, cbow_context_words, params, None)<br><br>#define the model and the model class<br>model_class = get_model(config[&#39;model_name&#39;])<br>model =  model_class(len(vocab))<br>model</pre><p>We get the below model output;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZeOwBD9zF0ysvICbUQwCCQ.png" /><figcaption>skip-gram model</figcaption></figure><p>Now, let’s understand the shape of the different layers of the model.</p><pre>#get the layers and their shapes<br><br>for name, params in model.named_parameters():<br>    print(name, &quot;:&quot;, params.shape)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*GVRLUw7x5HpG1M6ypDHrKw.png" /><figcaption>Skipgram model layers</figcaption></figure><p>As we can see, we have defined 2 layers, an embedding layer, and a linear layer.</p><p><em>Note: 321 is the size of the vocabulary and 300 is the EMBED_DIMENSION</em></p><p>Now, we will load a single batch into the model. We can load a batch by running one iteration on the data loader.</p><pre>#load a single batch<br>inputs, _ = next(iter(data_l))<br>print(&quot;Shape of the input: {}&quot;.format(inputs.shape))<br><br>#initiate a summary writer from the tensorboard library<br>tb_writer = SummaryWriter(&#39;runs/skipgram_implementation&#39;)<br><br>#add the model graph on the tensorboard<br>tb_writer.add_graph(model, inputs)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*r7kICHfpAcncRi7wFEpP6w.png" /><figcaption>Input size</figcaption></figure><p>Now, we can run the magic command in Python to get the model graph in the tensorboard.</p><pre>%load_ext tensorboard<br>%tensorboard --logdir runs/ --port 8000</pre><p>After running the above command, we get a beautiful visualization on tensorboard. (<em>A gif of the same is below</em>)</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FKv3DnuDL6nk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DKv3DnuDL6nk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FKv3DnuDL6nk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/39c37e7bce37381ad165b46afe0ddb16/href">https://medium.com/media/39c37e7bce37381ad165b46afe0ddb16/href</a></iframe><p>As you can see, we have an input layer that takes the input of the above shape i.e. [272] and gives the final output of size 272 X 321. This is because, for each word, given as input to the model, we have a probability distribution equal to the size of the vocabulary.</p><p>Now, without much ado, let’s create final training and validation functions to train the model.</p><pre>import os<br>import numpy as np<br>import json<br>import torch<br><br><br>class Trainer:<br>    &quot;&quot;&quot;Main class for model training&quot;&quot;&quot;<br>    <br>    def __init__(<br>        self,<br>        model,<br>        epochs,<br>        train_dataloader,<br>        train_steps,<br>        val_dataloader,<br>        val_steps,<br>        checkpoint_frequency,<br>        criterion,<br>        optimizer,<br>        device,<br>        model_dir,<br>        model_name,<br>    ):  <br>        self.model = model<br>        self.epochs = epochs<br>        self.train_dataloader = train_dataloader<br>        self.train_steps = train_steps<br>        self.val_dataloader = val_dataloader<br>        self.val_steps = val_steps<br>        self.criterion = criterion<br>        self.optimizer = optimizer<br>        self.checkpoint_frequency = checkpoint_frequency<br>        self.device = device<br>        self.model_dir = model_dir<br>        self.model_name = model_name<br><br>        self.loss = {&quot;train&quot;: [], &quot;val&quot;: []}<br>        self.model.to(self.device)<br><br>    def train(self):<br>        for epoch in range(self.epochs):<br>            self._train_epoch(epoch)<br>            self._validate_epoch(epoch)<br>            print(<br>                &quot;Epoch: {}/{}, Train Loss={:.5f}, Val Loss={:.5f}&quot;.format(<br>                    epoch + 1,<br>                    self.epochs,<br>                    self.loss[&quot;train&quot;][-1],<br>                    self.loss[&quot;val&quot;][-1],<br>                )<br>            )<br><br>            # self.lr_scheduler.step()<br><br>            if self.checkpoint_frequency:<br>                self._save_checkpoint(epoch)<br><br>    def _train_epoch(self, epoch):<br>        self.model.train()<br>        running_loss = []<br><br>        for i, batch_data in enumerate(self.train_dataloader, 1):<br>            inputs = batch_data[0].to(self.device)<br>            labels = batch_data[1].to(self.device)<br>            outputs = self.model(inputs)<br>            loss = self.criterion(outputs, labels)<br>            loss.backward()<br>            self.optimizer.step()<br>            self.optimizer.zero_grad()<br><br>            running_loss.append(loss.item())<br><br>            if i == self.train_steps:<br>                break<br><br>        epoch_loss = np.mean(running_loss)<br>        tb_writer.add_scalar(&#39;Training loss&#39;,<br>                        epoch_loss,<br>                        epoch)<br>        self.loss[&quot;train&quot;].append(epoch_loss)<br><br>    def _validate_epoch(self, epoch):<br>        self.model.eval()<br>        running_loss = []<br><br>        with torch.no_grad():<br>            for i, batch_data in enumerate(self.val_dataloader, 1):<br>                inputs = batch_data[0].to(self.device)<br>                labels = batch_data[1].to(self.device)<br><br>                outputs = self.model(inputs)<br>                loss = self.criterion(outputs, labels)<br><br>                running_loss.append(loss.item())<br>                if i == self.val_steps:<br>                    break<br><br>        epoch_loss = np.mean(running_loss)<br>        tb_writer.add_scalar(&#39;Validation loss&#39;,<br>                        epoch_loss,<br>                        epoch)<br>        self.loss[&quot;val&quot;].append(epoch_loss)<br><br>    def _save_checkpoint(self, epoch):<br>        &quot;&quot;&quot;Save model checkpoint to `self.model_dir` directory&quot;&quot;&quot;<br>        epoch_num = epoch + 1<br>        if epoch_num % self.checkpoint_frequency == 0:<br>            model_path = &quot;checkpoint_{}.pt&quot;.format(str(epoch_num).zfill(3))<br>            model_path = os.path.join(self.model_dir, model_path)<br>            torch.save(self.model, model_path)<br><br>    def save_model(self):<br>        &quot;&quot;&quot;Save final model to `self.model_dir` directory&quot;&quot;&quot;<br>        model_path = os.path.join(self.model_dir, &quot;model.pt&quot;)<br>        torch.save(self.model, model_path)<br><br>    def save_loss(self):<br>        &quot;&quot;&quot;Save train/val loss as json file to `self.model_dir` directory&quot;&quot;&quot;<br>        loss_path = os.path.join(self.model_dir, &quot;loss.json&quot;)<br>        with open(loss_path, &quot;w&quot;) as fp:<br>            json.dump(self.loss, fp)</pre><p><em>Note: We have also added the summary writer in the training function to log both the training and validation loss which will be visualized once the training is completed.</em></p><pre>import os<br>import torch<br>import torch.nn as nn<br><br>def train(config):<br>    <br>    os.makedirs(config[&quot;model_dir&quot;])<br><br>    train_params = {<br>            &#39;batch_size&#39;: config[&#39;train_batch_size&#39;],<br>            &#39;shuffle&#39;: True,<br>            &#39;num_workers&#39;: 0<br>        }<br><br>    val_params = {<br>            &#39;batch_size&#39;: config[&#39;val_batch_size&#39;],<br>            &#39;shuffle&#39;: True,<br>            &#39;num_workers&#39;: 0<br>        }<br>    <br>    #dataloaders<br>    train_dataloader, vocab = dataloader_vocab(text_doc, config[&#39;model_name&#39;], MAX_SEQUENCE_LENGTH, cbow_context_words, train_params, None)<br>    val_dataloader, vocab = dataloader_vocab(text_doc, config[&#39;model_name&#39;], MAX_SEQUENCE_LENGTH, cbow_context_words, val_params, vocab)<br> <br>    vocab_size = len(vocab)<br>    print(f&quot;Vocabulary size: {vocab_size}&quot;)<br><br>    model_class = get_model(config[&#39;model_name&#39;])<br>    model = model_class(vocab_size=vocab_size)<br>    criterion = nn.CrossEntropyLoss()<br><br>    optimizer = optim.Adam(model.parameters(), lr=config[&#39;learning_rate&#39;])<br><br>    device = torch.device(&quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;)<br><br>    trainer = Trainer(<br>        model=model,<br>        epochs=config[&#39;epochs&#39;],<br>        train_dataloader=train_dataloader,<br>        train_steps=config[&quot;train_steps&quot;],<br>        val_dataloader=val_dataloader,<br>        val_steps=config[&quot;val_steps&quot;],<br>        criterion=criterion,<br>        optimizer=optimizer,<br>        checkpoint_frequency=config[&quot;checkpoint_frequency&quot;],<br>        device=device,<br>        model_dir=config[&quot;model_dir&quot;],<br>        model_name=config[&quot;model_name&quot;],<br>    )<br><br>    trainer.train()<br>    print(&quot;Training finished.&quot;)<br>    print(&quot;Model artifacts saved to folder:&quot;, config[&quot;model_dir&quot;])<br>    <br>    return model, vocab_size</pre><p>Once we have defined the final training and validation functions, we can run the above function to train our skip-gram model.</p><pre>#setting the config<br>config_model = model_config(&quot;skipgram&quot;)<br><br>#saving the model<br><br>model, vocab_size = train(config)<br>tb_writer.close()</pre><p>Finally, let’s visualize our training and validation loss that we logged during training on the tensorboard summary writer (run the same tensorboard command).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JbrCz9v-MN1-lcr7WXfLIg.png" /><figcaption>Training Loss</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nAFEbQ6dDBLW9GRVh2sRgw.png" /><figcaption>Validation loss</figcaption></figure><p>As we can see, with every additional epoch, both the training loss and validation loss are decreasing which tells us that we have successfully trained our skipgram model from scratch using PyTorch in Python.</p><p>This brings us to the end of this blog. I have tried to be as detailed as possible to make this blog more understandable to you. Still, if you feel any need to visit the code and understand how the <strong>CBOW</strong> model is implemented, feel free to click on the below GIT repo link.</p><p><em>Repo: </em><a href="https://github.com/mayank1903/word2vec_implementation_from_scratch"><em>https://github.com/mayank1903/word2vec_implementation_from_scratch</em></a></p><p>Also, you can read more of my interesting articles/blogs. Some of the links are added below;</p><ol><li>Anomaly detection in Images — Using feature descriptors: <a href="https://medium.com/farmart-blog/anomaly-detection-in-images-using-feature-descriptors-c7124669bdfb">https://medium.com/farmart-blog/anomaly-detection-in-images-using-feature-descriptors-c7124669bdfb</a></li><li>Profiling — Key to Python code optimization: <a href="https://medium.com/farmart-blog/profiling-key-to-python-code-optimisation-ff635679d185">https://medium.com/farmart-blog/profiling-key-to-python-code-optimisation-ff635679d185</a></li></ol><p>I will be back with more such interesting blogs and to help the community at large, until then,</p><p>Happy Reading ;)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=875e948941a9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/farmart-blog/skipgram-implementation-from-scratch-pytorch-875e948941a9">Skipgram implementation from scratch — Pytorch</a> was originally published in <a href="https://medium.com/farmart-blog">The Good Food Economy</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>