SONUB 首頁

只有我們的團隊會閱讀。請不要填寫密碼或卡號。

謝謝!每則意見我們都會閱讀,並用來決定下一步改進什麼。

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

如何開啟通知

此瀏覽器或網址無法使用通知。請使用支援通知的瀏覽器,透過安全連線訪問SONUB。

如果通知被遮蔽,請在瀏覽器設定中允許通知。

  1. 開啟位址列旁的站點設定或瀏覽器的站點設定,將此網站的通知設為允許。
  2. 在裝置設定中允許瀏覽器傳送通知,並檢查勿擾模式或專注模式。
  3. 返回SONUB並重新整理頁面。通知開啟後,鈴鐺將不再顯示。

在iPhone或iPad上,將SONUB新增到主螢幕,從主螢幕開啟後允許通知。

用五個步驟做出人工智慧

每個專案都要經過同樣的五個步驟:準備、與視覺化、資料前處理、和模型。在裡做一個小小的垃圾訊息過濾器,把五個步驟全部走一遍。

雜亂的文字碎片進入一台過濾機器,重複的內容和碎屑掉了出來,最後出來一疊整齊乾淨的資料
5

模型評估

模型好不好,要看它在從沒見過的資料上的分數。還在改動東西的時候用 valid 部分,test 部分只在最後用一次。

只看準確率可能會被騙。基準線每次都說「不是垃圾訊息」,就能答對一半以上,但它一則垃圾訊息也抓不到。所以垃圾訊息過濾器還要看另外兩個數字:

  • 精確率:模型說是垃圾訊息的訊息中,真正是垃圾訊息的有多少。
  • 召回率:真正的垃圾訊息中,模型抓到了多少。
  • F1 把這兩個數字合成一個。

最後,讀一讀答錯的地方。它們會告訴你下一圈要改什麼。

一步一步看邏輯

  1. 預測是不是垃圾訊息 p ≥ 0.5
  2. 數四種情況 tp · fp · fn · tn
  3. 計算分數 accuracy · F1
  4. 和基準線比較 baseline
  5. 讀答錯的地方 wrong answers

機率在 0.5 或以上時,就判斷這則訊息是垃圾訊息。這樣每個答案都會落進四種情況之一:tp(垃圾訊息,判為垃圾訊息)、fp(正常訊息,判為垃圾訊息)、fn(垃圾訊息,判為正常訊息)和 tn(正常訊息,判為正常訊息)。這就是。

accuracy = (tp + tn) / 全部,precision = tp / (tp + fp),recall = tp / (tp + fn),F1 = 2 × precision × recall / (precision + recall)。程式也為基準線算出同樣的分數,只有模型的 F1 更高時才使用模型。

用真實工具做同樣的事

scikit-learn 為這裡的每個分數都準備了現成的函式,confusion_matrix 可以做出四種情況的表。

  • accuracy_score
  • precision_score
  • recall_score
  • f1_score
  • confusion_matrix

動手輸入並實驗

打開 5-evaluate.ipynb,在 valid 部分上執行所有儲存格。讀一讀答錯的地方,在 notes.md 裡寫下下一次要試的一個改動。只有全部完成後,才把 part = "valid" 改成 part = "test",再全部執行一次。

5-evaluate.ipynb本筆記本的完整程式碼
# Step 5. Model evaluation — score the model on data it never saw, and compare it with the baseline.
import csv
import json
import math

part = "valid"  # change to "test" only once, at the very end
with open("data/vocab.json", encoding="utf-8") as f:
    vocab = json.load(f)
with open("model.json", encoding="utf-8") as f:
    model = json.load(f)
with open(f"data/{part}.csv", newline="", encoding="utf-8") as f:
    rows = [(r["text"], int(r["spam"])) for r in csv.DictReader(f)]


def predict(text):
    x = [vocab[w] for w in dict.fromkeys(text.split()) if w in vocab]
    p = 1 / (1 + math.exp(-(model["bias"] + sum(model["weights"][i] for i in x))))
    return 1 if p >= 0.5 else 0


def scores(answers):
    tp = sum(1 for (_, y), a in zip(rows, answers) if y == 1 and a == 1)
    fp = sum(1 for (_, y), a in zip(rows, answers) if y == 0 and a == 1)
    fn = sum(1 for (_, y), a in zip(rows, answers) if y == 1 and a == 0)
    tn = sum(1 for (_, y), a in zip(rows, answers) if y == 0 and a == 0)
    precision = tp / (tp + fp) if tp + fp else 0.0
    recall = tp / (tp + fn) if tp + fn else 0.0
    f1 = 2 * precision * recall / (precision + recall) if precision + recall else 0.0
    return {"accuracy": (tp + tn) / len(rows), "precision": precision, "recall": recall, "f1": f1, "table": (tp, fp, fn, tn)}


model_answers = [predict(text) for text, _ in rows]
base = scores([model["baseline"]] * len(rows))
mine = scores(model_answers)

print(f"{part}: {len(rows)} rows")
print(f"{'':10} {'baseline':>9} {'model':>7}")
for key in ("accuracy", "precision", "recall", "f1"):
    print(f"{key:10} {base[key]:9.3f} {mine[key]:7.3f}")

tp, fp, fn, tn = mine["table"]
print("\nconfusion table   said spam  said normal")
print(f"  really spam     {tp:9}  {fn:11}")
print(f"  really normal   {fp:9}  {tn:11}")

print("\nwrong answers")
for (text, y), a in zip(rows, model_answers):
    if y != a:
        print(f"  answer {y}, model {a}: {text}")

print("\nuse the model" if mine["f1"] > base["f1"] else "\nkeep the baseline")
Run ▸ Run All Cells你應該看到的結果
valid: 18 rows
            baseline   model
accuracy       0.556   0.778
precision      0.000   1.000
recall         0.000   0.500
f1             0.000   0.667

confusion table   said spam  said normal
  really spam             4            4
  really normal           0           10

wrong answers
  answer 1, model 0: dear friend i need your help to move my money. you will get a share
  answer 1, model 0: we are hiring. work two hours a day and earn more
  answer 1, model 0: make money fast. join my team now
  answer 1, model 0: exclusive discount for our best customers. click the link

use the model

完成標準

在 valid 上,模型的 F1 是 0.667,基準線是 0.000,所以程式說要使用模型。召回率只有 0.5:它漏掉了用沒學過的單字寫的垃圾訊息,例如 money、hiring 和 share。你已經為下一圈寫下了一個改動,例如增加那類垃圾訊息的例子,或者保留只出現一次的單字。

遇到問題時

ZeroDivisionError 表示某一部分裡沒有垃圾訊息,或者模型一則都沒判為垃圾訊息;程式已經為這種情況保護了 precision 和 recall。如果你的分數不一樣,在執行 5 之前重新執行筆記本 3 和 4,因為每一步都讀取上一步的檔案。

可預約時間已過