<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aahan-Chauhan</title>
    <description>The latest articles on DEV Community by Aahan-Chauhan (@aahanchauhan).</description>
    <link>https://dev.to/aahanchauhan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4014080%2F4062f111-0b89-4de7-b384-3da4bc6a86f6.png</url>
      <title>DEV Community: Aahan-Chauhan</title>
      <link>https://dev.to/aahanchauhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aahanchauhan"/>
    <language>en</language>
    <item>
      <title>Neural Networks: Weights, Activation, and Backpropagation</title>
      <dc:creator>Aahan-Chauhan</dc:creator>
      <pubDate>Sun, 06 Sep 2026 22:51:56 +0000</pubDate>
      <link>https://dev.to/aahanchauhan/neural-networks-weights-activation-and-backpropagation-jd6</link>
      <guid>https://dev.to/aahanchauhan/neural-networks-weights-activation-and-backpropagation-jd6</guid>
      <description>&lt;p&gt;If you've ever looked at a neural network diagram and every wondered what is actually happening in these networks, this post is just for you. I pulled this post together after researching the properties of neural networks, and writing out what is finally there to click. No analogies that lead to no explanations, just the math behind neural networks, one piece at a time.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Simplest Possible Net——A Perceptron&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Before neural networks got keep, they started shallow. A &lt;strong&gt;perceptron&lt;/strong&gt; is a neural net with zero hidden layers, invented back in 1943. Perceptions work when data is &lt;em&gt;linearly separable&lt;/em&gt;, meaning you could draw a line and cleanly split the classes on either side of it.&lt;/p&gt;

&lt;p&gt;Let's take a simple example with an equation: D=y-2x-3. Everything where D&amp;gt;=0 is Class 1 and everything where D&amp;lt;0 is Class 2. This is basically how a model works: a single linear boundary that decides which side the example falls on.&lt;/p&gt;

&lt;p&gt;However, real data is rarely this cooperative. The moment your classes tangle in a way no straight line can separate, you will need more than one layer. This is really where the deep learning begins.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What A Neuron is Actually Computing&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Zoom into any single neuron in a network, and this is what entire computation occuring inside of it: &lt;/p&gt;

