<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mitansh Gor</title>
    <description>The latest articles on DEV Community by Mitansh Gor (@mitanshgor).</description>
    <link>https://dev.to/mitanshgor</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F999457%2F1e38e750-fe49-48f5-8f41-8b0a1ea57000.png</url>
      <title>DEV Community: Mitansh Gor</title>
      <link>https://dev.to/mitanshgor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mitanshgor"/>
    <language>en</language>
    <item>
      <title>Personal Health Agent (PHA): Multi-Agent Health System</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Thu, 27 Nov 2025 05:33:43 +0000</pubDate>
      <link>https://dev.to/mitanshgor/personal-health-agent-pha-multi-agent-health-system-1i6l</link>
      <guid>https://dev.to/mitanshgor/personal-health-agent-pha-multi-agent-health-system-1i6l</guid>
      <description>&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=cHYuEzxDJb4" rel="noopener noreferrer"&gt;VIDEO LINK&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the last few years, we saw different types of AI systems try to answer this question:&lt;br&gt;
PH-LLM (2024) → focused on personalized coaching from wearable data.&lt;br&gt;
PHIA (2024) → acted like an agent that analyzes your data, writes code, and explains results.&lt;br&gt;
IR Prediction + IR Explainer Agent (2025) → estimated your metabolic risk and explained it like a doctor.&lt;/p&gt;

&lt;p&gt;All of these systems were important steps. But each one had a weakness:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A single LLM tries to do everything.&lt;/li&gt;
&lt;li&gt;It must read data, interpret medical meaning, check safety, and give advice.&lt;/li&gt;
&lt;li&gt;This often leads to confident hallucinations — answers that sound correct but are actually wrong.&lt;/li&gt;
&lt;li&gt;It also mixes medical reasoning with casual coaching, which creates risk. And one model cannot be an expert in every health domain.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9314ab1b0c2ovlg8crme.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9314ab1b0c2ovlg8crme.png" alt="banner" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the big idea behind the Personal Health Agent (PHA) is simple:&lt;/p&gt;

&lt;p&gt;Instead of one giant model doing everything, why not create multiple smaller agents — each with a role — working together like a mini health team?&lt;br&gt;
This solves the main problem with earlier systems: no single AI can fully handle the complexity of real human health. But a team of specialized agents can.&lt;/p&gt;

&lt;p&gt;Because health is multi-dimensional—where your sleep affects your stress, stress affects metabolic health, exercise influences sleep, and food and biomarkers impact everything—one AI model can’t realistically handle all of it at once. Just like a hospital relies on different specialists, a digital health system also needs different “roles”: one agent to interpret raw data, one agent to check medical safety, and one agent to translate insights into simple coaching. This is why multi-agent systems are becoming so important: they add structure, accountability, and more reliable reasoning, solving many of the issues earlier single-LLM systems struggled with.&lt;/p&gt;

&lt;h2&gt;
  
  
  A System of Specialized Agents
&lt;/h2&gt;

&lt;p&gt;When I look at the PHA system, what stands out first is the Analyst Agent. I think of this agent like a friend who loves numbers and graphs. Its whole job is to read my raw data — sleep hours, steps, heart rate, and other signals — and turn them into clear observations. It doesn’t try to coach me or act like a doctor. It just says, “Here’s what I see happening in your data,” which makes everything feel clean and easy to understand.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy58om1b0a4jbjs6ocgvd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy58om1b0a4jbjs6ocgvd.png" alt="dsAgent" width="800" height="339"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then there is the Domain Expert Agent, which to me is the “doctor brain” of the whole system. This agent takes what the Analyst found and checks if it makes medical sense. It worries about safety, accuracy, and whether the explanation aligns with real health science. I really like this part because it stops the system from giving random or misleading advice. It acts like the strict friend who always says, “Wait, is this actually correct?”&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx4rkm5lh1in07tfqpz67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx4rkm5lh1in07tfqpz67.png" alt="domainExp" width="800" height="310"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next comes the Coach Agent, the most human-feeling part of PHA. This agent talks to me like a supportive friend who wants me to succeed. It takes all the technical pieces and translates them into simple, everyday advice I can act on. No medical jargon. No complicated charts. Just small, doable suggestions like, “Try walking after dinner” or “Aim for an earlier bedtime tonight.” It’s friendly and practical, which is exactly what people need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg315ncyax16diti30hvt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg315ncyax16diti30hvt.png" alt="healthcoach" width="800" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What ties everything together is how these agents work as a team. Instead of one giant AI trying to do everything — and often making confident mistakes — each agent stays in its own lane. One analyzes, one verifies, one coaches. Because of this teamwork, the final answer feels clearer, safer, and much more trustworthy. It’s almost like having a mini health team inside your phone, and that makes the whole system feel both smart and human at the same time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Technical Deep Dive
&lt;/h2&gt;

&lt;p&gt;Even though all agents may start from the same base LLM (like Gemini or GPT-class models), they are trained separately, so each one becomes good at one thing instead of being average at everything.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finetuning
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Analyst Agent is finetuned from wearable logs (heart rate, steps, sleep), time-series patterns, data summaries, and statistical examples. It becomes good at reading numbers and spotting patterns.&lt;/li&gt;
&lt;li&gt;Domain Expert needs more serious data for finetuning, like medical guidelines, curated clinical examples, safe/unsafe reasoning samples, and explanations validated by experts. It learns how to avoid hallucinations and unsafe claims&lt;/li&gt;
&lt;li&gt;Coach Agent is the “human” one. It is finetuned on a friendly conversational dataset, lifestyle advice examples, behavior-change coaching patterns. It learns to talk in a simple, supportive way.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxutm9nruja3q39zgfr6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxutm9nruja3q39zgfr6h.png" alt="data set" width="800" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This division is what makes the final system feel more stable than earlier systems like PHIA or the IR Agent, where a single LLM had to juggle all roles and often made confident mistakes.&lt;/p&gt;

&lt;p&gt;The process itself was simple but very intentional. Here’s how I imagine it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We first collected examples for each agent’s role.&lt;/li&gt;
&lt;li&gt;Then we cleaned and organized everything.&lt;/li&gt;
&lt;li&gt;Then we cleaned and organized everything.&lt;/li&gt;
&lt;li&gt;After that, we fine-tuned each agent separately.&lt;/li&gt;
&lt;li&gt;Then we tested the agents together.&lt;/li&gt;
&lt;li&gt;Finally, we fixed mistakes and improved the dataset.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  System architecture and Data Flow
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbpizblec7crocjocrwah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbpizblec7crocjocrwah.png" alt="system arch" width="800" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Underneath all of this, there’s a system architecture that makes their collaboration possible. I picture it like a small command center. The user’s data flows into a main controller, which decides which agent speaks first and who validates whom. The Analyst generates its interpretation, the Domain Expert checks it, and the Coach turns everything into a simple message. Each agent is isolated enough to stay focused, but interconnected enough to build on each other’s work. There’s also a safety layer running quietly in the background — something like a “medical guardrail” — to make sure the final output stays safe, consistent, and responsible before it reaches the user.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data Pipeline Flow
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;My raw data comes in (wearable data, blood test results, communication history)&lt;/li&gt;
&lt;li&gt;The system cleans and prepares the data.&lt;/li&gt;
&lt;li&gt;The Analyst Agent gets the cleaned data first, and it gives a report.&lt;/li&gt;
&lt;li&gt;The Expert Agent double-checks everything and makes sure the explanation is medically correct and safe.&lt;/li&gt;
&lt;li&gt;The Coach Agent gets the approved explanation and rewrites the message in simple, friendly language&lt;/li&gt;
&lt;li&gt;A final safety filter reviews the message and checks for risky things like any medical diagnosis, unsafe suggestions, and harmful claims.&lt;/li&gt;
&lt;li&gt;I receive the final output.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspd5g9lkz788hjcvok15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fspd5g9lkz788hjcvok15.png" alt="pipeline inference" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Inference time
&lt;/h3&gt;

&lt;p&gt;During inference — which is just a fancy word for “when the system actually responds to me” — the agents talk to each other almost like coworkers passing notes in a group chat. I don’t see any of this, but it’s happening behind the scenes. The Analyst looks at my raw data and says, “Here’s what I think is happening.” The Domain Expert looks at what the Analyst wrote and either approves it or corrects it. And finally, the Coach turns their combined reasoning into something that sounds human and helpful. What I receive as a single answer is actually the teamwork of multiple roles stitched together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tools used
&lt;/h3&gt;

&lt;p&gt;To make all of this work, the system relies on a set of tools that help each agent extend its abilities. The Analyst might use data-processing tools to clean and summarize wearable data. The Domain Expert might have access to reference tools that help it compare findings against medical knowledge or validated guidelines. The Coach might use prompt templates designed for empathy, clarity, and motivation. And the main controller uses orchestration tools — something like LangGraph or a custom workflow engine — to decide who talks when and how information flows between them. These tools don’t replace the agents, but they give them structure, context, and external capabilities they wouldn’t have on their own.&lt;/p&gt;

&lt;h3&gt;
  
  
  Collection of data
&lt;/h3&gt;

&lt;p&gt;Everything starts with a real study they ran called &lt;strong&gt;WEAR-ME&lt;/strong&gt;, which included more than a thousand Fitbit users. People had to explicitly opt-in, sign digital consent, link their wearable device, and even go for a blood draw at a Quest Diagnostics center. So the data wasn’t scraped or assumed—it came from real people who agreed to share it for research. That part mattered to me because it made the whole system feel responsible, not rushed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fozrz0zw4prfut4yef2yk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fozrz0zw4prfut4yef2yk.png" alt="data1" width="800" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once someone joined the study, their data flowed through a very controlled pipeline.So each person had a mix of: (1) wearable time-series, (2) lab biomarkers, (3) self-reported history, (4) lifestyle questionnaires.&lt;/p&gt;

&lt;p&gt;What I liked is that the system doesn’t directly reach into a live Fitbit account or do anything “real-time.” Instead, the PHA gets access to summaries of the participant’s data, organized into tables—like a daily summary table, an activities table, and even a population-level reference table so the system could compare one user’s values against others in their age group. This gave context, not just raw numbers.&lt;/p&gt;

&lt;p&gt;Inside the actual agent pipeline, each sub-agent only receives the part of the data that’s relevant to its job. The system never dumps the full raw dataset into every agent. Instead, the orchestrator acts like a coordinator who hands the right information to the right specialist. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34ozz316vec63s0dh1mi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F34ozz316vec63s0dh1mi.png" alt="data2" width="800" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nothing is fetched unless the user asks something that genuinely needs it. And the system also avoids asking the user for things it already knows—like their sleep hours or steps—because the orchestrator checks the available data first.&lt;/p&gt;

&lt;h2&gt;
  
  
  PHA vs Gemini Results
&lt;/h2&gt;

&lt;p&gt;PHA wasn’t just a small improvement. It was in a completely different league.&lt;/p&gt;

&lt;p&gt;From the end-user side, I noticed that people consistently picked PHA as the best experience. Not just once or twice — almost half the time. And this is after comparing it side-by-side against a single Gemini agent and a parallel multi-agent system.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftsqsilw9r4hl35m3n5xs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftsqsilw9r4hl35m3n5xs.png" alt="enduser" width="800" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But the biggest eye-opener for me was the expert evaluation. Experts didn’t just prefer PHA — they almost abandoned the Gemini single-agent baseline. Only around 4–5% of expert rankings put Gemini in first place, while PHA was chosen as #1 about 80% of the time. That’s not a small margin.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5yxu0ccew7z9qmoo2mba.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5yxu0ccew7z9qmoo2mba.png" alt="expUser" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Strengths of the system
&lt;/h2&gt;

&lt;p&gt;What struck me most was how real the system felt. Not just in terms of architecture, but in how thoroughly it was tested. It wasn’t a small demo — it involved thousands of human annotations and hundreds of hours of expert evaluation. Because of that, the strengths and limitations felt very honest.&lt;/p&gt;

&lt;p&gt;One of the biggest strengths I noticed is how well the whole system comes together. The three-agent setup — the data scientist, the domain expert, and the coach — isn’t just theoretical. In the evaluations, PHA actually outperformed both a single-agent system and a parallel multi-agent system. End-users preferred talking to PHA almost half the time, and experts loved it even more, choosing it as the best system in about 80% of cases. To me, that says the collaboration between these agents isn’t just helpful — it genuinely changes the quality of the conversation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcbzp9nzsn8e7xq9ytewz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcbzp9nzsn8e7xq9ytewz.png" alt="daAdv" width="800" height="286"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The data agent in particular felt like a major upgrade. It was much better at breaking down messy, vague health questions into proper statistical analyses, catching missing data, choosing the right timeframes, and generating correct code. I could see why the system became more trustworthy — it wasn’t guessing. It was reasoning over the user’s actual data. And the domain expert agent made a big difference too. It produced safer, more accurate, and more personalized medical explanations. Users rated its responses as far more trustworthy than the base model, and clinicians preferred its summaries because they were more complete and clinically meaningful.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnr1mvix1o1lhgkrxff2f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnr1mvix1o1lhgkrxff2f.png" alt="domainAdv" width="800" height="624"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The health coach agent also impressed me. It wasn’t just delivering motivational lines. It followed real coaching principles — active listening, SMART goals, motivational interviewing — and this made conversations feel more natural and supportive. Users were more engaged, and the conversations ended more naturally. It reminded me of talking to a real human coach who’s actually paying attention.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpetug6re3d588sbi666a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpetug6re3d588sbi666a.png" alt="coachAdv" width="800" height="722"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As a whole system, PHA feels more thoughtful. The orchestrator doesn’t just mix answers together — it understands the user’s goal, assigns the right agent, reflects on the output, and remembers important details for later turns. This gives the conversation a sense of direction and personalization that the baselines just didn’t have.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations of the System
&lt;/h2&gt;

&lt;p&gt;But the system isn’t perfect. One thing that stood out immediately was the cost. PHA is slower and more computationally expensive because each request triggers multiple agents and reflection steps. Where the single-agent system might respond in around 35 seconds, PHA could take over 200. It’s powerful, but not cheap.&lt;/p&gt;

&lt;p&gt;Another limitation is that the diagnostic reasoning, while improved, is still not bulletproof. Sometimes the domain expert agent didn’t complete its reasoning chain, and its reliance on web search occasionally pulled in conflicting information. This reminded me that, even though it acts like a doctor in some ways, it’s not actually one — and the authors make that very clear.&lt;/p&gt;

&lt;p&gt;Bias is also a real concern. The system personalizes advice based on user traits, conditions, and context… which is great, but it also means it can unintentionally repeat patterns or assumptions from datasets that might not represent everyone equally. The paper calls out this risk directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjq9spxcb92u8fqw3qlnw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjq9spxcb92u8fqw3qlnw.png" alt="Limitations" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The coach agent, despite being strong overall, also had a weakness: it wasn’t great at tracking user progress over time. It did a good job helping set goals, but didn’t always follow up on them in later turns. For long-term coaching, that’s something they’ll need to improve.&lt;/p&gt;

&lt;p&gt;And finally — maybe the biggest limitation — everything was tested in short-term interactions. We don’t yet know whether PHA can support behavior change over weeks or months, which is where health coaching really matters. The team also notes that this is a research framework, not a clinical tool, and major regulatory work would be needed before deploying anything like this in the real world.&lt;/p&gt;

&lt;p&gt;So when I put it all together, here’s how I personally see it:&lt;br&gt;
PHA is a huge improvement over past systems — smarter, safer, more human, and more useful — but it’s still early. It has real strengths, but also real gaps. It’s powerful as an idea, promising as a system, and clearly built with care. But it also reminds me that great AI doesn’t replace medical care — it supports it, and it still has a lot of growing up to do.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;After reading the PHA paper, the biggest thing I realized is that this system isn’t just an AI answering health questions — it feels more like a small team working behind the scenes to help you make sense of your own data. The evaluations show that this multi-agent design really does lead to better, clearer, and more personalized guidance compared to older single-agent approaches.&lt;/p&gt;

&lt;p&gt;At the same time, the authors are very honest about their limits. The system can still make mistakes, it can be biased depending on the data, and it should never replace real medical care. It’s powerful, but it’s also early — more like a research blueprint than a ready product.&lt;/p&gt;

&lt;p&gt;To me, PHA represents a hopeful direction: AI that supports people in understanding their health, not by taking over, but by helping them make smarter choices with empathy, safety, and transparency in mind&lt;/p&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>health</category>
      <category>google</category>
    </item>
    <item>
      <title>PHIA: The Agentic LLM That Writes Code, Analyzes Your Data &amp; Explains Health Insights</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Thu, 27 Nov 2025 05:33:34 +0000</pubDate>
      <link>https://dev.to/mitanshgor/phia-the-agentic-llm-that-writes-code-analyzes-your-data-explains-health-insights-4g1a</link>
      <guid>https://dev.to/mitanshgor/phia-the-agentic-llm-that-writes-code-analyzes-your-data-explains-health-insights-4g1a</guid>
      <description>&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=UR2MTLJ1d4E" rel="noopener noreferrer"&gt;VIDEO LINK&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I honestly felt like I was looking at the “next version” of what health AI should be. Whenever I asked myself questions like, “Is my sleep improving?” or “Does exercising at night change my deep sleep?”, I realized how hard it is to answer these without real analysis.&lt;/p&gt;

