<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sampath Kumaran Ganesan on Medium]]></title>
        <description><![CDATA[Stories by Sampath Kumaran Ganesan on Medium]]></description>
        <link>https://medium.com/@sampathkumaran?source=rss-aad9d491ee75------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dT38G3uZO3XjaYR-m3nlug.jpeg</url>
            <title>Stories by Sampath Kumaran Ganesan on Medium</title>
            <link>https://medium.com/@sampathkumaran?source=rss-aad9d491ee75------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 23:24:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sampathkumaran/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Quantum-Optimized Drone Fleet Management: From CQM to the Sky — Section 1]]></title>
            <link>https://sampathkumaran.medium.com/quantum-optimized-drone-fleet-management-from-cqm-to-the-sky-section-1-c02bcbb132b1?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/c02bcbb132b1</guid>
            <category><![CDATA[quantum-physics]]></category>
            <category><![CDATA[quantum-mechanics]]></category>
            <category><![CDATA[quantum-computing]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Thu, 06 Aug 2026 07:10:06 GMT</pubDate>
            <atom:updated>2026-08-06T07:10:06.148Z</atom:updated>
            <content:encoded><![CDATA[<p>Constrained Quadratic Model (CQM) for energy aware drone routing benchmarked against OR — TOOLS and visualized in real time</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ozoIVK5MnYQNlyXolc4Yvw.png" /></figure><p>The need for autonomous drone delivery is getting bigger now and most people believe it is going to be the trend in future. It may seem simple but it has a lot of constraints like battery capacity, payload capacity, delivery deadlines, battery swapping requirements etc., 😨</p><p>You might ask me a question like whether it is necessary to have a drone deliver a parcel?? This problem is not only related to it but also in Medical Emergency Logistics, Disaster Recovery, Smart Cities etc., It reduces traffic congestion, green house gas emissions, save energy substantially. So, It is becoming a necessity rather than sophistication.</p><p>The constraints mentioned like battery capacity etc., makes this vehicle routing problem as a <strong>combinatorial optimization</strong> problem whose complexity increases rapidly with the number of delivery locations as search space grows exponentially over time.</p><p>So, You might think of what is an optimization problem first?? 🤔</p><p>Optimization is maximizing or minimizing a function (often called as cost function or objective function or fitness function) while satisfying some constraints in order to find the best solution to a problem.</p><p>In combinatorial optimization, the objective is to determine an optimal combination of discrete decision variables that minimizes or maximizes a predefined objective / cost function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*uRW3T0lUUjDz7jITg_EALw.gif" /><figcaption>sample illustration of TSP courtesy: graphdeeplearning</figcaption></figure><p>The above image depicts example of combinatorial optimization (TSP — Travelling Salesman problem); given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?</p><p>In order to see why this problem is difficult, we need to know understand computational complexity — How much time and memory is required for solving a problem.</p><p><strong>Problem Type P </strong>— These are the problems that can be solved in polynomial time like O(N), O(N²), O(n log N) etc., e.g., Addition of two numbers. Easier to solve and easier to verify it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*EG_oDrqNQNy3ze3BjEJGew.png" /><figcaption>Problem Type P</figcaption></figure><p><strong>Problem Type NP</strong> — These are the problems that are hard to solve but easy to verify. e.g., Sudoku puzzles — Hard to solve but once you solve it, it is easy to verify or make sure it is correct.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*NLlodLkIDnOttsbHUGXrYg.png" /><figcaption>Problem Type NP</figcaption></figure><p><strong>Problem Type NP-Complete </strong>— These are the harder problems in NP type but once a solution is found for harder problem, it becomes easier. e.g., Let’s say you solve a master problem in a bunch of problems in NP type, you can easily solve the sub problems similar to it and can be easily verified.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*DUrvU6Ukw_SvWke27Z90iQ.png" /><figcaption>Problem Type NP — COMPLETE</figcaption></figure><p><strong>Problem Type NP-Hard</strong> — These are the hardest problems that may be NP-Complete or even outside of it because there are problems that cannot be verified easily also.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*I4A_YKeuLHGqbZNMAvrLzQ.png" /><figcaption>Problem Type — NP — HARD</figcaption></figure><p>Our Vehicle Routing problem is of type NP-Hard because it is an extension of TSP (Travelling Salesman Problem) because in TSP, there will be only one drone and no capacity constraints and exactly one time all customers parcel must be delivered and returned to the depot.</p><p>To understand the scale of our problem; consider there are 20 customers and if there is only one vehicle (becomes TSP because of 1 drone) the number of possible tours is <strong>6.08 x 10¹⁶</strong> which is very huge 🥶.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*hb8AXN4G9nkpK6Mrn--zkg.png" /></figure><p>19! → A single tour is a continuous loop so it does not matter where you start. We pick one location as an anchor point and avoid counting the shifted versions of the same loop. So, it always means (20–1)! → 19!</p><p>/ 2 → The distance between say location A to B is same as B to A also while moving forward and backward respectively. We divide the total permutations by 2 to only count distinct physical loops.</p><p>Now, We can see the reason why our problem of multi-vehicle routing with energy constraints becomes increasingly complex as the number of locations and drones increase.</p><p>So, First we will discuss about classical optimization approaches and followed by why quantum optimization fits here well.</p><p><strong>Classical Optimization:</strong></p><p>Over the years, there were a lot of different classical optimization algorithm types like exact algorithms, approximation algorithms, and heuristic/metaheuristic methods. The selection depends upon computational efficiency, solution quality, and scalability.</p><p>Most of the logistics companies like FedEx, UPS, Amazon etc., relies on these classical optimization algorithms and even google maps routing.</p><p><strong>Exact Optimization Methods</strong> — These methods almost always produce optimal solutions provided, there are computational resources available. Although exact algorithms produce optimal solutions, their computational requirements increase rapidly with problem size, making them impractical for large-scale routing problems. Some of the examples of this type are: Integer Linear Programming (ILP), Dynamic Programming etc.,</p><p><strong>Approximation Methods</strong> — These methods provide solutions close to optimal solutions but within a performance bound. However, many practical variants of the Vehicle Routing Problem, including drone routing with multiple operational constraints, lack efficient approximation algorithms with strong performance guarantees.</p><p><strong>Heuristic Methods </strong>— These methods produce often satisfactory results close to optimal within a short span of time with the specific rules for the use case as they do not go through the entire search space. The problem with these methods is it can stuck at a local optima and generally do not guarantee the best possible solution. Some of the examples of this method are Nearest Neighbors, greedy construction etc.,</p><p><strong>Metaheuristic Methods</strong> — Mainly used for exploring large combinatorial search spaces. They balance exploration and exploitation to search for high-quality solutions while avoiding premature convergence. They have used in various Vehicle Routing Problem variants but their main concern is, the solution depends upon initialization, stopping criteria, search strategy etc., More work on tuning these is required. Also, these methods are stochastic or probabilistic and repeated runs, do not provide the results. Some of the examples of this method are Genetic Algorithm, Tabu search etc.,</p><p><strong>Google OR-Tools — </strong>Among the classical optimization frameworks, Google OR-Tools has become one of the most widely adopted open-source libraries for solving routing and scheduling problems. OR-Tools provides highly optimized implementations of constraint programming, local search algorithms, and specialized routing heuristics, making it an excellent baseline for benchmarking combinatorial optimization problems. Although OR-Tools does not guarantee globally optimal solutions for large NP-hard problems, it consistently produces high-quality solutions within practical computation times.</p><p><strong>Why Benchmark Against OR-Tools? </strong>— When evaluating a new optimization approach — particularly a quantum or hybrid quantum-classical method, it is essential to compare its performance against a strong classical baseline.</p><p>We will compare OR-Tools performance with a <strong>Constrained Quadratic Model (CQM)</strong>-based quantum optimization approach across several metrics, including:</p><ul><li>Total route distance</li><li>Energy consumption</li><li>Constraint satisfaction</li><li>Computational runtime</li></ul><p>The goal is not to demonstrate that quantum optimization universally outperforms classical methods. Instead, the objective is to understand where each approach excels, identify their respective strengths and limitations, and explore whether quantum optimization offers advantages for solving increasingly complex Drone Electric Vehicle Routing Problems (E — VRP).</p><h4>Quantum Optimization:</h4><p>Quantum Computing (QC) is another paradigm of computing using quantum mechanics like superposition and entanglement which allow us to have systems that can solve optimization problems at very very quick time with its massive parallelization capabilities.</p><p>The key components are:</p><ul><li><strong>Superposition </strong>— Superposition means that unlike a classical bit, which can only be a 0 or 1, a quantum bit (qubit) can exist in a linear combination of both states at the same time. This allows quantum computers to process many possibilities simultaneously, giving them immense parallelism.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*fWOaQECo5j6Ee87IOTZ4TQ.gif" /><figcaption>Illustration of Quantum Superposition</figcaption></figure><ul><li><strong>Entanglement </strong>— Entanglement, on the other hand, is a special quantum connection between qubits where the state of one qubit is directly linked to the state of another, no matter how far apart they are. This correlation enables qubits to work together in powerful ways, making quantum computing capable of solving problems that are extremely difficult for classical systems.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*8ROM2Tg_mojSE-y7ypj7PQ.png" /><figcaption>Illustration of Quantum Entanglement</figcaption></figure><p>We represent qubit using Dirac notation. <strong>∣0⟩, ∣1⟩. </strong>Qubits are two state systems and represented as vectors.</p><p><strong>0 → ∣0⟩ and 1 → ∣1⟩ — </strong>classical bit -&gt; qubit.</p><p><strong><em>NOTE — </em></strong><em>Quantum computers do not magically solve NP-hard problems in polynomial time. Instead, they may provide computational advantages for certain optimization tasks through quantum search and specialized optimization algorithms.</em></p><p>In this section, we established why the problem of drone vehicle routing with constraints is a complex one and we discussed about classical optimizations, their pros and cons. In the next section, we will deep dive into quantum optimization and an exciting demo of energy constrained drone vehicle routing problem.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c02bcbb132b1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Forecasting Logical Qubit Failure with Graph Neural Networks — Part 1 — Basics and Bit flip code]]></title>
            <link>https://sampathkumaran.medium.com/forecasting-logical-qubit-failure-with-graph-neural-networks-part-1-basics-and-bit-flip-code-2ac763bb08ac?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/2ac763bb08ac</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[quantum-computing]]></category>
            <category><![CDATA[quantum-physics]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Fri, 12 Jun 2026 06:14:20 GMT</pubDate>
            <atom:updated>2026-06-12T06:14:20.647Z</atom:updated>
            <content:encoded><![CDATA[<p>Can Quantum Computers Predict Their Own Failures Before They Happen?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*zaV1fKA1NldF4l7kNx2lXg.png" /></figure><p>Quantum Computing is poised to change the industries like drug discovery &amp; pharmaceuticals, logistics, material science &amp; chemistry, climate modelling &amp; simulation etc., in near term for good.</p><p>Quantum computers differ from classical computers because of quantum phenomena like Superposition, Entanglement and Interference which makes them incredibly efficient for optimization problems.</p><p>Quantum computers are very delicate systems since it deals with sub-atomic particles like electrons or photons or trapped ions etc., Let us say that we have a quantum computer which makes use of electrons. Electron can have spin as one property along with mass and charge. Spin can be Spin up or Spin down or can be in linear combination of both Spin up and Spin down state (Superposition principle).</p><p>Even a small temperature noise or electromagnetic fields or even vibrations can cause the quantum system to lose its coherence. We call that as quantum decoherence (Loss of information of its system to the environment) thus making the system to behave more like a classical counterpart.</p><p>This is the main reason why quantum computers are not that prevalent like classical computers. As of now, the quantum chip must be kept at 0 Kelvin or -273 degree Celsius which is colder than outer space🥶usually placed in liquid helium.</p><p>We are in the era of NISQ (Noisy Intermediate Scale Quantum) quantum systems. Most of the research in reducing these errors are happening in hardware side of quantum systems. In this blog (inspired from my paper), we will focus on software side of reducing some of these errors that can occur in quantum systems.</p><p>In classical computing, the basic errors like bit-flip (0 to 1 or 1 to 0) can be prevented by using replication and then taking majority voting also called as 3-bit repetition code. The main steps are Encoding, Transmission and Decoding.</p><p>Say, We want to prevent a bit D which is 0. Encoding phase is; create a replica of it three times which is 000. In Transmission phase, the encoded string is saved in memory and sent over a network. A bit flip can occur random due to noise and 000 becomes 010. The receiver gets the corrupted data string but it decodes the data using majority vote to get the correct bit D which is 0.</p><p>But in quantum systems it is not possible since we can’t replicate a qubit; thanks to No-Cloning Theorem🙄which states “<em>It is impossible to create an exact copy of an arbitrary unknown quantum state</em>”.</p><p>We have to rely on different kind of techniques compared to classical systems to mitigate errors. There are many different types of quantum error correction codes but the main 4 types are: Bit flip code, Phase flip code, Shor’s code and Surface code.</p><p>Let us quickly look into bit flip, phase flip in quantum systems in this part.</p><p>Bit Flip Code — We know that because of no cloning theorem in quantum, we can’t use repetition of qubits as in classical systems. But we can make use of Entanglement (Two qubits are entangled, if we measure one qubit, we will know the outcome other qubit no matter how far they are apart) property of quantum.</p><p>There are 3 main steps in this code:</p><p>Encoding — We use a qubit Q say |0&gt; as logical qubit. We will store the information on this logical qubit into three physical qubits. Below is the sample format. Here L means logical qubit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/128/1*ruPSIPW5ZuDqORRe_5sgfg.png" /></figure><p>We say a generic qubit as,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/182/1*_8fW030QTTPuQgLumtQ1Iw.png" /></figure><p>where alpha and beta are complex amplitudes. The encoded qubit now becomes,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/246/1*hkxpVUDE8dR6HqhRocsIZw.png" /></figure><p>Now, the information is spread across 3 qubits. You can ask me a question like how do we create this sort of circuit. The answer is very simple. We take our original qubit and use two <em>ancillary </em>qubits (also called as ancilla qubits) and a sequence of two CNOT gates (similar to classical XOR gate. It is a multi qubit gate, first qubit is called as control qubit and second qubit is target qubit. The target qubit will fire only when control qubit is |1&gt;) following that to create an entangled state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/181/1*tPV75xj00LJuUtd7B67H5Q.png" /></figure><p>Below is the circuit diagram for this: Here, q0 is the control qubit and q1, q2 are the target qubits.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*eFhegMsBjniDU5bHaJPQCw.png" /></figure><p>Syndrome Measurement — Now, let us assume an error occurred in the second qubit, it just flips from |0&gt; to |1&gt; or |1&gt; to |0&gt;. We can simulate the bit flip using a Pauli X gate (similar to classical NOT gate). Now, the error operator will be like,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/173/1*_Aul1gVwpg2S8WrfVOH4bA.png" /></figure><p>Where I is the Identity Gate (similar to Identity matrix in classical system). We use tensor product when we are using multi-qubits.</p><p>Now, Applying this gate, we get:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/171/1*UGczh0n4OXF4k5s2hhKYNw.png" /></figure><p>Now, we can write this as,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/245/1*bLHMMRwoZL6lQBS8z1qbqg.png" /></figure><p>We can see that alpha and beta are preserved and still exists!!! If we directly measure the qubits, superposition will be lost. So, we measure parity checks. We compare qubit 1 with qubit 2, qubit 2 with qubit 3. These measurements are called as “<em>Syndromes</em>”.</p><p>We will use something called as “<em>stabilizers”</em>, that will help us to detect errors without destroying the delicate quantum information. Stabilizers are mathematical operators (matrices) based on combinations of Pauli X, Y, Z gates.</p><p>The code uses two stabilizers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/116/1*WH0wMXEUYizOdvy0LtOoVA.png" /></figure><p>where Z is a Pauli-Z gate which is used to flip only the phase of the qubit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/142/1*M3WjX-epDo450165Xr8fTA.png" /></figure><p>Stabilizer should satisfy this condition,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/121/1*djY7HfXNjOAU8suZQWtMFg.png" /></figure><p>You can see from the above equation, it is similar to psi-ket be the eigen vector and +1 is the eigen value of that operator. If eigen value is +1, it means qubit is stabilized.</p><blockquote>Quick Math refresher: <strong>S * v = a * v</strong></blockquote><blockquote>Where S is the matrix or operator acting on a vector, v is the eigen vector and a is the eigen value (scalar).</blockquote><p>Let us see an example, Applying Z on |0&gt;,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/114/1*JRsLqY0b_I_0TY-O7raNMA.png" /></figure><p>We can say that |0&gt; is stabilized by Z.</p><p>Now, we can apply Z on |1&gt;,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/129/1*PJSvND6uKemr86EuNHgUfQ.png" /></figure><p>We can say that |1&gt; is not stabilized by Z.</p><p>The 3-qubit bit flip code uses 2 stabilizers S1 = Z1 * Z2 and S2 = Z2 * Z3.</p><p>For valid codes,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/191/1*DhK0E95maNeffoz1_8U56g.png" /></figure><p>We can see that S1 = +1 and S2 = +1. Now, Let us see what happens after an error has occurred in qubit 2. Applying S1 and S2 to this error state;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/172/1*r_UYNcfB1sekzR_sLo4luA.png" /></figure><p>We will get the syndrome as (-1, -1). We also have a syndrome table for this sort of code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*iVwiA-0ah9rNAZjEgSAT3w.png" /></figure><p>Let us see the syndrome measurement circuit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*gbK9s86OMCRQnOcWkgSj-Q.png" /></figure><p>ancilla 0 measures Z1 * Z2 and ancilla 1 measures Z2 * Z3. Only syndrome information is measured. The amplitudes alpha and beta are left untouched.</p><p>Since our syndrome is (-1, -1), we can infer from the above table that error occurred on qubit 2.</p><p>Correction — Since we have identified the qubit where the X flip error occurred, we can correct it by applying the X gate onto the second qubit to reverse the flip to the original state.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/239/1*wcshXD6lyg6PSLFuq2D4Ew.png" /></figure><p>The above operation tells us that we introduced error to qubit 2 by applying X2 and same can be reversed by applying the same X2 again after identifying the error qubit😎. It is possible because, applying twice of X gate will return identity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/85/1*CHqbiPJItFKHmPI23xrPxA.png" /></figure><p>Now, we know how to correct bit flip error which is one type of quantum error correction. This the simplest quantum error correction code. We went elaborate here to understand stabilizers well which will be useful when we discuss our main topic with surface error correction code.</p><p>Even using Surface code (most used one — which we will see in next part), errors continuously accumulate over time during quantum computation. These decoders will say “<em>What error just happened?</em>”.</p><p>But we will discuss about a more powerful question — “<em>Can we predict an error before it occurs?</em>”. We will discuss how Graph Neural Networks (GNN) can transform how quantum error correction works from reactive to predictive one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/1*xwSMAJH3diU79MizJ1vgGA.png" /></figure><p>Graph Neural Networks (GNN) — Unlike traditional neural networks, which operate on structured data like images or text, GNNs help graph structures to learn meaningful representations of nodes, edges, and entire graphs.</p><p>This part covers the basics that we will use in next part where will get to the core of forecasting error in quantum systems using GNN.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2ac763bb08ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why AI Hasn’t “Solved” Drug Discovery]]></title>
            <link>https://sampathkumaran.medium.com/why-ai-hasnt-solved-drug-discovery-8aca6fb3c616?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/8aca6fb3c616</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[computational-biology]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[medicine]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Mon, 09 Mar 2026 10:40:53 GMT</pubDate>
            <atom:updated>2026-03-09T10:40:53.741Z</atom:updated>
            <content:encoded><![CDATA[<p>Binding isn’t efficacy, datasets lie, and biology doesn’t care about your AUC</p><p>Drug discovery is often framed as a search problem. In principle, if we could explore enough of chemical space and find molecules that bind to the right biological targets, we should be able to design new medicines.</p><p>In reality, the problem is far more complicated.</p><p><strong>Chemical space —</strong> We have nearly 10⁶³ different kinds of drug like chemical compounds in our world. 😲</p><p><strong>Biological space — </strong>We have nearly 10⁵ different kinds of biomolecules like proteins, carbohydrates, nucleic acids etc., that were identified till now as biological target which we need to influence with those chemicals to produce the desired medical effect.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7k9pmJxez1dokFh3Y1xqRA.png" /></figure><p>So, we can understand the complexity of drug discovery since the search space is huge.</p><p>First, we need to know some basic terminology in drug design.</p><p><strong>Molecules </strong>— It is a combination of two or more atoms chemically bonded together. We know very water molecule contains two hydrogen atoms and one oxygen atom (H2o)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*wrux7unk43MZ_6e9v695Jg.jpeg" /></figure><p><strong>SMILES </strong>— Now, you might be wondering how to program a molecule for the computer to understand 🤔. Here comes a representation called as SMILES (Simplified Molecular Input Line Entry System). It is a dictionary of how atoms and their bonds connect to form a molecule along with a set of grammar rules. for e.g., <strong>O=C=O</strong> is the SMILES representation of Carbon Dioxide (CO2). From the SMILES string, you can see that atoms are represented by their atomic symbols and bonds between them are represented as (“=”: double bond, “-”: single bond, ‘#’: triple bond) etc.., There will be no space between the characters.</p><p><strong>Target </strong>— A target is the specific thing in the body a drug is meant to act on. For e.g., COVID-19 is caused by a virus. That virus has important parts it needs to survive and infect us. Any one of those parts can be a drug target. For COVID-19, the main target is “spike protein”. The virus uses spike protein as a hook and grabs lung cells and enter into it. If you block the spike, the virus can’t enter cells.<strong> </strong>COVID vaccines train your immune system to recognize the spike protein. Antibodies bind to the spike and neutralize the virus.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1e41Wa5gq1jmD7otjVv6DA.png" /></figure><p><strong>Assay — </strong>It is a test or experiment used to check whether something works or not. Think of it as like a lab test that tells whether the drug works as it suppose to work. for e.g., In COVID-19, an assay answers questions like “Does this drug stop the virus?”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HjXRPcJ4C9Hb2upevR76hA.png" /></figure><p><strong>Binding —</strong> It means, a drug is physically attached to the target. Strong binding — The drug attaches to the target for a long time. Weak binding — the drug attaches to the target for a short time.</p><p><strong>Affinity </strong>— It is closely related to Binding but very precise. It measures how strongly a molecule binds to a target protein. It is measured using dissociation constant (KD). Lower KD means stronger binding and Higher KD means weaker binding. AI models often predict binding affinity, not real drug effectiveness.</p><p><strong>Selectivity —</strong> It is the most important property of a drug. It means the drug binds strongly to only the desired targets and not to any other proteins. Many drugs fail because of poor selectivity.</p><p><strong>ADMET — </strong>This is super critical in drug discovery. The abbreviation stands for: A — Absorption, D — Distribution, M —Metabolism, E — Excretion and T — Toxicity. A molecule may bind well in docking but poor absorption, high toxicity mail fail the drug. This is a major reason AI predictions fail.</p><p><strong>De novo design — </strong>De novo design means designing a brand-new drug molecule from scratch. It’s like inventing a new tool instead of modifying an old one.</p><p><strong>Efficacy — </strong>It is a measure of how well the drug works when it is working. It is nothing but “How strong is this medicine at fixing the problem?”. High efficacy means the drug works well and vice-versa.</p><p><strong>RDKIT </strong>— We will be using the RDKIT as the cheminformatics software. It can seamlessly work together with machine learning libraries like scikit-learn. Molecules will be represented as Objects. Object attributes are atoms, bonds and their connectivity. Methods are functions to manipulate the molecules and analyze them.</p><p><strong>Molecular Docking — </strong>Molecular docking is a computer technique used in drug discovery to predict how a small molecule (drug candidate) fits into a protein target in the body. Molecular docking can predict if a molecule fits into a protein, but a good fit does not guarantee a good medicine — because real biological systems are far more complex than a simple lock-and-key interaction.</p><p><strong>Virtual Screening — </strong>It is a technique where scientists use computers to search millions or billions of molecules to find ones that might work as drugs. Instead of physically testing chemicals in a lab (which is slow and expensive), researchers simulate the interaction between molecules and a disease target on a computer.</p><p><strong>Molecular Fingerprints — </strong>Very important concept in Machine Learning for molecules. Fingerprints convert molecules into binary vectors. It is used for similarity search, QSAR modelling etc.,</p><p><strong>QSAR (Quantitative Structure–Activity Relationship)</strong> — It is one of the oldest AI models in the drug industry. It tries to learn molecular structure to biological activity. The flow is like SMILES → Fingerprints → Machine Learning Model → Predict Biological Activity.</p><p><strong>Activity Cliffs — </strong>This is one of the hardest part in the AI based drug discovery. An activity cliff happens when two molecules that look almost identical chemically show a huge difference in biological activity. A tiny change in a molecule can make the drug go from very effective to completely useless. Most AI models thinks like similar molecules has similar activity. This is called as Similarity Principle. But Activity Cliffs violate this principle. Even tiny molecular changes can produce massive biological effects, making it extremely difficult for models trained on limited datasets to generalize.</p><p><strong>Pharmacokinetics (PK) and Pharmacodynamics (PD) — </strong>Pharmacokinetics describes how a drug moves through the body (absorption, distribution, metabolism, and excretion), while pharmacodynamics describes how the drug interacts with its biological target to produce a therapeutic effect. An ideal drug should satisfy both PK and PD. Most AI drug discovery models predict things like: binding affinity, Molecular similarity, Docking scores. But a successful drug needs more than that like PK properties.</p><p>We also need to know a bit about the phases in drug discovery. We will see an example of COVID-19 drug discovery to explain the phases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fsY8cgortTSKLfL3Ttzrfw.png" /></figure><p><strong>Target Identification </strong>— Scientists first study the virus to find a <strong>weak point</strong>. For e.g., In COVID-19, Spike Protein allows the virus to enter the human cell. RNA polymerase helps the virus to replicate itself. So, In this phase, we identify a biomolecule target that could be associated with one of COVID-19 virus’s mechanism. In short, Target Identification is finding the engine part of the virus that keeps it running.</p><p><strong>Hit Discovery</strong> — Scientists test <strong>thousands or millions of molecules</strong> to see if any can stop the virus. The methods used during the covid are: 1. Screening existing drugs (<strong>drug repurposing</strong>), 2. AI searching chemical libraries and 3. Lab experiments on viral proteins. The famous hit discovery was Remdesivir for COVID-19. One main thing to notice is that the chemical needs to bind to the biological target and also should not bind to other biomolecules that are not of interest.</p><p><strong>Lead Optimization — </strong>After hit discovery, Scientist’s work on How can we make the molecule a better drug? In Hit Discovery we will identify many hits that will then go through subsequent screening and assays to narrow down the list to a smaller number of therapeutically useful compounds, called leads. The early hits are usually weak. Scientists improve them so they: Block the virus more strongly, Stay in the body long enough and Safe for Humans.</p><p><strong>Pre-clinical Testing — </strong>Is it safe before giving it to humans? Scientists test the drug in Cell Cultures, Animal models (mice, monkeys). Remdesivir was tested in infected animal models before human trials. During this phase, scientists check whether it stops the virus?, Does it cause toxicity? and what dose it works? Simple Analogy is, “Test the tool in controlled environments before using it in real life”.</p><p><strong>Clinical Trials — </strong>Scientists test Does the drug work in people? Human Trials happen in three phases:</p><p>Phase 1: Small group (20–100 people). Check safety and determine the dose</p><p>Phase 2: Hundreds of patients. Does it work against COVID-19? Check the side effects.</p><p>Phase 3: Thousands of patients. Confirm effectiveness of the drug and compare with existing medications.</p><p>For COVID-19, Large trials showed Paxlovid reduces hospitalization risk.</p><p><strong>Drug Approval — </strong>If the clinical trials are successful, regulators approve the drug. Regulators like FDA, review safety, manufacturing quality and its effectiveness. For COVID-19, the approved drugs are Remdesivir, Paxlovid, Molnupiravir.</p><p>In this blog, we explored some of the basic terminology used in drug discovery — molecules, SMILES, biological targets, assays, binding, molecular docking, and virtual screening — along with the major phases of drug development from target identification to clinical trials and drug approval.</p><p>Drug discovery is often portrayed as a problem that AI should easily solve: feed molecular data into a model, optimize a loss function, and discover the next blockbuster drug. In reality, the problem is far more complex.</p><p>Even if we can computationally explore billions of molecules using techniques like virtual screening, molecular docking, and de novo design, success in the real world depends on far more than just binding.</p><p>A molecule must not only bind to the intended target but must also be selective, safe, stable in the body, metabolically viable, and clinically effective. Concepts like ADMET, pharmacokinetics (PK), pharmacodynamics (PD), assay variability, and activity cliffs make the landscape highly nonlinear and difficult to model.</p><p>This is why many AI models that perform well on benchmarks — predicting binding affinity or optimizing docking scores — often fail when translated into real biological systems. Biology is not a smooth optimization surface, and the datasets we train on capture only a tiny fraction of the true complexity.</p><p>Understanding these foundational concepts is the first step toward appreciating both <strong>the potential and the limitations of AI in drug discovery</strong>.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8aca6fb3c616" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can AI Detect Heart Toxicity from Beating Cells (Cardiomyocytes) using cardiac organoids?]]></title>
            <link>https://sampathkumaran.medium.com/can-ai-detect-heart-toxicity-from-beating-cells-cardiomyocytes-using-cardiac-organoids-1586b9981443?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/1586b9981443</guid>
            <category><![CDATA[biotechnology]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[biology]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Mon, 22 Dec 2025 08:12:06 GMT</pubDate>
            <atom:updated>2025-12-22T08:12:06.168Z</atom:updated>
            <content:encoded><![CDATA[<p>How Motion-Based Deep Learning Reveals Early Cardiotoxicity</p><blockquote>The answer to the question is Yes. Let’s say you take a medicine or drug that harms your heart. The first signal is not cell death or change in structure of the cells but rather alterations in how the cells beat like irregular timing, contraction &amp; relaxation alterations.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*r8fMZvH6OgupReSgWkRxZQ.png" /><figcaption>Overview of AI-driven cardiotoxicity screening process</figcaption></figure><p>In this post, we will delve deep into cardiac organoids + AI for identifying early cardiotoxicity in patients.</p><p>Cardiotoxicity means damage or harmful effects on the heart caused by drugs, chemicals, or other therapies. It can lead to heart failure or even sudden death. The traditional way of detecting Cardiotoxicity is through ECG, Echocardiography and troponin levels in blood. But there are several disadvantages through traditional approach like — Toxicity is often detected late, after damage has occurred. Poor ability to predict patient-specific risk. Most of the drugs are tested on animals like rodents — Animal hearts differ from human ion-channel physiology.</p><p>This is where Organoid AI comes in…</p><p>What is an Organoid? — It is a miniature, lab-grown organ models from stem cells or patient tissue.</p><p>So, You might have a question now like why we need AI here??🤷‍♂️🤷‍♀️</p><p>The answer is, Human observers may miss the changes like irregular beat timing, delayed relaxation (The heart cells take longer than normal to relax after each beat) and reduced contraction strength (The heart cells do not squeeze as strongly as they should) in microscopy videos, but AI is exceptionally good at detecting consistent patterns in motion over time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/1*-j29dbCbpFmvqg2qnjYung.png" /><figcaption>Microscopic comparison of cardiac cell behavior</figcaption></figure><p>So, we say that during delayed relaxation, the heart fills less efficiently between beats and during reduced contraction, the heart pumps out less blood per beat.</p><p>This makes beating cardiac cells — derived from human iPSCs, an ideal system for early cardiotoxicity detection.</p><p>iPSC — (induced Pluripotent Stem Cells). iPSCs are adult human cells that are reprogrammed back into a stem-cell state and can then be turned into heart cells, brain cells, or other tissues in the lab for drug-safety testing.</p><p>Organoid AI is combining organoids with AI to measure, predict and optimize medical decisions.</p><p>Below are potential advantages of Organoid AI:</p><ol><li>Cardiac organoids are derived from human iPSCs — it expresses human ion channels and also patient centric.</li><li>As we said earlier, Organoid AI focuses on motion and dynamics like heart beat, relaxation and contraction of beating cells rather than structural indications like cell death thereby providing early warnings.</li><li>It can detect from microscopic videos that are too subtle for human eye to predict.</li><li>It is safer, faster and also scalable.</li></ol><p>So, we can safely say that using organoid AI, we can predict cardiotoxicity earlier and more accurately by learning subtle functional changes in beating human heart tissue — before damage becomes clinically visible.</p><p><em>I used sample synthetic data of beating cardiac microtissue videos and apply controlled motion perturbations that mimic known cardiotoxic phenotypes. This allows us to demonstrate how AI detects functional instability without requiring proprietary wet-lab data.</em></p><p>Now, let’s observe how normally beating iPSC-derived cardiomyocytes (beating cells) appear under the microscope.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*Q5HK0EtQBcI43Oeq6YVhQg.gif" /><figcaption>Normal beating cells</figcaption></figure><p>You can see from the above video, the cells are beating at a rhythm and there is no evidence of structural changes in it.</p><p>Now, let’s observe how delayed relaxation in beating iPSC-derived cardiomyocytes (beating cells) appear under the microscope.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*TokinVIt3siPorgQcfs-YQ.gif" /><figcaption>Delayed relaxation of beating cells</figcaption></figure><p>Now, we can see that, it is very tough for the human to correctly identify Cardiotoxicity in early stages itself. This is just one example of delayed relaxation. There is where Organoid AI shines.</p><p>Now, we can see how cardiotoxicity can be predicted with CNN (Convolutional Neural Networks) + optical flow. We use a two-stream neural network for detecting drug-induced cardiotoxicity from iPSC-derived cardiac organoid beating videos.</p><p>The first stream is called as spatial stream — CNN extracts the appearance features from the video frames.</p><p>The second stream is called as temporal stream — CNN extracts the motion features using optical flow.</p><p>Then, these two streams will be fused for the final prediction.</p><p>Now, we will do a quick refresh of CNN and Optical Flow.</p><p><strong>CNN (Convolutional Neural Networks) </strong>— It is a type of neural network that sees and learns patterns in images similar to how we see the images with shapes and objects but in a mathematical way. In our case, we use microscopy frame of beating heart cells. The patterns it is going to learn are how cells move, contract, and relax.</p><p>Below are the steps for processing a single frame of the microscopic video (30 frames per second).</p><p>Input — Passing a raw microscopic image. We see the shape and texture very well. But for a CNN, it is just a grid of pixel intensities (numbers).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*lIbxLAFUrN54Jv9zPIqaQA.png" /></figure><p>Now, let us see the steps in it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/1*S8VfJrhepOpQZ0-zKZvUyA.png" /></figure><p>STEP 1 — <strong>Convolutional Layer</strong> — It is used for identifying the edges and textures. Small filters also called patches (e.g., 5×5) slide over the image. Each filter learns a different visual patterns. Now, the CNN will learn the cell boundaries and striations (the characteristic striped or banded appearance of certain muscle tissues like cardiac muscle). The output of the convolution step is called as feature map. It is not an image. Now, we can say that CNN now sees meaningful patterns combined from different filters rather than pixels only.</p><p>STEP 2 — <strong>Max Pooling Layer (Downsampling layer)</strong> — It will keep only the strongest signal and throw away tiny details. It is like saying, I don’t need exact pixel location — just tell me if something important is happening here. So, it makes the model faster, robust and small shifts in cells do not confuse the model.</p><p>STEP 3 — <strong>Deeper Layers — </strong>So, initially we saw that convolution and pooling layer learnt about edges and textures. Now, in the deeper conv and pooling layers will combine the simple textures learnt in previous layers into meaning representations. It can identify the relaxation region, contraction regions over the subsequent layers. Now, the CNN will learn strong vs weak contraction and smooth vs delayed relaxation.</p><p>STEP 4 — <strong>Learning Beat Rhythm over time and outputs the result</strong> (Dense Layers) — We know that video is a sequence of frames. So, CNN for every frame process the frame in a sequence manner, it can learn the temporal patterns (learning over time), it identifies beat rate, beat to beat variability. The CNN combines everything it learned and answers questions like: Is this beating normal?, Does this look toxic?. Now, it learns about contraction, relaxation and beat irregularity which is very tough for humans to interpret. Now, at the end, we will use the normal feed forward network to make the final classification of whether it is toxic or normal etc.,</p><p><strong>So, CNNs don’t understand “heart cells” — they understand motion, patterns, and change. That’s exactly what cardiotoxicity looks like at its earliest stage.</strong></p><p><strong>Optical Flow — </strong>Cardiotoxicity is primarily a <em>motion problem</em>, not a static image problem. We know that cardiotoxicity shows up as irregular heart beats, delayed relaxation and weaker contraction. These changes are hard to see in a single microscopy frame, but very clear when you analyze how pixels move over time.</p><p>That’s exactly what optical flow captures. Optical flow measures how each pixel moves between consecutive video frames. In our case, Instead of asking <em>“What do the cells look like?”, </em>optical flow asks <em>“How do the cells move?”. </em>This is very important for cardiotoxicity.</p><p>We start with a microscopic video as saw earlier. Then go frame by frame like Frame 1 → Frame 2 → Frame 3 → … Then optical flow is computed between the frames. <strong>Frame(t) → Frame(t+1) → Flow map (u, v)</strong></p><ol><li>t — Frame number</li><li>u — horizontal motion</li><li>v — vertical motion</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QHj0gkLHe2wrxkMU3OL_SA.png" /><figcaption>Optical Flow steps in microscopic beating cell images</figcaption></figure><p>So, Optical flow allows the AI to “see” how heart cells move over time, turning subtle changes in beating dynamics into measurable signals of cardiotoxicity.</p><p>Typically, we use dense optical flow which computes motion for each and every pixel. We use Farneback Algorithm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-7kPld8l4XXPAsYvn553xQ.png" /><figcaption>Farneback Algorithm</figcaption></figure><p><strong>STEP 1 </strong>— Take two consecutive frames from the video as t and t + 1 from our microscopic cell video. The goal is to estimate how every pixel moved from frame t to frame t + 1.</p><p><strong>STEP 2</strong> — Compute Gaussian Pyramids, i.e., create images in different sizes like 224x224, 112x112, 56x56 etc., It is because to capture both very small or large motions of pixels.</p><p><strong>STEP 3 — </strong>Calculate the Optical flow<strong> — </strong>For each pixel, estimate the local neighborhood modelled as quadratic polynomials. The displacement that best aligns these polynomials gives the motion vector.</p><p><strong>STEP 4 —</strong>Raw motion vectors into interpretable visual outputs.</p><p>Now, we will calculate the optical flow image for the entire video. Then we use dual stream mode like only raw image and only optical flow images pass those to CNN and then fuse the output of both and then do the decision making with feed forward neural network.</p><p>Let’s see the entire architecture of the two-stream model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9CHC4fUvtihMIiglzsfXXw.png" /></figure><p>Code is provided as .ipynb notebook at the end.</p><p>Now, we have understood how organoid AI can be useful in future. We have looked into only cardiac organoids but can also be used as brain organoids, kidney organoids and other organs also which shall help in detecting diagnosis earlier, precision medicine etc.,</p><p>But, there are some caveats also like it lacks full vascular system, variations in cell maturity and mainly ethical considerations which needs to be handles with care.</p><p>Thanks for reading!!!</p><p><a href="https://github.com/sampathkumaran/Cardiotoxicity/tree/main">GitHub - sampathkumaran/Cardiotoxicity</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1586b9981443" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Autonomous Drones Powered by Quantum Reservoir Computing (QRC) — Part 1]]></title>
            <link>https://sampathkumaran.medium.com/autonomous-drones-powered-by-quantum-reservoir-computing-qrc-part-1-db418cd310b4?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/db418cd310b4</guid>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[robotics]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[quantum-computing]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Fri, 14 Nov 2025 11:41:14 GMT</pubDate>
            <atom:updated>2025-11-14T11:41:14.804Z</atom:updated>
            <content:encoded><![CDATA[<h3>Autonomous Drones Powered by Quantum Reservoir Computing (QRC) — Part 1</h3><p>Harnessing Quantum Dynamics for Real-Time Flight Control and Adaptive Intelligence</p><p>What is <strong>Reservoir Computing</strong> — It is a machine learning approach, significantly different from traditional ML techniques especially for processing time series data, sequential data like speech recognition, robot controls, natural language processing etc.,</p><p>Reservoir Computing uses a reservoir — a fixed, random and high dimensional Recurrent Neural Network (RNN) which acts a non-linear computational medium. It is called as “<strong>Echo State Network</strong>” (ESN). This reservoir layer does not need any training.</p><p>The next layer from the reservoir is called as Readout Layer. It is used as a layer to map the reservoir states to outputs. It can be any supervised ML algorithm and its parameters needs to be learned.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*XdOl1v8whcUXtmUypLvoGQ.gif" /><figcaption>Sample Illustration of Reservoir Computing</figcaption></figure><p>Let’s see analogy for reservoir computing, a pond with rain drops.</p><p>Think of the pond as a “<strong>Reservoir</strong>” — a complex system that naturally responds to inputs. The water has its own physics that create intricate ripple patterns.</p><p>When the rain drops (<strong>Input</strong>) fall on the pond, they create ripples that spread out, bounce off edges, and interact with each other in complex ways.</p><p>Reading the ripples (<strong>Output</strong>) — Instead of trying to control how the water moves (which would be incredibly difficult), you simply learn to “read” the ripple patterns on the surface.</p><p><strong>Blue drops</strong> (inputs) falling into the pond at different locations</p><p><strong>Expanding ripples</strong> that interact and create complex patterns (reservoir processing)</p><p><strong>Red dots</strong> that light up as ripples pass through them (output layer reading the patterns)</p><p>The pond does all the complex processing naturally! You don’t need to engineer how the ripples should behave — you just use the pond’s natural dynamics and learn to interpret the results.</p><p>Ok, Now you might have a big question — Why should I use Reservoir Computing while traditional machine learning / deep learning works very well??🤷‍♀️🤷‍♂️</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*VKzSgPppmMd8MyEYLnEUTg.png" /><figcaption>Reservoir Computing — Courtesy: European Space Agency</figcaption></figure><p>The answer is, the computational power of Reservoir Computing is very high especially for processing complex temporal data. Normal RNN suffers from vanishing gradient problem or exploding gradient problem but in reservoir computing, it uses fixed and random connections mitigate this problem of traditional RNN’s enabling the training of Readout Layer smooth and very fast. We will be optimizing only the Readout layer not the reservoir layer.</p><p>Since, it’s computational power being very high and high training efficiency, it is suitable for real time analysis, online learning which traditional deep learning techniques struggles with low compute power environments. Also, deep learning models require a large amount of training data while Reservoir Computing requires only small amount of data provided sufficient reservoir size is present.</p><p><strong>Echo State Network </strong>(ESN) — It consists of three layers as depicted in the above figure. The three layers are an input layer, reservoir (hidden layer) and readout (output) layer.</p><p>The input is any of a time varying data or sequential data. Reservoir is the key layer and it is responsible for capturing and processing the temporal dependencies in the input data. This is nothing but a fixed RNN layer where the weights / parameters are fixed and not trained. They are randomly initialized.</p><p>The reservoir contains a property called as “<strong>Echo State Property</strong>” — The reservoir has the ability to retain the relevant information from the input data over time i.e., the past information reverberates in the reservoir dynamics which allows to capture temporal dynamics. It can forget the irrelevant details of the input overtime.</p><p>The nodes in the reservoir layer contains an activation function like sigmoid, hyperbolic tangent functions etc., which determines the non-linearity of the layer.</p><p>The connections between the nodes in the reservoir layer can be sparse or dense depends upon the computational power and efficiency needed. The nodes are recurrently connected with each other with randomly initialized weights.</p><p>The size of the reservoir means the number of nodes (neurons) in the reservoir layer. A larger reservoir size means the system can capture more complex patterns but it may more computational resources.</p><p>Spectral radius — It scales the weight of connections in the reservoir layer. It is always less than 1 to prevent signal amplification.</p><p>The readout layer may be a simple linear regression or any supervised learning algorithm to map the outputs from the reservoir to output states.</p><p><strong>Liquid State Machines (LSM) </strong>— It is an extension to Echo State Network by incorporating additional principles from biological neural networks.</p><p>Liquid — It means the RNN which is highly interconnected in the reservoir. Liquid means it continuously processing and integrating the incoming information.</p><p>Plasticity — This is what makes the ESN to LSM. It uses a concept called as <strong>Synaptic Plasticity</strong> which exists in biological neural networks. It means the ability of the synapses (connections between the neurons) to modify strength of it depending upon the activity. When two neurons fire together repeatedly, the connection between them becomes stronger. There is also one rule in neuroscience called as Hebbian Rule — Neurons that fire together, wire together.</p><p>In ESN, we use fixed RNN, but in LSM we use synaptic plasticity and adaptation mechanisms inspired by biological neural networks i.e., the connections between the nodes in the reservoir can adapt and change over time based on input data and network’s activity. This improves the performance of the system.</p><p><strong>Quantum Computing </strong>— Quantum computer works by quantum physics which deals with sub-atomic particles like ‘electrons’ or ‘photons’ or other ‘ions’. In Quantum Computing, we use qubits (Quantum Bits) to represent information.</p><p>Unlike the classical computers, qubits can be in state ‘0’ or ‘1’ or linear combination of both. This phenomenon is called as <strong>Superposition</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aTRofR_mF408tM5i.gif" /></figure><p>The other property of quantum physics is <strong>Entanglement </strong>— If qubits are said to be entangled, if we measure the value of one qubit, the value of other qubit can be known even if they are separated by light years. This enables the quantum systems to be parallel rather than serial, so that operations / tasks can be performed in real quick time that even supercomputers don’t even come close to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/0*NIPVjrWjjq-EijMR.jpg" /></figure><p>In this post, we will use Reservoir Computing with quantum mechanics called <strong>Quantum Reservoir Computing (QRC)</strong> for controlling an autonomous drone.</p><p>We saw an analogy of pond and rain drops in the earlier section. Now, let us make the pond quantum.</p><p>In Quantum Reservoir Computing, the “pond” is no longer made of water, it’s made of <strong>quantum particles</strong> (qubits).</p><p>This quantum pond follow the strange rules of quantum mechanics., i.e.,</p><ul><li>The “ripples” can be in multiple states at once (superposition)</li><li>They can be mysteriously connected regardless of distance (entanglement)</li><li>They process information in fundamentally different ways than classical systems</li></ul><p>The key advantage is that quantum systems naturally create extremely complex patterns from simple inputs, potentially allowing them to process certain types of information more efficiently than classical computers.</p><p>In the next and final part, we will discuss about the mathematical aspects of QRC and the demo of autonomous drone control with QRC.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db418cd310b4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[VLM’s Simplified]]></title>
            <link>https://sampathkumaran.medium.com/vlms-simplified-bbaf4cde7e96?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbaf4cde7e96</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[computer-vision]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Sun, 21 Sep 2025 05:56:23 GMT</pubDate>
            <atom:updated>2025-09-21T05:56:23.262Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/537/1*rRc-xndmEseCDGASHI3XIQ.png" /></figure><p>In this post, we will look into how Vision Transformers work and we will see an application of Vision Language Model (VLM) — Visual Language Retrieval (VLR) — Searching image via text.</p><p>This post assumes that you have knowledge about how language models work. If you need to brush up or learn about LLM (Large Language Models), please navigate this series of mine titled “<strong>LLM’s Simplified</strong>” which covers <a href="https://medium.com/@sampathkumaran/llms-simplified-tokens-and-embeddings-f275e6ce016e">tokens and embeddings</a>, <a href="https://medium.com/@sampathkumaran/llms-simplified-attention-mechanism-85199d9012d7">attention mechanism</a>, <a href="https://medium.com/@sampathkumaran/llms-simplified-feed-forward-network-ffn-24ec761e664a">feed forward networks</a>, <a href="https://medium.com/@sampathkumaran/llms-simplified-language-modelling-and-decoding-2402ae5eb85c">language modelling and decoding</a>.</p><p><strong>Vision Transformers (ViT):</strong></p><p>We know that in language transformers, the text needs to converted to numerical representations before being passed to the decoder i.e., it needs to be tokenized. Now, you must be confused “How do i convert image to numerical representations like tokens?”🙄</p><p>You might be asking a question like “Do I need to use every pixel as a token???”🤔</p><p>Let’s say that you have an image of a human. Every pixel in the human image do not say anything but a patches of the images can say something. So, VLM’s use this idea of converting images into patches say 16 x 16 from the whole image of say 512 x 512. We can say that the image is cut into grids. Below is the same illustration of patching the image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/412/1*rzmj4LBttH8swX9qO1NFIA.png" /><figcaption>Sample Illustration only</figcaption></figure><p>Once the patches have been generated, it is then flattened like the below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*CXEp7-1y0vBb9NPILjlwFA.png" /><figcaption>Sample Illustration only</figcaption></figure><p>The flattened image looks like a sequence. These flattened image patches can be thought of as the “tokens” of a picture. However, unlike text tokens, we can’t simply assign each patch an ID, because image patches are highly specific to each picture and rarely repeat across images — unlike words in a text vocabulary.</p><p>These embeddings are then fed into the Transformer model, where they are treated just like text tokens. From this point onward, the training process works in essentially the same way for both images and text. Due to these similarities, the Vision Transformers is often used to make all kinds of language models multimodal. This structural match makes it easy to align image embeddings with text embeddings.</p><p>Before we see how to train embeddings, we need to understand what is semantic similarity and lexical similarity?</p><p><strong>Semantic similarity </strong>measures how close the meanings of two pieces of text (or other data) are, regardless of exact wording.</p><p><strong>Lexical similarity</strong> measures how similar two texts are in surface form (words, spelling, characters).</p><p>Let’s see some examples:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dywqUrx0zREj6OX4DVSAcA.png" /></figure><p>Now, we know about semantic similarity, we shall see how an embedding works?</p><p>Embeddings are a way to represent complex data (like words, sentences, images, or even users and products) as vectors (lists of numbers) in a continuous space, typically of lower dimensions. These vectors capture semantic or structural information so that similar things are close together in this space.</p><p>Now, you must be curious how embeddings learn to make semantic similarity?🤔</p><p>Think of an embedding as a way to give every word, sentence, or image a <strong>GPS coordinate in the world of meaning</strong>.</p><p>At first, The model sees huge amounts of text (and/or images with captions). For e.g., <em>‘A dog is running’, ‘A puppy is playing’.</em></p><p>Now, the model sees dog and puppy are used in similar place in sentences near words like <em>play</em> or <em>run. </em>Now, it decides it must close in the map.</p><p>Instead of dictionary entries, each word/image gets stored as a <strong>vector of numbers</strong> (e.g., [0.12, 0.14 ….]). Similar meanings means vectors point in similar directions.</p><p>The model learns by playing a matching game: If word + context match then pull vectors closer. If they don’t match then push them apart.</p><p><strong>CLIP — Contrastive Language-Image Pre-training:</strong></p><p>CLIP is a model developed to connect images and text in a shared representation space. It has two parts:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/918/1*TJwt2BJeER-IfoWsP3Dmaw.png" /><figcaption>CLIP Architecture</figcaption></figure><p><strong>Image Encoder</strong>: It is either a ViT or Resnet model. Input is either image patches or CNN (convolutional neural networks) features. It outputs a single vector embedding say 512 dimensions.</p><p><strong>Text Encoder</strong>: It is a Transformer (like a smaller GPT-style or BERT-style model). The input is a tokenized caption like (“A photo of hibiscus”). It outputs a single vector embedding say 512 dimensions.</p><p>Both Image as well as Text Encodings are projected into a same vector space. So, image of a dog is kept closer to a dog image.</p><p>Now, you must be confused like how it is possible? Is it a magic?🤷‍♂️🤷‍♀️</p><p>Let’s decode it…</p><ol><li>Input: A batch of images and their corresponding text captions.</li><li>Encoding: Each image is passed through the image encoder and each caption is passed through the text encoder producing image and text embeddings respectively.</li><li>Similarity Calculation: This the heart of this algorithm. Compute cosine similarity between every image embedding and every text embedding in the batch. e.g., Say you have 4 images (represented as I1, I2, I3, I4) and their corresponding captions (represented as T1, T2, T3, T4).</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*_gWm2gR4CTCgZ4dxTzpjVg.png" /><figcaption>Similarity Matrix Calculation</figcaption></figure><p>We see that it will generate a N x N matrix, where N is 4.</p><p>Each row is one image compared with all captions in the batch. Each column is one caption compared with all images in the batch. The diagonal entries (I1–T1, I2–T2, etc.) are the true pairs. Other than diagonal are mismatches. During training, make the diagonal similarities high and other similarities low. If you only compared I1 with T1, the model wouldn’t learn to discriminate. By comparing all possible pairs in the batch, the model sees hard negatives (similar captions/images that aren’t the correct match).</p><p>This matrix is fed into the contrastive loss usually symmetric cross-entropy loss. Model learns to increase similarity for the true pairs (diagonal) and learns to decrease similarity for false pairs (off-diagonal).</p><p>Let’s see this step using a simple python code:</p><pre>import torch<br>import torch.nn.functional as F<br><br># Example: batch of 4, embedding size 512<br>N, D = 4, 512<br>image_emb = torch.randn(N, D)<br>text_emb = torch.randn(N, D)<br><br># Normalize (important for cosine similarity)<br>image_emb = F.normalize(image_emb, p=2, dim=-1)<br>text_emb = F.normalize(text_emb, p=2, dim=-1)<br><br># Cosine similarity matrix<br>similarity_matrix = image_emb @ text_emb.T  # shape: (N, N)<br><br>print(similarity_matrix)<br><br># OUTPUT<br>tensor([[ 0.81,  0.12, -0.22,  0.33],<br>        [ 0.05,  0.79,  0.10, -0.04],<br>        [-0.18,  0.08,  0.92,  0.25],<br>        [ 0.12, -0.15,  0.30,  0.85]])</pre><h4><strong>DEMO 1 — TEXT TO IMAGE</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*g7TeorVGQnj-IB0Asf5KCA.png" /><figcaption>Text to Image</figcaption></figure><p>I have used flowers dataset from torchvision library, which contains about 1020 images of different flowers. So, I intentionally gave all the captions as <strong>“A photo of {flower category}”.</strong> As you can see from the bottom of those images, “A photo of a chrysanthemum” and “A photo of buttercup”.</p><p>I trained CLIP using images paired with their captions. Since CLIP maps both images and text into the <strong>same embedding space</strong>, it allows us to search for images using text queries, and search for captions using images.</p><p>The captions themselves don’t mention colors or attributes explicitly. Yet, when I queried <em>“green and pink”</em>, CLIP retrieved a flower containing exactly those colors — spot on. To push it further, I tried a more abstract query: <em>“marble coloured flower”</em>. Once again, the result was accurate.</p><p>It’s incredible to see how well CLIP understands visual concepts, even when the exact words were never in the captions.</p><h4><strong>DEMO 2 — IMAGE TO TEXT</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1009/1*tHwxenhAUontAxnsmfb9uQ.png" /><figcaption>Image to Text</figcaption></figure><p>In the reverse direction, we can upload a flower image and let CLIP retrieve its category through the associated captions. For example, when I uploaded a <strong>sunflower image</strong>, it returned the correct caption without any ambiguity. I then tried a <strong>pink hibiscus</strong>, which is slightly less prominent in the dataset — and CLIP still got it right.</p><p>Once again, the results were spot on, showing how well the model generalizes across both common and less distinctive flowers.</p><p>The demo was created using transformers, streamlit, faiss, torchvision and torch libraries. We used the CLIP model from huggingface.</p><ol><li>We load the images and captions dataset.</li><li>We load the pre-trained clip model (already trained with 400M image and captions pairs) from openai through huggingface.</li><li>We then create text and image embeddings and save them in faiss (for fast similarity search).</li><li>Then start the streamlit (a quick UI library for demo) server and try using this simple use case and have fun.</li></ol><p>Let’s see some important parts of the code especially step 3 which is the soul of this demo.</p><p>Load the clip model, its processor and load the dataset.</p><pre>&quot;&quot;&quot;<br>model: CLIP model (has both image encoder and text encoder).<br>processor: handles preprocessing (resizing, tokenizing, normalization).<br>device: CPU or GPU.<br>&quot;&quot;&quot;<br><br>model, processor, device = load_clip()</pre><pre>&quot;&quot;&quot;<br>Reads CSV with image paths + captions.<br>Two parallel lists: images_raw and captions.<br>&quot;&quot;&quot;<br><br>df = pd.read_csv(&quot;data\Flowers102\captions.csv&quot;)<br>captions = df[&#39;caption&#39;].to_list()<br>images_raw = df[&#39;image_path&#39;].to_list()</pre><p>Load each image, create image embeddings and normalize it</p><pre>&quot;&quot;&quot;<br>Open image with PIL (pillow).<br>Convert to RGB.<br>&quot;&quot;&quot;<br><br>img = Image.open(im).convert(&#39;RGB&#39;)<br>images.append(img)<br><br>&quot;&quot;&quot;<br>Preprocess image (resize, normalize).<br>Pass through CLIP’s Vision Encoder (ResNet or ViT).<br>Output = a 512-D embedding.<br>&quot;&quot;&quot;<br><br>img_in = processor(images=img, return_tensors=&quot;pt&quot;).to(device)<br>with torch.no_grad():<br>    emb_img = model.get_image_features(**img_in)<br><br><br>&quot;&quot;&quot;<br>Normalize → makes embeddings comparable with cosine similarity.<br>&quot;&quot;&quot;<br><br>emb_img = emb_img / emb_img.norm(dim=-1, keepdim=True)</pre><p>Create text embeddings and normalize it</p><pre><br>&quot;&quot;&quot;<br>Preprocess text (tokenize).<br>Pass through CLIP’s Text Encoder (Transformer).<br>Output = also a 512-D embedding.<br>&quot;&quot;&quot;<br><br>txt_in = processor(text=[captions[index]], return_tensors=&quot;pt&quot;, padding=True).to(device)<br>with torch.no_grad():<br>    emb_txt = model.get_text_features(**txt_in)<br><br><br>&quot;&quot;&quot;<br>Normalize → again, for cosine similarity.<br>&quot;&quot;&quot;<br><br>emb_txt = emb_txt / emb_txt.norm(dim=-1, keepdim=True)</pre><p>Collect embeddings and convert to NumPy stack</p><pre>&quot;&quot;&quot;<br>Store each embedding.<br>&quot;&quot;&quot;<br><br>img_embs.append(emb_img.cpu().numpy())<br>txt_embs.append(emb_txt.cpu().numpy())<br><br>&quot;&quot;&quot;<br>Stack into numpy arrays<br>&quot;&quot;&quot;<br><br>img_embs = np.vstack(img_embs).astype(&quot;float32&quot;)<br>txt_embs = np.vstack(txt_embs).astype(&quot;float32&quot;)</pre><pre>&quot;&quot;&quot;<br>Create FAISS indices for fast similarity search.<br>Inner product (IndexFlatIP) is used (since vectors are normalized → IP equals cosine similarity).<br>&quot;&quot;&quot;<br><br>img_index = faiss.IndexFlatIP(img_embs.shape[1])<br>img_index.add(img_embs)<br>txt_index = faiss.IndexFlatIP(txt_embs.shape[1])<br>txt_index.add(txt_embs)</pre><p>Now, the embeddings are saved, we can perform text to image search and vice-versa.</p><pre>&quot;&quot;&quot;<br>Input is query like &quot;green and pink&quot;.<br>It is passed to the CLIP processor to pre-process it.<br>using the CLIP model (model) get the features for the query that we have given.<br>output from CLIP model is normalized and converted to numpy array.<br>Now, We can search the image using the embeddings in Faiss \<br>with our query embedding to get the accurate matches since both text and images are \<br>projected to same vector space<br>&quot;&quot;&quot;<br><br>txt_in = processor(text=[query], return_tensors=&quot;pt&quot;, padding=True).to(device)<br>with torch.no_grad():<br>    q_emb = model.get_text_features(**txt_in)<br>q_emb = q_emb / q_emb.norm(dim=-1, keepdim=True)<br>q_emb = q_emb.cpu().numpy()<br><br>D, I = img_index.search(q_emb, k)</pre><p><a href="https://github.com/sampathkumaran/VLM-CLIP-DEMO">Code to Download</a> to simulate the demo shown above and be wondered by the power of VLM’s.</p><p><em>You’ve probably heard the buzzword “</em><strong><em>multimodal</em></strong><em>” a lot these days. Well, you just built a demo of it!</em></p><p>A single model can understand both <strong>language</strong> and <strong>vision</strong>. By projecting text and images into the <strong>same embedding space</strong>, CLIP allows them to be compared directly — unlocking seamless cross-modal understanding.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbaf4cde7e96" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Brain-Inspired Robot Navigation — Part 5]]></title>
            <link>https://sampathkumaran.medium.com/brain-inspired-robot-navigation-part-5-34b684b9c3ac?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/34b684b9c3ac</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[neuroscience]]></category>
            <category><![CDATA[robotics]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Thu, 18 Sep 2025 09:09:06 GMT</pubDate>
            <atom:updated>2025-09-18T09:09:06.290Z</atom:updated>
            <content:encoded><![CDATA[<p>If you haven’t read through my previous parts of this series, please look into it to understand how neurons work, basics of SNN, encoding for SNN, neuron models for SNN, R-STDP learning rules.</p><ul><li><a href="https://sampathkumaran.medium.com/brain-inspired-robot-navigation-part-1-150230f202d2">Brain-Inspired Robot Navigation — Part 1</a></li><li><a href="https://sampathkumaran.medium.com/brain-inspired-robot-navigation-part-2-50561c4b61ef">Brain-Inspired Robot Navigation - Part 2</a></li><li><a href="https://sampathkumaran.medium.com/brain-inspired-robot-navigation-part-3-db5278a07f0a">Brain-Inspired Robot Navigation — Part 3</a></li><li><a href="https://sampathkumaran.medium.com/brain-inspired-robot-navigation-part-4-f88e32eb8856">Brain-Inspired Robot Navigation — Part 4</a></li></ul><p>Now, let’s get to the interesting part — a simple demo of robot learning to navigate to the goal in a grid environment (This can be extended to any environment be it in ROS2 + Gazebo or Pybullet or Mujoco or webots or even simple gymnasium etc.,). The complexity of environment increases the time requried for model to learn the intricacies of the environment, actions it has to take, learn to get the cumilative rewards etc.,</p><p>I wanted to demo the energy efficiency, sparsity of Spiking Neural Networks (SNN) when compared with Artificial Neural Network (ANN). So, I took a simple 5 x 5 grid with two obstacles and a goal (5, 5) with the agent (robot) at (1,1) position. Below is the robot learned to reach the goal position. I ran both ANN and SNN in parallel for 300 episodes (one start to end of the agent navigating through the grid) to benchmark it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*mGWA-RCLFLTPv5bWdw2BAw.gif" /></figure><p><strong>Energy Efficiency </strong>— We know that in SNN, neurons only compute and communicate when they spike, unlike ANN neurons which always update with dense floating-point multiplications.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/969/1*o5LeNX7b0P7vxw5INVmF-w.png" /></figure><p>In an ANN, energy cost is proportional to <strong>number of neurons × number of weights × timesteps</strong>. It performs <strong>multiply–accumulate (MAC)</strong> operations at every timestep.</p><p>From the above figure, we can see that SNN is almost <strong>2.34x </strong>times<strong> </strong>energy efficient than ANN.</p><p>In SNN, the neurons integrate inputs until they spike. If no spike occurs, no communication or weight update happens. Energy cost is proportional to the <strong>number of spikes × synaptic operations (SynOps — </strong>number of spike transmissions<strong>)</strong>. This is less when compared with MAC and very less when using <strong>neuromorphic hardware.</strong></p><p><strong>Sparsity</strong> — In ANN, every neuron computes a value at every timestep. In an SNN, neurons only fire (spike) when their membrane potential crosses a threshold. Most of the time, neurons are silent. This silence is called as sparsity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-IXzTte-HGEpoeYmav4WHw.png" /></figure><p>From the above figure, we can see that <strong>96.3% </strong>of neurons are sparse or inactive during inference. That is a massive boost when compared with ANN where there is no sparsity.</p><p>Now, you might have a question what is <strong>Neuromorphic Hardware</strong>???</p><p>They are brain inspired computing technology that mimics the way human brain actually works. Because it mimics the brain’s event-driven communication (only firing when needed), it consumes far less power compared to traditional processors. It can be used in Robotics, AI, Edge Computing, Real time data analysis. Some famous neuromorphic hardware are Intel Loihi / Loihi 2, SpiNNaker, IBM True North etc.,</p><p><strong>SNN ADVANTAGES:</strong></p><ol><li>Neurons only spike when needed (event-driven).</li><li>Sparse activity drastically reduces compute — so, it lowers compute power</li><li>Allows neuroscience-inspired learning rules (e.g., Hebbian learning (neurons wire together fire together — More biologically aligned / plausible but very harder to train) , <strong>STDP — </strong>We are already discussed in part 4)</li><li>More robust to noise because of event driven communication</li><li>Memory efficient</li></ol><p><strong>SNN DISADVANTAGES:</strong></p><ol><li>Training is difficult — may need surrogate gradients</li><li>Training is very slow in CPU / GPU</li><li>Fewer pre-trained models</li><li>They shine mostly on temporal/event-driven tasks (robotics, speech, vision sensors etc., ).</li><li>Harder to tune than ANN</li></ol><p><strong>APPLICATIONS:</strong></p><ol><li>Neuromorphic Hardware &amp; Low-Power AI (edge devices, IoT, wearables)</li><li>Dynamic Vision Sensors (DVS) — These sensors spike only on pixel changes, detecting obstacles in real-time like in self driving cars.</li><li>Speech &amp; Audio Processing (Hearing aids / cochlear implants), keyword spotting like ‘Hey Google’ in very less power and many more…</li></ol><p><strong>From my perspective, Neuroscience Inspired AI will be next big thing!!!</strong></p><p>Thanks for reading!!!</p><p><a href="https://github.com/sampathkumaran/SNN/tree/main">Demo Benchmark Code</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=34b684b9c3ac" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Brain-Inspired Robot Navigation — Part 4]]></title>
            <link>https://sampathkumaran.medium.com/brain-inspired-robot-navigation-part-4-f88e32eb8856?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/f88e32eb8856</guid>
            <category><![CDATA[robotics]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[neuroscience]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Tue, 16 Sep 2025 00:28:36 GMT</pubDate>
            <atom:updated>2025-09-16T00:28:36.830Z</atom:updated>
            <content:encoded><![CDATA[<h3>Brain-Inspired Robot Navigation — Part 4</h3><h4>SNN based Robot Navigation using R-STDP and Surrogate Gradients</h4><p>If you haven’t read through my previous parts of this series, please look into it to understand how neurons work, basics of SNN, encoding for SNN, neuron models for SNN.</p><ul><li><a href="https://sampathkumaran.medium.com/brain-inspired-robot-navigation-part-1-150230f202d2">Brain-Inspired Robot Navigation — Part 1</a></li><li><a href="https://sampathkumaran.medium.com/brain-inspired-robot-navigation-part-2-50561c4b61ef">Brain-Inspired Robot Navigation - Part 2</a></li><li><a href="https://sampathkumaran.medium.com/brain-inspired-robot-navigation-part-3-db5278a07f0a">Brain-Inspired Robot Navigation — Part 3</a></li></ul><h4>Spike Timing Dependent Plasticity (STDP):</h4><p>We know that neurons communicate with each other via spikes through synapses. The strength of the connection between the neurons is called as synaptic weights.</p><p>Let’s say we have two neurons (A &amp; B). If neuron A fires just before neuron B, then it is said like neuron A contributed to neuron B’s firing. So, the connection between them gets stronger.</p><p>If neuron A fires just after neuron B fired, then neuron A did not contribute to the firing of neuron B, so the connection between them gets weaker.</p><p>STDP is learning based on the exact timing of spikes and the connection between neurons gets strengthened or weakened based on it. The closer in time the spikes occur, the stronger the effect on learning. STDP is unsupervised training, it depends only the timing of the spikes i.e., synaptic weights gets adjusted based on the timing difference between pre and post synaptic spikes.</p><p>Each time the neurons spike, we adjust synaptic weight depending on who spikes first.</p><p>Let ‘w’ be the synaptic weight between neuron A and neuron B. STDP modifies the weight w based on the timing difference. Think about similar to backpropagation that modifies the weight values based on the gradient of the cost function with respect to time in normal ANN.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/690/1*t_MTPr76-vMQ854Udt1Grw.png" /></figure><p><strong>Δw —</strong> Change in synaptic weight, <strong>LTP </strong>— Long Term Potentiation, <strong>LTD </strong>— Long Term Depression</p><p>If <strong>Δt &gt; 0 </strong>: Pre (neuron A) fired before Post (neuron B) (LTP) — strengthened</p><p>If <strong>Δt &lt; 0</strong> : Post (neuron B) fired before Pre (neuron A) (LTD) — weakened</p><p><strong>τ</strong> : “forgetting timescale” (how quickly the effect fades with time difference).</p><p>Now, you might have a question why there is an exponential term in the equation? 🤔</p><p>The answer is, If spikes are very close in time i.e., |<strong>Δt</strong>| is small, then weight update will be higher and if |<strong>Δt</strong>| is high, then the weight update will be lesser or almost no change.</p><p>Also, the sign before the Amplitude (A) is to have case distinction to denote + for <strong>Δw &gt; 0 </strong>and — for<strong> Δw &lt; 0.</strong></p><p>Now, you should have another question what is the use of “<strong>τ</strong>” (tau)? 😕</p><p>It controls how far apart in time, spikes can still influence plasticity. If the time difference is very small, the update is going to be high. If the time difference is very high, the plasticity effect decays exponentially with “<strong>τ</strong>”. Small τ (e.g., 10 ms): only <em>very close spikes</em> matter. Large τ (e.g., 50 ms): spikes can still interact even if they are tens of ms apart. (ms) — milliseconds.</p><p>If you are still confused, τ is like a stopwatch. If a neuron fires, it starts the stopwatch. If the connected neuron fires within the window of the stopwatch timer, weights will be updated. If the connected neuron fires after the stopwatch window, nothing will get updated.</p><h4>Reward Modulated Spike Timing Dependent Plasticity (R-STDP):</h4><p>We saw that STDP is unsupervised and based solely on the spike timing. So, how about we add a reward signal (reinforcement) to the spike timing!!! 😇 — We will get R-STDP.</p><p>It combines Hebbian learning (timing-based) with reinforcement signals (reward-driven). Reward works like an extra multiplier on the STDP timing rule.</p><p>Let’s see an analogy:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*p1nSCQGmRK7ZeoLfHaMZKQ.png" /></figure><p>If a student in the class answers the question before the teacher answers, his confidence will be high (Potentiation) — In the above figure left side. But if the student answers the question after the teacher answers, his confidence will be low (Depression) — In the above figure right side. Now the teacher hands out rewards (gold stars) for good answers. If the student’s timing was right and the teacher rewards it, they learn much faster. If no reward, the effect is much weaker.</p><p>The mathematical equation is very simple — multiply <strong>R</strong> (Reward) with <strong>Δw </strong>from the STDP equation.</p><p>We need to know one more important concept called as Eligibility traces.</p><p><strong>Eligibility traces</strong> in R-STDP are central to bridging the gap between fast spiking activity and delayed reinforcement signals.</p><p>In reward-modulated STDP (R-STDP), the plasticity also needs to account for delayed rewards (like in reinforcement learning). But rewards often come seconds later, long after the spikes that should be credited. So we need a memory mechanism to “hold on” to which synapses were active until the reward arrives.</p><p>Let’s see an analogy:</p><p>Imagine synapses put “sticky notes” whenever they might deserve a reward (based on spike timing). The sticky note fades away with time (τ). If a reward comes in while the note is still there means, the synapse updates its weight in the note’s direction. If no reward comes, the note disappears and nothing changes.</p><p>Now, You must be thinking that ok, I can do robot navigation just using SNN with R-STDP 😇😇.</p><p>But in real scenarios, there are some disadvantages of using R-STDP alone.</p><p><strong>Credit Assignment Problem</strong> — R-STDP struggles with the temporal credit assignment problem — it’s difficult to determine which specific actions or neural activities contributed to rewards that arrive after significant delays. In navigation tasks, the robot might only receive reward feedback after completing a trajectory, making it hard to identify which intermediate decisions were beneficial.</p><p><strong>Local Learning</strong> — R-STDP is inherently a local learning rule that only considers correlations between pre- and post-synaptic spike timings along with a global reward signal. This locality means it cannot effectively propagate error information through multiple layers of a deep network, limiting the complexity of behaviors it can learn.</p><p><strong>Slow Convergence — </strong>The stochastic nature of spike-based learning and the reliance on correlational learning rather than gradient-based optimization often results in slower convergence compared to gradient-based methods. In complex navigation environments, this can require extensive training time.</p><p>R-STDP performs poorly when the optimal action depends on long-term temporal context rather than just the current state. Robot navigation often requires memory of past locations and obstacles, which pure R-STDP struggles to maintain effectively.</p><p>What ????? 🙄🙄🙄 You might be asking what is the solution then???🤷‍♂️ 🤷‍♀️</p><p>The solution is hybrid model combining <strong>Surrogate Gradients and R-STDP.</strong></p><p>Training neural networks usually relies on a method called backpropagation, which requires calculating gradients (how much a change in input affects the output). However, in SNNs, the spike generation function is non-differentiable — it’s like a step function that jumps from 0 to 1. This makes it impossible to compute gradients in the usual way.</p><p>To get around this, we use surrogate gradients.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MZAKIbgC9xjrDDqu1oGAOQ.png" /></figure><p><strong>Real Spike</strong> — Like flipping a switch ON/OFF instantly → impossible to know “how close” you were.</p><p><strong>Surrogate gradient — </strong>Like replacing the switch with a <strong>dimmer knob</strong> <em>just while learning</em> → now you can measure “how much” turning changes the brightness.</p><p>We pretend the sharp ON/OFF spike is smooth during training so we can compute gradients, but we keep the real spikes during inference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uds8w2UOOtjt6OVvGsZUwQ.png" /></figure><p><strong>During the forward pass:</strong> The network uses the real spiking function (the step function) to decide if a neuron spikes.</p><p><strong>During the backward pass: </strong>Instead of using the true (non-existent) gradient of the step function, a smooth, differentiable function (the surrogate) is used to approximate the gradient. The most commonly used surrogate function are sigmoid, piecewise-linear and fast sigmoid.</p><p>The gradient-based component provides precise credit assignment across temporal sequences and network layers, addressing one of R-STDP’s main weaknesses. This helps the network learn which specific neurons and time points contributed to successful navigation outcomes.</p><p>Surrogate gradients can quickly establish a reasonable initial policy for navigation, while R-STDP continues to refine and adapt the learned behavior based on ongoing experience. This two-phase approach significantly reduces training time.</p><p>The combination allows the network to learn both global optimization objectives (through gradients) and local spike-timing patterns (through R-STDP). This dual learning mechanism often produces more robust navigation policies that generalize better to novel environments.</p><p>This combination can work in sequential as well as in parallel training paradigm. We will use the sequential training paradigm for simple robot navigation demo in a grid based environment.</p><p><strong>STAGE 1 — Surrogate Gradient Pre-Training:</strong></p><p>Quickly learns the basics like How to move one square at a time (up, down, left, right), How to recognize the goal square and walls (blocked squares), General rules like “don’t try to move into walls” etc.,</p><p>Just think of this stage as <strong>computer simulation</strong> stage where the robot can simulate many of moves quickly.</p><p><strong>STAGE 2— R-STDP Fine-Tuning:</strong></p><p>Now the robot is placed on a <strong>real physical grid</strong> where things aren’t perfect like, Some squares in the grid might be slippery (taking two tries to move), certain paths might have obstacles that appear and disappear etc.,</p><p>Without pre-training, the robot would wander around randomly, maybe taking thousands of attempts just to accidentally find the goal once. Without fine-tuning, it would follow textbook paths that might be inefficient or impossible on the real grid.</p><p>In the next (final) part, we will see the demo of robot navigation using all the concepts that we have learned so far.</p><p>We will conclude with advantages / disadvantages, applications of SNN, bit of neuromorphic computing.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f88e32eb8856" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LLM’s Simplified — Language Modelling and Decoding]]></title>
            <link>https://sampathkumaran.medium.com/llms-simplified-language-modelling-and-decoding-2402ae5eb85c?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/2402ae5eb85c</guid>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Thu, 11 Sep 2025 12:52:15 GMT</pubDate>
            <atom:updated>2025-09-11T12:52:15.100Z</atom:updated>
            <content:encoded><![CDATA[<h3>LLM’s Simplified — Language Modelling and Decoding</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/0*oYSnOZsyIkx63rmB.png" /></figure><p>We have come to the last part of the decoding the architecture of LLM’s like GPT. We have covered <a href="https://medium.com/@sampathkumaran/llms-simplified-tokens-and-embeddings-f275e6ce016e"><strong>Tokens and Embeddings</strong></a>, <a href="https://medium.com/@sampathkumaran/llms-simplified-attention-mechanism-85199d9012d7"><strong>Self Attention</strong></a>, <a href="https://medium.com/@sampathkumaran/llms-simplified-feed-forward-network-ffn-24ec761e664a"><strong>Feed Forward Network</strong></a> in the previous posts in the same order.</p><p>In this post we will look into Language Modelling Head (LM Head) and decoding strategies while building LLM’s.</p><h4>LM Head:</h4><p>We knew that the tokenizer splits the text into tokens followed by token ids to token embeddings and merged with positional embeddings to create inputs to the stack of transformer blocks. The stack of transformers blocks do all the processing of the input and generates contextualized embeddings which are then passed to the LM head which produces the probability score for the next token.</p><p>The model uses an embedding lookup table that maps each token ID to a d-dimensional embedding. With a vocabulary of size v, an input of N tokens (each of dimension d) is processed, and the output is a probability distribution over the v tokens representing the likelihood of the next token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*1KMn75hwbkuhG4tPKgRchA.png" /></figure><p>Let us use the above figure as an example. We can assume like we have a vocabulary size of 5000. Embedding dimension is 3. Now, we have an input sequence like “I like spiking neural”. The LLM needs to generate the token to be appended to the input sequence one token at a time. For this input, the expected next token is <em>“networks”</em>. The LM head outputs probability scores over all 5000 vocabulary tokens. The token with the highest probability is typically chosen as the next token — in this case, <em>“networks”</em>.</p><h4>Decoding / Sampling:</h4><p>The output from the LM head is a probability score for each token in the vocabulary. The method of choosing a single token from the probability distribution is called the <em>decoding / sampling strategy</em>. There are different kinds of decoding strategies.</p><blockquote>We will use the text as <strong>“the best way to study for the exam is to …”</strong></blockquote><p><strong>Greedy Decoding — </strong>Always selects the token with the highest probability at each step. It is very fast and simple. Can lead to repetitive or generic text; may miss more creative or contextually appropriate outputs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*96c4_v9MyXilqdcAZpP2_Q.png" /></figure><p><strong>Beam Search</strong> — Keeps track of the top k most likely sequences at each step (where k is the beam width). At each step, it expands all sequences in the beam and keeps the top k overall. Better than greedy — avoids getting trapped by a locally optimal but globally weak choice. Computationally more expensive; can still be repetitive if the beam is too narrow.</p><p>Beam search at first is confusing. Suppose beam size = 3. Possible next tokens are say “review” (0.45), “practice” (0.27), “read” (0.18) for “the best way to study for the exam is to” input. Now what beam search will do is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2C25NtH5d0Eva6HxfPzhpg.png" /></figure><p>In the next step it expands again, keeping top k overall scoring full sequences. Unlike greedy (which keeps only 1 sequence), beam search carries multiple beams forward. At the end, you choose the highest-scoring full sequence which is drawn in red color line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*p54dr8TgC41N8wVasmOdhw.png" /></figure><p><strong>Top-K Sampling —</strong> Instead of always choosing the single most probable token (greedy) or keeping multiple beams (beam search), Top-k sampling restricts the next-token candidates to the top k tokens with the highest probabilities, then samples randomly among them. But it can generate a silly output if the k value is higher.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*YN3AKBPrQap3M6qiO1RHJg.png" /></figure><p>In our case, review, practice, read are taken and randomly sampled and it selected practice. It provides diversity in text generation.</p><p><strong>Top-P Sampling (Nucleus Sampling) —</strong> Instead of fixing the number of tokens (k), we take the smallest set of tokens whose cumulative probability ≥ p (e.g., p=0.9). Then, sample randomly from this set. Adapts to the uncertainty of the model; often produces more natural text than top-k.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*TEnUhC9BRpPAoNPCf1JSTg.png" /></figure><p><strong>Temperature Scaling — </strong>Adjusts the probability distribution before sampling. A higher temperature (&gt;1) makes the distribution more uniform (more random), while a lower temperature (&lt;1) makes it sharper (more deterministic). It allows control over randomness and creativity. Too high temperature or too low temperature will harm the output quality.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NRJFbii5VZHWJb4mdJdZjQ.png" /></figure><p>So, we can see from the figure:</p><ul><li><strong>T = 0.7 (sharpened)</strong> → “review” dominates (~0.55), others shrink.</li><li><strong>T = 1.0 (default)</strong> → original distribution.</li><li><strong>T = 1.5 (flattened)</strong> → “review” softens (~0.36), low-probability tokens like <em>memorize</em> and <em>sleep</em> rise, making outputs more diverse.</li></ul><p>So, we have covered the entire architecture of autoregressive model like GPT from tokens till decoding.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2402ae5eb85c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[LLM’s Simplified — Feed Forward Network (FFN)]]></title>
            <link>https://sampathkumaran.medium.com/llms-simplified-feed-forward-network-ffn-24ec761e664a?source=rss-aad9d491ee75------2</link>
            <guid isPermaLink="false">https://medium.com/p/24ec761e664a</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[generative-ai-tools]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Sampath Kumaran Ganesan]]></dc:creator>
            <pubDate>Thu, 11 Sep 2025 08:10:05 GMT</pubDate>
            <atom:updated>2025-09-11T08:10:05.907Z</atom:updated>
            <content:encoded><![CDATA[<h3>LLM’s Simplified — Feed Forward Network (FFN)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/0*ucXjISUxZp8iViKB.png" /></figure><p>In the last post, we saw that the transformer block in the LLM like GPT contains two components. self attention component and feed forward network component. Learn about <a href="https://medium.com/@sampathkumaran/llms-simplified-attention-mechanism-85199d9012d7">self attention mechanism</a> from this post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/222/0*xy8IAsPH530nxL4g.png" /><figcaption>Transformer Block</figcaption></figure><p>In this post, we will discuss about the feed forward network component in the transformer block.</p><p>The feed forward network takes the input (tokens) from the self attention layer and process them independently. The same network is applied to all tokens. We know that the output from the self attention layer contains mixed information of all the tokens. We need a mechanism to transform each token into a richer representation. Confusing 😕?</p><p>Let us see an analogy to understand better.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*JvfbLMjZ6Zh2PLd-Tixcdg.png" /></figure><p>Let us consider a classroom in which students (tokens) in a class room needs to prepare a report. Before creating their own reports the students conduct a group discussion and share their ideas with each other. Let us say that student ‘Bob’ shares an idea of using some chart type in the report. This idea can be liked by the another student ‘Alice’ which she can implement that similar chart type in her report. This is similar to self-attention where each token in a text looks into other tokens and get what information is important to include in its representation.</p><p>After the discussion, each student sits down to write and edit their own report. Each student may follow the same template of the report but their content would different and unique. They took inspiration from others and made a powerful representation of their own report. We can say that ‘Alice’ might have used the idea of ‘Bob’ using a chart type but ‘Alice’ polishes it with her data and make her own styles and make it look rich. This is similar to Feed forward networks where it processes each token’s information separately, using the same network, to make the final version even better.</p><h4>Architecture of FFN:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*dJEmemvM0r05Var91I5taQ.png" /></figure><p>FFN consists of two linear transformations (normal Multi-layer perceptron) with a non-linear activation function in between.</p><p>The first linear layer is called as “Expansion Layer”. It expands the token vectors into a much higher dimensional space with more features. You can ask me a question here, why we need expansion 🤔?</p><p>The reason is to give the model much room to think to discover hidden patterns. For e.g., your feature dimension is only 256, it is not enough to represent complex patterns. By stretching it to say 1024 or 2048, we provide the model to combine features in different ways. Researchers also found that without expansion, transformers performs worse.</p><p>The rule of thumb is 4 * embedding dim.</p><p><strong>We will use the text as “I like spiking neural networks” with dummy features for explanation.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/419/1*9ibBCUl8ZrFNv6jOrTcXnA.png" /><figcaption>Mathematical expression of Input from attention component</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1ma9Zlv4WBXrpySeFNJmkA.png" /></figure><p>Let us consider that we have 2 — dimensional tokens as input from the self attention component.</p><p>So, for our input, B = 1, L = 5, d = 2.</p><p>We will consider only the “I” token for the sake of brevity. Now, in the expansion layer, the 2-dimensional token is expanded into 8 dimensions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/406/1*53Pfngla27LQw68JwjY0og.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q8_8YgC2GWtzbgzvcv5x6A.png" /></figure><p>The output from the step 1 is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/161/1*o6LZi074FMNHWhh_nyamng.png" /><figcaption>Output from first linear layer</figcaption></figure><p>This output h is passed to the non-linear activation layer.</p><p>We use ReLU (Rectified Linear Unit) activation function in most of deep learning but in transformers, most common non-linear activation function is GELU (Gaussian Error Linear Unit).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*8gvrUJ-HvjVLTtgmzwHPqg.png" /></figure><p>From the above figure, we can see that ReLU cuts everything below 0. GELU softly transitions around 0, letting slightly negative values contribute.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*EXCXrHD7giJnjKa3r9EbUg.png" /></figure><p>ReLU gradient jumps from 0 to 1. If derivative is <strong>0</strong> (like ReLU for negative x), learning stops for that neuron which is called as dead neuron problem. GELU gradient (derivative of activation function) is smooth, allowing small negative values to still pass gradients.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/134/1*_k_4rBgeLrEvVfYdboVREQ.png" /></figure><p>Applying GELU activations for the output from the first layer is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oehjzguPjppqXU5jzPAQfA.png" /></figure><p>After expanding, the model decides which signals are important and which can be ignored. It acts a filter where the strong signals pass through whereas low or weak signals gets filtered. Without this filter, the model will learn only straight line representation (Linear) which is not expressive.</p><p>After filtering, the information now is filtered back to original size (Shrinking phase).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/154/1*gcgCtsi0hycDfi5ySaz5cA.png" /></figure><p>We use the same kind of linear layers (MLP) for the last phase. Now, we project the expanded dim to original dim.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/162/1*QOrZEjpzXiTPOJCzzmqvKA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kTTv6WNEEz5WVbS8Rp_aNA.png" /></figure><p>We can see that 8 — dimensions got shrinked to 2 — dimensions. So it can be added back to the original input (residual connection) and fit neatly with the rest of the Transformer. This compression forces the network to distill the most important information from the expanded features.</p><p>So, the entire operation is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/348/1*0Eckwl18WfTyj18URqRrGg.png" /></figure><p>The final output will be a rich representation of each token.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hdMUhYLWbuEOFU9ngXJatQ.png" /></figure><p>Now, Let us see a simple pytorch implementation of FFN that we discussed.</p><pre>import torch<br>import torch.nn as nn<br>import torch.nn.functional as F<br><br># Input embeddings (after attention) from the image<br># Each token is a vector of dimension 2<br>tokens = torch.tensor([<br>    [0.3, -0.5],   # &quot;I&quot;<br>    [0.7, 0.2],    # &quot;like&quot;<br>    [-0.4, 0.8],   # &quot;spiking&quot;<br>    [0.9, -0.1],   # &quot;neural&quot;<br>    [0.5, 0.6]     # &quot;networks&quot;<br>])<br><br># Define a simple Feed Forward Network (FFN)<br>class FFN(nn.Module):<br>    def __init__(self, d=2, d_ff=8):   # expand from 2 -&gt; 4 -&gt; 2<br>        super().__init__()<br>        self.fc1 = nn.Linear(d, d_ff)   # expansion phase<br>        self.fc2 = nn.Linear(d_ff, d)   # shrink phase<br>        self.activation = nn.GELU()     # nonlinear activation function<br><br>    def forward(self, x):<br>        return self.fc2(self.activation(self.fc1(x)))<br><br># Instantiate FFN<br>ffn = FFN(d=2, d_ff=8)<br><br># Pass tokens through FFN<br>output = ffn(tokens)<br><br>print(&quot;Input tokens:\n&quot;, tokens)<br>print(&quot;\nOutput tokens after FFN:\n&quot;, output)</pre><p>In the post, we will discuss about Language Modelling head and decoding strategies for building LLM’s.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24ec761e664a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>