SONUB 首页

只有我们的团队会阅读。请不要填写密码或卡号。

谢谢!每条意见我们都会阅读,并用来决定下一步改进什么。

关闭 发送
ZH 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,因为每一步都读取上一步的文件。

可预约时间已过