&lt;p&gt;That is exactly the problem the PHIA team talks about on page 1 of the paper. They explain that even a simple question like “Do I sleep better after exercising?” requires many steps: checking recent data, comparing different days, calculating metrics, and then interpreting everything in the context of what “healthy” even means. And honestly, I could relate — that’s the kind of analysis I never do on my own.&lt;br&gt;
What really caught my attention is that the paper says today’s LLMs struggle with numerical reasoning, meaning they often miscalculate or oversimplify things. I’ve definitely seen models do that — giving a confident answer but completely messing up basic math. (Limitation of PH-LLM)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijr3r0wiwgfamfnpd0dv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijr3r0wiwgfamfnpd0dv.png" alt="int" width="800" height="452"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So PHIA tries to fix this problem by giving an LLM the ability to plan, use tools, write code, and search the web. It doesn’t just “chat.” It becomes more like a small data analyst that works with your wearable data step-by-step.&lt;/p&gt;

&lt;h2&gt;
  
  
  PH-LLM vs PHIA
&lt;/h2&gt;

&lt;p&gt;PH-LLM was an earlier coaching system that worked only with simple, pre-aggregated 30-day summaries and relied purely on the LLM’s internal reasoning. That meant it couldn’t actually analyze detailed daily wearable data, couldn’t run calculations, and completely failed on questions that required numbers or step-by-step reasoning. PHIA addresses these limitations by granting the LLM three new capabilities: (1) it can generate authentic Python code to analyze raw wearable data, (2) it can plan and break down tasks using an agent loop, and it can search the web to incorporate fresh, verified health knowledge.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj8xekitn7246fd5p1e9k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj8xekitn7246fd5p1e9k.png" alt="difference" width="800" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Because of this, PHIA shifts from being “just a health coach” to becoming a true data analyst + coach, delivering insights that are more accurate, personalized, and grounded in both the user’s data and real health science. PHIA feels like the bridge between “AI as a friendly coach” and “AI as a personal data scientist.” By mixing reasoning, code execution, and web search, it finally unlocks the kind of deeper, more accurate insights that wearables have always had the potential to provide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqkihmir6plcn25xlyp4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnqkihmir6plcn25xlyp4.png" alt="diff" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How PHIA Works
&lt;/h2&gt;

&lt;p&gt;PHIA more like a mini health data analyst that can think step-by-step, write code, check your wearable data, search the web, and then explain everything in normal language. The PHIA paper shows this clearly — PHIA literally cycles through think → act → observe just like a human analyst would.&lt;/p&gt;

&lt;p&gt;I’ll explain this in the simple way I understood it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft9fqs7m9hl4jtzdwrc61.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft9fqs7m9hl4jtzdwrc61.png" alt="ReAct" width="800" height="571"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead of guessing answers, PHIA acts more like a small data analyst that plans its steps, checks your wearable data, and only then explains what it found. The paper describes this using the ReAct loop— &lt;strong&gt;Thought → Action → Observation&lt;/strong&gt; — and seeing it in action helped me understand why PHIA is so different.&lt;/p&gt;

&lt;p&gt;PHIA starts by thinking through the question. If I ask something like “Is my resting heart rate improving?”, it doesn’t jump to a quick reply. It pauses and decides what it needs—maybe comparing two weeks of data or calculating averages. Then, PHIA takes action by writing and running Python code in a safe &lt;strong&gt;virtual environment tool&lt;/strong&gt;. This lets it analyze real daily time-series data using Pandas, just like a real data scientist. The nice part is that this removes mathematical mistakes that normal LLMs often make. And if its code crashes, PHIA actually fixes the error and tries again (&lt;u&gt;recurrent trials system&lt;/u&gt;), which the paper highlights as one of its strengths.&lt;/p&gt;

&lt;p&gt;In their evaluation, PHIA scored &lt;strong&gt;84% accuracy&lt;/strong&gt; on objective questions, way higher than simple LLM reasoning. This happens because PHIA can recover when the code fails.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5p6ma2p9tzg1o75gjizq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5p6ma2p9tzg1o75gjizq.png" alt="ReAct Arch" width="800" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a question requires more than numbers—like understanding recommended sleep hours—PHIA uses a built-in web &lt;strong&gt;search tool&lt;/strong&gt; to fetch verified information from trusted sources.  Sometimes the user needs context, like: “Is this amount of sleep normal for my age?” “What workouts improve resting heart rate?” “Is my stress score healthy?”&lt;br&gt;
PHIA has a built-in search tool that pulls information from verified online sources. This mixing of data + domain knowledge is what makes PHIA feel smart and practical.&lt;/p&gt;

&lt;p&gt;What impressed me most is how PHIA blends everything together. It doesn’t just give numbers or copy facts. It calculates, it checks, it researches, and then it explains the result in clear language. The examples in the paper, like &lt;u&gt;comparing a user’s sleep with national guidelines&lt;/u&gt;, really show how these steps come together. In the end, PHIA feels less like a chatbot and more like someone doing careful, step-by-step reasoning to help you understand your own health patterns.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Deep Dive Into PHIA’s Technical Architecture
&lt;/h2&gt;

&lt;p&gt;PHIA is not a fine-tuned model. It’s an agent framework built around &lt;strong&gt;&lt;u&gt;Gemini 1.0 Ultra&lt;/u&gt;&lt;/strong&gt;, with two major tools: (1) Python data analysis runtime, (2) Web search tool.&lt;/p&gt;

&lt;p&gt;These tools are orchestrated using the ReAct (Reason + Act) agent pattern, which is why PHIA can do multi-step reasoning without fine-tuning. This design gives PHIA abilities that raw LLMs don’t have: planning, correction, stepwise attention, and grounded analysis.&lt;/p&gt;

&lt;h3&gt;
  
  
  No Fintuning
&lt;/h3&gt;

&lt;p&gt;One of the most interesting things I learned from the paper is that PHIA is not fine-tuned like PH-LLM. Instead of modifying the model’s weights, the authors built a smart scaffolding around the model and taught it how to act like an agent.&lt;/p&gt;

&lt;p&gt;Fine-tuning a huge LLM like Gemini Ultra is expensive, slow, requires tons of supervised data, and risky (can break general reasoning ability).&lt;/p&gt;

&lt;p&gt;So instead, PHIA keeps the base model untouched and teaches the model how it should “think” and use tools using few-shot ReAct examples.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This is a totally different philosophy: PH-LLM → change the model. PHIA → change the system around the model&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Process
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The authors started with thousands of wearable-related questions (objective + open-ended). They needed a tiny set of example tasks that could teach the model how to behave like an agent.&lt;/li&gt;
&lt;li&gt;Instead of randomly picking examples, they converted every question into numerical embeddings using Sentence-T5. This turns each question into a vector representing its meaning, so similar questions sit close together in vector space.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flpyn3yfndl2r7mcb4vox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flpyn3yfndl2r7mcb4vox.png" alt="Process" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They ran k-means clustering (k=20) on these embeddings to automatically group similar questions into 20 clusters. Each cluster represents a whole “type” of question, like sleep trends, workout comparisons, anomaly detection, correlations, and so on.&lt;/li&gt;
&lt;li&gt;From each cluster, they selected the most central question — the one closest to the cluster centroid.This gives 20 representative queries, each standing in for a larger family of similar queries.&lt;/li&gt;
&lt;li&gt;For each of these 20 representative queries, the team manually wrote a complete ReAct agent trajectory that included every step of reasoning:

&lt;ul&gt;
&lt;li&gt;Thought: what the model should plan to do&lt;/li&gt;
&lt;li&gt;Action (Python): the exact Pandas code needed&lt;/li&gt;
&lt;li&gt;Observation: the real output that the code would return&lt;/li&gt;
&lt;li&gt;Thought again: interpreting the output&lt;/li&gt;
&lt;li&gt;Action (Search): when domain knowledge is needed&lt;/li&gt;
&lt;li&gt;Observation: the retrieved information&lt;/li&gt;
&lt;li&gt;Final Answer: a clear, natural-language explanation. These aren’t partial examples — they are full step-by-step walkthroughs that demonstrate how an agent should behave.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;These 20 full trajectories were inserted into PHIA’s few-shot prompt, acting as demonstrations inside its system instructions. So the model is not fine-tuned — instead, it is taught by example how to: plan before acting, decide when to run code, decide when to search the web, fix code errors, combine numerical results with domain knowledge, and produce a final personalized insight.&lt;/li&gt;

&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;This is called “behavior cloning via prompting.” : It will learn the “pattern of behavior” from the examples, even without changing its internal parameters.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;PHIA is a perfect example of how powerful LLMs become when they are paired with the right scaffolding instead of relying only on internal reasoning. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpezd2e5c7ozuk7p1ljgh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpezd2e5c7ozuk7p1ljgh.png" alt="graph" width="800" height="696"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations in the PHIA
&lt;/h2&gt;

&lt;p&gt;PHIA is a huge step forward, especially compared to simple chat-based coaching. But it’s still early-stage. It shows what’s possible when LLMs use tools, but it also exposes how much work is left to make an AI truly trustworthy in personal health.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;PHIA hasn’t been tested on real people yet, so we still don’t know if it truly helps users change habits or understand their health better in everyday life.&lt;/li&gt;
&lt;li&gt;It only uses wearable data, and I feel it needs more context—like nutrition, medical records, or mood logs—to give deeper and more holistic insights.&lt;/li&gt;
&lt;li&gt;PHIA isn’t medically validated, so even though it sounds smart, its recommendations haven’t been checked by doctors or health experts for real accuracy. Personalization still feels limited, because sometimes PHIA gives generic advice even when the user’s data is available right there.&lt;/li&gt;
&lt;li&gt;Its reasoning depends heavily on prompting and Gemini Ultra, which means its behavior might change across models or updates, making it less consistent.&lt;/li&gt;
&lt;li&gt;Error handling is better than basic LLMs but still imperfect, since PHIA can misread data columns or fail on messy real-world data.&lt;/li&gt;
&lt;li&gt;Its toolset is still narrow, and I’d love to see it generate visualizations, analyze other health signals, or track long-term goals instead of just answering one question at a time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Learnings from the paper
&lt;/h2&gt;

&lt;p&gt;Reading the PHIA paper honestly changed the way I think about building AI systems. Before this, I used to focus mainly on making the model smarter, but PHIA showed me that the real progress comes from giving the model the right tools and the right structure. Seeing how PHIA uses the ReAct loop—think, act, observe—made me realize how important disciplined reasoning is for avoiding hallucinations and building trust.&lt;/p&gt;

&lt;p&gt;The use of synthetic data was another eye-opener, because it proved that we can train and evaluate personal-health agents responsibly without touching real user data. On top of that, the way the authors handled safety—650 hours of human review, strict guardrails, and cautious refusal behavior—reminded me that responsible AI isn’t optional; it’s part of the engineering.&lt;/p&gt;

&lt;p&gt;More than anything, PHIA taught me that the future of AI isn’t “a bigger model,” but a system where models, tools, data, and reasoning frameworks all work together. As an AI engineer, this shifted how I think about agents: not as chatbots, but as full pipelines designed to solve real problems with accuracy, humility, and safety.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Insights I’m Taking With Me
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;PHIA wins because it’s structured, not because the LLM is smarter.&lt;/li&gt;
&lt;li&gt;Reasoning and architecture beat raw model size.&lt;/li&gt;
&lt;li&gt;PHIA shines when a question needs data + external knowledge + multi-step logic, not just basic statistics.&lt;/li&gt;
&lt;li&gt;Its real strength is disciplined reasoning, not raw computation.&lt;/li&gt;
&lt;li&gt;PHIA’s strict safety guardrails make it trustworthy in a way normal LLMs aren’t.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ai</category>
      <category>llm</category>
      <category>google</category>
      <category>health</category>
    </item>
    <item>
      <title>PH-LLM - A LLM that gives personalized sleep and fitness coaching using wearable data.</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Thu, 27 Nov 2025 05:32:57 +0000</pubDate>
      <link>https://dev.to/mitanshgor/ph-llm-a-llm-that-gives-personalized-sleep-and-fitness-coaching-using-wearable-data-2m7f</link>
      <guid>https://dev.to/mitanshgor/ph-llm-a-llm-that-gives-personalized-sleep-and-fitness-coaching-using-wearable-data-2m7f</guid>
      <description>&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=G-n7BMqxJqk" rel="noopener noreferrer"&gt;VIDEO LINK&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PH-LLM (Personal Health Large Language Model) is a version of Gemini Ultra that has been fine-tuned specifically for sleep and fitness coaching. It isn’t just chatting—it actually learns from up to 30 days of wearable data, understands your patterns, and gives expert-level insights. The research paper shows PH-LLM scoring 79% on sleep medicine exams and 88% on fitness exams, which is on par or better than the expert groups they tested with.&lt;/p&gt;

&lt;p&gt;What really caught my interest is that the system can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;analyze daily-resolution sensor data&lt;/li&gt;
&lt;li&gt;generate personalized insights&lt;/li&gt;
&lt;li&gt;Predict how rested or tired you will feel based only on wearable data and speak to you like a sleep coach or fitness expert.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5efo1s358xmkr5lfp16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj5efo1s358xmkr5lfp16.png" alt="system can solve 3 tasks" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What Problem PH-LLM Tries to Solve
&lt;/h2&gt;

&lt;p&gt;Wearables track you, but they don’t talk to you. They tell you what happened, but not why it happened or how to fix it. PH-LLM aims to act like a personal health coach — one that understands your data, your patterns, your goals, and the science behind all of it.&lt;/p&gt;

&lt;p&gt;Instead of treating sensor data like random numbers on a dashboard, it tries to interpret them the way a human expert would. If your bedtime has been drifting later over the past two weeks, or your deep sleep dropped right after you increased workout intensity, PH-LLM doesn’t just point it out — it explains why it happened and how it connects to your goals. It transforms passive metrics into an actual conversation about your habits.&lt;/p&gt;

&lt;p&gt;The system was trained specifically for this kind of reasoning. The researchers fine-tuned Gemini Ultra so it could combine textbook knowledge with real-world wearable data. Interestingly, PH-LLM doesn’t just “sound smart.” In testing, it actually outperformed sleep experts on sleep medicine exam questions and matched them in fitness knowledge. That means the model isn’t just giving generic advice — it has a near-expert understanding of the underlying science.&lt;/p&gt;

&lt;p&gt;But what really makes PH-LLM different is that it doesn’t stop at general knowledge. It looks at a person’s actual patterns over weeks. If you always sleep well on weekdays but crash on weekends, it's noticeable. If your HRV is dropping while your workouts are getting harder, it connects the dots. In one of the paper’s examples, the model realized a user had a very regular sleep schedule but consistently slept too little, and it suggested shifting bedtime by small increments rather than giving a one-size-fits-all rule. That’s the kind of nuance real coaches provide, but most apps don’t.&lt;/p&gt;

&lt;h2&gt;
  
  
  High-level Working of the PH-LLM
&lt;/h2&gt;

&lt;p&gt;Researchers took Gemini Ultra — a very capable general-purpose model — and taught it how to understand sleep and fitness the same way a human expert would. They basically turned a large language model into a personal health specialist.&lt;/p&gt;

&lt;p&gt;The process happened in two major steps. First, they fine-tuned the entire Gemini Ultra model on hundreds of detailed sleep and fitness case studies. These weren’t made-up examples — each case study was based on real wearable data from real people. The case studies included up to thirty days of information such as bedtimes, wake times, restlessness, workout intensity, heart rate metrics, and more. Alongside the data, sleep physicians and athletic trainers wrote expert-level insights, possible causes, and recommendations. These human-written explanations became the “teacher examples” that the model learned from. By imitating these expert responses over and over, PH-LLM learned how to talk like a coach and reason like one too.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy98wa021cc4x12jo4c5a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy98wa021cc4x12jo4c5a.png" alt="case study" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After this, the researchers added a second layer of training using something called a multimodal adapter. This part fascinated me because it let the model go beyond simple text input. Instead of only reading written summaries of the user’s data, PH-LLM also receives a compressed representation of the actual sensor values — the statistical patterns hidden in daily heart rate, HRV, sleep duration, respiratory rate, and other signals across fifteen days. These adapter-generated “soft tokens” get injected directly into the LLM’s internal understanding. In simple terms, the model doesn’t just read your data — it absorbs it. This is how PH-LLM can predict things like “Will this person feel tired tomorrow?” with accuracy on par with traditional machine learning approaches.&lt;/p&gt;

&lt;p&gt;The result of these two training phases is a model that can look at your daily metrics and instantly form a holistic picture. If your bedtime keeps drifting later, PH-LLM notices. If your deep sleep drops right after your workout intensity spikes, it picks up the relationship. If your HRV has been declining all week, it connects that to stress, recovery, or the need for rest. What impressed me is that PH-LLM isn’t just matching patterns — it can articulate the reasoning behind them, almost like an expert thinking out loud.&lt;/p&gt;

&lt;p&gt;The paper shows a great example of this. For one user, the model pointed out that the midsleep point was extremely consistent — meaning their circadian rhythm was stable — but their total sleep time was consistently too low. From there, PH-LLM suggested a gradual shift in bedtime over several days. This wasn’t a generic “try to sleep more” tip; it was a specific plan tailored to what the data actually showed. That kind of reasoning is the whole point of the system.&lt;/p&gt;

