DEV Community

Cover image for Building Regression in PyTorch
Ganesh Kumar
Ganesh Kumar

Posted on AI-assisted

Building Regression in PyTorch

Hello, I'm Ganesh Kumar, and I'm building LiveReview — a blast-radius aware AI code review built for your business-critical systems. Star us to help devs discover the project, give it a try, and share your feedback to help improve the product.

Let's first build the simplest possible regression problem ourselves. This will let you see every part of the neural network.

Let create a data for linear regression

We'll create:

y=2x+1

So our training data is:

x y
1 3
2 5
3 7
4 9
5 11

This is intentionally simple because we already know the answer:

y=2x+1

Our neural network's job is to discover:

w approx 2
and:
b approx 1

Linear Regression Function

Our model is simply:

       x
       │
       ▼
┌─────────────┐
│ Linear      │
│             │
│ y = wx + b  │
└──────┬──────┘
       │
       ▼
       ŷ
Enter fullscreen mode Exit fullscreen mode

In PyTorch:

import torch
import torch.nn as nn

# Dataset
x = torch.tensor([
    [1.0],
    [2.0],
    [3.0],
    [4.0],
    [5.0]
])

y = torch.tensor([
    [3.0],
    [5.0],
    [7.0],
    [9.0],
    [11.0]
])


# Model
model = nn.Linear(1, 1)

print("Weight:", model.weight)
print("Bias:", model.bias)
Enter fullscreen mode Exit fullscreen mode

At this point PyTorch randomly initializes:

w

and:

b

Maybe you get something like:

w=0.37

b=-0.12

So the initial model might be:

ŷ =0.37x-0.12

Obviously, that's bad.
And It will randomly change for every run.

Training Neural Network

loss_function = nn.MSELoss()

optimizer = torch.optim.SGD(
    model.parameters(),
    lr=0.01
)

for epoch in range(1000):

    # Forward pass
    prediction = model(x)

    # Calculate MSE
    loss = loss_function(prediction, y)

    # Calculate gradients
    optimizer.zero_grad()
    loss.backward()

    # Update weight and bias
    optimizer.step()

    if epoch % 100 == 0:
        print(
            epoch,
            loss.item(),
            model.weight.item(),
            model.bias.item()
        )
Enter fullscreen mode Exit fullscreen mode

Eventually you'll get approximately:

w=2

b=1

So the model learned:

ŷ =2x+1

Wrapping Up

Finaly this is how it looks like when combined all the steps together:

     Dataset
        │
        ▼
 x = [1,2,3,4,5]
 y = [3,5,7,9,11]
        │
        ▼
┌───────────────┐
│ Linear        │
│               │
│ ŷ = wx + b    │
│               │
│ w = ?         │
│ b = ?         │
└───────┬───────┘
        │
        ▼
    Prediction
        │
        ▼
       MSE
        │
        ▼
  loss.backward()
        │
        ▼
    Gradients
        │
        ▼
 optimizer.step()
        │
        ▼
   Update w, b
        │
        └──────→ repeat
Enter fullscreen mode Exit fullscreen mode

There is no activation function yet.

That's intentional.

Once this is completely clear, we can introduce a hidden layer.

Then we'll change the dataset to something that cannot be represented by a straight line, such as y=x^2.


Your team's attention is limited, and the deluge of AI-generated code is making it harder to keep production reliable and secure without slowing you down.

I'm building LiveReview, a blast-radius aware AI code review built for your business-critical systems.

Instead of presenting every diff with equal emphasis, LiveReview scores each change by blast radius — how far its impact reaches through your call graph — so you can focus attention where it actually matters.

Spend code review effort where business risk is highest — not spread evenly across every diff.

⭐ Star it on GitHub:

GitHub logo HexmosTech / LiveReview

Blast-Radius Aware AI Code Review for Business-Critical Systems

LiveReview

gitleaks.yml osv-scanner.yml govulncheck.yml semgrep.yml dependabot-enabled mcp-testcases.yml

LiveReview: Blast-Radius Aware AI Code Review for Business-Critical Systems

LiveReview is an AI code reviewer that scores every hunk of a diff by blast radius: how far a change reaches through your call graph, how much persistent state it touches, and how well-tested it is. A 3-line change to a shared auth check can outrank a 300-line UI tweak. Your team's attention goes to the highest-risk code first, not spread evenly across every diff.

blast-radius-demo.mp4

LiveReview's Blast Radius & Review Priority scoring, live in the diff viewer.
















The exact math, not a black box Visualize blast radius at a glance Every factor that feeds the score

How does Blast Radius scoring work? (a more technical explanation)

Here's the goal:

  • A 3-line fix in a function used by 40 other files, that also writes to a database, should score high.
  • A 300-line UI change in one file, fully covered by…




Click below to try LiveReview with your codebase:

LiveReview Banner

Top comments (0)