Try   HackMD

【論文筆記】Editing Models with Task Arithmetic

(這篇真的很有趣!)

論文連結:
https://arxiv.org/abs/2212.04089

Overview

這篇論文介紹了一種新的概念:task arithmetic,是利用 task vector 來對模型進行編輯(editing)的方法。

  • Editing: 指模型 pre-trained 之後,任何對模型進行修改的行為
  • Task vector: 指定模型在 weight space 的某個方向,如果將模型的 weight 往此方向修改,會增進此 task 的表現

作者指出透過算術運算(arithmetic operations),這些 task vectors 可以用來把模型往指定的目標修改。

透過這些實驗,他們發現 task arithmetic 是非常容易、有效率且有效果的 model editing 方法。

Task Vectors and Arithmetic Operations

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

首先定義 task vector。令

θpreRd 為 pre-trained model 的 weights,
θft
是由這個 pre-trained model 根據某個 target task fine-tune 後的 weights,則相對應的 task vector 定義為
τ=θftθpre
,也就是兩者的 element-wise difference。

這篇論文中,他們進行了三種 arithmetic operation 的實驗,分別是:

  • Forgetting via negation:使用採取了 negation 之後的 task vector,可以使對應的 target task 表現變差,但維持其他 tasks 的表現,可以運用在讓 pre-trained model 忘掉某些學過的內容,例如:讓語言模型減少生成有害的內容
  • Learning via addition:透過相加多個 task vectors,來讓 multi-task model 在各個任務上面都能有好的表現,或者增進在單一任務上的表現
  • Task analogies:如果四個 tasks A, B, C, D 具有 “A is to B as C is to D” 的關係,則可以透過將 C 的 task vector 加上 B 和 A 的差值,來增進 D 的表現

針對以上的運算,新的 model weights 定義為

θnew=θ+λτ
這裡
λ
是 scaling factor,會根據額外的 validation set 來決定。

Forgetting via Negation

目標是透過對 task vector 採負號,讓 target task 的表現變差,且幾乎不會讓其他的任務表現被影響。為了要衡量其他任務的表現,作者除了會計算 target task 的準確度,也會另外計算 control tasks(表示 general 的任務) 的準確度。

Image Classification

這個實驗使用了 CLIP model 以及 8 個不同的 target tasks,control tasks 的衡量使用到的是 ImageNet。每個任務對應的 task vector 是利用 CLIP 一一針對每個任務 fine-tune 後得到的。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

上表顯示在各個方法之下 target task 和 control task 的平均 accuracy。其中 gradient ascent 和 random vector 是作者用來比較的兩個 baselines,gradient ascent 是指直接往增加 loss 的方向進行 fine-tune,random vector 是指針對 model 的每層 layer 的 weights 都加上一個隨機採樣出來的 vector 來得到新的 weight。

可以發現加上一個 negative task vector 可以最有效減少 target task 的準確度,而且不會對 control task 有太大的影響;相比之下,用 gradient ascent 會讓 control task 嚴重受影響,而用 random vector 幾乎沒有對減少 target task 準確度有什麼幫助。

Text Generation

這個實驗的目標是希望可以減少 GPT-2 生成有害內容(toxic generations)的比例。模型使用的即是 GPT-2,task vector 則是用 Civil Comments 這個 dataset 裡面 toxicity score 大於 0.8 的資料 fine-tune 模型後得到的。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

上表顯示在各個方法之下 target task 和 control task 的結果,第一行是有害生成的比例,第二行為相對應的 toxic score,第三行則是以 WikiText-103 這個 dataset 作為 control task 的衡量。和先前在 image classification 的實驗一樣,他們用了 gradient ascent 和 random vector 兩個方法作為 baseline,另外還直接試著在 non-toxic sample 上進行 fine-tune。

我們可以看到透過 negating task vector,有害生成的比例下降很多,也比其他方法來的更好,而且在 control task 上不會有太大的影響。

Learning via Addition

這個方法的目標是透過相加多個 task vectors,來讓 multi-task model 在多個任務上面都能有好的表現,或者增進某個 model 在單一任務上的表現。

Image Classification

這個實驗使用的模型一樣是 CLIP,並且也一樣 fine-tune 在 8 種任務上來得到 task vectors。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