&lt;p&gt;Another thing I appreciated is that PH-LLM adapts its responses based on how much data it has. When the researchers removed parts of the input — like today’s sleep metrics or the last week of workout logs — PH-LLM still adjusted its explanations in a sensible way. To me, this shows that the model doesn’t rely on memorized patterns but actually understands the structure of sleep and fitness behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  Technical Working
&lt;/h2&gt;

&lt;p&gt;The foundation of PH-LLM is Gemini Ultra 1.0, Google’s flagship multimodal model. In this work, it functions mainly as a text LLM (the vision-side is not used). Structurally, it’s a Transformer decoder with extremely large context and token embedding dimensions. This gives it the capacity needed to reason across long-form case studies, sleep explanations, and multi-step fitness logic.&lt;/p&gt;

&lt;p&gt;The base model (Gemini Ultra 1.0) already has strong performance in medical question answering and general health reasoning. But by itself, it doesn’t know how to interpret raw wearable data — it needs domain-specific training.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finetuning
&lt;/h3&gt;

&lt;p&gt;The first major training step is a full-parameter finetuning of Gemini Ultra on 857 expert-annotated sleep and fitness case studies. Each case study includes: (1) Up to 30 days of daily metrics, (2) Aggregated (mean, variance, percentiles) statistics, (3) Expert-written insights, etiologies, and recommendations. This dataset is unique because each example combines real sensor patterns with expert-level reasoning. When the researchers fine-tuned the model, they essentially taught it:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“When you see patterns like this in the data, here’s how a real sleep doctor or athletic trainer explains it.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2r28f6w5d75qwklf8o5e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2r28f6w5d75qwklf8o5e.png" alt="Fine Tuned" width="800" height="237"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To teach Gemini Ultra how to behave like a real sleep and fitness coach, the researchers fine-tuned the entire model—not just a small part of it—using a large collection of expert-written examples. Each example paired a user’s real wearable data with the exact explanation a sleep doctor or athletic trainer would give. There were about thirteen hundred of these pairs for sleep and fifteen hundred for fitness, and the model was trained on them over roughly fifteen hundred optimization steps. Instead of using shortcuts like LoRA, they updated all of the model’s weights directly, following a smooth cosine schedule for the learning rate so the model gradually stabilized as it learned. After this full training process, the base Gemini model effectively “became” PH-LLM: an LLM that now understands how to read multi-day sensor patterns and talk like a personal health expert.&lt;/p&gt;

&lt;h3&gt;
  
  
  Multimodal Adapter for Sensor Data
&lt;/h3&gt;

&lt;p&gt;Once PH-LLM can generate coaching advice, the second challenge is enabling it to interpret raw numerical sensor data for prediction tasks (like estimating how tired someone will feel).&lt;/p&gt;

&lt;p&gt;To do this, the researchers added a custom MLP-based multimodal adapter. This is the most “technical” part of the architecture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvb7hndcteg79bo0a1qgn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvb7hndcteg79bo0a1qgn.png" alt="Multimodal Adapter" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;How the adapter works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For each of the 20 wearable sensor signals (HRV, resting HR, sleep duration, etc.), the system collects 15 days of values.&lt;/li&gt;
&lt;li&gt;It computes standardized mean and variance for each signal.&lt;/li&gt;
&lt;li&gt;These 40 numbers (20 means + 20 variances) feed into a multi-layer perceptron (MLP):

&lt;ul&gt;
&lt;li&gt;Input: 40&lt;/li&gt;
&lt;li&gt;Hidden layers: 1024 → 4096 → 1024 (ReLU activations)&lt;/li&gt;
&lt;li&gt;Output: 4 “soft tokens,” each of size 14,336 (the embedding size of PH-LLM).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;These 4 soft tokens are prepended to the text input as if they were real tokens.&lt;/li&gt;

&lt;li&gt;The LLM then processes the numerical data inside its own embedding space, letting its reasoning layers combine subjective sleep patterns with wearable readings&lt;/li&gt;

&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;The LLM never sees raw numbers — it sees learned embeddings representing the person’s physiological state. This allows PH-LLM to achieve machine-learning level accuracy in predicting subjective sleep outcomes, without needing a separate ML pipeline.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Input Format and Context Handling
&lt;/h3&gt;

&lt;p&gt;The system uses two kinds of input representations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Textual Representations

&lt;ul&gt;
&lt;li&gt;Daily tables written out in text&lt;/li&gt;
&lt;li&gt;Time ranges (bedtime, sleep duration)&lt;/li&gt;
&lt;li&gt;Percentile comparisons&lt;/li&gt;
&lt;li&gt;Metric summaries&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Soft Token Representation via Adapter

&lt;ul&gt;
&lt;li&gt;Encodes underlying numerical structure&lt;/li&gt;
&lt;li&gt;Injected directly into the model’s attention layers&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwyollwpfqcdqvz3ggyqf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwyollwpfqcdqvz3ggyqf.png" alt="input format" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Because PH-LLM was trained on long, structured case studies, it naturally handles: multi-day patterns, variations in missing data, differences in available context.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Output Structure
&lt;/h3&gt;

&lt;p&gt;PH-LLM produces multi-part responses in the same structure experts use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insights: Patterns detected in the data&lt;/li&gt;
&lt;li&gt;Etiology: Possible causes based on sleep medicine frameworks (like RU-SATED)&lt;/li&gt;
&lt;li&gt;Recommendations: Personalized, SMART-style advice&lt;/li&gt;
&lt;li&gt;Readiness Scoring (Fitness): Evaluation of fatigue, HRV trends, and recovery loads&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Full7svmjpuvkmtlv0ek4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Full7svmjpuvkmtlv0ek4.png" alt="output structure" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Because the model was trained on structured expert templates, it learns to produce cohesive, medically-grounded narratives instead of generic advice.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Evaluation Architecture
&lt;/h3&gt;

&lt;p&gt;The team built a secondary system called &lt;strong&gt;AutoEval&lt;/strong&gt;, which is an LLM finetuned to grade PH-LLM’s responses against expert criteria. This created an automated loop for model validation, enabling: fast benchmarking, ablation studies, large-scale quality scoring.&lt;br&gt;
AutoEval itself is built using Gemini Pro with LoRA finetuning&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fflzsbauapv7l5oq46m0x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fflzsbauapv7l5oq46m0x.png" alt="Evaluation Arch" width="800" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Strengths of the system
&lt;/h2&gt;

&lt;p&gt;What I like most about PH-LLM is how personal it feels. Instead of giving generic sleep or fitness advice, it actually looks at your patterns over weeks and speaks to you the way a real coach would. When it notices your bedtime shifting or your deep sleep dropping after intense workouts, it doesn’t just state the numbers—it explains what those changes mean and why they matter.&lt;/p&gt;

&lt;p&gt;The system’s expert-level reasoning also stands out. In the paper, PH-LLM &lt;strong&gt;performs as well as or better than trained professionals&lt;/strong&gt; on board-style sleep and fitness exams, scoring &lt;u&gt;79% in sleep medicine&lt;/u&gt; and &lt;u&gt;88% in fitness&lt;/u&gt;. This gives its recommendations a level of credibility that most consumer health apps don’t have.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1yk9ktks5jjb3gl55nj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu1yk9ktks5jjb3gl55nj.png" alt="trained professionals vs PHLLM" width="800" height="197"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another strength is how smoothly it blends wearable data with human-like reasoning. Using the multimodal adapter, PH-LLM can predict subjective feelings like tiredness or restfulness based purely on sensor trends, something even many traditional ML models struggle with.&lt;/p&gt;

&lt;p&gt;Finally, PH-LLM adapts well when information is missing or incomplete. The paper shows that when certain sleep or workout metrics are removed, the model still adjusts its reasoning instead of failing outright. That flexibility makes it feel more intelligent and reliable, almost like it truly understands how sleep and fitness behaviors change from day to day.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations
&lt;/h2&gt;

&lt;p&gt;The biggest one, in my opinion, is its dependence on the quality of the wearable data itself. If your device misreads your sleep stages or your heart rate jumps around because the watch wasn’t snug, PH-LLM will still try to interpret that noise as if it’s meaningful. The paper even points out that the model sometimes references data incorrectly or forms conclusions that don’t perfectly match the input — small confabulations that become more noticeable when the data is messy&lt;/p&gt;

&lt;p&gt;Another issue is that the model sometimes struggles with consistency when giving recommendations. For sleep insights, the fine-tuning helped a lot, but for fitness coaching the improvements were smaller, and in certain sections like “training load,” PH-LLM actually performed worse than the base Gemini model and human experts.&lt;strong&gt;&lt;u&gt; That tells me the model doesn’t fully grasp every fitness scenario as deeply as it understands sleep patterns.&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The paper itself admits there were demographic skews — more middle-aged users, fewer younger or older participants, and no information about race or ethnicity. That means the &lt;strong&gt;eva&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And finally, there’s the broader limitation that PH-LLM, no matter how smart it sounds, is not a medical device. It can give coaching-style suggestions, but it isn’t validated for clinical decision-making. Sometimes its tone feels authoritative, which can make the advice sound more medically precise than it actually is.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Takeaways
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fprfuqfo9k4xwc0ftalxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fprfuqfo9k4xwc0ftalxb.png" alt="summarize" width="800" height="446"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What impressed me most is the model’s ability to turn a messy week of habits into a simple, actionable story. It doesn’t lecture or overwhelm. It just explains what’s going on and nudges you in the right direction. At the same time, it’s important to remember that PH-LLM isn’t a medical system. It still makes small mistakes, relies heavily on the data it sees, and carries the biases of the Fitbit-dominated population it was trained on.&lt;/p&gt;

&lt;p&gt;But the core idea feels powerful. PH-LLM represents a shift from “apps that measure you” to “systems that understand you.” This paper is a glimpse of where personal health AI is heading, and it sets a strong foundation for the next models I’ll be reviewing in this series. As I move into PHIA, the IR Explainer Agent, and later multi-agent systems, I can already see how all these ideas start to connect. PH-LLM feels like the first major building block in creating an AI that doesn’t just track your health—but helps you improve it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Data + code: &lt;a href="https://github.com/google-health/consumer-health-research" rel="noopener noreferrer"&gt;https://github.com/google-health/consumer-health-research&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;NSCA: &lt;a href="https://www.nsca.com/certification/cscs/certified-strength-and-conditioning-specialist-exam-description" rel="noopener noreferrer"&gt;https://www.nsca.com/certification/cscs/certified-strength-and-conditioning-specialist-exam-description&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Healthcare Board Exam: &lt;a href="https://www.boardvitals.com/" rel="noopener noreferrer"&gt;https://www.boardvitals.com/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>google</category>
      <category>ai</category>
      <category>health</category>
      <category>llm</category>
    </item>
    <item>
      <title>AI for Personal Health — My Review, Reflections &amp; Real-Life Understanding</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Thu, 27 Nov 2025 05:32:31 +0000</pubDate>
      <link>https://dev.to/mitanshgor/ai-for-personal-health-my-review-reflections-real-life-understanding-12kh</link>
      <guid>https://dev.to/mitanshgor/ai-for-personal-health-my-review-reflections-real-life-understanding-12kh</guid>
      <description>&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=0JXDsAcQ6JU" rel="noopener noreferrer"&gt;VIDEO LINK&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;1. Introduction&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I’ve been using wearables like Fitbit and Pixel Watch for a while, and I’ve always had the same question in the back of my mind:&lt;br&gt;
&lt;strong&gt;“These devices collect so much data… but what does it actually mean for my health?”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I could see my steps, sleep score, heart rate, all the usual numbers — but I didn’t really know how to connect them to real insights about my body. That curiosity is what pulled me into this world of AI for personal health.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2fxpfyi8zq3goovtaoh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2fxpfyi8zq3goovtaoh.png" alt="i am personal coach" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As I started reading research papers and experimenting with data, I realized something important:&lt;br&gt;
&lt;strong&gt;AI is slowly becoming the “missing link” between raw wearable data and actual, useful guidance.&lt;/strong&gt;&lt;br&gt;
It can interpret patterns, explain them in simple language, and sometimes even coach you like a personal guide.&lt;/p&gt;

&lt;p&gt;This blog series is my attempt to share what I learned — not as a researcher writing a formal report, but as a student trying to make sense of a fast-moving field in a friendly, simple way.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;What This Series Will Cover (Based on the Timeline of Publications)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I studied four papers and mapped out how AI in personal health evolved over the past few years.&lt;br&gt;
Here’s the order I’ll follow in this series — from earlier work to the most recent:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pkx6pw5z4uwcispwd3v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7pkx6pw5z4uwcispwd3v.png" alt="timeline" width="800" height="436"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Intro Blog (this one)&lt;/strong&gt; - Why I’m exploring this topic and how the whole series is structured.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blog 1 (2024)&lt;/strong&gt; — &lt;strong&gt;PH-LLM&lt;/strong&gt; - A large language model that gives personalized sleep and fitness coaching using wearable data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blog 2 (2024)&lt;/strong&gt; — &lt;strong&gt;PHIA&lt;/strong&gt; - An agentic LLM that writes code, analyzes your data, and turns it into meaningful health insights.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blog 3 (2025)&lt;/strong&gt; — &lt;strong&gt;IR Prediction + IR Explainer Agent&lt;/strong&gt; - A combination of machine learning and an LLM-based explainer to estimate metabolic risk.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blog 4 (2025)&lt;/strong&gt; — &lt;strong&gt;Personal Health Agent (PHA)&lt;/strong&gt; - A multi-agent system where different AI “roles” collaborate — data analyst, coach, and domain expert.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Blog 5 (My Project, 2025)&lt;/strong&gt; - My own proof-of-concept that mixes ideas from all four systems to create a unified health agent architecture. My goal is to keep everything straightforward, even if someone is reading about AI for the first time.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;Why This Topic Matters (and What I Hope You’ll Learn)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Most of us don’t think much about our health until something feels wrong. But our wearables are quietly tracking us every day — our sleep, heart rate, movement, and even stress signals. That means they’re collecting clues long before we ever feel anything.&lt;/p&gt;

&lt;p&gt;AI can turn those clues into something meaningful by noticing patterns we might miss, predicting early risks, explaining things in plain English, and even giving small suggestions that feel personal. To me, this makes health feel more understandable and less like a set of random numbers.&lt;/p&gt;

&lt;p&gt;Through this series, I want to share what I learned about these systems in a simple, relatable way. I’ll talk about how wearables work with AI, how different models make sense of health data, why LLMs and multi-agent systems matter, where the ethical issues show up, and how these ideas connect to real projects.&lt;/p&gt;

&lt;p&gt;I’m writing as a student — sharing what made sense to me, what confused me at first, and what finally “clicked.”  And at the end of the series, I’ll also walk through my own proof-of-concept project where I combine everything I learned into one architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;strong&gt;10. References&lt;/strong&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;PH-LLM: A Personal Health LLM for Sleep &amp;amp; Fitness Coaching&lt;br&gt;
Nature Medicine (2025)&lt;br&gt;
&lt;a href="https://www.nature.com/articles/s41591-025-03888-0" rel="noopener noreferrer"&gt;https://www.nature.com/articles/s41591-025-03888-0&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;PHIA: Transforming Wearable Data into Health Insights Using LLM Agents&lt;br&gt;
Google Research Blog (2024)&lt;br&gt;
&lt;a href="https://research.google/blog/advancing-personal-health-and-wellness-insights-with-ai/" rel="noopener noreferrer"&gt;https://research.google/blog/advancing-personal-health-and-wellness-insights-with-ai/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SHARP Framework: Principles for Building Health &amp;amp; Wellness LLMs&lt;br&gt;
Google (2025)&lt;br&gt;
&lt;a href="https://services.google.com/fh/files/blogs/winslow_2025_sharp_framework.pdf" rel="noopener noreferrer"&gt;https://services.google.com/fh/files/blogs/winslow_2025_sharp_framework.pdf&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Insulin Resistance Prediction From Wearables + Routine Blood Biomarkers&lt;br&gt;
arXiv (2025)&lt;br&gt;
&lt;a href="https://arxiv.org/pdf/2505.03784" rel="noopener noreferrer"&gt;https://arxiv.org/pdf/2505.03784&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Personal Health Agent (PHA): Multi-Agent System for Data + Coaching + Expertise&lt;br&gt;
arXiv (2025)&lt;br&gt;
&lt;a href="https://arxiv.org/pdf/2508.20148" rel="noopener noreferrer"&gt;https://arxiv.org/pdf/2508.20148&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>google</category>
      <category>llm</category>
      <category>health</category>
      <category>ai</category>
    </item>
    <item>
      <title>Nested Learning — My Reflections on a Model That Learns How to Learn</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Mon, 17 Nov 2025 02:57:13 +0000</pubDate>
      <link>https://dev.to/mitanshgor/nested-learning-my-reflections-on-a-model-that-learns-how-to-learn-14b5</link>
      <guid>https://dev.to/mitanshgor/nested-learning-my-reflections-on-a-model-that-learns-how-to-learn-14b5</guid>
      <description>&lt;p&gt;I recently came across a paper called &lt;a href="https://research.google/blog/introducing-nested-learning-a-new-ml-paradigm-for-continual-learning/" rel="noopener noreferrer"&gt;&lt;strong&gt;Nested Learning: The Illusion of Deep Learning&lt;/strong&gt;&lt;/a&gt; by Behrouz and the team — the same researchers behind Titans and Atlas. It really caught my attention because it challenges what we usually think “deep learning” means. The paper says that depth in neural networks isn’t just about stacking layers — it’s about how many layers of learning the system can apply to itself. Instead of just updating weights, this model learns how to improve its own learning process.&lt;/p&gt;

