F1 小註解

上次去墨西哥剛好有 F1 賽車 (2026年10月30日至11月1日),但我們沒有這個行程;只看到路上歡迎賽車手的佈置。這篇要講的是另外一個 F1 – token-level 的 F1。比起賽車的 F1,它離我更近一點。

F1 分數是 Precision 與 Recall 的調和平均數,明眼人應該可以立刻發現有算術平均數和幾何平均數的味道 – 只是省了開根號。

這個 F1 在詞級(Token-level)要怎麼用呢?計算步驟如下:

前置擷取與正規化(Normalization)

  1. 模型可控的提示,下以貪婪解碼生成可控長度的預測字串。
  2. 預測字串與標準答案各自做相同正規化:
    • 全部轉小寫(lowercase)
    • 移除標點符號(punctuation)
    • 移除英文冠詞(a、an、the)
    • 以空白切分並去除多餘空白,得到詞序列 $P$(預測)與 $G$(標準答案)

詞袋重疊與 F1 公式

把 $P$ 與 $G$ 當作多重集合(計入每個詞出現的次數),計算兩者交集的詞數 Common,也就是預測 P 和標準答案 G 裡面有幾個詞是一樣的 。

Common = \sum_w \min(\text{count}_P(w), \text{count}_G(w))$:

根據定義,我們可以用 common 來表示精準率 (precision) 、召回率 (recall) 和 F1 的關係。

$\text{Common} = \sum_w \min(\text{count}_P(w), \text{count}_G(w))$

F1 特例:精確率和召回率兩者皆空為 1.0;只有一邊為空或 Common}=0 時為 0.0。

舉例來說:

標準答案:["The Eiffel Tower", "Eiffel Tower"] 正規化後為 ["eiffel", "tower"](長度 2)

模型輸出第一行:"in the Eiffel Tower, Paris" 正規化為 ["in", "eiffel", "tower", "paris"](長度 4)

計算:Common = 2,Precision = 2/4 = 0.5,Recall = 2/2 = 1.0,F1 = 2 * 2 / (0.5 + 1) = 4 / 6 ~= 0.667

上面這個模型輸出算是 “像" 標準答案,但不是百分之百相容 (此時 F1 = 1)。不管 in the Eiffel Tower、on the Eiffel、"the Eiffel, Paris" 都會被扣分,雖然這些回答在語意上和標準答案並沒有差別。

R 小筆記 – 2

R 真是不一般, 首先它不像別的語言, index 是從 0 開始. 而是從 1 開始. 再來呢, 很多地方的 1 看起來是 1 但不是 1; 看起來是 0 但也不是 0; 2 也不是平常的 2. 底下舉例說明.

上次說到用 X predict Y, 像是身高預測. 因為最低身高不是 0, 這條 minimum error 的線是有截距的. 此時一般線性迴歸的斜率公式是:

  • 模型:y = α + βx
  • 斜率:β = r(x,y) × sd(y)/sd(x)

假設強制要求通過原點, 他的迴歸如下.

  • 模型:y = βx(無截距項)
  • 斜率:β = Σ(xy)/Σ(x²)

最後, 假設原本是有截距的迴歸, 但我把它的數據先做正規化, 那它會跟誰一樣嗎?

x <- c(1.2, 2.8, 3.5, 1.9, 4.1, 2.3, 3.8, 1.6, 2.9, 3.2)
y <- c(2.4, 5.1, 6.8, 3.2, 7.9, 4.1, 7.2, 2.8, 5.5, 6.1)

# 標準化(z-score normalization)
x_norm <- (x - mean(x)) / sd(x)
y_norm <- (y - mean(y)) / sd(y)

# 方法0:一般線性迴歸斜率公式
slope0 <- cor(x, y) * sd(y) / sd(x)

# 方法1:通過原點迴歸
slope1 <- sum(x * y) / sum(x^2)

