<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <id>https://kb.bargady.online/blog</id>
    <title>Ahmed BARGADY | Knowledge Base Blog</title>
    <updated>2026-05-26T00:00:00.000Z</updated>
    <generator>https://github.com/jpmonette/feed</generator>
    <link rel="alternate" href="https://kb.bargady.online/blog"/>
    <subtitle>Ahmed BARGADY | Knowledge Base Blog</subtitle>
    <icon>https://kb.bargady.online/img/logo.png</icon>
    <entry>
        <title type="html"><![CDATA[Bypassing the Security Gates: Overcoming SSL Mismatches, OAuth Errors, and Google's Malware Scanner in a Self-Hosted n8n Research Pipeline]]></title>
        <id>https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline</id>
        <link href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline"/>
        <updated>2026-05-26T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[Bypassing the Security Gates]]></summary>
        <content type="html"><![CDATA[<p><img decoding="async" loading="lazy" alt="Bypassing the Security Gates" src="https://kb.bargady.online/assets/images/n8n-security-pipeline-9836168d857b0d059b07c66b132f1124.png" width="1024" height="1024" class="img_ev3q"></p>
<p>Automating academic research workflows should be a straightforward task: trigger a webhook on a file update, pull the document from cloud storage, feed it to a Large Language Model (LLM) agent for deep analysis, commit the output to a repository, and notify the team.</p>
<p>However, when your academic focus is <strong>Advanced Persistent Threat (APT) Detection</strong>, <strong>Malware Analysis</strong>, and <strong>Systems Security</strong>, your pipeline is virtually guaranteed to collide with aggressive, automated cloud defenses.</p>
<p>This is a comprehensive technical write-up detailing how we built an advanced research summarization pipeline using self-hosted n8n (v2.21.7) on Docker, the infrastructure roadblocks we encountered across transport, application, and storage layers, and the engineering workarounds used to resolve them.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-the-workflow-architecture">1. The Workflow Architecture<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#1-the-workflow-architecture" class="hash-link" aria-label="Direct link to 1. The Workflow Architecture" title="Direct link to 1. The Workflow Architecture" translate="no">​</a></h2>
<p>The objective of this pipeline is to ingest newly published academic security research, synthesize the core threat models using generative AI, store the resulting Markdown reports on GitHub, and broadcast summary cards to Slack.</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">┌───────────────┐      ┌─────────────┐      ┌───────────────┐</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">│  WebHook/API  ├─────►│ Google Drive├─────►│ HTTP Get Node │</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">│    Trigger    │      │  File List  │      │(Abuse Bypass) │</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">└───────────────┘      └─────────────┘      └───────┬───────┘</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">                                                    │ (Stream Binary)</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">                                                    ▼</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">┌───────────────┐      ┌─────────────┐      ┌───────────────┐</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">│ GitHub Commit ◄──────┤ Gemini Agent◄──────┤  Extract text │</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">│ &amp; Slack Alert │      │ (Pro/Flash) │      │   from PDF    │</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">└───────────────┘      └─────────────┘      └───────────────┘</span><br></span></code></pre></div></div>
<p>The system runs inside a self-hosted Docker container routed through a permanent Cloudflare Tunnel mapping local port <code>5678</code> to our public custom subdomain <code>https://n8n.yourdomain.com</code>.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-the-troubleshooting-journey-battles--breakthroughs">2. The Troubleshooting Journey: Battles &amp; Breakthroughs<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#2-the-troubleshooting-journey-battles--breakthroughs" class="hash-link" aria-label="Direct link to 2. The Troubleshooting Journey: Battles &amp; Breakthroughs" title="Direct link to 2. The Troubleshooting Journey: Battles &amp; Breakthroughs" translate="no">​</a></h2>
<p>Getting this system to execute seamlessly required resolving multiple independent failure points. Below is the detailed post-mortem of each technical struggle.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="battle-1-the-local-docker-vs-cloudflare-ssl-interstitial">Battle 1: The Local Docker vs. Cloudflare SSL Interstitial<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#battle-1-the-local-docker-vs-cloudflare-ssl-interstitial" class="hash-link" aria-label="Direct link to Battle 1: The Local Docker vs. Cloudflare SSL Interstitial" title="Direct link to Battle 1: The Local Docker vs. Cloudflare SSL Interstitial" translate="no">​</a></h3>
<p>Our first hurdle appeared during the initial Google OAuth2 handshake. Whenever we attempted to connect our Google Drive credential inside n8n, the callback window threw a fatal error:</p>
<div class="language-text codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-text codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">Error: The OAuth callback state is invalid!</span><br></span></code></pre></div></div>
<p>At the same time, normal browser windows attempting to access the dashboard were met with a red "Dangerous Site" interstitial page warning that the connection was insecure.</p>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-root-cause">The Root Cause<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#the-root-cause" class="hash-link" aria-label="Direct link to The Root Cause" title="Direct link to The Root Cause" translate="no">​</a></h4>
<p>n8n utilizes highly secure tracking parameters (a <code>state</code> parameter cached in the browser's cookies) to prevent Cross-Site Request Forgery (CSRF) attacks during external authorization loops.</p>
<p>Because n8n was running locally over basic HTTP inside the Docker container while Cloudflare routed it externally via HTTPS, a mismatch occurred. The browser's security policies aggressively stripped or altered secure cookie contexts on unverified HTTP-origin redirects. The browser cached the old insecure HTTP state and refused to validate the new secure proxy certificate, resulting in corrupted authentication states.</p>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-solution">The Solution<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#the-solution" class="hash-link" aria-label="Direct link to The Solution" title="Direct link to The Solution" translate="no">​</a></h4>
<ul>
<li class=""><strong>Cloudflare Policy Alignments:</strong> We ensured that the Cloudflare domain's global SSL/TLS encryption setting was set to <strong>Full (Strict)</strong>, forcing valid public edge certificates to match the browser's expected state.</li>
<li class=""><strong>Purging the Browser's Security Cache:</strong> We cleared the underlying browser security ledger by navigating to <code>chrome://net-internals/#hsts</code> (for Chrome-based browsers). Under the <em>Delete domain security policies</em> form, we targeted <code>n8n.yourdomain.com</code> and cleared its state.</li>
<li class=""><strong>Incognito Handshake:</strong> By logging into n8n via a clean <strong>Incognito Session</strong>, we bypassed standard profile memory. This generated a completely pristine, matched state token, immediately turning our credential connection indicator a secure, solid green.</li>
</ul>
<hr>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="battle-2-the-google-drive-403-forbidden-api-wall">Battle 2: The Google Drive 403 Forbidden API Wall<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#battle-2-the-google-drive-403-forbidden-api-wall" class="hash-link" aria-label="Direct link to Battle 2: The Google Drive 403 Forbidden API Wall" title="Direct link to Battle 2: The Google Drive 403 Forbidden API Wall" translate="no">​</a></h3>
<p>With the domain secured and authenticated, the pipeline successfully pulled file lists but failed immediately on the file download action:</p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"status"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"rejected"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"reason"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"Forbidden - perhaps check your credentials? Request failed with status code 403"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><br></span></code></pre></div></div>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-root-cause-1">The Root Cause<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#the-root-cause-1" class="hash-link" aria-label="Direct link to The Root Cause" title="Direct link to The Root Cause" translate="no">​</a></h4>
<p>Initially, we suspected a data modeling issue where n8n was passing a parent Folder ID instead of an individual File ID. However, verifying our JSON schema showed that n8n was successfully mapping a valid file ID (<code>1A2b3C4d5E6f7G8h9I0j_SampleFileID</code>) for the target paper <code>apt_detection_paper.pdf</code>.</p>
<p>The file's general sharing permissions on Google Drive were already set to "Anyone with the link can edit" — yet the API consistently returned a hard 403.</p>
<p>The breakthrough came when inspecting the raw JSON response payload returned directly by Google's backend servers:</p>
<div class="language-json codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-json codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token property">"error"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token property">"code"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">403</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token property">"message"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"This file has been identified as malware or spam and cannot be downloaded."</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token property">"errors"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">      </span><span class="token punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        </span><span class="token property">"domain"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"global"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        </span><span class="token property">"reason"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"cannotDownloadAbusiveFile"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        </span><span class="token property">"message"</span><span class="token operator" style="color:rgb(137, 221, 255)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"This file has been identified as malware or spam and cannot be downloaded."</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">      </span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token punctuation" style="color:rgb(199, 146, 234)">}</span><br></span></code></pre></div></div>
<p>Because <code>apt_detection_paper.pdf</code> is an academic paper on APT detection and network defense, it contains explicit exploit sequences, code structures, and attack diagrams. Google Drive's automated static-analysis scanners flagged these patterns as active malware.</p>
<p>When an automated script requests a file flagged for abuse via the standard <code>files.get</code> endpoint, Google’s API flatly denies the binary stream unless an explicit security override parameter is supplied.</p>
<hr>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="battle-3-overcoming-ui-limitations-with-raw-rest-http">Battle 3: Overcoming UI Limitations with Raw REST HTTP<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#battle-3-overcoming-ui-limitations-with-raw-rest-http" class="hash-link" aria-label="Direct link to Battle 3: Overcoming UI Limitations with Raw REST HTTP" title="Direct link to Battle 3: Overcoming UI Limitations with Raw REST HTTP" translate="no">​</a></h3>
<p>We wanted to configure the native Google Drive node to bypass this block, but n8n's UI was too restrictive:</p>
<ul>
<li class="">n8n's native Google Drive Download node limits options to <em>Put Output File in Field</em>, <em>Google File Conversion</em>, and <em>File Name</em>.</li>
<li class="">It provides no interface parameter to inject raw query-string parameters directly to the API endpoint request.</li>
</ul>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-solution-swapping-to-an-http-request-node">The Solution: Swapping to an HTTP Request Node<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#the-solution-swapping-to-an-http-request-node" class="hash-link" aria-label="Direct link to The Solution: Swapping to an HTTP Request Node" title="Direct link to The Solution: Swapping to an HTTP Request Node" translate="no">​</a></h4>
<p>To bypass the UI constraint, we removed the native Google Drive node and replaced it with a generic, high-flexibility <strong>HTTP Request Node</strong> linked directly to the REST API endpoints.</p>
<p>We configured the custom HTTP node as follows:</p>
<table><thead><tr><th style="text-align:left">Parameter</th><th style="text-align:left">Configuration Value</th></tr></thead><tbody><tr><td style="text-align:left"><strong>Method</strong></td><td style="text-align:left">GET</td></tr><tr><td style="text-align:left"><strong>URL</strong></td><td style="text-align:left"><code>https://www.googleapis.com/drive/v3/files/{{ $json.id }}?alt=media&amp;acknowledgeAbuse=true</code></td></tr><tr><td style="text-align:left"><strong>Authentication</strong></td><td style="text-align:left">Predefined Credential Type</td></tr><tr><td style="text-align:left"><strong>Credential Type</strong></td><td style="text-align:left">Google Drive OAuth2 API</td></tr><tr><td style="text-align:left"><strong>Credential Link</strong></td><td style="text-align:left">Select existing authenticated Google Drive Account</td></tr><tr><td style="text-align:left"><strong>Response Format</strong></td><td style="text-align:left">File <em>(Instructs n8n to parse the incoming buffer as a binary stream)</em></td></tr><tr><td style="text-align:left"><strong>Put Output in Field</strong></td><td style="text-align:left"><code>data</code></td></tr></tbody></table>
<blockquote>
<p>[!NOTE]
During setup, we caught a syntax error where an accidental extra symbol was introduced inside the dynamic bracket syntax. Making sure the evaluation preview resolved to a clean path without rogue characters was key to establishing the connection.</p>
</blockquote>
<p>By explicitly appending <code>acknowledgeAbuse=true</code> and setting <code>alt=media</code>, Google's API recognized our automated bypass statement, cleared the security barrier, and allowed the academic binary payload to stream cleanly onto our canvas.</p>
<hr>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="3-key-technical-takeaways">3. Key Technical Takeaways<a href="https://kb.bargady.online/blog/bypassing-security-gates-n8n-research-pipeline#3-key-technical-takeaways" class="hash-link" aria-label="Direct link to 3. Key Technical Takeaways" title="Direct link to 3. Key Technical Takeaways" translate="no">​</a></h2>
<ul>
<li class=""><strong>Verify Raw Response Payloads:</strong> A generic node error like <code>403 Forbidden</code> can mean many things. Whenever an integration fails, inspect the raw JSON string response or write a simple script to verify the exact string message sent by the host API.</li>
<li class=""><strong>Beware of Security Papers and AI Pipelines:</strong> Automated antivirus and static analysis engines in public cloud platforms (Google Workspace, Microsoft OneDrive) aggressively flag security-related research, exploit logs, and code-heavy PDF payloads. Always build an escape hatch into your data ingest layers.</li>
<li class=""><strong>Keep the HTTP Request Node as a Secret Weapon:</strong> Native integration nodes in automation suites are highly efficient, but they often abstract away lower-level parameter manipulations. Knowing when to fall back to a raw HTTP client with predefined OAuth context saves hours of integration deadlock.</li>
</ul>
<p>The research pipeline is now fully active, securely processing state-of-the-art security manuscripts, extracting intelligence, and updating our collaborative environments in real-time!</p>]]></content>
        <author>
            <name>Ahmed BARGADY</name>
        </author>
        <category label="n8n" term="n8n"/>
        <category label="Self-Hosted" term="Self-Hosted"/>
        <category label="Cloudflare" term="Cloudflare"/>
        <category label="Docker" term="Docker"/>
        <category label="Google Drive" term="Google Drive"/>
        <category label="OAuth2" term="OAuth2"/>
        <category label="Security Pipeline" term="Security Pipeline"/>
        <category label="Troubleshooting" term="Troubleshooting"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Running AutoResearch by Andrej Karpathy Locally on Mac Mini M4 with Ollama]]></title>
        <id>https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4</id>
        <link href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4"/>
        <updated>2026-04-02T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[AutoResearch on Mac Mini M4]]></summary>
        <content type="html"><![CDATA[<p><img decoding="async" loading="lazy" alt="AutoResearch on Mac Mini M4" src="https://kb.bargady.online/assets/images/autoresearch-hero-2830b65fc5ad1cd005b960b02f59bac4.png" width="1024" height="1024" class="img_ev3q"></p>
<p>Andrej Karpathy recently released <strong>AutoResearch</strong>, an open-source tool that fundamentally changes how we approach machine learning experiments. Instead of manually tweaking code and hyperparameters, AutoResearch shifts the researcher's role to orchestrating high-level goals while an AI agent handles the repetitive loop of proposing, implementing, and evaluating ML experiments.</p>
<p>In this post, I'll walk you through how to set up and run AutoResearch locally on your Mac Mini M4 using <strong>Ollama</strong> and local LLMs.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-is-autoresearch">What is AutoResearch?<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#what-is-autoresearch" class="hash-link" aria-label="Direct link to What is AutoResearch?" title="Direct link to What is AutoResearch?" translate="no">​</a></h2>
<p>AutoResearch is an autonomous agent framework. You define a research direction in a <code>program.md</code> file, and the agent takes over:</p>
<ol>
<li class="">It reads the goal and modifies your training script (e.g., <code>train.py</code>).</li>
<li class="">It runs a fixed-time training loop (usually around 5 minutes).</li>
<li class="">It evaluates the validation metrics.</li>
<li class="">If the new code improves performance, it commits the changes via Git. If not, it reverts them.</li>
</ol>
<p>Originally designed for powerful cloud GPUs and premium APIs like Claude or GPT-4, the community has quickly adapted it to run on local hardware.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="why-mac-mini-m4-and-ollama">Why Mac Mini M4 and Ollama?<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#why-mac-mini-m4-and-ollama" class="hash-link" aria-label="Direct link to Why Mac Mini M4 and Ollama?" title="Direct link to Why Mac Mini M4 and Ollama?" translate="no">​</a></h2>
<p>The Mac Mini M4, especially with 16GB or 32GB of Unified Memory, is an excellent machine for running local, quantized LLMs. By combining it with Ollama, you get:</p>
<ul>
<li class=""><strong>Zero API Costs</strong>: Run your experiments 24/7 without worrying about skyrocketing API bills.</li>
<li class=""><strong>Data Privacy</strong>: Your proprietary code and datasets never leave your machine.</li>
<li class=""><strong>"Agentic Engineering"</strong>: You get hands-on experience with managing an AI researcher on your own hardware.</li>
</ul>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="prerequisites">Prerequisites<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#prerequisites" class="hash-link" aria-label="Direct link to Prerequisites" title="Direct link to Prerequisites" translate="no">​</a></h2>
<p>Before we start, make sure you have the following installed on your Mac:</p>
<ul>
<li class=""><strong>Git</strong>: Pre-installed on macOS or available via <code>brew install git</code>.</li>
<li class=""><strong>Homebrew</strong>: The standard macOS package manager.</li>
<li class=""><strong>Ollama</strong>: For running local LLMs.</li>
</ul>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-install-ollama">1. Install Ollama<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#1-install-ollama" class="hash-link" aria-label="Direct link to 1. Install Ollama" title="Direct link to 1. Install Ollama" translate="no">​</a></h3>
<p>If you don't have Ollama installed, you can easily get it via Homebrew:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">brew </span><span class="token function" style="color:rgb(130, 170, 255)">install</span><span class="token plain"> ollama</span><br></span></code></pre></div></div>
<p>Once installed, start the Ollama service:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">brew services start ollama</span><br></span></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-pull-a-capable-model">2. Pull a Capable Model<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#2-pull-a-capable-model" class="hash-link" aria-label="Direct link to 2. Pull a Capable Model" title="Direct link to 2. Pull a Capable Model" translate="no">​</a></h3>
<p>For an agentic coding task, you need a model that excels at reasoning and generating Python code. Models like <code>qwen2.5-coder</code> or <code>llama3</code> are great starting points.</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">ollama pull qwen2.5-coder</span><br></span></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="3-install-uv-optional-but-recommended">3. Install <code>uv</code> (Optional but Recommended)<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#3-install-uv-optional-but-recommended" class="hash-link" aria-label="Direct link to 3-install-uv-optional-but-recommended" title="Direct link to 3-install-uv-optional-but-recommended" translate="no">​</a></h3>
<p><code>uv</code> is a blazingly fast Python package manager written in Rust. It makes setting up the environment much smoother.</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token function" style="color:rgb(130, 170, 255)">curl</span><span class="token plain"> </span><span class="token parameter variable" style="color:rgb(191, 199, 213)">-LsSf</span><span class="token plain"> https://astral.sh/uv/install.sh </span><span class="token operator" style="color:rgb(137, 221, 255)">|</span><span class="token plain"> </span><span class="token function" style="color:rgb(130, 170, 255)">sh</span><br></span></code></pre></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-by-step-setup">Step-by-Step Setup<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#step-by-step-setup" class="hash-link" aria-label="Direct link to Step-by-Step Setup" title="Direct link to Step-by-Step Setup" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-1-clone-the-repository">Step 1: Clone the Repository<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#step-1-clone-the-repository" class="hash-link" aria-label="Direct link to Step 1: Clone the Repository" title="Direct link to Step 1: Clone the Repository" translate="no">​</a></h3>
<p>You can clone the original repository or look for a community fork optimized for local macOS execution.</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token function" style="color:rgb(130, 170, 255)">git</span><span class="token plain"> clone https://github.com/karpathy/autoresearch.git</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token builtin class-name" style="color:rgb(255, 203, 107)">cd</span><span class="token plain"> autoresearch</span><br></span></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-2-initialize-the-environment">Step 2: Initialize the Environment<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#step-2-initialize-the-environment" class="hash-link" aria-label="Direct link to Step 2: Initialize the Environment" title="Direct link to Step 2: Initialize the Environment" translate="no">​</a></h3>
<p>Set up your Python virtual environment and install the required dependencies. Using <code>uv</code>, this is incredibly fast:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">uv venv</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token builtin class-name" style="color:rgb(255, 203, 107)">source</span><span class="token plain"> .venv/bin/activate</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">uv pip </span><span class="token function" style="color:rgb(130, 170, 255)">install</span><span class="token plain"> </span><span class="token parameter variable" style="color:rgb(191, 199, 213)">-r</span><span class="token plain"> requirements.txt</span><br></span></code></pre></div></div>
<p><em>(Note: Depending on the specific fork or updates, you might need to install additional packages like <code>litellm</code> to route requests to Ollama.)</em></p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-3-configure-the-local-agent">Step 3: Configure the Local Agent<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#step-3-configure-the-local-agent" class="hash-link" aria-label="Direct link to Step 3: Configure the Local Agent" title="Direct link to Step 3: Configure the Local Agent" translate="no">​</a></h3>
<p>The original code might default to OpenAI or Anthropic. You'll need to point the agent to your local Ollama instance. If the codebase uses LiteLLM or a similar router, set your environment variables to route traffic to localhost:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token builtin class-name" style="color:rgb(255, 203, 107)">export</span><span class="token plain"> </span><span class="token assign-left variable" style="color:rgb(191, 199, 213)">OPENAI_API_BASE</span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token string" style="color:rgb(195, 232, 141)">"http://localhost:11434/v1"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token builtin class-name" style="color:rgb(255, 203, 107)">export</span><span class="token plain"> </span><span class="token assign-left variable" style="color:rgb(191, 199, 213)">OPENAI_API_KEY</span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token string" style="color:rgb(195, 232, 141)">"ollama"</span><span class="token plain"> </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Dummy key</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token builtin class-name" style="color:rgb(255, 203, 107)">export</span><span class="token plain"> </span><span class="token assign-left variable" style="color:rgb(191, 199, 213)">MODEL</span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token string" style="color:rgb(195, 232, 141)">"qwen2.5-coder"</span><span class="token plain"> </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Or whatever model you pulled</span><br></span></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-4-define-your-research-goal">Step 4: Define Your Research Goal<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#step-4-define-your-research-goal" class="hash-link" aria-label="Direct link to Step 4: Define Your Research Goal" title="Direct link to Step 4: Define Your Research Goal" translate="no">​</a></h3>
<p>Open <code>program.md</code> in your favorite editor. This is where you tell the agent what to do. Keep it focused and provide clear, measurable constraints.</p>
<p>Example <code>program.md</code>:</p>
<div class="language-markdown codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-markdown codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token title important punctuation" style="color:rgb(199, 146, 234)">#</span><span class="token title important"> Goal</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">Improve the validation loss of the simple CNN in </span><span class="token code-snippet code keyword" style="font-style:italic">`train.py`</span><span class="token plain"> on the MNIST dataset.</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token title important punctuation" style="color:rgb(199, 146, 234)">#</span><span class="token title important"> Constraints</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token list punctuation" style="color:rgb(199, 146, 234)">-</span><span class="token plain"> Do not increase the training time beyond 5 minutes per run.</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token list punctuation" style="color:rgb(199, 146, 234)">-</span><span class="token plain"> You may modify the architecture, learning rate, or optimizer.</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token list punctuation" style="color:rgb(199, 146, 234)">-</span><span class="token plain"> Log the validation loss at the end of the script for evaluation.</span><br></span></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-5-start-the-loop">Step 5: Start the Loop!<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#step-5-start-the-loop" class="hash-link" aria-label="Direct link to Step 5: Start the Loop!" title="Direct link to Step 5: Start the Loop!" translate="no">​</a></h3>
<p>Run the orchestrator script. The exact command depends on the repository structure, but it usually looks like this:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">python main.py</span><br></span></code></pre></div></div>
<p>Now, sit back and watch. Your Mac Mini M4 will use the local LLM to generate code changes, execute the training script, and commit improvements.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="important-considerations-for-m4-users">Important Considerations for M4 Users<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#important-considerations-for-m4-users" class="hash-link" aria-label="Direct link to Important Considerations for M4 Users" title="Direct link to Important Considerations for M4 Users" translate="no">​</a></h2>
<ol>
<li class=""><strong>Memory Management</strong>: Running an LLM (the researcher) and training a model simultaneously consumes a lot of Unified Memory. If you only have 16GB, stick to smaller models (e.g., 7B or 8B parameters) and lightweight training tasks.</li>
<li class=""><strong>Throughput</strong>: Your Mac won't match an H100 GPU cluster. The agent's iteration loop will be slower, but it's perfect for overnight runs.</li>
<li class=""><strong>The Evaluation Metric</strong>: The agent relies entirely on the metric you define. Ensure your <code>train.py</code> outputs a clean, parseable score (like a final validation loss) for the agent to read.</li>
</ol>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="conclusion">Conclusion<a href="https://kb.bargady.online/blog/autoresearch-karpathy-mac-mini-m4#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion" translate="no">​</a></h2>
<p>Running AutoResearch locally on a Mac Mini M4 with Ollama is a fantastic way to explore the future of AI-driven ML development without incurring cloud costs. By delegating the repetitive code-test-evaluate loop to an agent, you can focus on the bigger picture: asking the right questions and defining better research constraints.</p>
<p>Happy (automated) researching!</p>]]></content>
        <author>
            <name>Ahmed BARGADY</name>
        </author>
        <category label="Artificial Intelligence" term="Artificial Intelligence"/>
        <category label="Machine Learning" term="Machine Learning"/>
        <category label="AutoResearch" term="AutoResearch"/>
        <category label="Mac Mini M4" term="Mac Mini M4"/>
        <category label="Ollama" term="Ollama"/>
        <category label="Karpathy" term="Karpathy"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Understanding the PRISMA Method]]></title>
        <id>https://kb.bargady.online/blog/prisma-method-guide</id>
        <link href="https://kb.bargady.online/blog/prisma-method-guide"/>
        <updated>2026-01-08T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[PRISMA stands for Preferred Reporting Items for Systematic Reviews and Meta-Analyses.]]></summary>
        <content type="html"><![CDATA[<div class="theme-admonition theme-admonition-info admonition_xJq3 alert alert--info"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>What is PRISMA?</div><div class="admonitionContent_BuS1"><p>PRISMA stands for Preferred Reporting Items for Systematic Reviews and Meta-Analyses.</p><p>It is an evidence-based minimum set of items for reporting in systematic reviews and meta-analyses. While it primarily focuses on reviews evaluating randomized trials, it enables transparent and complete reporting for diverse types of research.</p></div></div>
<p>As a PhD student, your <strong>"State of the Art" (SOTA)</strong> is not just a summary of what you read; it is a scientific study in itself. PRISMA ensures your SOTA is transparent, reproducible, and unbiased.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="why-your-supervisor-wants-you-to-use-it">Why Your Supervisor Wants You to Use It<a href="https://kb.bargady.online/blog/prisma-method-guide#why-your-supervisor-wants-you-to-use-it" class="hash-link" aria-label="Direct link to Why Your Supervisor Wants You to Use It" title="Direct link to Why Your Supervisor Wants You to Use It" translate="no">​</a></h2>
<ol>
<li class=""><strong>Reproducibility</strong>: If another researcher uses your search keywords and criteria, they should find the exact same set of papers.</li>
<li class=""><strong>Bias Reduction</strong>: It prevents "cherry-picking" only the papers that support your hypothesis.</li>
<li class=""><strong>Quality Control</strong>: It forces you to evaluate the quality of the papers you include.</li>
</ol>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-prisma-flow-diagram-2020">The PRISMA Flow Diagram (2020)<a href="https://kb.bargady.online/blog/prisma-method-guide#the-prisma-flow-diagram-2020" class="hash-link" aria-label="Direct link to The PRISMA Flow Diagram (2020)" title="Direct link to The PRISMA Flow Diagram (2020)" translate="no">​</a></h2>
<p>The most recognizable part of PRISMA is the flow diagram. This charts the lifecycle of a paper from your initial search to the final selection.</p>
<!-- -->
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-4-phases-of-the-flow">The 4 Phases of the Flow<a href="https://kb.bargady.online/blog/prisma-method-guide#the-4-phases-of-the-flow" class="hash-link" aria-label="Direct link to The 4 Phases of the Flow" title="Direct link to The 4 Phases of the Flow" translate="no">​</a></h2>
<ol>
<li class=""><strong>Identification</strong>: You search databases (IEEE, PubMed, Scopus). You note down the exact number of results.</li>
<li class=""><strong>Screening</strong>:<!-- -->
<ul>
<li class=""><strong>Deduplication</strong>: Removing the same paper found in multiple databases.</li>
<li class=""><strong>Title/Abstract Screening</strong>: Quickly rejecting irrelevant papers (e.g., wrong topic).</li>
</ul>
</li>
<li class=""><strong>Eligibility</strong>: You read the full text of remaining papers. You exclude papers that don't meet specific criteria (e.g., "Not written in English" or "Review articles only").</li>
<li class=""><strong>Included</strong>: The final set of papers you will analyze in your thesis.</li>
</ol>
<div class="theme-admonition theme-admonition-tip admonition_xJq3 alert alert--success"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>PhD Pro-Tip</div><div class="admonitionContent_BuS1"><p>Start a spreadsheet or use a tool (like Zotero or Excel) <strong>TODAY</strong>. You need to track the numbers as you go. Trying to reconstruct "how many papers I rejected based on title" three months later is a nightmare.</p></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="the-prisma-checklist-simplified">The PRISMA Checklist (Simplified)<a href="https://kb.bargady.online/blog/prisma-method-guide#the-prisma-checklist-simplified" class="hash-link" aria-label="Direct link to The PRISMA Checklist (Simplified)" title="Direct link to The PRISMA Checklist (Simplified)" translate="no">​</a></h2>
<p>While the flow diagram is visual, the Checklist is structural. It consists of 27 items you must include in your report.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="section-1-introduction">Section 1: Introduction<a href="https://kb.bargady.online/blog/prisma-method-guide#section-1-introduction" class="hash-link" aria-label="Direct link to Section 1: Introduction" title="Direct link to Section 1: Introduction" translate="no">​</a></h3>
<ul>
<li class=""><strong>Rationale</strong>: Describe the explicit question you are trying to answer. <em>Why does this review need to be done?</em></li>
<li class=""><strong>Objectives</strong>: Provide an explicit statement of questions being addressed with reference to participants, interventions, comparisons, outcomes, and study design (PICO).</li>
</ul>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="section-2-methods-crucial-for-reproducibility">Section 2: Methods (Crucial for reproducibility)<a href="https://kb.bargady.online/blog/prisma-method-guide#section-2-methods-crucial-for-reproducibility" class="hash-link" aria-label="Direct link to Section 2: Methods (Crucial for reproducibility)" title="Direct link to Section 2: Methods (Crucial for reproducibility)" translate="no">​</a></h3>
<p>You must document exactly how you searched.</p>
<table><thead><tr><th style="text-align:left">Item</th><th style="text-align:left">Requirement</th></tr></thead><tbody><tr><td style="text-align:left"><strong>Eligibility Criteria</strong></td><td style="text-align:left">Specify study characteristics (e.g., PICO, length of follow-up) and report characteristics (e.g., years considered, language).</td></tr><tr><td style="text-align:left"><strong>Information Sources</strong></td><td style="text-align:left">Describe all information sources (e.g., databases with dates of coverage, contact with study authors) in the search and date last searched.</td></tr><tr><td style="text-align:left"><strong>Search Strategy</strong></td><td style="text-align:left">Present full electronic search strategy for at least one database, including any limits used, such that it could be repeated.</td></tr><tr><td style="text-align:left"><strong>Selection Process</strong></td><td style="text-align:left">State the process for selecting studies (i.e., screening, eligibility, included in systematic review).</td></tr></tbody></table>
<div class="theme-admonition theme-admonition-warning admonition_xJq3 alert alert--warning"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>Do not hide your query</div><div class="admonitionContent_BuS1"><p>You must include the exact Boolean string you used.</p><p>Bad: "I searched for AI and Healthcare."</p><p>Good: "("Artificial Intelligence" OR "Machine Learning") AND ("Healthcare" OR "Clinical") limited to years 2018-2024."</p></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="section-3-results">Section 3: Results<a href="https://kb.bargady.online/blog/prisma-method-guide#section-3-results" class="hash-link" aria-label="Direct link to Section 3: Results" title="Direct link to Section 3: Results" translate="no">​</a></h3>
<ul>
<li class=""><strong>Study Selection</strong>: Give numbers of studies screened, assessed for eligibility, and included in the review, with reasons for exclusions at each stage, ideally with a flow diagram.</li>
<li class=""><strong>Study Characteristics</strong>: For each study, present characteristics for which data were extracted (e.g., study size, PICOS, follow-up period) and provide the citations.</li>
</ul>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="section-4-discussion">Section 4: Discussion<a href="https://kb.bargady.online/blog/prisma-method-guide#section-4-discussion" class="hash-link" aria-label="Direct link to Section 4: Discussion" title="Direct link to Section 4: Discussion" translate="no">​</a></h3>
<ul>
<li class=""><strong>Summary of Evidence</strong>: Summarize the main findings including the strength of evidence for each main outcome.</li>
<li class=""><strong>Limitations</strong>: Discuss limitations at study and outcome level (e.g., risk of bias), and at review-level (e.g., incomplete retrieval of identified research).</li>
</ul>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-by-step-implementation-for-your-phd">Step-by-Step Implementation for your PhD<a href="https://kb.bargady.online/blog/prisma-method-guide#step-by-step-implementation-for-your-phd" class="hash-link" aria-label="Direct link to Step-by-Step Implementation for your PhD" title="Direct link to Step-by-Step Implementation for your PhD" translate="no">​</a></h2>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-1-define-your-protocol">Step 1: Define your Protocol<a href="https://kb.bargady.online/blog/prisma-method-guide#step-1-define-your-protocol" class="hash-link" aria-label="Direct link to Step 1: Define your Protocol" title="Direct link to Step 1: Define your Protocol" translate="no">​</a></h3>
<p>Before searching, define your <strong>Inclusion</strong> and <strong>Exclusion</strong> criteria.</p>
<ul>
<li class=""><strong>Inclusion</strong>: "Papers published between 2015-2025," "Empirical studies," "English language."</li>
<li class=""><strong>Exclusion</strong>: "Opinion pieces," "White papers," "Technical reports without validation."</li>
</ul>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-2-the-search-identification">Step 2: The Search (Identification)<a href="https://kb.bargady.online/blog/prisma-method-guide#step-2-the-search-identification" class="hash-link" aria-label="Direct link to Step 2: The Search (Identification)" title="Direct link to Step 2: The Search (Identification)" translate="no">​</a></h3>
<p>Run your query in Scopus, Web of Science, Google Scholar, etc. Export all results as <code>.bib</code> or <code>.ris</code> files.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-3-deduplication">Step 3: Deduplication<a href="https://kb.bargady.online/blog/prisma-method-guide#step-3-deduplication" class="hash-link" aria-label="Direct link to Step 3: Deduplication" title="Direct link to Step 3: Deduplication" translate="no">​</a></h3>
<p>Import all <code>.bib</code> files into a reference manager (Zotero, Mendeley, or EndNote). Use their "Find Duplicates" feature. Record the number of duplicates removed.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-4-screening-the-fast-pass">Step 4: Screening (The Fast Pass)<a href="https://kb.bargady.online/blog/prisma-method-guide#step-4-screening-the-fast-pass" class="hash-link" aria-label="Direct link to Step 4: Screening (The Fast Pass)" title="Direct link to Step 4: Screening (The Fast Pass)" translate="no">​</a></h3>
<p>Read only Titles and Abstracts.</p>
<ul>
<li class=""><strong>Is it relevant?</strong> -&gt; Keep.</li>
<li class=""><strong>Is it irrelevant?</strong> -&gt; Delete.</li>
</ul>
<p><strong>Record the number of rejections.</strong></p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-5-eligibility-the-deep-dive">Step 5: Eligibility (The Deep Dive)<a href="https://kb.bargady.online/blog/prisma-method-guide#step-5-eligibility-the-deep-dive" class="hash-link" aria-label="Direct link to Step 5: Eligibility (The Deep Dive)" title="Direct link to Step 5: Eligibility (The Deep Dive)" translate="no">​</a></h3>
<p>Download the full PDFs of the survivors. Read them.</p>
<ul>
<li class=""><strong>Does this actually fit my criteria?</strong></li>
<li class="">If you reject it here, you must note the specific reason (e.g., "Paper turned out to be a review, not an experiment").</li>
</ul>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="step-6-synthesis">Step 6: Synthesis<a href="https://kb.bargady.online/blog/prisma-method-guide#step-6-synthesis" class="hash-link" aria-label="Direct link to Step 6: Synthesis" title="Direct link to Step 6: Synthesis" translate="no">​</a></h3>
<p>The remaining papers constitute your "State of the Art."</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="recommended-tools">Recommended Tools<a href="https://kb.bargady.online/blog/prisma-method-guide#recommended-tools" class="hash-link" aria-label="Direct link to Recommended Tools" title="Direct link to Recommended Tools" translate="no">​</a></h2>
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>note</div><div class="admonitionContent_BuS1"><p>You don't have to do this manually in Excel.</p></div></div>
<ul>
<li class=""><strong><a href="https://www.rayyan.ai/" target="_blank" rel="noopener noreferrer" class="">Rayyan</a></strong>: The industry standard for screening. You upload your references, and you can quickly press "Include" or "Exclude" (via keyboard shortcuts) based on abstract. It tracks the PRISMA numbers for you.</li>
<li class=""><strong><a href="https://www.covidence.org/" target="_blank" rel="noopener noreferrer" class="">Covidence</a></strong>: Excellent for Cochrane-style reviews (paid, but check if your university has a license).</li>
<li class=""><strong><a href="https://parsif.al/" target="_blank" rel="noopener noreferrer" class="">Parsifal</a></strong>: A free tool specifically for software engineering systematic reviews.</li>
</ul>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="conclusion">Conclusion<a href="https://kb.bargady.online/blog/prisma-method-guide#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion" translate="no">​</a></h2>
<p>PRISMA makes your thesis <strong>defensible</strong>. When your jury asks, <em>"Why didn't you include the paper by Dr. Smith?"</em>, you can look at your PRISMA diagram and say:</p>
<blockquote>
<p>"I identified it during the search, but it was excluded during the eligibility phase because it did not meet the criterion of having empirical validation."</p>
</blockquote>
<p>That is the power of a systematic review.</p>]]></content>
        <author>
            <name>Ahmed BARGADY</name>
        </author>
        <category label="prisma" term="prisma"/>
        <category label="systematic-review" term="systematic-review"/>
        <category label="methodology" term="methodology"/>
        <category label="phd" term="phd"/>
        <category label="research" term="research"/>
        <category label="academic-writing" term="academic-writing"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Code Wiki by Google]]></title>
        <id>https://kb.bargady.online/blog/code-wiki-by-google</id>
        <link href="https://kb.bargady.online/blog/code-wiki-by-google"/>
        <updated>2025-12-30T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[Google has recently introduced Code Wiki, a groundbreaking AI-driven system designed to transform how developers understand and document code. By keeping documentation continuously synchronized with the codebase, Code Wiki aims to solve one of the most persistent challenges in software development: the gap between code and its documentation.]]></summary>
        <content type="html"><![CDATA[<p>Google has recently introduced <strong>Code Wiki</strong>, a groundbreaking AI-driven system designed to transform how developers understand and document code. By keeping documentation continuously synchronized with the codebase, Code Wiki aims to solve one of the most persistent challenges in software development: the gap between code and its documentation.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="a-new-era-of-code-understanding">A New Era of Code Understanding<a href="https://kb.bargady.online/blog/code-wiki-by-google#a-new-era-of-code-understanding" class="hash-link" aria-label="Direct link to A New Era of Code Understanding" title="Direct link to A New Era of Code Understanding" translate="no">​</a></h2>
<p>The traditional approach to documentation often involves static files that quickly become outdated as code evolves. This disconnect forces developers to spend valuable time deciphering complex source code rather than building new features. Google's Code Wiki addresses this by creating a <strong>living, structured wiki</strong> for every repository that evolves alongside the code.</p>
<blockquote>
<p>"Developers should spend their time building, not deciphering. The era of manual, outdated documentation and endless code-reading is over." - Google Developers Blog</p>
</blockquote>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="key-features">Key Features<a href="https://kb.bargady.online/blog/code-wiki-by-google#key-features" class="hash-link" aria-label="Direct link to Key Features" title="Direct link to Key Features" translate="no">​</a></h2>
<p>Code Wiki brings several innovative features to the table:</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-automated--always-up-to-date">1. Automated &amp; Always Up-to-Date<a href="https://kb.bargady.online/blog/code-wiki-by-google#1-automated--always-up-to-date" class="hash-link" aria-label="Direct link to 1. Automated &amp; Always Up-to-Date" title="Direct link to 1. Automated &amp; Always Up-to-Date" translate="no">​</a></h3>
<p>The system scans the entire codebase and regenerates documentation after every change. This ensures that the docs are never stale and always reflect the current state of the software.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-intelligent--context-aware">2. Intelligent &amp; Context-Aware<a href="https://kb.bargady.online/blog/code-wiki-by-google#2-intelligent--context-aware" class="hash-link" aria-label="Direct link to 2. Intelligent &amp; Context-Aware" title="Direct link to 2. Intelligent &amp; Context-Aware" translate="no">​</a></h3>
<p>Powered by Gemini, Code Wiki features an integrated chat interface that understands your repository end-to-end. Unlike generic AI models, this assistant has deep knowledge of your specific project, allowing it to answer highly specific questions.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="3-integrated--actionable">3. Integrated &amp; Actionable<a href="https://kb.bargady.online/blog/code-wiki-by-google#3-integrated--actionable" class="hash-link" aria-label="Direct link to 3. Integrated &amp; Actionable" title="Direct link to 3. Integrated &amp; Actionable" translate="no">​</a></h3>
<p>Documentation is no longer isolated from the code. Every section of the wiki and every chat response is hyper-linked directly to the relevant files, classes, and definitions. This allows developers to seamlessly transition between high-level concepts and the actual implementation.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="4-dynamic-visualizations">4. Dynamic Visualizations<a href="https://kb.bargady.online/blog/code-wiki-by-google#4-dynamic-visualizations" class="hash-link" aria-label="Direct link to 4. Dynamic Visualizations" title="Direct link to 4. Dynamic Visualizations" translate="no">​</a></h3>
<p>For complex systems, text isn't always enough. Code Wiki automatically generates architecture, class, and sequence diagrams that updated instantly with code changes. This helps in visualizing complex relationships without the manual effort of drawing diagrams.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="availability-and-future-plans">Availability and Future Plans<a href="https://kb.bargady.online/blog/code-wiki-by-google#availability-and-future-plans" class="hash-link" aria-label="Direct link to Availability and Future Plans" title="Direct link to Availability and Future Plans" translate="no">​</a></h2>
<p>Currently, the <strong>Code Wiki website</strong> is in <a href="http://codewiki.google/?utm_source=blog&amp;utm_medium=gfd&amp;utm_campaign=launch_post" target="_blank" rel="noopener noreferrer" class="">public preview</a> for public repositories. It ingests open-source projects and hosts interactive documentation for them.</p>
<p>Google is also working on a <strong>Gemini CLI extension</strong> for Code Wiki. This will allow teams to run the system locally and securely on internal and private repositories—a game-changer for companies dealing with legacy code or projects where original authors are no longer available.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="community-reaction">Community Reaction<a href="https://kb.bargady.online/blog/code-wiki-by-google#community-reaction" class="hash-link" aria-label="Direct link to Community Reaction" title="Direct link to Community Reaction" translate="no">​</a></h2>
<p>The announcement has sparked considerable interest in the developer community. According to <a href="https://www.infoq.com/news/2025/11/google-code-wiki/" target="_blank" rel="noopener noreferrer" class="">InfoQ</a>, early reactions are mixed but generally positive. Developers are excited about the potential to reduce manual context gathering, while some are curious about how it will balance with existing local workflows. There is also a strong demand for features like exporting the generated documentation to Markdown or PDF.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="conclusion">Conclusion<a href="https://kb.bargady.online/blog/code-wiki-by-google#conclusion" class="hash-link" aria-label="Direct link to Conclusion" title="Direct link to Conclusion" translate="no">​</a></h2>
<p>Code Wiki represents a significant shift towards "instant understanding" in software development. by automating the drudgery of documentation and providing intelligent tools to explore code, Google is betting on a future where developers can onboard faster and understand systems more deeply with less effort.</p>]]></content>
        <author>
            <name>Ahmed BARGADY</name>
        </author>
        <category label="Code Wiki" term="Code Wiki"/>
        <category label="Google" term="Google"/>
        <category label="Artificial Intelligence" term="Artificial Intelligence"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Function Gemma - Function Calling to the Edge]]></title>
        <id>https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge</id>
        <link href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge"/>
        <updated>2025-12-29T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[Introducing FunctionGemma: Precise Function Calling for the Edge]]></summary>
        <content type="html"><![CDATA[<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="introducing-functiongemma-precise-function-calling-for-the-edge">Introducing FunctionGemma: Precise Function Calling for the Edge<a href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge#introducing-functiongemma-precise-function-calling-for-the-edge" class="hash-link" aria-label="Direct link to Introducing FunctionGemma: Precise Function Calling for the Edge" title="Direct link to Introducing FunctionGemma: Precise Function Calling for the Edge" translate="no">​</a></h2>
<p>We are excited to introduce <strong>FunctionGemma</strong>, a specialized version of the Gemma 3 270M model, explicitly fine-tuned for function calling. Designed with edge devices in mind, FunctionGemma bridges the gap between natural language user instructions and executable API actions, bringing powerful AI capabilities directly to your local devices.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="why-functiongemma">Why FunctionGemma?<a href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge#why-functiongemma" class="hash-link" aria-label="Direct link to Why FunctionGemma?" title="Direct link to Why FunctionGemma?" translate="no">​</a></h3>
<p>Running AI models on edge devices—like mobile phones, IoT boards (e.g., NVIDIA Jetson Nano), and wearables—presents unique challenges. Memory is limited, and latency must be minimal. Most importantly, users demand privacy and the ability to work offline. FunctionGemma addresses these needs by offering a lightweight yet highly effective model that specializes in one critical task: <strong>translating human intent into code.</strong></p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="key-features">Key Features<a href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge#key-features" class="hash-link" aria-label="Direct link to Key Features" title="Direct link to Key Features" translate="no">​</a></h3>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-built-for-the-edge">1. Built for the Edge<a href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge#1-built-for-the-edge" class="hash-link" aria-label="Direct link to 1. Built for the Edge" title="Direct link to 1. Built for the Edge" translate="no">​</a></h4>
<p>At just <strong>270 million parameters</strong>, FunctionGemma is incredibly efficient. It supports quantization, allowing it to run with low memory and low latency on hardware-constrained environments. This makes it possible to build responsive agents that don't rely on a constant internet connection.</p>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-specialized-for-action">2. Specialized for Action<a href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge#2-specialized-for-action" class="hash-link" aria-label="Direct link to 2. Specialized for Action" title="Direct link to 2. Specialized for Action" translate="no">​</a></h4>
<p>Unlike general-purpose chat models, FunctionGemma is laser-focused on <strong>function calling</strong>. It takes a user's natural language instruction (e.g., "Turn on the living room lights") and a list of available tools, and outputs a structured function call (e.g., <code>turn_on_light(room="living_room")</code>).</p>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="3-high-accuracy-via-fine-tuning">3. High Accuracy via Fine-Tuning<a href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge#3-high-accuracy-via-fine-tuning" class="hash-link" aria-label="Direct link to 3. High Accuracy via Fine-Tuning" title="Direct link to 3. High Accuracy via Fine-Tuning" translate="no">​</a></h4>
<p>While the base model is capable, FunctionGemma really shines when fine-tuned for specific tasks. Google's internal benchmarks show that fine-tuning on a target dataset can boost accuracy significantly—jumping from <strong>58% to 85%</strong> in "Mobile Actions" evaluations. This adaptability allows developers to create highly reliable agents for their specific use cases.</p>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="4-unified-workflow">4. Unified Workflow<a href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge#4-unified-workflow" class="hash-link" aria-label="Direct link to 4. Unified Workflow" title="Direct link to 4. Unified Workflow" translate="no">​</a></h4>
<p>FunctionGemma isn't just about calling functions; it handles the full loop. It can:</p>
<ol>
<li class=""><strong>Parse</strong> a user's request.</li>
<li class=""><strong>Generate</strong> the appropriate function call.</li>
<li class=""><strong>Summarize</strong> the tool's output back to the user in natural language.</li>
</ol>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="under-the-hood">Under the Hood<a href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge#under-the-hood" class="hash-link" aria-label="Direct link to Under the Hood" title="Direct link to Under the Hood" translate="no">​</a></h3>
<p>FunctionGemma leverages Gemma's 256k vocabulary, which is optimized for JSON and multilingual inputs. This ensures that JSON structures—common in API payloads—are tokenized efficiently, reducing sequence length and speeding up inference on edge devices.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="getting-started">Getting Started<a href="https://kb.bargady.online/blog/function-gemma-function-calling-to-the-edge#getting-started" class="hash-link" aria-label="Direct link to Getting Started" title="Direct link to Getting Started" translate="no">​</a></h3>
<p>FunctionGemma is integrated into a broad ecosystem of tools, making it easy to adopt:</p>
<ul>
<li class=""><strong>Fine-tuning</strong>: Compatible with Hugging Face Transformers, Keras, and Axolotl.</li>
<li class=""><strong>Deployment</strong>: Ready for LiteRT-LM, vLLM, and Vertex AI.</li>
<li class=""><strong>Resources</strong>: Check out the <a href="https://github.com/google-gemini/gemma-cookbook/blob/main/Gemma/Function_Calling/FunctionGemma_Training.ipynb" target="_blank" rel="noopener noreferrer" class="">fine-tuning cookbook</a> and demos on the Google AI Edge Gallery.</li>
</ul>
<p>FunctionGemma represents a significant step forward in making AI agents faster, more private, and more accessible. We can't wait to see what you build with it!</p>]]></content>
        <author>
            <name>Ahmed BARGADY</name>
        </author>
        <category label="Gemma" term="Gemma"/>
        <category label="Edge AI" term="Edge AI"/>
        <category label="Function Calling" term="Function Calling"/>
        <category label="Artificial Intelligence" term="Artificial Intelligence"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Chatterbox - Alternative Open Source to ElevenLabs]]></title>
        <id>https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs</id>
        <link href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs"/>
        <updated>2025-12-25T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[In the rapidly evolving landscape of Generative AI, Text-to-Speech (TTS) technology has become a cornerstone for creating immersive digital experiences. While proprietary giants like ElevenLabs have long dominated the field with their hyper-realistic voice cloning and low-latency APIs, the open-source community has been eager for a contender that offers similar capabilities without the associated costs or privacy concerns.]]></summary>
        <content type="html"><![CDATA[<p>In the rapidly evolving landscape of Generative AI, Text-to-Speech (TTS) technology has become a cornerstone for creating immersive digital experiences. While proprietary giants like ElevenLabs have long dominated the field with their hyper-realistic voice cloning and low-latency APIs, the open-source community has been eager for a contender that offers similar capabilities without the associated costs or privacy concerns.</p>
<p>Enter <strong>Chatterbox</strong>, a groundbreaking open-source TTS model family developed by Resemble AI. Chatterbox is designed to democratize high-quality speech synthesis, offering features that were previously exclusive to premium, closed-source models.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="what-is-chatterbox">What is Chatterbox?<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#what-is-chatterbox" class="hash-link" aria-label="Direct link to What is Chatterbox?" title="Direct link to What is Chatterbox?" translate="no">​</a></h2>
<p>Chatterbox is a suite of state-of-the-art TTS models that prioritize realism, control, and efficiency. It is built to run locally, giving developers full ownership of their data and the flexibility to fine-tune the models for their specific needs.</p>
<p>The family includes three main variants:</p>
<ol>
<li class=""><strong>Chatterbox-Turbo (350M)</strong>: Optimized for speed, this model uses a distilled 1-step decoder to achieve ultra-low latency, making it perfect for real-time conversational agents.</li>
<li class=""><strong>Chatterbox-Multilingual (500M)</strong>: A robust model supporting over <strong>23 languages</strong>, breaking down language barriers for global applications.</li>
<li class=""><strong>Chatterbox (500M)</strong>: The standard model that offers advanced creative controls, including Classifier-Free Guidance (CFG) for tweaking style and intonation.</li>
</ol>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="key-features-that-set-it-apart">Key Features that Set It Apart<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#key-features-that-set-it-apart" class="hash-link" aria-label="Direct link to Key Features that Set It Apart" title="Direct link to Key Features that Set It Apart" translate="no">​</a></h2>
<p>Chatterbox isn't just about reading text; it's about conveying emotion and nuance.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-zero-shot-voice-cloning">1. Zero-Shot Voice Cloning<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#1-zero-shot-voice-cloning" class="hash-link" aria-label="Direct link to 1. Zero-Shot Voice Cloning" title="Direct link to 1. Zero-Shot Voice Cloning" translate="no">​</a></h3>
<p>One of the most sought-after features in modern TTS is the ability to clone a voice from a small sample. Chatterbox excels here, allowing you to clone any voice using just a <strong>10-second reference audio clip</strong>. This works "zero-shot," meaning no fine-tuning is required.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-native-paralinguistic-support">2. Native Paralinguistic Support<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#2-native-paralinguistic-support" class="hash-link" aria-label="Direct link to 2. Native Paralinguistic Support" title="Direct link to 2. Native Paralinguistic Support" translate="no">​</a></h3>
<p>Human speech is filled with non-verbal cues. Chatterbox understands this and supports "paralinguistic tags" directly in the input text. You can make the model <strong>[laugh]</strong>, <strong>[cough]</strong>, <strong>[chuckle]</strong>, <strong>[clear throat]</strong>, or even <strong>[gasp]</strong> just by typing those tags. This level of control is a game-changer for creating dynamic and lifelike characters.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="3-industry-leading-performance">3. Industry-Leading Performance<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#3-industry-leading-performance" class="hash-link" aria-label="Direct link to 3. Industry-Leading Performance" title="Direct link to 3. Industry-Leading Performance" translate="no">​</a></h3>
<p>In human preference benchmarks, <strong>Chatterbox-Turbo</strong> has been shown to outperform major competitors, including ElevenLabs Turbo v2.5 and Cartesia Sonic 3. Its 1-step distillation process ensures that this quality comes with blazing-fast generation speeds.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="getting-started">Getting Started<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#getting-started" class="hash-link" aria-label="Direct link to Getting Started" title="Direct link to Getting Started" translate="no">​</a></h2>
<p>Integrating Chatterbox into your Python projects is straightforward.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="installation">Installation<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#installation" class="hash-link" aria-label="Direct link to Installation" title="Direct link to Installation" translate="no">​</a></h3>
<p>First, install the package via pip:</p>
<div class="language-bash codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-bash codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token plain">pip </span><span class="token function" style="color:rgb(130, 170, 255)">install</span><span class="token plain"> chatterbox-tts</span><br></span></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="basic-usage">Basic Usage<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#basic-usage" class="hash-link" aria-label="Direct link to Basic Usage" title="Direct link to Basic Usage" translate="no">​</a></h3>
<p>Here is a simple example of how to use the Turbo model for zero-shot voice cloning with expressive tags:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token keyword" style="font-style:italic">import</span><span class="token plain"> torchaudio </span><span class="token keyword" style="font-style:italic">as</span><span class="token plain"> ta</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">from</span><span class="token plain"> chatterbox</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">tts_turbo </span><span class="token keyword" style="font-style:italic">import</span><span class="token plain"> ChatterboxTurboTTS</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 1. Load the model (uses CUDA if available)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">model </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> ChatterboxTurboTTS</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">from_pretrained</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">device</span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token string" style="color:rgb(195, 232, 141)">"cuda"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 2. Define your text with paralinguistic tags</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">text </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">"Hi there [chuckle], I'm strictly an open-source model now!"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 3. Generate audio using a 10s reference clip</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Replace 'reference_voice.wav' with your target voice sample</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">wav </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> model</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">generate</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">text</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> audio_prompt_path</span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token string" style="color:rgb(195, 232, 141)">"reference_voice.wav"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 4. Save the result</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">ta</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">save</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string" style="color:rgb(195, 232, 141)">"output.wav"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> wav</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> model</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">sr</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><br></span></code></pre></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="why-this-matters">Why This Matters<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#why-this-matters" class="hash-link" aria-label="Direct link to Why This Matters" title="Direct link to Why This Matters" translate="no">​</a></h2>
<p>For developers and creators, Chatterbox represents a significant shift. It proves that open-source models can compete with—and in some cases surpass—proprietary solutions. Whether you are building a virtual assistant, dubbing videos, or creating accessible content, Chatterbox provides a high-quality, free, and private toolset to bring your ideas to life.</p>
<p>Give it a try and let your applications speak for themselves!</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="references">References<a href="https://kb.bargady.online/blog/chatterbox-alternative-open-source-to-elevenlabs#references" class="hash-link" aria-label="Direct link to References" title="Direct link to References" translate="no">​</a></h2>
<ul>
<li class=""><a href="https://github.com/resemble-ai/chatterbox" target="_blank" rel="noopener noreferrer" class="">Chatterbox Repo on GitHub</a></li>
<li class=""><a href="https://huggingface.co/ResembleAI/chatterbox-turbo" target="_blank" rel="noopener noreferrer" class="">Chatterbox Documentation on HF</a></li>
</ul>]]></content>
        <author>
            <name>Ahmed BARGADY</name>
        </author>
        <category label="Chatterbox" term="Chatterbox"/>
        <category label="Text To Speech" term="Text To Speech"/>
        <category label="Artificial Intelligence" term="Artificial Intelligence"/>
    </entry>
    <entry>
        <title type="html"><![CDATA[Deep Graph Library (DGL)]]></title>
        <id>https://kb.bargady.online/blog/deep-graph-library-dgl</id>
        <link href="https://kb.bargady.online/blog/deep-graph-library-dgl"/>
        <updated>2025-12-24T00:00:00.000Z</updated>
        <summary type="html"><![CDATA[This page summarizes the core concepts of the Deep Graph Library (DGL), focusing on graph construction, feature handling, and the message-passing paradigm used to implement architectures like GCN and GAT.]]></summary>
        <content type="html"><![CDATA[<p>This page summarizes the core concepts of the <a href="https://www.dgl.ai/" target="_blank" rel="noopener noreferrer" class="">Deep Graph Library</a> (DGL), focusing on graph construction, feature handling, and the message-passing paradigm used to implement architectures like <em>GCN</em> and <em>GAT</em>.</p>
<div class="theme-admonition theme-admonition-info admonition_xJq3 alert alert--info"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>Source</div><div class="admonitionContent_BuS1"><p>Based on the official <a href="https://www.dgl.ai/dgl_docs/" target="_blank" rel="noopener noreferrer" class="">Deep Graph Library Documentation</a></p></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="1-creating-a-graph-dglgraph">1. Creating a Graph (DGLGraph)<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#1-creating-a-graph-dglgraph" class="hash-link" aria-label="Direct link to 1. Creating a Graph (DGLGraph)" title="Direct link to 1. Creating a Graph (DGLGraph)" translate="no">​</a></h2>
<p>In DGL, a graph is defined by a set of edges connecting nodes. Nodes are identified by integers starting from 0.</p>
<p>Basic Construction</p>
<p>You define edges using two arrays: Source Nodes (u) and Destination Nodes (v).</p>
<div class="theme-tabs-container tabs-container tabList__CuJ"><ul role="tablist" aria-orientation="horizontal" class="tabs"><li role="tab" tabindex="0" aria-selected="true" class="tabs__item tabItem_LNqP tabs__item--active">PyTorch Tensors</li><li role="tab" tabindex="-1" aria-selected="false" class="tabs__item tabItem_LNqP">NetworkX</li></ul><div class="margin-top--md"><div role="tabpanel" class="tabItem_Ymn6"><div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token keyword" style="font-style:italic">import</span><span class="token plain"> dgl</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">import</span><span class="token plain"> torch</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Define a graph with 4 nodes (0, 1, 2, 3)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Edges: 0-&gt;1, 1-&gt;2, 2-&gt;3, 3-&gt;0 (A cycle)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">u </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> torch</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">tensor</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token number" style="color:rgb(247, 140, 108)">0</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">1</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">2</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">3</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"> </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Source nodes</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">v </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> torch</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">tensor</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token number" style="color:rgb(247, 140, 108)">1</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">2</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">3</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">0</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"> </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Destination nodes</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Create the graph</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">g </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> dgl</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">graph</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">u</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> v</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">print</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string-interpolation string" style="color:rgb(195, 232, 141)">f"Number of nodes: </span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token string-interpolation interpolation">g</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token string-interpolation interpolation">num_nodes</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token string-interpolation string" style="color:rgb(195, 232, 141)">"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">print</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string-interpolation string" style="color:rgb(195, 232, 141)">f"Number of edges: </span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token string-interpolation interpolation">g</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token string-interpolation interpolation">num_edges</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token string-interpolation string" style="color:rgb(195, 232, 141)">"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><br></span></code></pre></div></div></div><div role="tabpanel" class="tabItem_Ymn6" hidden=""><div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token keyword" style="font-style:italic">import</span><span class="token plain"> dgl</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">import</span><span class="token plain"> networkx </span><span class="token keyword" style="font-style:italic">as</span><span class="token plain"> nx</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Create using NetworkX first</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">nx_g </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> nx</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">cycle_graph</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token number" style="color:rgb(247, 140, 108)">4</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Convert to DGL</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">g </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> dgl</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">from_networkx</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">nx_g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">print</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string-interpolation string" style="color:rgb(195, 232, 141)">f"Number of nodes: </span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token string-interpolation interpolation">g</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token string-interpolation interpolation">num_nodes</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token string-interpolation string" style="color:rgb(195, 232, 141)">"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><br></span></code></pre></div></div></div></div></div>
<div class="theme-admonition theme-admonition-tip admonition_xJq3 alert alert--success"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>Bi-directional Graphs</div><div class="admonitionContent_BuS1"><p>DGL graphs are directed by default. To make them undirected (bi-directional), you must explicitly add reverse edges or use <code>dgl.add_reverse_edges(g)</code>.</p></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="2-node-and-edge-features">2. Node and Edge Features<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#2-node-and-edge-features" class="hash-link" aria-label="Direct link to 2. Node and Edge Features" title="Direct link to 2. Node and Edge Features" translate="no">​</a></h2>
<p>Graphs in DGL are not just structure; they store data. You can store feature vectors (embeddings) directly on the nodes and edges using the <code>ndata</code> and <code>edata</code> interfaces.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Assign a 5-dimensional random vector to each node</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 'feat' is just a name we choose. You can name it 'h', 'attr', 'x', etc.</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">ndata</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token string" style="color:rgb(195, 232, 141)">'feat'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"> </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> torch</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">randn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token number" style="color:rgb(247, 140, 108)">4</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">5</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Assign a 2-dimensional vector to each edge (e.g., edge type embeddings)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">edata</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token string" style="color:rgb(195, 232, 141)">'weight'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"> </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> torch</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">randn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token number" style="color:rgb(247, 140, 108)">4</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token number" style="color:rgb(247, 140, 108)">2</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Accessing features of Node 0</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">print</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">ndata</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token string" style="color:rgb(195, 232, 141)">'feat'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token number" style="color:rgb(247, 140, 108)">0</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span></code></pre></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="3-message-passing-the-core-engine">3. Message Passing (The Core Engine)<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#3-message-passing-the-core-engine" class="hash-link" aria-label="Direct link to 3. Message Passing (The Core Engine)" title="Direct link to 3. Message Passing (The Core Engine)" translate="no">​</a></h2>
<p>This is the most critical concept in DGL. It abstracts the mathematical formula of GNNs.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="mathematical-formulation">Mathematical Formulation<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#mathematical-formulation" class="hash-link" aria-label="Direct link to Mathematical Formulation" title="Direct link to Mathematical Formulation" translate="no">​</a></h3>
<span class="katex-display"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msubsup><mi>h</mi><mi>v</mi><mrow><mo stretchy="false">(</mo><mi>l</mi><mo>+</mo><mn>1</mn><mo stretchy="false">)</mo></mrow></msubsup><mo>=</mo><mi>σ</mi><mrow><mo fence="true">(</mo><munder><mo>∑</mo><mrow><mi>u</mi><mo>∈</mo><mi mathvariant="script">N</mi><mo stretchy="false">(</mo><mi>v</mi><mo stretchy="false">)</mo></mrow></munder><mtext>Message</mtext><mo stretchy="false">(</mo><msubsup><mi>h</mi><mi>u</mi><mrow><mo stretchy="false">(</mo><mi>l</mi><mo stretchy="false">)</mo></mrow></msubsup><mo separator="true">,</mo><msubsup><mi>h</mi><mi>v</mi><mrow><mo stretchy="false">(</mo><mi>l</mi><mo stretchy="false">)</mo></mrow></msubsup><mo separator="true">,</mo><msub><mi>e</mi><mrow><mi>u</mi><mi>v</mi></mrow></msub><mo stretchy="false">)</mo><mo fence="true">)</mo></mrow></mrow><annotation encoding="application/x-tex">h_v^{(l+1)} = \sigma \left( \sum_{u \in \mathcal{N}(v)} \text{Message}(h_u^{(l)}, h_v^{(l)}, e_{uv}) \right)</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.185em;vertical-align:-0.247em"></span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.938em"><span style="top:-2.453em;margin-left:0em;margin-right:0.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em">v</span></span></span><span style="top:-3.113em;margin-right:0.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mopen mtight">(</span><span class="mord mathnormal mtight" style="margin-right:0.01968em">l</span><span class="mbin mtight">+</span><span class="mord mtight">1</span><span class="mclose mtight">)</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.247em"><span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:3.6em;vertical-align:-1.55em"></span><span class="mord mathnormal" style="margin-right:0.03588em">σ</span><span class="mspace" style="margin-right:0.1667em"></span><span class="minner"><span class="mopen"><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:2.05em"><span style="top:-4.05em"><span class="pstrut" style="height:5.6em"></span><span style="width:0.875em;height:3.600em"><svg xmlns="http://www.w3.org/2000/svg" width="0.875em" height="3.600em" viewBox="0 0 875 3600"><path d="M863,9c0,-2,-2,-5,-6,-9c0,0,-17,0,-17,0c-12.7,0,-19.3,0.3,-20,1
c-5.3,5.3,-10.3,11,-15,17c-242.7,294.7,-395.3,682,-458,1162c-21.3,163.3,-33.3,349,
-36,557 l0,84c0.2,6,0,26,0,60c2,159.3,10,310.7,24,454c53.3,528,210,
949.7,470,1265c4.7,6,9.7,11.7,15,17c0.7,0.7,7,1,19,1c0,0,18,0,18,0c4,-4,6,-7,6,-9
c0,-2.7,-3.3,-8.7,-10,-18c-135.3,-192.7,-235.5,-414.3,-300.5,-665c-65,-250.7,-102.5,
-544.7,-112.5,-882c-2,-104,-3,-167,-3,-189
l0,-92c0,-162.7,5.7,-314,17,-454c20.7,-272,63.7,-513,129,-723c65.3,
-210,155.3,-396.3,270,-559c6.7,-9.3,10,-15.3,10,-18z"></path></svg></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.55em"><span></span></span></span></span></span></span><span class="mop op-limits"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:1.05em"><span style="top:-1.809em;margin-left:0em"><span class="pstrut" style="height:3.05em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight">u</span><span class="mrel mtight">∈</span><span class="mord mathcal mtight" style="margin-right:0.14736em">N</span><span class="mopen mtight">(</span><span class="mord mathnormal mtight" style="margin-right:0.03588em">v</span><span class="mclose mtight">)</span></span></span></span><span style="top:-3.05em"><span class="pstrut" style="height:3.05em"></span><span><span class="mop op-symbol large-op">∑</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.516em"><span></span></span></span></span></span><span class="mspace" style="margin-right:0.1667em"></span><span class="mord text"><span class="mord">Message</span></span><span class="mopen">(</span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.938em"><span style="top:-2.453em;margin-left:0em;margin-right:0.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">u</span></span></span><span style="top:-3.113em;margin-right:0.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mopen mtight">(</span><span class="mord mathnormal mtight" style="margin-right:0.01968em">l</span><span class="mclose mtight">)</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.247em"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em"></span><span class="mord"><span class="mord mathnormal">h</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.938em"><span style="top:-2.453em;margin-left:0em;margin-right:0.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em">v</span></span></span><span style="top:-3.113em;margin-right:0.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mopen mtight">(</span><span class="mord mathnormal mtight" style="margin-right:0.01968em">l</span><span class="mclose mtight">)</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.247em"><span></span></span></span></span></span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em"></span><span class="mord"><span class="mord mathnormal">e</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em"><span class="pstrut" style="height:2.7em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em">uv</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em"><span></span></span></span></span></span></span><span class="mclose">)</span><span class="mclose"><span class="delimsizing mult"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:2.05em"><span style="top:-4.05em"><span class="pstrut" style="height:5.6em"></span><span style="width:0.875em;height:3.600em"><svg xmlns="http://www.w3.org/2000/svg" width="0.875em" height="3.600em" viewBox="0 0 875 3600"><path d="M76,0c-16.7,0,-25,3,-25,9c0,2,2,6.3,6,13c21.3,28.7,42.3,60.3,
63,95c96.7,156.7,172.8,332.5,228.5,527.5c55.7,195,92.8,416.5,111.5,664.5
c11.3,139.3,17,290.7,17,454c0,28,1.7,43,3.3,45l0,9
c-3,4,-3.3,16.7,-3.3,38c0,162,-5.7,313.7,-17,455c-18.7,248,-55.8,469.3,-111.5,664
c-55.7,194.7,-131.8,370.3,-228.5,527c-20.7,34.7,-41.7,66.3,-63,95c-2,3.3,-4,7,-6,11
c0,7.3,5.7,11,17,11c0,0,11,0,11,0c9.3,0,14.3,-0.3,15,-1c5.3,-5.3,10.3,-11,15,-17
c242.7,-294.7,395.3,-681.7,458,-1161c21.3,-164.7,33.3,-350.7,36,-558
l0,-144c-2,-159.3,-10,-310.7,-24,-454c-53.3,-528,-210,-949.7,
-470,-1265c-4.7,-6,-9.7,-11.7,-15,-17c-0.7,-0.7,-6.7,-1,-18,-1z"></path></svg></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:1.55em"><span></span></span></span></span></span></span></span></span></span></span></span>
<p>DGL uses a specific syntax for this: <code>update_all(message_func, reduce_func)</code>.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="built-in-functions-dglfunction">Built-in Functions (<code>dgl.function</code>)<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#built-in-functions-dglfunction" class="hash-link" aria-label="Direct link to built-in-functions-dglfunction" title="Direct link to built-in-functions-dglfunction" translate="no">​</a></h3>
<p>DGL provides optimized C++ kernels for common operations, accessed via <code>dgl.function</code> <em>(usually imported as <code>fn</code>)</em>.</p>
<table><thead><tr><th style="text-align:left">Function Name</th><th style="text-align:left">Description</th><th style="text-align:left">Notation</th></tr></thead><tbody><tr><td style="text-align:left"><code>fn.copy_u</code></td><td style="text-align:left">Copy source node features to be the message.</td><td style="text-align:left"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mo>→</mo><mi>m</mi></mrow><annotation encoding="application/x-tex">u \to m</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2778em"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:0.4306em"></span><span class="mord mathnormal">m</span></span></span></span></td></tr><tr><td style="text-align:left"><code>fn.u_add_e</code></td><td style="text-align:left">Add source feature and edge feature.</td><td style="text-align:left"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mo>+</mo><mi>e</mi><mo>→</mo><mi>m</mi></mrow><annotation encoding="application/x-tex">u + e \to m</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222em"></span><span class="mbin">+</span><span class="mspace" style="margin-right:0.2222em"></span></span><span class="base"><span class="strut" style="height:0.4306em"></span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:0.4306em"></span><span class="mord mathnormal">m</span></span></span></span></td></tr><tr><td style="text-align:left"><code>fn.u_mul_e</code></td><td style="text-align:left">Multiply source feature by edge feature (Attention).</td><td style="text-align:left"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mo>×</mo><mi>e</mi><mo>→</mo><mi>m</mi></mrow><annotation encoding="application/x-tex">u \times e \to m</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6667em;vertical-align:-0.0833em"></span><span class="mord mathnormal">u</span><span class="mspace" style="margin-right:0.2222em"></span><span class="mbin">×</span><span class="mspace" style="margin-right:0.2222em"></span></span><span class="base"><span class="strut" style="height:0.4306em"></span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:0.4306em"></span><span class="mord mathnormal">m</span></span></span></span></td></tr><tr><td style="text-align:left"><code>fn.sum</code></td><td style="text-align:left">Sum up all incoming messages.</td><td style="text-align:left"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo>∑</mo><mi>m</mi><mo>→</mo><mi>h</mi></mrow><annotation encoding="application/x-tex">\sum m \to h</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em"></span><span class="mop op-symbol small-op" style="position:relative;top:0em">∑</span><span class="mspace" style="margin-right:0.1667em"></span><span class="mord mathnormal">m</span><span class="mspace" style="margin-right:0.2778em"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:0.6944em"></span><span class="mord mathnormal">h</span></span></span></span></td></tr><tr><td style="text-align:left"><code>fn.mean</code></td><td style="text-align:left">Average all incoming messages.</td><td style="text-align:left"><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mfrac><mn>1</mn><mi>N</mi></mfrac><mo>∑</mo><mi>m</mi><mo>→</mo><mi>h</mi></mrow><annotation encoding="application/x-tex">\frac{1}{N} \sum m \to h</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1.1901em;vertical-align:-0.345em"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.8451em"><span style="top:-2.655em"><span class="pstrut" style="height:3em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathnormal mtight" style="margin-right:0.10903em">N</span></span></span></span><span style="top:-3.23em"><span class="pstrut" style="height:3em"></span><span class="frac-line" style="border-bottom-width:0.04em"></span></span><span style="top:-3.394em"><span class="pstrut" style="height:3em"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.345em"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span><span class="mspace" style="margin-right:0.1667em"></span><span class="mop op-symbol small-op" style="position:relative;top:0em">∑</span><span class="mspace" style="margin-right:0.1667em"></span><span class="mord mathnormal">m</span><span class="mspace" style="margin-right:0.2778em"></span><span class="mrel">→</span><span class="mspace" style="margin-right:0.2778em"></span></span><span class="base"><span class="strut" style="height:0.6944em"></span><span class="mord mathnormal">h</span></span></span></span></td></tr></tbody></table>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="implementations">Implementations<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#implementations" class="hash-link" aria-label="Direct link to Implementations" title="Direct link to Implementations" translate="no">​</a></h3>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="a-simple-graph-convolution">A Simple Graph Convolution<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#a-simple-graph-convolution" class="hash-link" aria-label="Direct link to A Simple Graph Convolution" title="Direct link to A Simple Graph Convolution" translate="no">​</a></h4>
<p>Let's implement a basic layer where every node calculates the sum of its neighbors' features.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token keyword" style="font-style:italic">import</span><span class="token plain"> dgl</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">function </span><span class="token keyword" style="font-style:italic">as</span><span class="token plain"> fn</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 1. Define the Message: "copy_u" means "Copy the neighbor's 'feat' vector"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 2. Define the Reduce: "sum" means "Sum up those vectors"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">update_all</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">fn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">copy_u</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string" style="color:rgb(195, 232, 141)">'feat'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">'m'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> fn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token builtin" style="color:rgb(130, 170, 255)">sum</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string" style="color:rgb(195, 232, 141)">'m'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">'h_new'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># The result is stored in 'h_new' in ndata</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">print</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">ndata</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token string" style="color:rgb(195, 232, 141)">'h_new'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span></code></pre></div></div>
<h4 class="anchor anchorTargetStickyNavbar_Vzrq" id="edge-weighted-message-passing-gat-style">Edge-Weighted Message Passing (GAT Style)<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#edge-weighted-message-passing-gat-style" class="hash-link" aria-label="Direct link to Edge-Weighted Message Passing (GAT Style)" title="Direct link to Edge-Weighted Message Passing (GAT Style)" translate="no">​</a></h4>
<p>In architectures like Graph Attention Networks, we care about the edge data.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 1. Message: Multiply Source Feature ('feat') by Edge Weight ('weight')</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># This is analogous to the Attention Mechanism</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">update_all</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">fn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">u_mul_e</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string" style="color:rgb(195, 232, 141)">'feat'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">'weight'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">'m'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> fn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token builtin" style="color:rgb(130, 170, 255)">sum</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string" style="color:rgb(195, 232, 141)">'m'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">'h_weighted'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><br></span></code></pre></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="4-building-a-gnn-module">4. Building a GNN Module<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#4-building-a-gnn-module" class="hash-link" aria-label="Direct link to 4. Building a GNN Module" title="Direct link to 4. Building a GNN Module" translate="no">​</a></h2>
<p>To use DGL in a real project, you wrap the message passing logic inside a standard PyTorch <code>nn.Module</code>.</p>
<p>Here is a simple Graph Convolutional Layer (GCN) implementation:</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token keyword" style="font-style:italic">import</span><span class="token plain"> torch</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">nn </span><span class="token keyword" style="font-style:italic">as</span><span class="token plain"> nn</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">class</span><span class="token plain"> </span><span class="token class-name" style="color:rgb(255, 203, 107)">GCNLayer</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">nn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">Module</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token keyword" style="font-style:italic">def</span><span class="token plain"> </span><span class="token function" style="color:rgb(130, 170, 255)">__init__</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">self</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> in_feats</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> out_feats</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        </span><span class="token builtin" style="color:rgb(130, 170, 255)">super</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">GCNLayer</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> self</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">__init__</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># A linear layer to transform features before aggregation</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        self</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">linear </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> nn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">Linear</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">in_feats</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> out_feats</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token keyword" style="font-style:italic">def</span><span class="token plain"> </span><span class="token function" style="color:rgb(130, 170, 255)">forward</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">self</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> feature</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 1. Create a local scope to avoid dirtying the graph</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">        </span><span class="token keyword" style="font-style:italic">with</span><span class="token plain"> g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">local_scope</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 2. Add the current features to the graph</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">ndata</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token string" style="color:rgb(195, 232, 141)">'h'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"> </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> feature</span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 3. Message Passing: </span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Send copy of 'h', Sum them up, store as 'h_agg'</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">update_all</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">fn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">copy_u</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string" style="color:rgb(195, 232, 141)">'h'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">'m'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> fn</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token builtin" style="color:rgb(130, 170, 255)">sum</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string" style="color:rgb(195, 232, 141)">'m'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(195, 232, 141)">'h_agg'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 4. Retrieve the aggregated result</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            h_agg </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">ndata</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token string" style="color:rgb(195, 232, 141)">'h_agg'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># 5. Pass through Linear Layer and return</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">            </span><span class="token keyword" style="font-style:italic">return</span><span class="token plain"> self</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">linear</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">h_agg</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><br></span></code></pre></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="5-training-loop">5. Training Loop<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#5-training-loop" class="hash-link" aria-label="Direct link to 5. Training Loop" title="Direct link to 5. Training Loop" translate="no">​</a></h2>
<p>Training a DGL model looks almost identical to training a standard image or text model in PyTorch.</p>
<div class="theme-admonition theme-admonition-note admonition_xJq3 alert alert--secondary"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"></path></svg></span>Key Difference</div><div class="admonitionContent_BuS1"><p>In the forward pass, you must pass both the graph object (g) and the features (features).</p></div></div>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Initialize graph and features</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">g </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain"> </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">features </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">ndata</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token string" style="color:rgb(195, 232, 141)">'feat'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">labels </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Initialize Model, Loss, and Optimizer</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">model </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> GCNLayer</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">in_feats</span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token number" style="color:rgb(247, 140, 108)">5</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> out_feats</span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token number" style="color:rgb(247, 140, 108)">2</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">optimizer </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> torch</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">optim</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">Adam</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">model</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">parameters</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> lr</span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token number" style="color:rgb(247, 140, 108)">0.01</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Training Loop</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token keyword" style="font-style:italic">for</span><span class="token plain"> epoch </span><span class="token keyword" style="font-style:italic">in</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(130, 170, 255)">range</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token number" style="color:rgb(247, 140, 108)">100</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token punctuation" style="color:rgb(199, 146, 234)">:</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    model</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">train</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Forward Pass</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    logits </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> model</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> features</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Calculate Loss (e.g., CrossEntropy)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    loss </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> F</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">cross_entropy</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">logits</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> labels</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Backward Pass</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    optimizer</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">zero_grad</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    loss</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">backward</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    optimizer</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">step</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">    </span><span class="token keyword" style="font-style:italic">print</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string-interpolation string" style="color:rgb(195, 232, 141)">f"Epoch </span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token string-interpolation interpolation">epoch</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token string-interpolation string" style="color:rgb(195, 232, 141)"> | Loss: </span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">{</span><span class="token string-interpolation interpolation">loss</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token string-interpolation interpolation">item</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token string-interpolation interpolation punctuation" style="color:rgb(199, 146, 234)">}</span><span class="token string-interpolation string" style="color:rgb(195, 232, 141)">"</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span></code></pre></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="6-advanced-batching-graphs">6. Advanced: Batching Graphs<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#6-advanced-batching-graphs" class="hash-link" aria-label="Direct link to 6. Advanced: Batching Graphs" title="Direct link to 6. Advanced: Batching Graphs" translate="no">​</a></h2>
<p>When processing multiple small graphs (e.g., in a dataset of chemical molecules or system logs), DGL handles this by merging many small graphs into one giant Batched Graph.</p>
<div class="language-python codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#bfc7d5;--prism-background-color:#292d3e"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-python codeBlock_bY9V thin-scrollbar" style="color:#bfc7d5;background-color:#292d3e"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#bfc7d5"><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># List of individual graphs</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">graphs </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token plain">g1</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> g2</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> g3</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># Batch them into one object</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">batched_g </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> dgl</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">batch</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">graphs</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># You can run the EXACT same model code on this batched graph!</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">output </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> model</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">batched_g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> batched_g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">ndata</span><span class="token punctuation" style="color:rgb(199, 146, 234)">[</span><span class="token string" style="color:rgb(195, 232, 141)">'feat'</span><span class="token punctuation" style="color:rgb(199, 146, 234)">]</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># To get separate outputs again (Readout/Pooling)</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain"></span><span class="token comment" style="color:rgb(105, 112, 152);font-style:italic"># This sums up all nodes belonging to Graph 1, all nodes for Graph 2, etc.</span><span class="token plain"></span><br></span><span class="token-line" style="color:#bfc7d5"><span class="token plain">graph_level_embeddings </span><span class="token operator" style="color:rgb(137, 221, 255)">=</span><span class="token plain"> dgl</span><span class="token punctuation" style="color:rgb(199, 146, 234)">.</span><span class="token plain">sum_nodes</span><span class="token punctuation" style="color:rgb(199, 146, 234)">(</span><span class="token plain">batched_g</span><span class="token punctuation" style="color:rgb(199, 146, 234)">,</span><span class="token plain"> output</span><span class="token punctuation" style="color:rgb(199, 146, 234)">)</span><br></span></code></pre></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="notebook">Notebook<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#notebook" class="hash-link" aria-label="Direct link to Notebook" title="Direct link to Notebook" translate="no">​</a></h2>
<p>You can find a simple example of using DGL for GCN <a href="https://colab.research.google.com/gist/AhmedCoolProjects/8854b7564f147bc41d2f7bdd397b0da8/dgl-first-application.ipynb" target="_blank" rel="noopener noreferrer" class="">here on Colab</a>.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="resources">Resources<a href="https://kb.bargady.online/blog/deep-graph-library-dgl#resources" class="hash-link" aria-label="Direct link to Resources" title="Direct link to Resources" translate="no">​</a></h2>
<ul>
<li class=""><a href="https://docs.dgl.ai/" target="_blank" rel="noopener noreferrer" class="">DGL Documentation</a></li>
<li class=""><a href="https://github.com/dmlc/dgl" target="_blank" rel="noopener noreferrer" class="">DGL GitHub</a></li>
<li class=""><a href="https://www.dgl.ai/dgl_docs/tutorials/blitz/index.html" target="_blank" rel="noopener noreferrer" class="">DGL Examples</a></li>
</ul>]]></content>
        <author>
            <name>Ahmed BARGADY</name>
        </author>
        <category label="Deep Graph Library" term="Deep Graph Library"/>
        <category label="Graph Neural Network" term="Graph Neural Network"/>
    </entry>
</feed>