&lt;p&gt;While reading it, I realized this isn’t just another optimization trick. It actually feels like a glimpse into what real intelligence could be — an AI that doesn’t just react but reflects, improves, and evolves how it learns over time. The authors even built a prototype called Hope, a model that modifies itself using feedback, learning not just what to learn but how to learn better.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65zccs9kcg5k6ilukwrf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F65zccs9kcg5k6ilukwrf.png" alt="LearningToLearn" width="389" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Take on the Problems in Deep Learning and Transformers
&lt;/h2&gt;

&lt;p&gt;Today’s deep learning systems — even transformers — are strong but still limited in how they actually learn.&lt;/p&gt;

&lt;p&gt;Neural networks are called “deep” because of their layers, but their learning is flat — one optimizer like Adam or SGD updates everything the same way. Once training ends, the model stops learning, like a student who graduates and never studies again. :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm5sr0zxhnevjaum8ho0v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm5sr0zxhnevjaum8ho0v.png" alt="contextWindow" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Transformers improved context handling, but they only have short-term memory. They remember what’s inside their context window and forget the rest. Even if I teach GPT something new, it won’t remember it next time — its knowledge is frozen.&lt;/p&gt;

&lt;p&gt;To me, that’s what makes this paper exciting. It explores real, continuous learning, where models don’t just perform tasks but actually grow and evolve from their experiences — more like how the human brain learns over time.&lt;/p&gt;

&lt;h2&gt;
  
  
  How the Human Brain Actually Learns
&lt;/h2&gt;

&lt;p&gt;When I compared this idea to how our brain works, the difference was clear. The brain doesn’t just store facts — it keeps updating how it learns from every experience. Each new moment changes not only what we know but how we learn next time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc2xjpat5qyf0uesc61o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc2xjpat5qyf0uesc61o.png" alt="brain" width="706" height="726"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Like when I study late and remember less, my brain quietly adjusts — it learns how to learn better. We also have different kinds of memory: fast, short-term memory for quick thoughts and slow, long-term memory for what truly matters.&lt;/p&gt;

&lt;p&gt;What’s amazing is that all this happens at different speeds — reflexes form instantly, habits take time. That’s what makes our learning flexible and self-improving. This paper helped me realize that real intelligence isn’t just about storing knowledge — it’s about systems that can adapt the way they adapt.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Real Intelligence Learns at Many Speeds
&lt;/h2&gt;

&lt;p&gt;“Nested Learning” mirrors the way our brain learns at multiple speeds. In real life, not all learning happens instantly — some lessons come from quick feedback, and others sink in over time through reflection and repetition.&lt;/p&gt;

&lt;p&gt;For example, when I make a mistake in code, I fix it fast — that’s short-term learning. But when I notice a pattern of mistakes across projects and change how I approach debugging, that’s slow, higher-order learning. My brain is basically nesting layers of learning, one inside another.&lt;/p&gt;

&lt;p&gt;This is exactly what the paper argues AI should do. Instead of having one rigid update rule for all situations, it should have systems that operate on different timescales — fast ones for reacting to the present and slow ones for improving how it learns in the future. Real intelligence, human or artificial, grows when it can &lt;em&gt;learn fast, remember slow, and keep adjusting both.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7cf72h9sp4dz83sq58w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff7cf72h9sp4dz83sq58w.png" alt="levelBRain" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Optimizers Are Still “Shallow”
&lt;/h2&gt;

&lt;p&gt;One part that stood out to me was how the paper calls our current optimizers “shallow.” At first, that sounded odd — optimizers like Adam or SGD are what make models learn, right? But the point is deeper: they only operate at one level. They adjust the weights, but they never learn &lt;em&gt;how to optimize better&lt;/em&gt; on their own.&lt;/p&gt;

&lt;p&gt;Think about it like this — an optimizer is a rulebook. It says, “If error is high, change parameters this way.” That rule never changes, no matter how the model behaves or what patterns it encounters. It doesn’t evolve. It’s like a student who keeps using the same study method forever, even when it stops working.&lt;/p&gt;

&lt;p&gt;Nested Learning challenges that. It treats the optimizer as something that can &lt;em&gt;learn from its own history&lt;/em&gt; — almost like giving the optimizer memory and awareness. So instead of being a fixed rule, it becomes a learner itself. That’s why normal optimizers are called “shallow” — they only see one layer of the learning process, while true intelligence needs many.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Nested Learning Really Means
&lt;/h2&gt;

&lt;p&gt;When I finally got to the main idea — Nested Learning — it clicked for me that this isn’t just about deeper networks, but deeper &lt;em&gt;learning loops.&lt;/em&gt; Normally, a model learns by updating its parameters once per round. But in Nested Learning, there are multiple layers of learning stacked inside each other, each operating at its own level. &lt;/p&gt;

&lt;p&gt;The paper calls these “levels.”&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Level 1&lt;/strong&gt; is the fast learner — it adjusts to new data right away.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Level 2&lt;/strong&gt; is slower — it learns how well Level 1 is learning and changes its strategy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Level 3 and beyond&lt;/strong&gt; keep zooming out, letting the model reflect on its own updates and tweak the process itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxenaeptaijj1ljete540.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxenaeptaijj1ljete540.png" alt="levelsLearning" width="800" height="563"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc5wn947001ly0ew6abyi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc5wn947001ly0ew6abyi.png" alt="nestedLearning" width="800" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s like having a mind inside a mind inside a mind — each layer watching and improving the one below. What makes it powerful is that it never stops at one rule; it can always find a better way to learn. I realized that’s what makes it feel almost human — because that’s how we grow too, by not just learning facts, but by constantly refining &lt;em&gt;how&lt;/em&gt; we learn them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Power of Associative Memory
&lt;/h2&gt;

&lt;p&gt;In this paper, associative memory is what allows the model to connect surprise signals over time. Each time it encounters something unexpected, it doesn’t just correct the output; it stores that “surprise” pattern and learns from how surprises evolve. So instead of forgetting past mistakes, it builds a history of how it has been wrong before — and uses that as context for new learning.&lt;/p&gt;

&lt;p&gt;I liked how this turns memory from a passive storage system into an active, learning part of the network. It’s not just remembering data; it’s remembering &lt;em&gt;how learning felt last time.&lt;/em&gt; That’s what makes the system more adaptive and self-improving, just like how human intuition forms through repeated experiences.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmsc3ap0y9ran5cvszl20.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmsc3ap0y9ran5cvszl20.png" alt="associativeMemory" width="800" height="547"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  HOPE — The Model That Learns to Learn
&lt;/h2&gt;

&lt;p&gt;The paper introduces a model called &lt;strong&gt;HOPE (Hierarchical Optimizing Processing Ensemble)&lt;/strong&gt;, and it really ties everything together. HOPE is basically the first real example of Nested Learning in action. It builds on the older &lt;strong&gt;Titans architecture&lt;/strong&gt;, which was already designed for smart memory management — storing “surprising” experiences and forgetting the rest. But Titans could only update its parameters in two layers, which meant it was still limited to first-order learning.&lt;/p&gt;

&lt;p&gt;HOPE takes that concept and adds &lt;em&gt;self-modification&lt;/em&gt;. It doesn’t just store knowledge — it rewrites how it learns based on what it experiences. The more it learns, the better it gets at learning itself. That’s what makes it “hierarchical” — every layer is optimizing the one below it, creating an infinite loop of improvement.&lt;/p&gt;

&lt;p&gt;When I read that, it felt like looking at a prototype for true adaptive intelligence. HOPE doesn’t just grow its memory; it evolves its own way of thinking. It’s almost like the model is building its own brain architecture in real time — guided only by feedback and surprise.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2h73hnhrw783xirbqwv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp2h73hnhrw783xirbqwv.png" alt="hope" width="548" height="1064"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  CMS — Learning Across Multiple Memory Speeds
&lt;/h2&gt;

&lt;p&gt;One of the coolest parts of the paper was the &lt;strong&gt;Continuum Memory System (CMS)&lt;/strong&gt;. This idea clicked with me right away because it’s inspired by how our brain manages memory at different speeds. We have fast, short-term memory for reacting to the moment, and slower, long-term memory for storing what truly matters. CMS brings that same principle to AI.&lt;/p&gt;

&lt;p&gt;In HOPE, CMS creates layers of memory that operate on different time scales. Fast memory reacts instantly to new data, slow memory holds onto stable knowledge, and middle layers balance both. The system learns what to keep, what to adapt, and what to forget — automatically.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzbwdvohd05ptemienwye.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzbwdvohd05ptemienwye.png" alt="cms" width="800" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This makes the model more flexible and less likely to “forget” old knowledge when it learns something new, solving a big problem in continual learning. For me, CMS felt like giving the model an actual sense of &lt;em&gt;time&lt;/em&gt; — letting it learn short-term lessons without losing its long-term wisdom. It’s memory that grows, refines, and stays balanced, just like ours.&lt;/p&gt;

&lt;h2&gt;
  
  
  From Titans to HOPE — How the Architecture Evolved
&lt;/h2&gt;

&lt;p&gt;Before HOPE came along, there was the &lt;strong&gt;Titans architecture&lt;/strong&gt;, which was already an interesting idea. Titans worked like a long-term memory system for AI — it didn’t try to remember everything, only what was &lt;em&gt;surprising&lt;/em&gt;. Whenever the model saw something that didn’t match its expectations, it marked that as “important” and stored it. This made Titans good at keeping rare or unexpected experiences while forgetting routine ones, kind of like how our brain remembers unusual events more vividly than daily habits.  &lt;/p&gt;

&lt;p&gt;But Titans had a big limitation — it could only learn at &lt;strong&gt;two levels&lt;/strong&gt;. It could store knowledge (Level 1) and slightly adjust how it stored it (Level 2), but it couldn’t modify its own learning process. It was stuck with fixed update rules, so even though its memory was smart, its way of learning stayed static.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F11ji4gmdtjpf0zooj1ta.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F11ji4gmdtjpf0zooj1ta.png" alt="Titan" width="800" height="1217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s where &lt;strong&gt;HOPE (Hierarchical Optimizing Processing Ensemble)&lt;/strong&gt; came in as the next step. HOPE keeps Titans’ “surprise-based memory,” but adds &lt;strong&gt;self-modification&lt;/strong&gt; — meaning it can change how it learns over time. Instead of just remembering, it &lt;em&gt;reflects&lt;/em&gt; on how it learned and improves that process.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg95ycli6qj5dfgutnrh8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg95ycli6qj5dfgutnrh8.png" alt="Hope" width="756" height="1266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In simple terms:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Titans learns &lt;em&gt;what&lt;/em&gt; to remember.
&lt;/li&gt;
&lt;li&gt;HOPE learns &lt;em&gt;how&lt;/em&gt; to learn better next time.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This shift from reactive memory (Titans) to reflective learning (HOPE) is what made the architecture truly recursive — an AI that can not only adapt but evolve its own learning rules.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Takes on the Paper
&lt;/h2&gt;

&lt;p&gt;This paper made me rethink what “deep learning” really means. It’s not just about adding layers — it’s about adding &lt;em&gt;levels of learning.&lt;/em&gt; I liked how it pushed the idea that intelligence should evolve, not just perform.&lt;/p&gt;

&lt;p&gt;What stood out to me was the mindset shift. Instead of models that just learn tasks, it showed a system that learns &lt;em&gt;how to learn better.&lt;/em&gt; That’s the kind of loop real intelligence needs — self-awareness in its own process.&lt;/p&gt;

&lt;p&gt;I also liked the balance between fast and slow learning. It reminded me of how humans think — reacting quickly to new events while slowly refining long-term habits. The whole idea felt less like training a model and more like nurturing an evolving mind.&lt;/p&gt;

</description>
      <category>deeplearning</category>
      <category>google</category>
      <category>nestedlearning</category>
      <category>hope</category>
    </item>
    <item>
      <title>GEN-AI 5 : WGAN and WGAN-GP</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Sun, 20 Apr 2025 01:19:50 +0000</pubDate>
      <link>https://dev.to/mitanshgor/gen-ai-2-generative-ai-and-auto-encoders-do8</link>
      <guid>https://dev.to/mitanshgor/gen-ai-2-generative-ai-and-auto-encoders-do8</guid>
      <description>&lt;h2&gt;
  
  
  Why ❌  GAN
&lt;/h2&gt;

&lt;p&gt;In the ever-evolving landscape of generative models, GANs have taken center stage with their remarkable ability to generate data that mimics real-world distributions. But as with all great things, classic GANs came with caveats—training instability, vanishing gradients, and mode collapse, to name a few. &lt;/p&gt;

&lt;p&gt;Let’s dive deeper into these powerful models: WGAN and its enhanced cousin WGAN-GP, two sophisticated upgrades that fix many of GANs' shortcomings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyi492wjs6nr3z58wf2hg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyi492wjs6nr3z58wf2hg.png" alt="I0" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Wasserstein distance
&lt;/h2&gt;

&lt;p&gt;Wasserstein Distance, also known as the Earth Mover's Distance (EMD), is a mathematical measure of the distance between two probability distributions.&lt;/p&gt;

&lt;p&gt;Imagine you have two piles of sand—one symbolizing real data and the other representing data generated by a model. The goal is to reshape one pile into the other by moving portions of sand. The effort required depends not only on how much sand you move but also on how far you move it. This effort is what the Wasserstein Distance measures—a cost-efficient way to transform one distribution into another.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkddlg8za5taek2wm2503.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkddlg8za5taek2wm2503.gif" alt="I1" width="400" height="232"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What makes this distance especially powerful is its ability to compare distributions even when they don't overlap at all—a situation where traditional measures like JS divergence fail. It offers a continuous and interpretable signal for how "far off" the generated data is from the real data, making it incredibly useful for training generative models like WGANs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5lout24z5bpr2hnaw7vf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5lout24z5bpr2hnaw7vf.png" alt="I2" width="800" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Traditional GANs use Jensen-Shannon (JS) divergence to measure similarity between distributions.&lt;br&gt;
BUT :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;JS divergence becomes undefined or uninformative when distributions don't overlap.&lt;/li&gt;
&lt;li&gt;This leads to vanishing gradients—a huge problem for training&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By contrast, Wasserstein Distance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remains well-defined and continuous even when distributions are far apart.&lt;/li&gt;
&lt;li&gt;Provides meaningful gradients, allowing the generator to learn effectively from the start.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Improvement in WGAN wrt GAN
&lt;/h2&gt;

&lt;p&gt;WGAN is a variant of GAN that uses the Wasserstein Distance as its loss function instead of JS divergence.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdia5wcuesljnfdvcm59n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdia5wcuesljnfdvcm59n.png" alt="I3" width="685" height="328"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key differences from traditional GAN:&lt;/strong&gt;&lt;br&gt;
The discriminator is now called a critic. It doesn't classify data as real/fake, but scores it to reflect how “real” it looks.&lt;/p&gt;

&lt;p&gt;The loss function is based on the difference in critic scores for real vs. fake data.&lt;/p&gt;

&lt;p&gt;In simple terms:&lt;br&gt;
&lt;code&gt;Real Data → High Critic Score&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Fake Data → Low Critic Score&lt;/code&gt;&lt;br&gt;
&lt;code&gt;The generator improves by producing data that the critic scores higher.&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Working
&lt;/h2&gt;

&lt;p&gt;Just like in standard GANs, WGAN consists of two neural networks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A Generator (G) that tries to generate realistic data from random noise.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A Critic (C) (not called a discriminator here) that scores the realness of data, assigning higher values to real data and lower values to fake ones.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Unlike GANs, which use a &lt;strong&gt;sigmoid function&lt;/strong&gt; to classify samples as real or fake, WGAN's critic outputs real-valued scores—this small shift changes everything.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The Loss Function
&lt;/h3&gt;

&lt;p&gt;The heart of WGAN lies in replacing Jensen-Shannon divergence with the Wasserstein distance (also called Earth Mover’s Distance)—a metric that measures how much "effort" it takes to morph the generated distribution into the real one.&lt;/p&gt;

&lt;p&gt;This results in the following loss functions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2hxwngfpuhab0mlsvf7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2hxwngfpuhab0mlsvf7.png" alt="I4" width="800" height="317"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Critic's Loss:&lt;/p&gt;

&lt;p&gt;𝐿c = E[C(fake)]−E[C(real)]&lt;/p&gt;

&lt;p&gt;The critic aims to maximize the difference, rewarding real data with higher scores and penalizing fakes.&lt;/p&gt;

&lt;p&gt;Generator's Loss:&lt;/p&gt;

&lt;p&gt;𝐿G = −E[C(fake)]&lt;/p&gt;

