SONUB home

Only our team reads this. Don't include passwords or card numbers.

Thank you! We read every message and use it to decide what to improve next.

Close Send
EN English 한국어 日本語 简体中文 繁體中文 हिन्दी বাংলা اردو Español Português Français Deutsch Русский العربية Bahasa Indonesia Tiếng Việt ไทย Türkçe

How to enable notifications

Notifications are unavailable in this browser or on this address. Try a supported browser on the secure SONUB site.

If notifications are blocked, allow them in your browser settings.

  1. Open the site controls beside the address bar, or your browser’s site settings. Set Notifications for this site to Allow.
  2. In your device settings, allow notifications for your browser and check Do Not Disturb or Focus.
  3. Return to SONUB and reload the page. Once notifications are on, the bell disappears.

On iPhone or iPad, add SONUB to your Home Screen, open it there, then allow notifications.

Build AI in five steps

Every project goes through the same five steps: preparation, & visualization, data , and model evaluation. Build a small spam filter in your and walk through all five.

Messy pieces of writing go into a filter machine, duplicates and scraps fall out, and a neat stack of clean data comes out
4

Model training

Always start with the simplest possible answer, a baseline. Here it is "always say not spam". Any model you build must beat it, or it is not worth using.

The learning model gives every word a . Spam-like words get a positive weight and normal words a negative one. To judge a message, the model adds up the weights of its words and a bias, then squashes the total into a between 0 and 1.

Training means showing the model every example many times and nudging the weights a little after each guess. The loss measures how wrong the guesses are; it must go down.

The logic, step by step

  1. Make a baseline most common
  2. Turn text into word numbers features()
  3. Guess a probability
  4. Nudge the weights weight -= rate × error
  5. Save the model model.json

features() turns a message into the numbers of its known words, using vocab.json from step 3. The guess is p = 1 / (1 + e^-(bias + sum of the word weights)); this sigmoid turns any total into a probability.

error = p − y is how wrong the guess was (y is the real answer, 1 or 0). Each weight of a word in the message moves by rate × error in the opposite direction, so the next guess is a little closer. One pass over all the is an ; this program runs 20 epochs.

The weights and bias are saved in model.json. That file is the model: the numbers it learned.

The same job with real-world tools

With scikit-learn this is LogisticRegression().fit(X, y). Deep learning models (Keras Dense layers, or a written in PyTorch) are trained in the same way: guess, measure the loss, nudge the weights.

  • LogisticRegression().fit()
  • keras Sequential · Dense
  • PyTorch

Type and try

Open 4-train.ipynb and run all cells. Watch the loss after each group of epochs, then look at the words the model finds most spam-like and most normal. Change rate and run the training cell again to see what happens.

4-train.ipynbThe full code of this notebook
# Step 4. Model training — a baseline rule first, then a small learning model (logistic regression).
import csv
import json
import math
import random

with open("data/vocab.json", encoding="utf-8") as f:
    vocab = json.load(f)
with open("data/train.csv", newline="", encoding="utf-8") as f:
    train = [(r["text"], int(r["spam"])) for r in csv.DictReader(f)]


def features(text):
    """Text -> numbers: the numbers of the known words in the text."""
    return [vocab[w] for w in dict.fromkeys(text.split()) if w in vocab]


# 1) Baseline: always answer the most common label
spam = sum(y for _, y in train)
baseline = 1 if spam > len(train) - spam else 0
print("baseline answer:", baseline)

# 2) Logistic regression: one weight per word + bias -> probability of spam
weights = [0.0] * len(vocab)
bias = 0.0
rate = 0.3
data = [(features(text), y) for text, y in train]
random.seed(42)
for epoch in range(1, 21):
    random.shuffle(data)
    total = 0.0
    for x, y in data:
        p = 1 / (1 + math.exp(-(bias + sum(weights[i] for i in x))))
        p = min(max(p, 1e-9), 1 - 1e-9)
        total += -(y * math.log(p) + (1 - y) * math.log(1 - p))
        error = p - y  # how wrong the guess was
        bias -= rate * error
        for i in x:
            weights[i] -= rate * error
    if epoch == 1 or epoch % 5 == 0:
        print(f"epoch {epoch:2}  loss {total / len(data):.4f}")

with open("model.json", "w", encoding="utf-8") as f:
    json.dump({"baseline": baseline, "bias": bias, "weights": weights}, f)
print("saved model.json")

words = sorted(vocab, key=lambda w: weights[vocab[w]])
print("spam words:", ", ".join(words[-5:][::-1]))
print("normal words:", ", ".join(words[:5]))
Run ▸ Run All CellsWhat you should see
baseline answer: 0
epoch  1  loss 0.4373
epoch  5  loss 0.0944
epoch 10  loss 0.0481
epoch 15  loss 0.0322
epoch 20  loss 0.0241
saved model.json
spam words: message, now, confirm, here, your
normal words: the, is, are, remember, home

Done when

The loss goes down from about 0.44 to about 0.02, model.json is saved, and the spam words make sense (for example now, confirm, here, your).

If you get stuck

If the loss goes up or jumps around, the rate is too large; try 0.1. If you see math error, check the minus sign in math.exp(-(…)).

Booking time has passed