惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
博客园 - 叶小钗
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
美团技术团队
aimingoo的专栏
aimingoo的专栏
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
Last Week in AI
Last Week in AI
The GitHub Blog
The GitHub Blog
小众软件
小众软件
T
Tailwind CSS Blog
Martin Fowler
Martin Fowler
B
Blog RSS Feed
月光博客
月光博客
量子位
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
Y
Y Combinator Blog
B
Blog
MyScale Blog
MyScale Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
I Trained a Machine-Learning Model From Scratch in 40 Lin...
Devanshu Biswas · 2026-06-14 · via DEV Community

Devanshu Biswas

Everyone imports LinearRegression from scikit-learn and moves on. Today we build it by hand — and you'll see that "training a model" is just a line sliding into a cloud of points.

This is Day 1 of MachineLearningFromZero: every core algorithm built from scratch, no black-box library.

The model is two numbers

let m = Math.random(), b = Math.random();   // slope + intercept
const predict = x => m * x + b;              // ŷ = m·x + b

That's the whole "model". Training means finding the m and b that fit the data.

The loss: how wrong are we?

For each point, take (prediction − actual), square it (so big misses hurt more and signs don't cancel), and average. That's Mean Squared Error:

const mse = () =>
  data.reduce((s, [x, y]) => s + (predict(x) - y) ** 2, 0) / data.length;

Lower MSE = better line. Training = making this number small.

Gradient descent: roll downhill

The gradient tells us which way the loss increases. We want it to decrease, so we step the opposite way. Calculus gives clean formulas for the slope of MSE with respect to m and b:

function step(lr) {
  let dm = 0, db = 0;
  for (const [x, y] of data) {
    const err = predict(x) - y;   // how wrong, and which direction
    dm += 2 * err * x;            // ∂loss/∂m
    db += 2 * err;                // ∂loss/∂b
  }
  dm /= data.length; db /= data.length;
  m -= lr * dm;                   // step downhill
  b -= lr * db;
}

lr is the learning rate — the step size. Too small and training crawls; too big and it overshoots and the loss explodes. This one knob is the most important hyperparameter in all of ML.

Train = repeat the step

for (let epoch = 0; epoch < 200; epoch++) step(0.1);

As the line nears the best fit, the gradients shrink toward zero, so the steps shrink too — it settles on its own. Then prediction is trivial:

const score = predict(4.5);   // m * 4.5 + b

Why this matters

This exact loop — model → loss → gradient → step — is the engine under logistic regression, neural networks, and deep learning. Master it here on a single line and every later algorithm is a variation on this theme.

🌐 Watch the line learn live (drag the learning rate, hit Train): https://dev48v.infy.uk/ml/day1-linear-regression.html

Day 1 of MachineLearningFromZero. A new algorithm every day, built from scratch.