&lt;p&gt;The generator tries to minimize the critic’s ability to detect its fakes by generating better, more realistic data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Missing Something? 🤔
&lt;/h2&gt;

&lt;p&gt;The model that we learned until now has some issues.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The critic assigns real-valued scores to data samples, so nothing stops the critic from amplifying its outputs to maximize the loss. For example, it might assign real samples a score of +10,000. And fake samples a score of -10,000. This leads to :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The generator receiving extreme gradients, which are not helpful and can distort learning. &lt;/li&gt;
&lt;li&gt;Loss curves spike or crash. Generated samples look worse over time instead of better, making training unstable.&lt;/li&gt;
&lt;li&gt;Mode collapse and exploding gradients can occur.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How can we solve this problem?&lt;/strong&gt;&lt;br&gt;
Let's introduce a 1-Lipschitz Function. This will solve our problem. &lt;br&gt;
We will add a 1-Lipschitz Function to the critic (discriminator).&lt;br&gt;
In the Lipshitz function, we will give any 2 images (x1 and x2) as input. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqyaa5r2vhscshwtdqeh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpqyaa5r2vhscshwtdqeh.png" alt="I5" width="360" height="152"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Numerator: Absolute difference between the critic predictions&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Denominator: Average pixelwise absolute difference between two images&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What we are trying to do with this function is to limit the rate at which the predictions of the critic can change between 2 images, when comparing it with the real/pixel-wise difference of the image.&lt;/p&gt;

&lt;p&gt;You can get this using a graph by visualizing 2 (white) cones whose origin can be moved along the graph so that the whole graph always stays outside the cones.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fip68lijcqaz11cb3ytl7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fip68lijcqaz11cb3ytl7.gif" alt="I6" width="737" height="677"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By doing so, we are clipping the weights of the critic to lie within a small range, eliminating problems of exploding gradients.&lt;br&gt;
But again NEW PROBLEM !! &lt;br&gt;
Learning significantly decreases 😢 😢 &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Futcfv5rhwinwjilaquer.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Futcfv5rhwinwjilaquer.png" alt="I7" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A much better Solution is the WGAN-GP&lt;/p&gt;




&lt;h2&gt;
  
  
  Adding Gradient Penalty
&lt;/h2&gt;

&lt;p&gt;As the Lipshitz function did a partial job. We need to modify/tweak the function so that it can eliminate the problem of &lt;code&gt;Learning significantly decreases&lt;/code&gt;.&lt;br&gt;
The solution should be to penalize the Gradient if it's too far away from 1. &lt;/p&gt;

&lt;p&gt;The Lipschitz constraint is enforced by including a gradient penalty (GP) term in the loss function for the critic. The GP penalizes the model if the gradient norm deviates from 1&lt;br&gt;
The GP term, which we will be using with Lipschitz constraint, will measure the square difference between the gradient of the prediction wrt input image and 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fevcfujtd57fj59oku5c2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fevcfujtd57fj59oku5c2.png" alt="I8" width="456" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft56qfswe9xzp17xwgvij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft56qfswe9xzp17xwgvij.png" alt="III" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the above image, it's clear that the gradient penalty increases as we move away from 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are a few important things to take care of while using training WGAN-GP&lt;/strong&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;With Wasserstein loss, the Critic must be trained to converge before updating the Generator. To do so, we train the Critic several times between Generator updates. A typical ratio used is &lt;code&gt;3 to 5 Critic updates per Generator update&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Batch Normalization shouldn't be used in the critic. Batch Norm creates a correlation between images in the same batch. This makes the gradient penalty loss less effective. Experiments have shown that WGAN-GPs works great without it anyway&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Results after WGAN-GP model:
&lt;/h2&gt;

&lt;p&gt;Using Wasserstein distance + Lipschitz function + Gradient penalty has improved the results of the images generated significantly. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Traditional GANs suffer from vanishing gradients or mode collapse due to the Jensen-Shannon divergence. WGAN-GP, by using Wasserstein distance + gradient penalty, leads to more consistent and stable convergence. You get smoother generator updates, making training less sensitive to hyperparameters and architectural choices.&lt;/li&gt;
&lt;li&gt;In classic GANs, generators can produce a limited variety (mode collapse). WGAN-GP encourages the generator to explore the data distribution more fully, thanks to the meaningful gradient feedback from the critic. Hence, more diverse outputs, especially noticeable in image generation tasks.&lt;/li&gt;
&lt;li&gt;The loss correlates with sample quality, unlike traditional GAN loss, which becomes meaningless when the discriminator is too good. Hence, you can track training progress numerically, not just visually&lt;/li&gt;
&lt;li&gt;There are no sudden spikes or flat zones in gradients. Hence, more efficient and productive generator learning.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Disadvantages of WGAN
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Slower training, higher memory usage, and increased training time, especially on large datasets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hyperparameters are sensitive. Even a small change in hyperparameters can cause a lot of change. It needs to be tuned carefully.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Summarize
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A WGAN-GP uses the Wasserstein loss&lt;/li&gt;
&lt;li&gt;The WGAN-GP is trained using labels of 1 for real and -1 for fake&lt;/li&gt;
&lt;li&gt;No Sigmoid activation in the final layer of the Critic&lt;/li&gt;
&lt;li&gt;Include a GP term in the loss function for the Critic&lt;/li&gt;
&lt;li&gt;Train the &lt;strong&gt;&lt;u&gt;Critic multiple times&lt;/u&gt;&lt;/strong&gt; for each update of the Generator&lt;/li&gt;
&lt;li&gt;There are &lt;strong&gt;&lt;u&gt;no Batch Norm layers&lt;/u&gt;&lt;/strong&gt; in the Critic&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffarqvyv498oeh1bkku1r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffarqvyv498oeh1bkku1r.png" alt="II" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>generative</category>
      <category>deeplearning</category>
      <category>wgan</category>
      <category>wgangp</category>
    </item>
    <item>
      <title>GEN-AI 4 : GAN</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Sun, 20 Apr 2025 01:19:40 +0000</pubDate>
      <link>https://dev.to/mitanshgor/gen-ai-4-gan-how-ai-learns-to-generate-realistic-images-17bn</link>
      <guid>https://dev.to/mitanshgor/gen-ai-4-gan-how-ai-learns-to-generate-realistic-images-17bn</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpyxk9w3e576t3sa0kefw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpyxk9w3e576t3sa0kefw.png" alt="Image description" width="800" height="533"&gt;&lt;/a&gt;&lt;br&gt;
In the world of generative models, Variational Autoencoders (VAEs) were among the first to show us how machines can learn to create new data. But they came with limitations which sparked the need for a new direction in generative modeling. &lt;/p&gt;

&lt;p&gt;Enter Generative Adversarial Networks (GANs): a powerful, game-theoretic approach that changed the landscape by producing stunningly realistic outputs and pushing the boundaries of what machines can generate.&lt;/p&gt;

&lt;p&gt;Lets start by understanding the limitations of VAE.&lt;/p&gt;

&lt;h2&gt;
  
  
  Disadvantages of VAE
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Blurry Outputs:&lt;/strong&gt; In a VAE, two things are being balanced: how well the model can recreate the input data (reconstruction loss) and how closely the latent variables follow the assumed normal distribution (KL divergence). If this balance isn't right, it can cause problems. For example, if the model focuses too much on making the latent space look normal, it might not focus enough on accurately recreating the input data, leading to blurry or low-quality reconstructions. Finding the right balance is tricky, and if it's off, the output can look fuzzy or unclear.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Latent Space Constraints: When we assume a simple normal distribution for the latent variables in a VAE, it means we expect the data to follow a very basic pattern (like a bell curve). However, real-world data is often more complex and doesn't always fit this simple pattern. Because of this, the VAE might not capture all the details or variations in the data, leading to less accurate results and poor generation of new data. Essentially, the model's assumption limits its ability to understand and recreate the data’s true complexity fully.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Mode Averaging: When the data has many different patterns or "modes" (called multimodal data), VAEs can struggle to capture all of them. The model tends to average over these modes, meaning it might create a mix of features from different data patterns instead of focusing on the specific details of each mode. This can result in the model generating outputs that don’t accurately reflect the diversity of the data, often leading to a loss of important variations or details in the generated samples. Essentially, the model might not fully capture all the unique aspects of the data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexqc8hqb3nxa8wc90ais.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fexqc8hqb3nxa8wc90ais.png" alt="some10.png" width="800" height="788"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How is GAN better than VAE?
&lt;/h2&gt;

&lt;p&gt;While Variational Autoencoders (VAEs) offer a powerful framework for generative modeling, they come with certain limitations. To address these challenges, Generative Adversarial Networks (GANs) provide an alternative approach. Unlike VAEs, which rely on a probabilistic framework, GANs use two neural networks—a generator and a discriminator—that compete against each other to improve the quality of generated data. The adversarial setup in GANs enables them to produce sharper, more realistic outputs, particularly in applications like image generation, where detail and realism are crucial.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0hsyg8ggkyw0cjw04r11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0hsyg8ggkyw0cjw04r11.png" alt="s11.png" width="182" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s how GANs are better than VAEs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sharper Outputs&lt;/strong&gt;: GANs produce clearer and more detailed images because they focus on distinguishing real data from generated data, leading to more realistic results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Blurry Reconstructions&lt;/strong&gt;: Unlike VAEs, which can sometimes produce blurry outputs due to their reliance on a probabilistic framework, GANs avoid this issue by directly optimizing for realism.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Better for High-Quality Generation&lt;/strong&gt;: GANs excel in tasks where the goal is to generate high-quality data, like realistic images, videos, or audio, because the adversarial training encourages the generator to produce more lifelike results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Flexibility in Learning Complex Patterns&lt;/strong&gt;: GANs can learn complex data distributions better, especially when data has many modes (variety of patterns), without averaging out the details like VAEs might.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Working of GAN
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnszgqd2lzvg2svs0lo0a.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnszgqd2lzvg2svs0lo0a.jpg" alt="GAN.jpg" width="800" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A Generative Adversarial Network (GAN) is a machine learning model that consists of two neural networks, a generator and a discriminator, that work against each other in a process called adversarial training. Here's how it works in detail:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qmo8icxg9z835a2knp4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qmo8icxg9z835a2knp4.png" alt="image.png" width="790" height="218"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Generator:
&lt;/h3&gt;

&lt;p&gt;The generator is like an artist trying to create new data (e.g., images) that looks as real as possible. It starts with random noise and uses this as input to produce a generated output. The goal of the generator is to produce data that can fool the discriminator into thinking it’s real.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Discriminator:
&lt;/h3&gt;

&lt;p&gt;The discriminator is like a critic trying to tell whether a piece of data is real (from the training data) or fake (produced by the generator). It takes an input—either real data or a generated one—and outputs a probability that the input is real or fake. It can be considered as a supervised image classification problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adversarial Training:
&lt;/h3&gt;

&lt;p&gt;The key idea behind GANs is the competition between the generator and the discriminator:&lt;/p&gt;

&lt;p&gt;The generator tries to improve by creating more convincing, realistic data to fool the discriminator.&lt;/p&gt;

&lt;p&gt;The discriminator tries to improve by getting better at distinguishing real data from the fake data created by the generator.&lt;/p&gt;

&lt;p&gt;This process is a game where the generator tries to "cheat" by generating better data, and the discriminator tries to become more skilled at detecting fake data. Over time, both networks improve.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3vrlbludcpvjllfo9g1t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3vrlbludcpvjllfo9g1t.png" alt="some1.png" width="265" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Objective:
&lt;/h3&gt;

&lt;p&gt;Generator's Goal: Minimize how often the discriminator correctly identifies fake data. It wants to produce data that the discriminator can't distinguish from real data.&lt;/p&gt;

&lt;p&gt;Discriminator's Goal: Maximize its ability to correctly classify real vs. fake data, helping it get better at spotting fakes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhg52w0ldba0ahgpg9gs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhg52w0ldba0ahgpg9gs.png" alt="some2.png" width="182" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The interaction between the discriminator and generator in a GAN requires a delicate balance. If the discriminator is too good, it can easily distinguish real from fake data, leaving the generator with little feedback and hindering its ability to improve. On the other hand, if the generator is too strong, it may exploit weaknesses in the discriminator, producing fake data that the discriminator wrongly classifies as real (false negatives). The ideal situation occurs when the discriminator outputs a value close to 0.5, meaning it cannot distinguish between real and fake data, indicating that the generator is producing high-quality samples. For example, if the discriminator outputs ~1, the fake images are too realistic, and the generator won't be forced to improve. If it outputs ~0, the fake images are too obvious, and the generator needs more training. However, when the discriminator outputs ~0.5, it suggests the generator is performing well, producing convincing, realistic samples.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd47g58th8vmdc4u1489q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd47g58th8vmdc4u1489q.png" alt="some3.png" width="600" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Loss Functions:
&lt;/h3&gt;

&lt;p&gt;The generator's job in a GAN is to create fake data that looks so real that the discriminator can’t tell it apart from the real data. Instead of directly focusing on the quality of the data, the generator’s main goal is to trick the discriminator into thinking the fake data is real. The generator gets feedback from the discriminator, which helps it improve. The loss function for the generator encourages it to produce data that has a high probability of being classified as real by the discriminator&lt;/p&gt;

&lt;p&gt;The discriminator in a GAN's job is to tell whether the data it sees is real (from the true data source) or fake (generated by the generator). It outputs a probability (between 0 and 1) that the input is real.&lt;br&gt;
The goal of the discriminator is to correctly classify Real data as real and Fake data as fake.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fscfwv8mldnqp6ejri6s5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fscfwv8mldnqp6ejri6s5.png" alt="some4.png" width="299" height="168"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Together, the overall training objective for the GAN can be summarized as : &lt;br&gt;
&lt;code&gt;GAN Loss = Discriminator Loss + Generator Loss&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Training Process:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl8eefxz86uoy2mr8mx80.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl8eefxz86uoy2mr8mx80.png" alt="some5.png" width="800" height="216"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The training process of a Generative Adversarial Network (GAN) involves a series of steps that &lt;strong&gt;&lt;u&gt;&lt;em&gt;alternate&lt;/em&gt;&lt;/u&gt;&lt;/strong&gt; between training the discriminator and the generator.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize the generator and discriminator.&lt;/li&gt;
&lt;li&gt;Train the discriminator on real and fake data.&lt;/li&gt;
&lt;li&gt;Train the generator to fool the discriminator.&lt;/li&gt;
&lt;li&gt;Alternate between training the discriminator and the generator.&lt;/li&gt;
&lt;li&gt;Repeat until the generator produces convincing fake data.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;We must alternate the training of these two networks, making sure that we only update the weights of one network at a time!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Initially, the generator creates very poor, random data, and the discriminator easily detects it as fake. This adversarial training process continues until the generator has learned to produce realistic data, and the discriminator has become more adept at distinguishing real from fake data.&lt;/p&gt;

&lt;p&gt;Training can be stopped when the generator produces high-quality data that the discriminator cannot reliably distinguish from real data (i.e., the discriminator’s output approaches 0.5, meaning it cannot differentiate between real and fake data).&lt;/p&gt;