&lt;p&gt;S = b∑ w_i * x_i&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;x_i are the inputs coming in, such as the pixel values and whatever your data is.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;w_i are the weights (how much the network currently believes each input matters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;b is the bias that acts like a nudge that shifts the output independent of the inputs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;S is the logit, which is a unbounded value. The S value either shoots in the negative or positive direction indefinetely.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Stack raw logits like this across layers with nothing else added, and the whole network has a mathematical collapse into a linear function. Depth alone buys you nothing. You need a twist.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Non-Linear Twist That Makes Depth Matter&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;That very twist is the activiation function. This is applied to every logit before it moves onto the next layer.  One of them is sigmoid: &lt;/p&gt;

&lt;p&gt;ø(S) = 1 / (1+e)^-s&lt;/p&gt;

&lt;p&gt;Just as the equation says, the ø(s) pushes the S value closest to 1 as the ø(s) value is positive. If ø(s) is negative, then the S value moves towards 0.&lt;/p&gt;

&lt;p&gt;This nonlinearity is the backbone of how depth works.  With nonlinearity, each added layer can fold the decision boundary into complex shapes, which is exactly what is required to separate tangled data.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Stacking Neurons into Layers&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;layer&lt;/strong&gt; is just a bank of neurons, each computing its own weighted sum form the same inputs, and each with its own weights. The formula goes like this: &lt;/p&gt;

&lt;p&gt;z_i​ = ϕ (j∑​ x_j * ​w_ij​)&lt;/p&gt;

&lt;p&gt;The middle layers are called the &lt;strong&gt;hidden&lt;/strong&gt; layers. They are called hidden layers because the values are not directly obseved; only the input and the output (final value) are observed. The early layers tend to pick up the simple patterns while the deeper layers form abstract connection with the simple patterns made by the early layers.&lt;/p&gt;

&lt;p&gt;The final output layer turns those hidden features into a prediction: &lt;/p&gt;

&lt;p&gt;y_i ​= ϕ(j∑​ z_j​ * w_ij​)&lt;/p&gt;

&lt;p&gt;For a K-way classification, the K output neurons are used. These neurons are used as one confidence score per class. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Softmax: Turning Raw Scores into Real Probabilities&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Say that the output layer displays three logits: (0.01, -3.8, 4.2). These numbers are all currently unbounded and do not hold any significant meaning at the moment. Softmax fixes this by taking any of those three logits and converting them into probabilities between 0 and 1 that sum up to exactly 1.0. The formula for softmax is: &lt;/p&gt;

&lt;p&gt;p_j = (e^z_j) / (∑_k * e^zk)​​&lt;/p&gt;

&lt;p&gt;In the example above, the 4.2 value would come out closer to a 90% or higher confidence score, while -3.8 will approach closer to 0, a lower confidence score.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Loss Functions: Scoring the Damage&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;One the network makes a prediction, there must be a way to measure how inaccurate it was. For the numerical predictions, it is normally considered the &lt;strong&gt;Mean Squared Error&lt;/strong&gt;. For classification, it is almost always &lt;strong&gt;Cross-Entropy&lt;/strong&gt;. This is how the formula goes:&lt;/p&gt;

&lt;p&gt;CE = -n∑i=1 [y_i * log(p(yi​)) + (1-yi) * log(1-p(yi))]&lt;/p&gt;

&lt;p&gt;The intuition is quite simple: if the true label is 1 and the model confidently predicts closer to the O, the log term blows up and there are severe penalties. However, if the model was &lt;em&gt;correct&lt;/em&gt;, the penalty shrinks towards zero. Cross-entropy rewards confident correctness and punishes the confident mistakes. This is exactly what you want out of a loss function.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What the Network Actually Learns: Backdrops and Gradient Descent&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Here's the loop that turns a "useless" network into a "useful" network:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Feed a batch of training samples through the network.&lt;/li&gt;
&lt;li&gt;Compute the predictions as the output layer.&lt;/li&gt;
&lt;li&gt;Measure losses and how inaccurate the predictions were.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backpropagate&lt;/strong&gt; the error backwards through every layer so you can calculate the weight contributed to the error.&lt;/li&gt;
&lt;li&gt;Nudge every weight slightly in the direction that reduces the layer.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That fifth step is called the &lt;strong&gt;gradient descent&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;(w_ij)^new = w_ij - α * (∂E​) / (∂w_ij)&lt;/p&gt;

&lt;p&gt;α holds the &lt;strong&gt;learning rate&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Just picture the loss as a landscape. Training is descending that landscape, one gradient-calculated step at a time, trying to reach a low point. In a network with millions of billions of weights, that landscape has many dimensions with multiple valleys. This is part of why training is not guaranteed to find the &lt;em&gt;global&lt;/em&gt; best solution, just a good one.&lt;/p&gt;

&lt;p&gt;One pass through the &lt;em&gt;entire&lt;/em&gt; training is called an &lt;strong&gt;epoch&lt;/strong&gt;. Networks are trained across many epochs because each pass leaves weights in a sligtly different place. This is why seeing the same data again isn't wasted effort.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Watching It Happen Through a PyTorch Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Due to restrictions, the code commensts will be in []. Here's a real and complete neural network trained on MNIST in PyTorch.&lt;/p&gt;

&lt;p&gt;import torch&lt;br&gt;
import torch.nn as nn&lt;br&gt;
import torch.optim as optim&lt;br&gt;
from torchvision import datasets, transforms&lt;/p&gt;

&lt;p&gt;[Load Data]&lt;br&gt;
convertToTensor = transforms.ToTensor()&lt;br&gt;
train_data = datasets.MNIST(root='./data', train=True, download=True, transform=convertToTensor)&lt;br&gt;
train_loader = torch.utils.data.DataLoader(train_data, batch_size=64, shuffle=True)&lt;/p&gt;

&lt;p&gt;[Define the network: 784 input pixels -&amp;gt; 128 hidden neurons -&amp;gt; 10 output classes]&lt;br&gt;
model = nn.Sequential(&lt;br&gt;
    nn.Flatten(),&lt;br&gt;
    nn.Linear(28*28, 128),&lt;br&gt;
    nn.ReLU(),&lt;br&gt;
    nn.Linear(128, 10)&lt;br&gt;
)&lt;/p&gt;

&lt;p&gt;criterion = nn.CrossEntropyLoss()&lt;br&gt;
optimizer = optim.SGD(model.parameters(), lr=0.01)&lt;/p&gt;

&lt;p&gt;[Train]&lt;br&gt;
for epoch in range(5):&lt;br&gt;
    for images, labels in train_loader:&lt;br&gt;
        optimizer.zero_grad()&lt;br&gt;
        predictedLabels = model(images)&lt;br&gt;
        loss = criterion(predictedLabels, labels)&lt;br&gt;
        loss.backward()&lt;br&gt;
        optimizer.step()&lt;br&gt;
    print(f"Epoch {epoch}, Loss: {loss.item():.4f}")&lt;/p&gt;

&lt;p&gt;Every concept from this post is sitting amongst these lines of code: nn.linear is the weighted sum, nn.reLU is the activation function, criterion computers cross-entropy loss, loss.backward() runs backpropagation, and optimizer.step() is the gradient descent update. This is basically just theory executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The One-Sentence Summary&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;A neural network is a stack of weighted sums broken up by non-linear activations, trained by repeatedly measuring how wrong its predictions are and nudging every weight a tiny bit in the direction that makes it less wrong — and that exact loop, scaled up, is what's running behind every AI tool you've ever used.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>beginners</category>
      <category>deeplearning</category>
      <category>pytorch</category>
    </item>
    <item>
      <title>Entropy and Cross-Entropy, Explained</title>
      <dc:creator>Aahan-Chauhan</dc:creator>
      <pubDate>Tue, 25 Aug 2026 23:43:19 +0000</pubDate>
      <link>https://dev.to/aahanchauhan/entropy-and-cross-entropy-explained-f3m</link>
      <guid>https://dev.to/aahanchauhan/entropy-and-cross-entropy-explained-f3m</guid>
      <description>&lt;p&gt;If you've spent any time around machine learning the familiarizing yourself with the concept, you have seen the term "entropy" and "cross entropy" show up everywhere. These two distinct terms often show up in topics regarding: classification models, neural networks, and understanding language models. But if you are like me a few weeks ago, you would not be able to write down the formulas without &lt;em&gt;feeling&lt;/em&gt; the meaning of these terms.&lt;/p&gt;

&lt;p&gt;This post is my attempt to build this intuition of yours from the ground up. This blog will cover topics ranging from entropy to cross-entropy. As you read this post, these topics will finally connect to why they are the loss functions of choice in machine learning.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;PART 1: What Entropy Actually Measures&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Forget about the neural networks for a second. Entropy comes from the information theory and, at its core, it answers one main question: How surprised should you be by the outcome of a random event?&lt;/p&gt;

&lt;p&gt;Now let's image two coins:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Coin A is fair with 50% heads and 50% tails.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Coin B is rigged with 99% head and 1% tails.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If I flip Coin B and tell you the result, you will almost never be surprised if the outcome is heads. However, if I flip Coin A, you will have no idea what is coming. This is because every flip of Coin A is a genuine mystery that is only uncovered once the coin is flipped.&lt;/p&gt;

&lt;p&gt;Entropy is a formal way of measuring this same "amount of surprise." A fair coin like Coin A has high entropy (maximum uncertainity) while a rigged coin like Coin B has low entropy (easily predictable). &lt;/p&gt;

&lt;p&gt;Mathematically, the entropy for a probabilitiy distribution is: &lt;em&gt;H(P)=−x∑​p(x)log(p(x))&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now, lets unpack this formula and understand &lt;em&gt;why&lt;/em&gt; it makes sense.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;log(p(x)) basically represents the "surprise." Rare events—small p(x)—have a more negative log value, meaning higher surprise rates. Common events—large p(x)—have log values that are closer to 0, meaning lower surprise rates.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The negative sign flips to a positive sign, knows as a surprise score, since the probabilities are between 0 and 1 with the log being a negative.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Multiplying by the p(x) value and summing it up gives you the expected value of the surprise. The expected value of surprise is the average surprise you'd experience sampling from this distribution over and over.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So basically: Entropy is the average amount of "surprise" backed into a distribution.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Part 2: From Entropy to Cross-Entropy&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Entropy already assumes that you &lt;em&gt;know&lt;/em&gt; the true distribution of the outcomes. However, in machine learning, we rarely know the true distribution. We are trying to &lt;em&gt;predict&lt;/em&gt; the distribution.&lt;/p&gt;

&lt;p&gt;Here is the mathematical definition of cross-entropy: &lt;em&gt;H(P,Q)=−x∑​p(x)log(q(x))&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This is where cross-entropy plays a major role. It measures the surprise you experience when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The &lt;em&gt;true&lt;/em&gt; outcomes come from distribution P (considered the actual label of an image).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But you are using a &lt;em&gt;predicted&lt;/em&gt; distribution Q (your model's guess) to measure that particular surpise.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Notice the subtle but crucial difference from entropy: we still weight by the &lt;em&gt;true&lt;/em&gt; probabilitiy p(x), but we take the log of the &lt;em&gt;predicted&lt;/em&gt; probability q(x).&lt;/p&gt;

&lt;p&gt;Here is the intuition: if your model's predicted distribution Q is close to the true distribution P, then the cross-entropy will be low while the actual entropy is close to P. However, if the model is confidently wrong, the log term completely explodes and the cross-entropy shoots up.&lt;/p&gt;

&lt;p&gt;This is exactly something what we want in a loss function:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Confident and correct predictions are low loss.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Confident and high predictions are high loss.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Uncertain predictions are the moderate losses.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Part 3: Why Does This Matter For Neural Networks?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;When a classifier is trained, the model puts out a probability distribution over classes. Cross-entropy loss compares that predicted distribution against the true label, usually a one-hot vector.&lt;/p&gt;

&lt;p&gt;Since the true label is one-hot, the formula is properely simplified as almost every term in the sum becomes zero except for the one corresponding to the correct class. The equation goes like: Loss = -log(q(correct class)).&lt;/p&gt;

&lt;p&gt;During the training, the model is just trying to push the predicted probability of the &lt;em&gt;correct&lt;/em&gt; class as close to the value 1 as possible. This is because -log(1)=0 and -log(x) has x approaching 0.&lt;/p&gt;

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

&lt;p&gt;I am a high school senior that is interested in learning ML from the ground up and this is one of the first concepts that I have thoroughly researched in detail. I hope this blog was helpful to other students and adults like me that have a fond interest in ML.&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>beginners</category>
      <category>informationtheory</category>
      <category>deeplearning</category>
    </item>
  </channel>
</rss>