上圖顯示相加任一對 task vectors 在相對應分類上的表現。因為每個任務的難易度不同,為了要比較每個任務,他們將每個任務的 accuracy 都對 fine-tuned model 做標準化,所以 fine-tuned model 的 accuracy 皆為 1,另外 pre-trained model 的 accuracy 平均為 0.7。

從結果可以看到,對一個模型加上兩個 task vectors,能夠同時增進這個模型在兩個任務上的 accuracy,甚至這個結果能夠和只專門針對特定任務 fine-tuned 得到的模型相匹敵。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

上表顯示的是相加多個 task vectors 在所有任務上的平均準確率,可以看到隨著相加的 task vector 越多,這個 model 在多個任務上的表現也越好,當 8 個 task vectors 相加時,所有任務上的平均準確率可以達到 91.2%,儘管只使用了一個模型。

Natural Language Processing

除了 multi-task model 之外,作者也探討說相加多個 task vectors 是否能夠讓模型在單一任務上獲得更好的表現。這個實驗使用的模型是 T5-based model,並且在 GLUE benchmark 裡的 4 個文字分類任務上分別進行 fine-tune 得到 fine-tuned 模型。接著他們從 Hugging Face Hub 取了 427 個在多個任務上 fine-tuned 過的模型,並計算相對應的 task vectors,將這些 task vectors 加在前面提到由作者們自行 fine-tuned 的模型上。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

上表顯示各個方法在不同任務的表現,可以看到在 fine-tuned 模型上再加上 task vectors,能夠讓單一文字分類任務的表現更好。

Task Analogies

如果某四個任務具有 “A is to B as C is to D” 的關係,則就算我們在 task D 上只有一點點資料或甚至沒有任何資料,也可以利用前三個任務相對應的 task vectors 來改善 task D 的表現。我們可以利用

τnew=τC+(τBτA)

來得到 target task 的 task vector。這個運算稱作 task analogy。

作者同樣以 CLIP model 的 image classification 作為例子進行實驗,使用 ImageNet 中 125 個類別的圖片以及一個 sketch dataset,如此一來針對任意兩個類別 A 和 B,我們就可以有 ImageNet-A, ImageNet-B, Sketch-A 和 Sketch-B 四個 sub-classes。針對四個 sub-classes,分別進行 task analogy,例如:針對 ImageNet-A,利用 ImageNet-B、Sketch-A 和 Sketch-B 計算 ImageNet-A 的 task vector;接著再計算新的模型在分類任務上的準確率,結果如下表:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Samples per class 表示 few-shot experiment 使用的 sample 數量,task vectors 打勾或打叉表示 model 有沒有加上透過 task analogy 得到的 task vector,每個類別的 accuracy 表示在此任務上的準確率。

可以看到有使用 task analogy,在分類任務上會表現比沒有使用來的更好,而且就算沒有任何訓練的 sample 也可以有效果。

下圖顯示有和沒有使用由 task analogy 得到的 task vectors 下,隨著訓練的 samples 增多,分類準確率的變化。可以看到如果使用了 task vectors,沒有任何訓練資料的準確率,和沒有使用 task vectors 並使用 100 多個訓練資料的準確率差不多。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Discussion

作者針對 task vectors 額外做了一些討論:

Similarity between Task Vectors

首先作者比較了不同任務的 task vectors 之間的 cosine similarities,發現說不同的任務之間通常呈現 orthogonal,這個結果可以解釋為什麼相加不同 task vectors 通常不會互相影響。但當任務的類型比較相似時,他們的 cosine similarity 就會比較高,例如從表中可以看到 MNIST 和 SVHN、GTSRB 之間的 cosine similarities 比較高,而這三個任務都是做數字辨識的;另外 EuroSAT 和 RESISC45 都是做衛星圖像辨識的任務。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Limitation

因為針對 task vectors 做運算的時候,針對的是模型 weights 的 element-wise operations,所以最大的限制是這些操作必須在具有完全相同架構的模型上才能使用。

Conclusion

簡單總結,這篇論文提出了一個新的編輯模型的想法,透過對 task vectors 進行運算,可以讓模型表現變差、表現變好,或者在任務資料不足的時候也可以透過 task analogy 得到表現好的模型。對 task vectors 進行運算比起額外的 fine-tune 更有效率,而且表現也很不錯。