&lt;h2&gt;
  
  
  Situations in GAN
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1z6msqpfu3z786tw6egc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1z6msqpfu3z786tw6egc.png" alt="Imagesome6.png" width="418" height="121"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1) - We normalize the input range from &lt;a href="https://dev.tothe%20typical%20image%20range"&gt;0, 255&lt;/a&gt; to [-1, 1] instead of [0, 1] because the tanh activation function works better with inputs in the range of [-1, 1]. The tanh function outputs values between -1 and 1, and normalizing the data to this range helps ensure the gradients are stronger and more stable during training. In contrast, the sigmoid activation function has a range of [0, 1] and produces weaker gradients, which can slow down learning. Therefore, using [-1, 1] helps improve the training process.&lt;br&gt;
2) - The training process of GANs can be unstable because the generator and discriminator are constantly competing. Over time, the discriminator may become too good at distinguishing real from fake data, which could cause issues. However, this isn't always a problem because the generator might have already learned enough by that point. To improve stability, we can add a small amount of random noise to the training labels, which helps prevent the discriminator from becoming too dominant too quickly&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb4pbeagq47hzvvleztjf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb4pbeagq47hzvvleztjf.png" alt="Imagesome.png" width="259" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;3) - When the Discriminator Overpowers the Generator,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The discriminator becomes too good at distinguishing real from fake images.&lt;/li&gt;
&lt;li&gt;This causes the generator to receive weak feedback, and the loss signal becomes too weak to improve the generator.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The discriminator perfectly classifies real and fake images, causing the gradients to vanish, and the generator stops training.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Solutions to Weaken the Discriminator:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Increase the Dropout rate in the discriminator to reduce its ability to overfit.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduce the learning rate of the discriminator to slow down its training.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reduce the number of convolutional filters in the discriminator to limit its capacity.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add noise to the labels when training the discriminator to make it harder for the discriminator to distinguish real from fake.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Randomly flip labels of some images during training to confuse the discriminator and prevent it from becoming too powerful.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7q9cy3u8sc04fajzjvn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft7q9cy3u8sc04fajzjvn.jpg" alt="Image description" width="800" height="448"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;4) - When the Generator Overpowers the Discriminator:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The discriminator becomes too weak, and the generator tricks it with a small set of nearly identical images.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This results in &lt;strong&gt;&lt;u&gt;mode collapse&lt;/u&gt;&lt;/strong&gt;, where the generator produces limited variety in its outputs, focusing on a single observation that fools the discriminator.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Solutions to Weaken the Generator:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you find that your generator is suffering from mode collapse, you can try strengthening the discriminator using the opposite suggestions to those listed in the previous section. Also, you can try reducing the learning rate of both networks and increasing the batch size.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;5) - The generator's loss is evaluated based on the current discriminator, which constantly improves during training. This makes it hard to compare the generator's loss at different stages, as it may not reflect the actual quality of generated images. The loss can even increase over time, despite the images improving, because the discriminator is getting better, making it harder for the generator to fool it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffca9ule00a1mnua6d48q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffca9ule00a1mnua6d48q.png" alt="sonme8.pnh" width="304" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Disadvantages of GAN.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Mode Collapse: The generator may start producing limited, identical outputs, tricking the discriminator, and failing to capture the full diversity of the data.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fygkvklwy4ememu7pqrty.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fygkvklwy4ememu7pqrty.png" alt="some9.png" width="268" height="188"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uninformative Loss: The generator's loss may increase even as image quality improves, making it hard to track progress.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Vanishing Gradients: If the discriminator becomes too powerful, the generator may receive weak gradients, preventing meaningful learning.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hyperparameter Sensitivity: GANs are sensitive to choices like learning rates, dropout rates, and the number of layers, requiring careful tuning for optimal performance.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>gan</category>
      <category>van</category>
      <category>deeplearning</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>GEN-AI-3 : VAE</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Sun, 20 Apr 2025 01:19:30 +0000</pubDate>
      <link>https://dev.to/mitanshgor/gen-ai-5-wgan-and-wgan-gp-2245</link>
      <guid>https://dev.to/mitanshgor/gen-ai-5-wgan-and-wgan-gp-2245</guid>
      <description>&lt;p&gt;So far, we’ve seen how Autoencoders (AEs) can take data—like images, audio, or text—and compress it into a lower-dimensional space, only to reconstruct it again like a digital magician pulling a rabbit out of a hat. Pretty neat, right?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5kvoc1suag2yjgn3667.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw5kvoc1suag2yjgn3667.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But there’s a catch: while AEs are great at learning compact representations, they’re not exactly dreamers. Their latent space—the core of their compressed understanding—isn’t built for imagination. Try sampling from it randomly, and you’re more likely to get noise than meaningful data.&lt;/p&gt;

&lt;p&gt;Now, what if we wanted a model that not only compresses data but can also generate new, meaningful samples that look like they came from the original dataset? A model that understands probability, uncertainty, and can dream up new content with finesse?&lt;/p&gt;

&lt;p&gt;🎉 Enter the Variational Autoencoder (VAE).&lt;/p&gt;

&lt;p&gt;In this post, we’re going to unpack how VAEs work, why they’re a major leap forward from traditional autoencoders, and how they lay the groundwork for some of the most exciting generative models in AI today.&lt;/p&gt;

&lt;p&gt;Let’s dive in.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem with Regular Autoencoders 🧩
&lt;/h2&gt;

&lt;p&gt;Traditional autoencoders compress data by learning a direct mapping from input to a latent vector, and then decompress it using a decoder. While effective for feature learning, they suffer from:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4jb8wgld9qpoxj38sh7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi4jb8wgld9qpoxj38sh7.png" alt="Image description" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Disorganized Latent Space: Nearby latent points don’t necessarily produce similar outputs.&lt;/li&gt;
&lt;li&gt;Poor Generative Ability: Sampling randomly from the latent space usually results in noisy, incoherent outputs.&lt;/li&gt;
&lt;li&gt;No Uncertainty Modeling: The model doesn't capture how confident it is in the latent representation.&lt;/li&gt;
&lt;li&gt;We needed structure, smoothness, and the ability to reason probabilistically.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwds1p7qngk33z7lfa6s6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwds1p7qngk33z7lfa6s6.png" alt="Image description" width="641" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Variational Autoencoders
&lt;/h2&gt;

&lt;p&gt;VAEs solve these issues by introducing two key ideas:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Injecting randomness into the encoding process.&lt;/li&gt;
&lt;li&gt;Imposing constraints on the distribution of the latent space.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Instead of encoding an input to a fixed point, the VAE encodes it to a &lt;strong&gt;probability distribution—specifically&lt;/strong&gt;, a multivariate normal distribution centered at a point in the latent space. &lt;br&gt;
Or in other words,&lt;br&gt;
Instead of encoding input into a fixed latent vector, it encodes it into a &lt;strong&gt;distribution&lt;/strong&gt; (usually Gaussian), from which it &lt;strong&gt;samples&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By doing so, it not only compresses data, but VAEs &lt;strong&gt;learn to imagine variations&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faeyel02ruwfwdwbpnjdq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faeyel02ruwfwdwbpnjdq.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🎯 Constraints on the latent space
&lt;/h2&gt;

&lt;p&gt;To bring structure to the latent space, we constrain how encodings are distributed:&lt;/p&gt;

&lt;p&gt;🌀 Centering: Each encoded distribution should be centered as close to the origin (0, 0, ..., 0) as possible.&lt;br&gt;
📏 Unit Variance: The spread (standard deviation) of each distribution should be close to 1.&lt;/p&gt;

&lt;p&gt;The further the encoder deviates from these goals, the higher the loss during training.&lt;/p&gt;

&lt;p&gt;Why? This forces all encoded samples to live near the same area in latent space, enabling smooth interpolation and consistent generation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynnt1sj7uwv3fux9gwrj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fynnt1sj7uwv3fux9gwrj.png" alt="Image description" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Changes in VAE Encoder
&lt;/h2&gt;

&lt;p&gt;The encoder no longer outputs just a point, but rather, it parameterizes a distribution from which we can sample.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;x → (μ, σ) → z ~ N(μ, σ²)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3wkg1a19i7zfwfu56aj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft3wkg1a19i7zfwfu56aj.png" alt="Image description" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We do this because we want to generate new data by sampling from a continuous, meaningful latent space.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;z_mean (μ): The center of the distribution where we want our encoding to be.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;z_log_var (log(σ²)): The (log of) the variance, controlling the spread of that distribution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Together, they define a normal distribution, from which we will later sample to get a latent variable z for decoding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now the question is why log(σ²) and not just σ² ?&lt;/strong&gt;&lt;br&gt;
There are two  major reasons: positivity constraint, numerical stability&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variance Must Be Positive: If we try to output σ directly from a neural network, we need to ensure that the network only produces positive values. But neural nets naturally output values from (−∞,∞). Hence, log(σ²) guarantees that σ&amp;gt;0, always.&lt;/li&gt;
&lt;li&gt;We work in the log domain, where multiplications become additions, and exponentials become linear. It avoids premature underflow for tiny variances or instability from tiny gradients when optimizing 
σ directly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Imagine we want to allow the encoder to learn variances from 0.0001 to 1000:&lt;br&gt;
If we output σ directly, the network must learn to span that huge dynamic range. But if we output log(σ²), the values range from about:&lt;br&gt;
&lt;code&gt;log(0.0001)=−9.2&lt;/code&gt; to &lt;code&gt;log(1000)=6.9&lt;/code&gt;.&lt;br&gt;
A much more manageable range!&lt;/p&gt;

&lt;p&gt;But wait on!!&lt;br&gt;
There is one problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpl0h0wu3ww3pry99yl3r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpl0h0wu3ww3pry99yl3r.png" alt="Image description" width="404" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's go through it using the example&lt;/p&gt;

&lt;p&gt;Let’s imagine the encoder outputs:&lt;br&gt;
&lt;code&gt;μ=0.5&lt;/code&gt;&lt;br&gt;
&lt;code&gt;σ=1.0&lt;/code&gt;&lt;br&gt;
If we directly sample&lt;br&gt;
&lt;code&gt;z = np.random.normal(mu, sigma)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This operation:&lt;br&gt;
Picks a random number (say 0.23 or 1.42) with no guarantee&lt;/p&gt;

&lt;p&gt;There's no way to know how the output Z would change if μ or &lt;br&gt;
σ changed — because the randomness hides the function's slope&lt;/p&gt;

&lt;p&gt;And without a gradient, the network can’t learn.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can we deal with it?&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Reparameterization Trick
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftp15wzbenoh861qgt3px.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftp15wzbenoh861qgt3px.png" alt="Image description" width="400" height="284"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here’s the twist:&lt;/p&gt;

&lt;p&gt;Instead of sampling like this:&lt;br&gt;
&lt;code&gt;z∼N(μ,σ2)&lt;/code&gt;&lt;br&gt;
We reparameterize it as:&lt;br&gt;
&lt;code&gt;z = μ+ σ⋅ε where ε∼N(0,1)&lt;/code&gt;&lt;br&gt;
Here :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ε = Random noise from a fixed distribution&lt;/li&gt;
&lt;li&gt;μ, σ = Output from the encoder (learnable)&lt;/li&gt;
&lt;li&gt;z = Latent vector to feed into the decoder&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Why this works:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ε is independent of the network, so its randomness doesn’t interfere with gradient flow.&lt;br&gt;
μ and σ are now involved in a deterministic operation (addition and multiplication), so gradients can be calculated&lt;br&gt;
Now, you can compute:&lt;br&gt;
&lt;code&gt;&lt;br&gt;
∂z/∂μ = 1&lt;br&gt;
∂z/∂σ = ε&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8duwhgbungvqbjoygara.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8duwhgbungvqbjoygara.png" alt="Image description" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Backpropagation is happy again. 🎉&lt;/p&gt;

&lt;p&gt;This tiny trick makes backpropagation work through the stochastic layer. Without it, training would collapse.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0r6j5cn84w8jssmdq7ju.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0r6j5cn84w8jssmdq7ju.png" alt="Image description" width="330" height="220"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Changes in VAE Decoder wrt AE
&lt;/h2&gt;

&lt;p&gt;The decoder no longer sees a fixed vector in the latent space.&lt;br&gt;
Instead, it gets:&lt;br&gt;
&lt;code&gt;z=μ+σ⋅ε&lt;/code&gt;&lt;br&gt;
The decoder must be able to take any nearby sample around μ and still reconstruct a very similar output.&lt;/p&gt;




&lt;h2&gt;
  
  
  VAE Loss Function: More Than Just Reconstruction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0x5ro5zk3uubjp1zz7w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq0x5ro5zk3uubjp1zz7w.png" alt="Image description" width="220" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We used reconstruction loss for AutoEncoders. We cannot use just the reconstruction loss anymore.  We’re now dealing with probability distributions, and we need to keep the latent distributions close to a standard normal (N(0,1)).&lt;/p&gt;

&lt;p&gt;So we add a Kullback–Leibler (KL) divergence term:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Loss = ReconstructionLoss + β∗KLDivergence&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;KL measures how much our learned distribution (μ, σ²) deviates from the standard normal. A higher KL means our encoding is straying too far.&lt;/p&gt;

&lt;p&gt;Think of it like this:&lt;br&gt;
Reconstruction loss: "How well did we recreate the input?"&lt;br&gt;
KL divergence: "How wild is our latent distribution? Should we calm it down?"&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5hxif5cocmb8k1c9w07i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5hxif5cocmb8k1c9w07i.png" alt="Image description" width="800" height="323"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What's the use case of the β Parameter?&lt;/p&gt;

&lt;p&gt;The β coefficient balances reconstruction and regularization.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;If &lt;strong&gt;β is too small&lt;/strong&gt;, the KL divergence is ignored. The latent space becomes disorganized, similar to a vanilla AE. Good reconstructions, bad generation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If &lt;strong&gt;β is too large&lt;/strong&gt;, the model prioritizes matching N(0,1) over reconstruction. All samples start looking the same—blurry outputs, poor expressiveness.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;✅ Sweet spot: When β is balanced, we get coherent generation and meaningful reconstructions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ju6apoe8ksc86mhxear.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4ju6apoe8ksc86mhxear.jpg" alt="Image description" width="800" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🧱 Disadvantages of VAEs
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Tends to generate &lt;strong&gt;blurry images&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;KL term is &lt;strong&gt;tricky to balance&lt;/strong&gt; with reconstruction loss&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not ideal for high-resolution data&lt;/strong&gt; (GANs often outperform here)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx032anta80601q6lu48r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx032anta80601q6lu48r.png" alt="Image description" width="289" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TL;DR: VAEs are smarter but also harder to tame.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧭 Wrap-up: When to Use What and Why It All Matters
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Autoencoders&lt;/strong&gt; when you need compression, denoising, or anomaly detection.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;VAEs&lt;/strong&gt; when you need controlled generation, diversity, and smooth interpolation.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;GANs&lt;/strong&gt; when you need photo-realism.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The future of generative models lies in &lt;strong&gt;hybrids&lt;/strong&gt; — combining VAEs, GANs, and Diffusion models.&lt;/p&gt;

</description>
      <category>deeplearning</category>
      <category>vae</category>
      <category>autoencoder</category>
      <category>generativeai</category>
    </item>
    <item>
      <title>GEN-AI 2 : Auto Encoders</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Sun, 20 Apr 2025 01:19:10 +0000</pubDate>
      <link>https://dev.to/mitanshgor/gen-ai-3-vae-2hoi</link>
      <guid>https://dev.to/mitanshgor/gen-ai-3-vae-2hoi</guid>
      <description>&lt;p&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  📌 What Are Generative Models?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Generative Models&lt;/strong&gt; are machine learning algorithms that learn the &lt;strong&gt;underlying patterns&lt;/strong&gt; or distribution of a dataset to &lt;strong&gt;generate new data&lt;/strong&gt; that resembles the original.&lt;/p&gt;

&lt;p&gt;In human terms:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;They look at thousands of pictures of cats... and then start &lt;em&gt;imagining&lt;/em&gt; their own cats.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1khlbnbmjo19tesy4k7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1khlbnbmjo19tesy4k7.png" alt="IIII3" width="266" height="189"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🌟 Popular Examples of Generative Models
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;What It Does&lt;/th&gt;
&lt;th&gt;Common Uses&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;GANs&lt;/strong&gt; (Generative Adversarial Networks)&lt;/td&gt;
&lt;td&gt;Pit 2 networks (Generator &amp;amp; Discriminator) against each other&lt;/td&gt;
&lt;td&gt;Deepfakes, art generation, face synthesis&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;strong&gt;VAEs&lt;/strong&gt; (Variational Autoencoders)&lt;/td&gt;
&lt;td&gt;Probabilistic version of AEs with smooth latent space&lt;/td&gt;
&lt;td&gt;Image reconstruction, data generation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Autoregressive Models&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Predict next element based on previous ones&lt;/td&gt;
&lt;td&gt;GPT, music generation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Diffusion Models&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Denoise images gradually to generate high-res outputs&lt;/td&gt;
&lt;td&gt;DALL·E 3, Stable Diffusion&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  🛣️ How Do Generative Models Learn?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3n8chlda40ejddng3m4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs3n8chlda40ejddng3m4.png" alt="I0" width="339" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Imagine you’re an artist learning how to draw cats. You spend hours going through cat books, sketching from photos, and watching cat videos on YouTube (because obviously). Over time, without memorizing each specific cat, you start to get a feel for what a “cat” is — four legs, whiskers, maybe some attitude.&lt;/p&gt;

&lt;p&gt;Their mission:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Learn the vibe of the data so well that they can create new things that look like it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The Typical Path:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Input Data&lt;/strong&gt; :  This is your model’s training time — a massive gallery of examples.&lt;br&gt;
Think: Thousands of cat pictures (or faces, music, text). No labels needed — just raw, unfiltered examples. The model isn’t told: “This is a cat.” It just sees cats, over and over. And from that, it begins to understand patterns.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Latent Representation&lt;/strong&gt; &lt;br&gt;
The model tries to compress all the messy, pixel-by-pixel cat data into something smaller and meaningful, kind of like the artist thinking: “Okay, I don’t remember every cat, but most cats have pointy ears, some fur texture, and this cat-ish outline... I can work with that.” In AI terms, this is the &lt;strong&gt;&lt;u&gt;latent space&lt;/u&gt;&lt;/strong&gt; — a multi-dimensional map where similar things live near each other. Imagine a universe where all cats are clustered in one corner, dogs in another, and so on.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fthdz4m86qtq3yb6ntfpe.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fthdz4m86qtq3yb6ntfpe.png" alt="D3" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reconstruction or Generation&lt;/strong&gt;:
Once the model has this compressed knowledge, it can do either of these:

