PEFT 文件

字首調優 (Prefix tuning)

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

字首調優 (Prefix tuning)

字首調優 (Prefix tuning) 透過在輸入序列前新增一系列特定於任務的向量來進行學習,同時保持預訓練模型凍結。這些字首引數被插入到模型的所有層中。

論文摘要如下:

微調是利用大型預訓練語言模型執行下游任務的實際方法。然而,它會修改所有語言模型的引數,因此需要為每個任務儲存一個完整的副本。在本文中,我們提出了字首調優 (prefix-tuning),這是一種用於自然語言生成任務的輕量級微調替代方案,它保持語言模型引數凍結,但最佳化一個小的連續任務特定向量(稱為字首)。字首調優受到提示詞 (prompting) 的啟發,允許後續的令牌將此字首視為“虛擬令牌”進行關注。我們將字首調優應用於 GPT-2 的表格到文字生成任務和 BART 的摘要任務。我們發現,透過僅學習 0.1% 的引數,字首調優在全資料設定下獲得了相當的效能,在低資料設定下優於微調,並且能更好地泛化到訓練期間未見過的主題的示例上。.

PrefixTuningConfig

class peft.PrefixTuningConfig

< >

( task_type: typing.Union[str, peft.utils.peft_types.TaskType, NoneType] = None peft_type: typing.Union[str, peft.utils.peft_types.PeftType, NoneType] = None auto_mapping: typing.Optional[dict] = None base_model_name_or_path: typing.Optional[str] = None revision: typing.Optional[str] = None inference_mode: bool = False num_virtual_tokens: int = None token_dim: int = None num_transformer_submodules: typing.Optional[int] = None num_attention_heads: typing.Optional[int] = None num_layers: typing.Optional[int] = None modules_to_save: typing.Optional[list[str]] = None encoder_hidden_size: int = None prefix_projection: bool = False )

引數

  • encoder_hidden_size (int) — 提示編碼器的隱藏層大小。
  • prefix_projection (bool) — 是否對字首嵌入進行投影。

這是用於儲存 PrefixEncoder 配置的配置類。

PrefixEncoder

class peft.PrefixEncoder

< >

( config )

引數

用於編碼字首的 `torch.nn` 模型。

示例

>>> from peft import PrefixEncoder, PrefixTuningConfig

>>> config = PrefixTuningConfig(
...     peft_type="PREFIX_TUNING",
...     task_type="SEQ_2_SEQ_LM",
...     num_virtual_tokens=20,
...     token_dim=768,
...     num_transformer_submodules=1,
...     num_attention_heads=12,
...     num_layers=12,
...     encoder_hidden_size=768,
... )
>>> prefix_encoder = PrefixEncoder(config)

屬性:

  • embedding (torch.nn.Embedding) — 字首編碼器的嵌入層。
  • transform (torch.nn.Sequential) — 如果 `prefix_projection` 為 `True`,則用於轉換字首嵌入的兩層 MLP。
  • prefix_projection (bool) — 是否對字首嵌入進行投影。

輸入形狀:(`batch_size`, `num_virtual_tokens`)

輸出形狀:(`batch_size`, `num_virtual_tokens`, `2*layers*hidden`)

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.