# 方法2:先正規化後當作一般線性迴歸
slope2 <- cor(x_norm, y_norm) * sd(y_norm) / sd(x_norm) 

cat("有截距 - 不通過原點迴歸:", slope0, "\n")
cat("通過原點迴歸:", slope1, "\n")
cat("標準化後做一般回歸:", slope2, "\n")

結果我們會得到 slope0 ≈ 1.85, slope1 ≈ 1.94, slope2 ≈ 0.93, 三個都不一樣.

方法 0 和方法 2 當中, 因為按照定義 :

sd(x_norm) = sd(y_norm) = 1, mean = 0

而 correlation 方面: cor(x,y) 又等於 cor(x_norm, y_norm),

所以 sd(y)/sd(x) ≠ 1 的情況下, 會導致 slope0 ≠ slope2.

方法 1 是強制通過原點, 而原本最佳解可能是有截距的, 所以 slope0 也不太容易巧合和 slope1 、slope2 一樣.

上面講的還沒有發揮到 R 的精神, 我們用 lm() 重做一次.

x <- c(1.2, 2.8, 3.5, 1.9, 4.1, 2.3, 3.8, 1.6, 2.9, 3.2)
y <- c(2.4, 5.1, 6.8, 3.2, 7.9, 4.1, 7.2, 2.8, 5.5, 6.1)

# 標準化
x_norm <- (x - mean(x)) / sd(x)
y_norm <- (y - mean(y)) / sd(y)

# === 手動計算(原始方法) ===
slope0_manual <- cor(x, y) * sd(y) / sd(x)
slope1_manual <- sum(x * y) / sum(x^2)
slope2_manual <- cor(x_norm, y_norm) * sd(y_norm) / sd(x_norm)

# === 使用 lm() 實現 ===
# 方法0:一般線性迴歸(有截距)
model0 <- lm(y ~ x)
slope0_lm <- coef(model0)[2]  # 取斜率係數

# 方法1:通過原點迴歸(無截距)
model1 <- lm(y ~ x - 1)  # -1 表示移除截距項
slope1_lm <- coef(model1)[1]  # 只有一個係數

# 方法2:標準化後的一般迴歸
model2 <- lm(y_norm ~ x_norm)
slope2_lm <- coef(model2)[2]  # 取斜率係數

# === 比較結果 ===
cat("=== 手動計算 vs lm() 比較 ===\n")
cat("方法0 - 有截距迴歸:\n")
cat("  手動計算:", slope0_manual, "\n")
cat("  lm() 結果:", slope0_lm, "\n")
cat("  差異:", abs(slope0_manual - slope0_lm), "\n\n")

cat("方法1 - 通過原點迴歸:\n")
cat("  手動計算:", slope1_manual, "\n")
cat("  lm() 結果:", slope1_lm, "\n")
cat("  差異:", abs(slope1_manual - slope1_lm), "\n\n")

cat("方法2 - 標準化後迴歸:\n")
cat("  手動計算:", slope2_manual, "\n")
cat("  lm() 結果:", slope2_lm, "\n")
cat("  差異:", abs(slope2_manual - slope2_lm), "\n\n")

# === 顯示完整模型資訊 ===
cat("=== 完整模型摘要 ===\n")
cat("\n--- 方法0:一般線性迴歸 ---\n")
print(summary(model0))

cat("\n--- 方法1:通過原點迴歸 ---\n")
print(summary(model1))

cat("\n--- 方法2:標準化後迴歸 ---\n")
print(summary(model2))

我們可以看到 model1 <- lm(y ~ x – 1) , 它不是真的減 1, -1 表示移除截距項. 還可以表示為 y ~ 0 + x. 兩者等效.

再來 slope2_lm <- coef(model2)[2] 的 [2] 不是第三個係數. 其中, 第一個係數 [1] 就固定是截距, 第二個係數 [2] 就固定是斜率.

Rrrrrrrrrrrrrrrr 啊啊啊啊啊啊啊啊啊啊….. (聲音漸尖)