&lt;ul&gt;
&lt;li&gt;Reconstruct what it saw: “Can I redraw the same cat just from memory?”&lt;/li&gt;
&lt;li&gt;Generate something new: “Can I draw a brand new cat that no one’s ever seen, but still looks legit?”
And here’s the twist — different types of models do this learning differently.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;This learning can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Explicit&lt;/strong&gt; (learning the actual data distribution like VAEs)&lt;br&gt;
These models are like a structured student. They try to understand the actual math behind how your data is spread out, trying to learn the probability distribution. They care about making the latent space smooth, organized, and logical.&lt;br&gt;
You can walk around the space, sample random points, and get meaningful results.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Implicit&lt;/strong&gt; (learning to fool a critic like in GANs)&lt;br&gt;
These models are the street-smart artists. They don’t care about formulas — they just want their results to look good and be convincing. GANs do this through a game:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One network (the Generator) tries to create fake data&lt;/li&gt;
&lt;li&gt;Another (the Discriminator) tries to spot the fakes&lt;/li&gt;
&lt;li&gt;They improve together in a loop until the fakes are too good to tell apart.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;They never explicitly learn the actual math of the data, just how to mimic it well enough to fool someone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flgdzfxle1iqpvx1ojoq2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flgdzfxle1iqpvx1ojoq2.gif" alt="Image description" width="320" height="160"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🏗️ Autoencoders Architecture (AE): Compress, Learn, Reconstruct
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw5fwby1a9qv7fq5l69p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw5fwby1a9qv7fq5l69p.png" alt="I3" width="638" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s say we’re dealing with image data (like handwritten digits from MNIST — each image is 28x28 pixels).&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Input Layer&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;You feed the model a raw input vector, say a flattened version of the original data (e.g., 28x28 image = 784 dims). This is just pixel intensity values (usually between 0 and 1 if normalized).&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Encoder&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The encoder is a neural network that compresses the input into a lower-dimensional representation — the latent vector z.&lt;br&gt;
&lt;code&gt;z=FuncEncoder(x)&lt;/code&gt;&lt;br&gt;
For example, you’re squeezing 784 dimensions down to 32. Hence size of the Z vector = 2.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwjbkny3e96uvdoqvzy15.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwjbkny3e96uvdoqvzy15.png" alt="D1" width="323" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We want the model to learn what matters most. By forcing it to compress, the model has to filter out unimportant noise and capture the essence of the data. This is called the bottleneck. You keep only the information about the image that matters.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can we do that?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We pass the input image (28X28) through 3 convolutional layers, one after the other.&lt;/li&gt;
&lt;li&gt;We use a stride of 2 (for eg), which means each layer reduces the image size by half while increasing the number of feature channels (kind of like zooming out but seeing more detail). &lt;/li&gt;
&lt;li&gt;So image (ColXRowXfeatureChannel) from 28X28X1 -&amp;gt; 14X14X32 -&amp;gt; 7X7X64 -&amp;gt; 4X4X128 sized image.&lt;/li&gt;
&lt;li&gt;Flatten (4 × 4 × 128) → 2048-dimensional vector&lt;/li&gt;
&lt;li&gt;2048-dimensional vector Input as a Fully Connected Layer. Gives 2-Dimensional Output (Latent Vector - Z) - a compact 2D representation of your image.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnz7xaay12u4n6ufii2gv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnz7xaay12u4n6ufii2gv.png" alt="Stride and Filter" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Decoder&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now the decoder takes that compressed vector z and tries to rebuild the original image. It is a reverse-engineering/mirror image of the encoder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;x ′ = FuncDecoder(z)&lt;/code&gt;&lt;br&gt;
It converts a 2D latent back to a 28*28 image. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How can we do that?&lt;/strong&gt;&lt;br&gt;
Instead of using normal convolutional layers that shrink things down, it uses transposed convolutions (or "deconvolutions") to build things back up to the original size. While a regular Conv layer with stride 2 shrinks the image, a Conv2DTranspose with stride 2 does the opposite — it doubles the size of the feature map. You can also use upsampling, which simply stretches the image to a larger size, but unlike transposed convolutions, it doesn't learn anything — it just resizes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here’s how the decoder does that:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with the 2-dimensional latent vector (Z), and pass it through a fully connected (dense) layer to expand it into a larger vector — say, 2048 dimensions.&lt;/li&gt;
&lt;li&gt;Reshape that 2048-dimensional vector back into a 4 × 4 × 128 tensor, like rewinding a movie to the middle scene.&lt;/li&gt;
&lt;li&gt;Now we apply transposed convolutional layers (Conv2DTranspose) one after another — each with a stride of 2 — which gradually upsamples the feature maps while reducing the number of channels: 4×4×128 → 7×7×64 → 14×14×32 → 28×28×1&lt;/li&gt;
&lt;li&gt;The final layer outputs an image that's the same size as the input — in this case, 28×28 pixels with 1 channel (grayscale).&lt;/li&gt;
&lt;li&gt;Optionally, we apply a sigmoid activation at the end so all pixel values are between 0 and 1 — perfect for comparing with the original input during loss calculation!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Femu4wpnr26jtk8e1yjyu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Femu4wpnr26jtk8e1yjyu.png" alt="III4" width="800" height="256"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the model reconstructs the 28*28 image, it checks the loss of the result by comparing it with the original image.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loss Func Check
&lt;/h3&gt;

&lt;p&gt;We want the input 𝑥 and the reconstruction 𝑥′ to be as close as possible.&lt;br&gt;
So we minimize a loss function that measures the difference between them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7h4qiuarqgyplohdsput.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7h4qiuarqgyplohdsput.png" alt="D2" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Two Common Losses:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Mean Square Error (MSE / L2 loss):  It computes the square of the difference between each element in the predicted output vector and the corresponding element in the ground truth, then takes the average across all elements. This loss is particularly useful when dealing with continuous data and is known for its sensitivity to outliers.&lt;br&gt;
&lt;code&gt;MSE Loss(J) = (1/n) * Σ(y_true - y_pred)^2&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Binary Cross-Entropy (BCE):  used to measure the difference between two probability distributions. We use a sigmoid activation in the final layer of the decoder so that  &lt;code&gt;0 &amp;lt; x′&amp;lt; 1&lt;/code&gt;, which makes it interpretable as a probability — a requirement for BCE to work.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0sj1rzqcxj0vsh4ixmoh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0sj1rzqcxj0vsh4ixmoh.png" alt="II0" width="800" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The model looks at that loss and works backward( backpropagation). It figures out, layer by layer, how much each connection (weight) contributed to the mistake. Then, using an optimizer like Adam or SGD, it tweaks those weights just a little to do better next time. This process repeats again and again, and over time, the model gets good at understanding what matters in the data, like learning the essence of a handwritten “3” without being told it’s a 3.&lt;/p&gt;




&lt;h2&gt;
  
  
  ❌ Limitations of Autoencoders
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0d7miukjze3oczbkrsc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh0d7miukjze3oczbkrsc.png" alt="copypasterMeme" width="800" height="1200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;When we try to sample (pick) a random point from the latent space, we often end up with strange or meaningless outputs. That’s because some regions of the space are crowded, while others are empty, so we might accidentally pick a point from a “dead zone” where the model has never seen any training data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Autoencoders don’t enforce any structure in the latent space. There's no rule saying, “the points should follow a neat, smooth pattern like a normal distribution.” So when we try to generate new data by sampling from the space, we don’t really know where to pick from — it's like throwing darts in the dark.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There are gaps or holes in the latent space — areas that never got used during training. The model has no clue how to decode these zones, so it often produces blurry or totally unrecognizable images.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Even points close together in space can produce wildly different outputs. For example, if (–1, –1) creates a nice image, (–1.1, –1.1) might give total noise. That’s because the model doesn’t care about making the space smooth or continuous — it's just trying to reconstruct training images.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In 2D latent spaces (for example, when visualizing MNIST), this problem isn’t too bad — everything is kind of squished together. But when we move to higher dimensions (like when generating complex stuff — faces, clothes, etc.), the gaps and discontinuities get worse. So even though more dimensions are needed to capture complex patterns, they also make the space harder to sample from safely.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>deeplearning</category>
      <category>ai</category>
      <category>generative</category>
      <category>autoencoder</category>
    </item>
    <item>
      <title>Gen-AI 1 : The Generative AI Universe</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Sun, 20 Apr 2025 01:19:02 +0000</pubDate>
      <link>https://dev.to/mitanshgor/gen-ai-1-the-generative-ai-universe-5gmh</link>
      <guid>https://dev.to/mitanshgor/gen-ai-1-the-generative-ai-universe-5gmh</guid>
      <description>&lt;p&gt;Alright, reader — buckle up! You’re about to embark on an exciting, mind-bending journey into the world of &lt;strong&gt;Generative AI&lt;/strong&gt;. This isn't just a technical deep dive; it's a story, a pathway — one that starts simple and grows into the cutting edge of machine creativity.&lt;/p&gt;

&lt;p&gt;Think of this as leveling up in a video game 🎮 — each model you learn is a new power-up!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F05lxwg5rditc6gibb5bp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F05lxwg5rditc6gibb5bp.png" alt="I1" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  🧱 Level 1: Autoencoders (AE) — &lt;em&gt;The Compression Artists&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;We kick things off with &lt;strong&gt;Autoencoders&lt;/strong&gt;, the humble but powerful networks that learn to &lt;em&gt;compress&lt;/em&gt; and &lt;em&gt;rebuild&lt;/em&gt; data. They’re the foundation — no frills, just the raw ability to reconstruct images like a neural .zip file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg8k7e1osr6v85zpralmi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg8k7e1osr6v85zpralmi.png" alt="I2" width="540" height="535"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  🌀 Level 2: Variational Autoencoders (VAE) — &lt;em&gt;Bringing Probability to the Party&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Next, we get a bit more &lt;strong&gt;creative&lt;/strong&gt; with &lt;strong&gt;VAEs&lt;/strong&gt;. Instead of just compressing, they &lt;strong&gt;model distributions&lt;/strong&gt;, letting us &lt;strong&gt;generate&lt;/strong&gt; brand-new samples. You’ll meet latent spaces that look like dreamy universes where cats, dogs, and unicorns each have their own neighborhoods.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsagg37mh1xqm1s3cib8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsagg37mh1xqm1s3cib8j.png" alt="I3" width="400" height="400"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  🎭 Level 3: Generative Adversarial Networks (GAN) — &lt;em&gt;The Ultimate Fake Artists&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Now things get spicy. &lt;strong&gt;GANs&lt;/strong&gt; introduce an epic &lt;em&gt;duel&lt;/em&gt; between two networks — a &lt;strong&gt;Generator&lt;/strong&gt; and a &lt;strong&gt;Discriminator&lt;/strong&gt;. The Generator tries to fool the Discriminator, and in doing so, creates stunningly realistic data. It’s like a counterfeiter and a detective locked in a neural arms race. 🕵️‍♂️💣&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjirvsljts88zar3n9xy6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjirvsljts88zar3n9xy6.png" alt="I4" width="498" height="315"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  🌊 Level 4: Wasserstein GANs (WGAN) — &lt;em&gt;Stability, Finally!&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;But GANs can be moody. That’s where &lt;strong&gt;WGANs&lt;/strong&gt; come in. With &lt;strong&gt;Wasserstein loss&lt;/strong&gt;, they bring calm and stability to the chaotic training process. Less mode collapse, more control.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkakgyl9mtmgbuvgev1ws.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkakgyl9mtmgbuvgev1ws.png" alt="I5" width="423" height="415"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  🌊🧼 Level 5: WGAN-GP — &lt;em&gt;Smooth Operators&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Add a &lt;strong&gt;Gradient Penalty (GP)&lt;/strong&gt; and voilà — &lt;strong&gt;WGAN-GP&lt;/strong&gt; is born. It regularizes training, keeping the Discriminator’s gradients smooth and ensuring even better results.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvxixck65359ola9ovkod.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvxixck65359ola9ovkod.png" alt="I6" width="335" height="220"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  🎨 Level 6: Conditional GANs (CGAN) — &lt;em&gt;The Mind Readers&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Want a GAN that listens to you? Say hello to &lt;strong&gt;CGANs&lt;/strong&gt;, where you can generate images &lt;strong&gt;based on labels&lt;/strong&gt;. “Hey GAN, draw me a sneaker!” And it does.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1e2gsq88b3bv8bp3tv0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1e2gsq88b3bv8bp3tv0o.png" alt="I7" width="392" height="220"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  💨 Level 7: Diffusion Models — &lt;em&gt;From Noise to Masterpiece&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Diffusion Models flip the script. They &lt;strong&gt;start with pure noise&lt;/strong&gt; and slowly reverse it to create images — like watching fog form into a painting. These are powering &lt;strong&gt;DALL·E 2&lt;/strong&gt;, &lt;strong&gt;Stable Diffusion&lt;/strong&gt;, and the latest text-to-image breakthroughs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8t10bgatuqc8tmuwtaq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe8t10bgatuqc8tmuwtaq.png" alt="I8" width="332" height="220"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  🔁 Level 8: Recurrent Neural Networks (RNNs) — &lt;em&gt;The Time Travelers&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;Now we move into sequence-land. &lt;strong&gt;RNNs&lt;/strong&gt; are the OGs of temporal data, remembering past inputs to generate text, music, and more. Great for early language modeling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Favm5ljp0ae14paoxkrul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Favm5ljp0ae14paoxkrul.png" alt="I9" width="323" height="220"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  ⚡ Level 9: Transformers — &lt;em&gt;The Reigning Champions&lt;/em&gt;
&lt;/h3&gt;

&lt;p&gt;And finally… the &lt;strong&gt;Transformers&lt;/strong&gt;. The architecture behind &lt;strong&gt;GPT&lt;/strong&gt;, &lt;strong&gt;BERT&lt;/strong&gt;, &lt;strong&gt;DALL·E&lt;/strong&gt;, and nearly every major AI breakthrough in recent years. With &lt;strong&gt;attention mechanisms&lt;/strong&gt;, they’ve redefined how machines understand and generate language, images, even code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3d8y6xur4jdwkk49idfi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3d8y6xur4jdwkk49idfi.png" alt="I10" width="352" height="220"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  🌈 What Awaits You
&lt;/h3&gt;

&lt;p&gt;By the end of this journey, you won’t just know how these models work — you’ll &lt;em&gt;see&lt;/em&gt; the connections, appreciate the evolution, and maybe even build your own!&lt;/p&gt;

&lt;p&gt;So…&lt;br&gt;&lt;br&gt;
&lt;strong&gt;Start simple. Go deep. Level up. And let the generative magic begin.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xm9mwg7fz9939uftznb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xm9mwg7fz9939uftznb.png" alt="I" width="392" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ai</category>
      <category>generative</category>
      <category>pathway</category>
      <category>chatgpt</category>
    </item>
    <item>
      <title>Carla Simulator 1 : How to Set Up CARLA Simulator 🏎️🔥</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Fri, 20 Dec 2024 22:06:12 +0000</pubDate>
      <link>https://dev.to/mitanshgor/carla-simulator-1-how-to-set-up-carla-simulator-toretto-style-3800</link>
      <guid>https://dev.to/mitanshgor/carla-simulator-1-how-to-set-up-carla-simulator-toretto-style-3800</guid>
      <description>&lt;p&gt;Yo, it's Dom here 🏁. Family's everything to me, but today, I'm pulling over from the street races to drop some knowledge about setting up the CARLA simulator. We're about to step into a virtual world where precision meets adrenaline. Buckle up, because we're cruising through what CARLA is, why it's your ultimate driving ally, and how to set it up like a pro.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwd0djiwqo7joe6syims1.jpg" alt="Image description" width="800" height="450"&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://drive.google.com/drive/home?dmr=1&amp;amp;ec=wgc-drive-hero-goto" rel="noopener noreferrer"&gt;check Video&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What’s CARLA, and Why Do We Need It? 🛠️🚗
&lt;/h3&gt;

&lt;p&gt;CARLA isn't just some fancy tool; it's the NOS of autonomous driving simulators. Built on Unreal Engine, this open-source bad boy helps you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Train perception algorithms 🎯.&lt;/li&gt;
&lt;li&gt;Learn driving policies.&lt;/li&gt;
&lt;li&gt;Validate your autonomous systems safely without denting a single bumper.&lt;/li&gt;
&lt;li&gt;From radars to cameras and even LIDAR, CARLA hooks up your virtual ride with every sensor imaginable. Need realistic urban setups? It’s got those. Want to replay every crash like a Fast &amp;amp; Furious movie? There's a recorder for that. 🌆🎥&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Why Simulators Like CARLA Matter 🚦
&lt;/h3&gt;

&lt;p&gt;So, why the heck do we need CARLA? Well, these simulators are game-changers for anyone in autonomous driving. You wouldn’t take your muscle car out on a high-speed chase without practicing first, right? The same goes for autonomous cars—they need to be trained in different environments and situations, and that's where CARLA comes in.&lt;/p&gt;

&lt;p&gt;We use simulators like CARLA for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Training algorithms that teach cars to drive themselves (without causing a wreck).&lt;/li&gt;
&lt;li&gt;Testing perception systems (sensors that make sure the car sees everything it needs to).&lt;/li&gt;
&lt;li&gt;Learning driving policies (figuring out how to make the car follow the rules of the road and not blow through red lights).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Without simulators, self-driving cars would be like a race car driver trying to learn by just jumping into a car and going 200 mph without practice. You need to test the machine before hitting the real world. And trust me, we don’t want any bad surprises when we’re going full throttle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up CARLA - Let's Get to Work 💻💥
&lt;/h3&gt;

&lt;p&gt;Alright, now let’s get down to business. listen up! You wanna set up CARLA on a Linux VM? It's gonna be a wild ride, but I've got your back. Follow these steps like you're gunning for a 10-second quarter mile. Let's get this show on the road. 🔥&lt;/p&gt;

&lt;h4&gt;
  
  
  Download CARLA
&lt;/h4&gt;

&lt;p&gt;First, hit up the CARLA GitHub repo to grab the carla-0.9.12-linux tarball. This is the start of your journey.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;wget https://tiny.carla.org/carla-0-9-12-linux&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;You can’t drive a car without a motor, right? This tarball's gonna be your engine. Now, let’s crack that tarball open and release the power inside. Run the command.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;tar -xzvf carla-0.9.12-linux&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Install NVIDIA Drivers
&lt;/h4&gt;

&lt;p&gt;If you’re running this on a beefy machine with a GPU (and you should be, because we’re going for high-speed performance here), we need to get the right drivers in place. Install the NVIDIA drivers:&lt;br&gt;
&lt;code&gt;sudo apt install nvidia-driver-535&lt;/code&gt;&lt;br&gt;
Once the drivers are installed, you’ve gotta give your system a little restart to make sure everything kicks into gear using &lt;code&gt;sudo reboot&lt;/code&gt;.&lt;br&gt;
After rebooting, make sure your NVIDIA drivers are running smooth. Run &lt;code&gt;nvidia-smi&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  Install Vulkan Tools
&lt;/h4&gt;

&lt;p&gt;Vulkan is your performance boost, just like nitrous on a muscle car. Install it with&lt;br&gt;
&lt;code&gt;sudo apt-get install vulkan-tools&lt;/code&gt;&lt;br&gt;
We need all the performance we can get for this simulation. Think of Vulkan as your nitrous system—helps CARLA run smoother and faster.&lt;/p&gt;
&lt;h4&gt;
  
  
  Download and Install XQuartz
&lt;/h4&gt;

&lt;p&gt;You can’t drive the machine if you can’t see the road. For that, we need XQuartz to get the display working on your Mac. Download it from &lt;a href="https://www.xquartz.org/" rel="noopener noreferrer"&gt;url&lt;/a&gt; and install it.&lt;/p&gt;
&lt;h4&gt;
  
  
  SSH into the VM with X11 Forwarding
&lt;/h4&gt;

&lt;p&gt;Now, you need to connect to your Linux VM from your Mac, with some special sauce (X11 forwarding). Use:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ssh -X &amp;lt;studentid&amp;gt;@host&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Export Display Port
&lt;/h4&gt;

&lt;p&gt;You’re not racing if you can’t see the track. Set the display port like this:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export DISPLAY=:0&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This ensures you’re looking at the right display—your virtual cockpit&lt;/p&gt;

&lt;h4&gt;
  
  
  Export XDG_RUNTIME
&lt;/h4&gt;

&lt;p&gt;For the system to know where to store the runtime files, do this:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export XDG_RUNTIME_DIR=/run/user/$(id -u)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Run this command to make sure everything’s running like a well-oiled machine &lt;code&gt;vulkaninfo&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  CARLA Python client
&lt;/h4&gt;

&lt;p&gt;To make sure your machine can talk to the CARLA simulator server, you're gonna need the CARLA Python client. Let’s get it installed fast, like we're revving up for a race. 🏁&lt;/p&gt;

&lt;p&gt;Run this command in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pip install carla&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Start Simulator
&lt;/h3&gt;

&lt;p&gt;Alright, you’ve done the hard work. Now it’s time to hit the gas! 🏁&lt;br&gt;
Let’s fire up CARLA and see what this machine can do. Run the command below, and watch the magic happen. 🚗💨&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;./CarlaUE4.sh -vulkan&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Boom, you're in the driver’s seat. CARLA’s about to show you what real simulation looks like. Let’s roll! 👊&lt;/p&gt;

&lt;p&gt;If you hit an error saying it can't access the display, it's like your car's stuck in the garage and the door won’t open. 🚗💥 Time to unlock that door with:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;xhost +local:root&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;if you're still facing display issues, let's put the pedal to the metal and get creative. 💥&lt;br&gt;
Hit it with this command:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export DISPLAY=localhost:10.0&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Connect to Your Cloud VM Using VNC
&lt;/h4&gt;

&lt;p&gt;we need to get that simulator running on the cloud VM and beam it over to your local machine. Think of it like driving a car in the clouds and watching it on your own screen. &lt;/p&gt;

&lt;p&gt;We’re gonna use SSH tunneling to forward the port where the CARLA simulator is running in the VM to your local screen. It's like connecting the engine to the wheels. 🏎️&lt;br&gt;
&lt;code&gt;ssh -L &amp;lt;VM_simulator_port&amp;gt;:localhost:5901 &amp;lt;your_vm_username&amp;gt;@&amp;lt;vm_ip_address&amp;gt;&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Now, you should be seeing the CARLA simulator on your local screen. It’s like you’ve got the wheel in your hands while the car’s on the cloud. 🌩️&lt;/p&gt;

&lt;h3&gt;
  
  
  What You'll See 🌟
&lt;/h3&gt;

&lt;p&gt;Just like how I always say, “I live my life a quarter mile at a time,” with CARLA, you can take your simulations step by step. It’s not just about fast cars—it’s about using the right tools to get things done the right way. So, whether you’re testing driving policies, training perception algorithms, or just want to feel the thrill of autonomous driving, CARLA’s your ticket.&lt;/p&gt;

&lt;p&gt;Now go out there, hit the gas, and get started with CARLA. It’s a wild ride, but you’ve got the skills to drive it home. 🏁&lt;/p&gt;

&lt;p&gt;Family’s got your back!&lt;/p&gt;




&lt;p&gt;Hold tight because, in the next blog, we’ll dive into adding cars, walkers, and more into CARLA. And who better to take the wheel for that than my close friend, right-hand man, and brother, Brian O’Conner? &lt;/p&gt;

&lt;p&gt;Stay tuned—he’s up next. 🔥&lt;br&gt;
Dominic Toretto, signing off. 🏎️💨&lt;br&gt;
check &lt;a href="https://dev.to/mitanshgor/carla-simulator-2-welcome-to-the-ride-brian-cooper-here-4jbi"&gt;Blog 2&lt;/a&gt; of Carla Simulator.&lt;/p&gt;

</description>
      <category>carla</category>
      <category>simulation</category>
      <category>linux</category>
      <category>ssh</category>
    </item>
    <item>
      <title>Carla Simulator 2 : Welcome to the Ride 🚗🏍️</title>
      <dc:creator>Mitansh Gor</dc:creator>
      <pubDate>Fri, 20 Dec 2024 22:03:50 +0000</pubDate>
      <link>https://dev.to/mitanshgor/carla-simulator-2-welcome-to-the-ride-brian-cooper-here-4jbi</link>
      <guid>https://dev.to/mitanshgor/carla-simulator-2-welcome-to-the-ride-brian-cooper-here-4jbi</guid>
      <description>&lt;p&gt;check &lt;a href="https://dev.to/mitanshgor/carla-simulator-1-how-to-set-up-carla-simulator-toretto-style-3800"&gt;Blog 1&lt;/a&gt; of Carla Simulator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://drive.google.com/drive/home?dmr=1&amp;amp;ec=wgc-drive-hero-goto" rel="noopener noreferrer"&gt;Check Video&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey, family! It’s Brian here — yes, the Brian O’Conner, the gearhead who always has your back on the road or in a high-speed chase. Dom handed me the keys to this blog, saying it’s time to dive deep into some next-gen simulator tech that fuels the autonomous revolution. Thanks, Dom, for trusting me. I’ll try not to wreck it (not that I ever wreck anything, right? 😉).&lt;/p&gt;

&lt;p&gt;Dom mentioned how the CARLA simulator lets us test and train autonomous vehicles in a realistic, open-source environment. It’s got the kind of details that make even Letty go, "Damn, that’s sharp." Our mission here? To break it down for you like we would a turbocharged engine. You’ll know everything about CARLA’s actors, vehicles, and how to control them with the finesse of a perfectly-timed NOS burst. And if you stick with me, we’ll even set up a Flask API to bring this simulation to life. Let’s ride! 🏍️&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq36abon970vjwbr4g3o0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq36abon970vjwbr4g3o0.png" alt=" " width="612" height="416"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Plan for This Blog
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Actors and Blueprints 101: Understand the essentials of what makes CARLA tick, from vehicles to walkers and how blueprints play a role.&lt;/li&gt;
&lt;li&gt;Dive into Vehicle and Walker Management: Learn how actors are spawned, manipulated, and destroyed — think of it like choosing the perfect car for a street race and knowing when to swap gears.&lt;/li&gt;
&lt;li&gt;Flask API Integration: Build Flask APIs step-by-step to control CARLA’s simulation programmatically. By the end, you’ll be setting up car chases, random pedestrians, and traffic with just a few lines of code.&lt;/li&gt;
&lt;li&gt;Hands-on Examples: Showcase how to populate the simulation with vehicles and walkers using our Flask API endpoints.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ready to start? Let’s shift into gear! 🏎️&lt;/p&gt;

&lt;h3&gt;
  
  
  Actors and Blueprints 101 🎨
&lt;/h3&gt;

&lt;p&gt;In CARLA, actors are the dynamic elements that breathe life into the simulation. This includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vehicles: Cars, trucks, bikes — the heart of the simulation.&lt;/li&gt;
&lt;li&gt;Walkers: Pedestrians who roam the environment.&lt;/li&gt;
&lt;li&gt;Traffic Entities: Traffic lights and stop signs.&lt;/li&gt;
&lt;li&gt;Sensors: Cameras, LiDAR, radar, and GPS for data collection.&lt;/li&gt;
&lt;li&gt;Blueprints: The DNA of Actors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of blueprints as pre-configured settings for creating actors. CARLA’s blueprint library includes attributes like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vehicle Colors&lt;/li&gt;
&lt;li&gt;Engine Power&lt;/li&gt;
&lt;li&gt;Walker Speed&lt;/li&gt;
&lt;li&gt;Sensor Ranges&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each blueprint is customizable, letting you tweak the simulation to match real-world scenarios. It’s like choosing a Skyline’s body kit and tuning it for maximum horsepower.&lt;/p&gt;

&lt;h3&gt;
  
  
  Managing Actors: Spawning, Interacting, and Destroying
&lt;/h3&gt;

&lt;p&gt;In CARLA:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spawning: Use the spawn_actor API to bring vehicles and walkers into the world.&lt;/li&gt;
&lt;li&gt;Interacting: Control the physics, speed, and direction of actors.&lt;/li&gt;
&lt;li&gt;Destroying: Remove actors once their role in the simulation is done.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Vehicle Physics 🏎️⚡&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Vehicles in CARLA come equipped with:&lt;/li&gt;
&lt;li&gt;Dynamic Control: Accelerate, brake, and steer.&lt;/li&gt;
&lt;li&gt;Collision Response: Simulates crashes and obstacle impacts.&lt;/li&gt;
&lt;li&gt;Environmental Response: Handles weather and road conditions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Walker Dynamics 🚶‍♂️&lt;br&gt;
Walkers can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Follow predefined paths.&lt;/li&gt;
&lt;li&gt;React dynamically to traffic.&lt;/li&gt;
&lt;li&gt;Walk at various speeds and animations.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Flask API Integration: Building the Engine 🚒
&lt;/h3&gt;

&lt;p&gt;Now that we’ve got the basics, it’s time to build a Flask API to interact with CARLA. Here’s how:&lt;/p&gt;
&lt;h3&gt;
  
  
  Flask API Endpoints
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Spawn Vehicles
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/spawn_vehicle', methods=['POST'])
def spawn_vehicle():
    data = request.get_json()
    model = data.get('model', 'vehicle.tesla.model3')

    blueprint_library = world.get_blueprint_library()
    vehicle_bp = blueprint_library.filter(model)[0]

    spawn_points = world.get_map().get_spawn_points()
    if not spawn_points:
        return jsonify({'error': 'No spawn points available'}), 400

    spawn_point = random.choice(spawn_points)
    vehicle = world.spawn_actor(vehicle_bp, spawn_point)

    return jsonify({'message': 'Vehicle spawned', 'id': vehicle.id})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;✨📸 Setting Up the Sensor Cameras&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To capture images from a vehicle's top view and front view, two cameras are mounted on the car. Here's the setup:&lt;/p&gt;

&lt;p&gt;The code retrieves camera blueprints (sensor.camera.rgb) from CARLA's blueprint library. Resolution settings (800x600) are defined for the cameras to ensure clear visuals. 📷&lt;/p&gt;

&lt;p&gt;Top Camera: Positioned directly above the car to capture a bird's-eye view.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff25ucce8b8x4z4bmiev8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff25ucce8b8x4z4bmiev8.png" alt=" " width="390" height="220"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Front Camera: Placed at the front of the car for a forward-facing perspective&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnpvkt0fxm4xdyyggign6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnpvkt0fxm4xdyyggign6.png" alt=" " width="800" height="346"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;camera_spawn_pointTop = carla.Transform(
    carla.Location(x=0.0, y=0.0, z=5.0),  # 5 meters above the car
    carla.Rotation(pitch=-90.0)  # Looking straight down
)
cameraTop = world.spawn_actor(camera_bp1, camera_spawn_pointTop, attach_to=vehicle)

camera_spawn_pointFront = carla.Transform(
    carla.Location(x=-5.0, y=0.0, z=3.0),  # 5 meters in front and 3 meters high
    carla.Rotation(pitch=-15.0)  # Slight downward tilt
)
cameraFront = world.spawn_actor(camera_bp2, camera_spawn_pointFront, attach_to=vehicle)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each camera is set up with a callback function (e.g., camera_callback1) to process the captured images. These images are saved as files and encoded in Base64 for easy transfer and storage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def camera_callback1(image, id, vehicleId, pos):
    array = image.raw_data
    img = Image.frombytes("RGBA", (image.width, image.height), bytes(array))
    img = img.convert("RGB")
    img.save(f"./images/{id}_{vehicleId}_{pos}.png", format="PNG")

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;🎥 Spectator Camera: Follow the Car
To make the simulation more immersive, a spectator camera is configured to follow the vehicle dynamically. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A spectator camera is moved relative to the car's position using the set_transform method.&lt;br&gt;
It stays a few meters behind and slightly above the car, aligned with its orientation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bumatjat1bpb7y8jj32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2bumatjat1bpb7y8jj32.png" alt=" " width="800" height="468"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def setup_spectator_camera(world, vehicle):
    vehicle_transform = vehicle.get_transform()  # Get the car's position and rotation
    spectator = world.get_spectator()  # Access the spectator camera

    spectator_transform = carla.Transform(
        vehicle_transform.location + carla.Location(x=-6, y=0, z=2),  # Offset behind and above the car
        vehicle_transform.rotation  # Match the car's orientation
    )
    spectator.set_transform(spectator_transform)  # Update the spectator's position

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The spectator camera is continually updated in a loop, ensuring it tracks the car as it moves. This creates a realistic following effect.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spawn Walkers
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/spawn_walker', methods=['POST'])
def spawn_walker():
    data = request.get_json()
    walker_count = data.get('count', 1)

    walker_blueprints = world.get_blueprint_library().filter('walker.pedestrian.*')
    spawn_points = [random.choice(world.get_map().get_spawn_points()) for _ in range(walker_count)]

    walkers = []
    for spawn_point in spawn_points:
        walker_bp = random.choice(walker_blueprints)
        walker = world.spawn_actor(walker_bp, spawn_point)
        walkers.append(walker)

    return jsonify({'message': f'{walker_count} walkers spawned', 'ids': [w.id for w in walkers]})

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Destroy All Actors
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/destroy_actors', methods=['POST'])
def destroy_actors():
    actors = world.get_actors()
    actors_to_destroy = [actor for actor in actors if actor.type_id.startswith('vehicle') or actor.type_id.startswith('walker')]

    for actor in actors_to_destroy:
        actor.destroy()

    return jsonify({'message': 'All actors destroyed'})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add Random Vehicles and Walkers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3pfcp0lm2zea7go9mar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu3pfcp0lm2zea7go9mar.png" alt=" " width="400" height="225"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@app.route('/populate_simulation', methods=['POST'])
def populate_simulation():
    data = request.get_json()
    vehicle_count = data.get('vehicles', 10)
    walker_count = data.get('walkers', 10)

    # Spawn Vehicles
    for _ in range(vehicle_count):
        vehicle_bp = random.choice(world.get_blueprint_library().filter('vehicle.*'))
        spawn_point = random.choice(world.get_map().get_spawn_points())
        world.spawn_actor(vehicle_bp, spawn_point)

    # Spawn Walkers
    for _ in range(walker_count):
        walker_bp = random.choice(world.get_blueprint_library().filter('walker.pedestrian.*'))
        spawn_point = random.choice(world.get_map().get_spawn_points())
        world.spawn_actor(walker_bp, spawn_point)

    return jsonify({'message': f'{vehicle_count} vehicles and {walker_count} walkers added'})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wrapping Up 🏎️&lt;/p&gt;

&lt;p&gt;And that’s how you take control of CARLA with Flask! Whether you’re simulating a high-speed chase or a casual Sunday drive, these tools let you shape your virtual world. Stay tuned for more deep dives and, as always, drive safe — or at least make it look cool while you’re not. 🚗🚨&lt;/p&gt;

</description>
      <category>carla</category>
      <category>flask</category>
      <category>python</category>
      <category>simulation</category>
    </item>
  </channel>
</rss>
