Transformers 文件

自動類 (Auto Classes)

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

自動類 (Auto Classes)

在許多情況下,可以從您提供給 from_pretrained() 方法的預訓練模型名稱或路徑中猜測出您想要使用的架構。自動類(AutoClasses)就是為此而生,它們可以根據預訓練權重/配置/詞彙表的名稱/路徑自動檢索相關模型。

例項化 AutoConfigAutoModelAutoTokenizer 中的任何一個,都將直接建立一個相關架構的類。例如:

model = AutoModel.from_pretrained("google-bert/bert-base-cased")

將建立一個 BertModel 的例項模型。

每個任務和每個後端(PyTorch、TensorFlow 或 Flax)都有一個 AutoModel 類。

擴充套件自動類

每個自動類都有一個方法,可以用您的自定義類進行擴充套件。例如,如果您定義了一個自定義模型類 NewModel,請確保您有一個 NewModelConfig,然後您可以像這樣將它們新增到自動類中:

from transformers import AutoConfig, AutoModel

AutoConfig.register("new-model", NewModelConfig)
AutoModel.register(NewModelConfig, NewModel)

然後您就可以像平常一樣使用自動類了!

如果您的 NewModelConfigPretrainedConfig 的子類,請確保其 model_type 屬性設定為您註冊配置時使用的相同鍵(此處為 "new-model")。

同樣,如果您的 NewModelPreTrainedModel 的子類,請確保其 config_class 屬性設定為您註冊模型時使用的相同類(此處為 NewModelConfig)。

AutoConfig

class transformers.AutoConfig

< >

( )

這是一個通用的配置類,當使用 from_pretrained() 類方法建立時,它將被例項化為庫中的一個具體配置類。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_pretrained

< >

( pretrained_model_name_or_path: typing.Union[str, os.PathLike[str]] **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型配置的 模型 ID
    • 一個包含使用 save_pretrained() 方法或 save_pretrained() 方法儲存的配置檔案的 目錄 路徑,例如 ./my_model_directory/
    • 一個指向已儲存配置 JSON 檔案 的路徑或 URL,例如 ./my_model_directory/configuration.json
  • cache_dir (str or os.PathLike, optional) — 如果不想使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,並覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下都會預設斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], optional) — 一個字典,包含按協議或端點使用的代理伺服器,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • return_unused_kwargs (bool, optional, defaults to False) — 如果為 False,則此函式僅返回最終的配置物件。

    如果為 True,則此函式返回一個 Tuple(config, unused_kwargs),其中 unused_kwargs 是一個字典,包含鍵不是配置屬性的鍵/值對:即 kwargs 中未用於更新 config 且否則將被忽略的部分。

  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上自定義模型定義在其自己的建模檔案中。此選項僅應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • kwargs(additional keyword arguments, optional) — kwargs 中任何鍵是配置屬性的值都將用於覆蓋載入的值。對於鍵不是配置屬性的鍵/值對的行為由 return_unused_kwargs 關鍵字引數控制。

從預訓練模型配置中例項化庫中的一個配置類。

要例項化的配置類是根據載入的配置物件的 model_type 屬性選擇的,或者當它缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來回退。

示例

>>> from transformers import AutoConfig

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-uncased")

>>> # Download configuration from huggingface.co (user-uploaded) and cache.
>>> config = AutoConfig.from_pretrained("dbmdz/bert-base-german-cased")

>>> # If configuration file is in a directory (e.g., was saved using *save_pretrained('./test/saved_model/')*).
>>> config = AutoConfig.from_pretrained("./test/bert_saved_model/")

>>> # Load a specific configuration file.
>>> config = AutoConfig.from_pretrained("./test/bert_saved_model/my_configuration.json")

>>> # Change some config attributes when loading a pretrained config.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-uncased", output_attentions=True, foo=False)
>>> config.output_attentions
True

>>> config, unused_kwargs = AutoConfig.from_pretrained(
...     "google-bert/bert-base-uncased", output_attentions=True, foo=False, return_unused_kwargs=True
... )
>>> config.output_attentions
True

>>> unused_kwargs
{'foo': False}

register

< >

( model_type config exist_ok = False )

引數

  • model_type (str) — 模型型別,如 “bert” 或 “gpt”。
  • config (PretrainedConfig) — 要註冊的配置。

為此類註冊一個新的配置。

AutoTokenizer

class transformers.AutoTokenizer

< >

( )

這是一個通用的分詞器類,當使用 AutoTokenizer.from_pretrained() 類方法建立時,它將被例項化為庫中的一個分詞器類。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_pretrained

< >

( pretrained_model_name_or_path *inputs **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中預定義分詞器的 模型 ID
    • 一個包含分詞器所需詞彙檔案的 目錄 路徑,例如使用 save_pretrained() 方法儲存的目錄,例如 ./my_model_directory/
    • 當且僅當分詞器只需要單個詞彙檔案時(如 Bert 或 XLNet),可以是一個指向單個已儲存詞彙檔案的路徑或 URL,例如:./my_model_directory/vocab.txt。(不適用於所有派生類)
  • inputs (其他位置引數, 可選) — 將傳遞給分詞器的 `__init__()` 方法。
  • config (PretrainedConfig, 可選) — 用於確定要例項化的分詞器類的配置物件。
  • cache_dir (str or os.PathLike, 可選) — 當不應使用標準快取時,下載的預訓練模型配置應快取到的目錄路徑。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,並覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 `revision` 可以是 git 允許的任何識別符號。
  • subfolder (str, 可選) — 如果相關檔案位於 huggingface.co 上的模型倉庫的子資料夾中(例如,對於 facebook/rag-token-base),請在此處指定。
  • use_fast (bool, 可選, 預設為 True) — 如果給定模型支援,則使用基於 Rust 的快速分詞器。如果給定模型沒有可用的快速分詞器,則返回普通的基於 Python 的分詞器。
  • tokenizer_type (str, 可選) — 要載入的分詞器型別。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上使用其自定義建模檔案定義的模型。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 `True`,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • kwargs (其他關鍵字引數, 可選) — 將傳遞給分詞器的 `__init__()` 方法。可用於設定特殊標記,如 `bos_token`, `eos_token`, `unk_token`, `sep_token`, `pad_token`, `cls_token`, `mask_token`, `additional_special_tokens`。更多詳情請參閱 `__init__()` 中的引數。

從預訓練模型的詞彙表中例項化庫中的一個分詞器類。

要例項化的分詞器類是根據配置物件(作為引數傳遞或儘可能從 `pretrained_model_name_or_path` 載入)的 `model_type` 屬性來選擇的,或者當該屬性缺失時,則透過對 `pretrained_model_name_or_path` 進行模式匹配來回退選擇。

示例

>>> from transformers import AutoTokenizer

>>> # Download vocabulary from huggingface.co and cache.
>>> tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")

>>> # Download vocabulary from huggingface.co (user-uploaded) and cache.
>>> tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-base-german-cased")

>>> # If vocabulary files are in a directory (e.g. tokenizer was saved using *save_pretrained('./test/saved_model/')*)
>>> # tokenizer = AutoTokenizer.from_pretrained("./test/bert_saved_model/")

>>> # Download vocabulary from huggingface.co and define model-specific arguments
>>> tokenizer = AutoTokenizer.from_pretrained("FacebookAI/roberta-base", add_prefix_space=True)

register

< >

( config_class slow_tokenizer_class = None fast_tokenizer_class = None exist_ok = False )

引數

  • config_class (PretrainedConfig) — 與要註冊的模型相對應的配置。
  • slow_tokenizer_class (PretrainedTokenizer, 可選) — 要註冊的慢速分詞器。
  • fast_tokenizer_class (PretrainedTokenizerFast, 可選) — 要註冊的快速分詞器。

在此對映中註冊一個新的分詞器。

AutoFeatureExtractor

class transformers.AutoFeatureExtractor

< >

( )

這是一個通用的特徵提取器類,當使用 AutoFeatureExtractor.from_pretrained() 類方法建立時,它將被例項化為庫中的一個特徵提取器類。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_pretrained

< >

( pretrained_model_name_or_path **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中預訓練特徵提取器的 模型 ID
    • 一個包含使用 save_pretrained() 方法儲存的特徵提取器檔案的 目錄 路徑,例如,./my_model_directory/
    • 一個指向已儲存的特徵提取器 JSON 檔案 的路徑或 URL,例如,./my_model_directory/preprocessor_config.json
  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,下載的預訓練模型特徵提取器應快取到的目錄路徑。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載特徵提取器檔案,並覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 一個用於按協議或端點指定代理伺服器的字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • token (strbool, 可選) — 用於遠端檔案的 HTTP Bearer 授權的 token。如果為 True,將使用執行 huggingface-cli login 時生成的 token(儲存在 ~/.huggingface 中)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • return_unused_kwargs (bool, 可選, 預設為 False) — 如果為 False,則此函式僅返回最終的特徵提取器物件。如果為 True,則此函式返回一個 Tuple(feature_extractor, unused_kwargs),其中 *unused_kwargs* 是一個字典,包含其鍵不是特徵提取器屬性的鍵/值對:即 kwargs 中未用於更新 feature_extractor 且在其他情況下被忽略的部分。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許 Hub 上自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • kwargs (dict[str, Any], 可選) — `kwargs` 中任何鍵是特徵提取器屬性的值將用於覆蓋載入的值。對於鍵 *不是* 特徵提取器屬性的鍵/值對的行為由 return_unused_kwargs 關鍵字引數控制。

從預訓練模型詞彙表中例項化庫中的一個特徵提取器類。

要例項化的特徵提取器類是根據配置物件(作為引數傳遞或儘可能從 `pretrained_model_name_or_path` 載入)的 `model_type` 屬性選擇的,或者當它缺失時,透過回退到對 `pretrained_model_name_or_path` 進行模式匹配來選擇。

當您想使用私有模型時,需要傳遞 token=True

示例

>>> from transformers import AutoFeatureExtractor

>>> # Download feature extractor from huggingface.co and cache.
>>> feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h")

>>> # If feature extractor files are in a directory (e.g. feature extractor was saved using *save_pretrained('./test/saved_model/')*)
>>> # feature_extractor = AutoFeatureExtractor.from_pretrained("./test/saved_model/")

register

< >

( config_class feature_extractor_class exist_ok = False )

引數

  • config_class (PretrainedConfig) — 與要註冊的模型相對應的配置。
  • feature_extractor_class (FeatureExtractorMixin) — 要註冊的特徵提取器。

為此類註冊一個新的特徵提取器。

AutoImageProcessor

class transformers.AutoImageProcessor

< >

( )

這是一個通用的影像處理器類,當使用 AutoImageProcessor.from_pretrained() 類方法建立時,它將被例項化為庫中的一個影像處理器類。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_pretrained

< >

( pretrained_model_name_or_path *inputs **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 這可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練 image_processor 的 *模型 ID*。
    • 一個包含使用 save_pretrained() 方法儲存的影像處理器檔案的 *目錄* 路徑,例如 ./my_model_directory/
    • 一個指向已儲存的影像處理器 JSON *檔案* 的路徑或 URL,例如 ./my_model_directory/preprocessor_config.json
  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,將下載的預訓練模型影像處理器快取到的目錄路徑。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載影像處理器檔案並覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 一個用於按協議或端點指定代理伺服器的字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • token (strbool, 可選) — 用於遠端檔案的 HTTP Bearer 授權的 token。如果為 True,將使用執行 huggingface-cli login 時生成的 token(儲存在 ~/.huggingface 中)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • use_fast (bool, 可選, 預設為 False) — 如果給定模型支援,則使用基於 torchvision 的快速影像處理器。如果給定模型沒有快速影像處理器,則返回基於 numpy 的普通影像處理器。
  • return_unused_kwargs (bool, 可選, 預設為 False) — 如果為 False,則此函式僅返回最終的影像處理器物件。如果為 True,則此函式返回一個 Tuple(image_processor, unused_kwargs),其中 *unused_kwargs* 是一個字典,包含其鍵不是影像處理器屬性的鍵/值對:即 kwargs 中未用於更新 image_processor 且在其他情況下被忽略的部分。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許 Hub 上自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • image_processor_filename (str, 可選, 預設為 "config.json") — 模型目錄中用於影像處理器配置的檔名。
  • kwargs (dict[str, Any], 可選) — `kwargs` 中任何鍵是影像處理器屬性的值將用於覆蓋載入的值。對於鍵 *不是* 影像處理器屬性的鍵/值對的行為由 return_unused_kwargs 關鍵字引數控制。

從預訓練模型詞彙表中例項化庫中的一個影像處理器類。

要例項化的影像處理器類是根據配置物件(作為引數傳遞或儘可能從 `pretrained_model_name_or_path` 載入)的 `model_type` 屬性選擇的,或者當它缺失時,透過回退到對 `pretrained_model_name_or_path` 進行模式匹配來選擇。

當您想使用私有模型時,需要傳遞 token=True

示例

>>> from transformers import AutoImageProcessor

>>> # Download image processor from huggingface.co and cache.
>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")

>>> # If image processor files are in a directory (e.g. image processor was saved using *save_pretrained('./test/saved_model/')*)
>>> # image_processor = AutoImageProcessor.from_pretrained("./test/saved_model/")

register

< >

( config_class image_processor_class = None slow_image_processor_class = None fast_image_processor_class = None exist_ok = False )

引數

為此類註冊一個新的影像處理器。

AutoVideoProcessor

class transformers.AutoVideoProcessor

< >

( )

這是一個通用的影片處理器類,當使用 AutoVideoProcessor.from_pretrained() 類方法建立時,它將被例項化為庫中的一個影片處理器類。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_pretrained

< >

( pretrained_model_name_or_path *inputs **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 字串,即 huggingface.co 上模型倉庫中託管的預訓練 video_processor 的 model id
    • 包含使用 save_pretrained() 方法儲存的影片處理器檔案的目錄路徑,例如 ./my_model_directory/
    • 已儲存的影片處理器 JSON 檔案的路徑或 URL,例如 ./my_model_directory/preprocessor_config.json
  • cache_dir (str or os.PathLike, optional) — 如果不應使用標準快取,則為下載的預訓練模型影片處理器應快取到的目錄路徑。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載影片處理器檔案並覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • token (str or bool, optional) — 用於遠端檔案的 HTTP Bearer 授權的令牌。如果為 True,將使用執行 huggingface-cli login 時生成的令牌(儲存在 ~/.huggingface 中)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • return_unused_kwargs (bool, optional, defaults to False) — 如果為 False,則此函式僅返回最終的影片處理器物件。如果為 True,則此函式返回一個 Tuple(video_processor, unused_kwargs),其中 unused_kwargs 是一個字典,包含其鍵不是影片處理器屬性的鍵/值對:即 kwargs 中未用於更新 video_processor 且在其他情況下被忽略的部分。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型檔案中定義模型。此選項只應為你信任且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • kwargs (dict[str, Any], optional) — kwargs 中任何鍵是影片處理器屬性的值將用於覆蓋載入的值。對於鍵不是影片處理器屬性的鍵/值對的行為由 return_unused_kwargs 關鍵字引數控制。

從預訓練模型詞彙表中例項化庫中的一個影片處理器類。

要例項化的影片處理器類是根據 config 物件的 model_type 屬性選擇的(可以作為引數傳遞,也可以從 pretrained_model_name_or_path 載入),或者在缺少該屬性時,透過對 pretrained_model_name_or_path 進行模式匹配來回退選擇。

當您想使用私有模型時,需要傳遞 token=True

示例

>>> from transformers import AutoVideoProcessor

>>> # Download video processor from huggingface.co and cache.
>>> video_processor = AutoVideoProcessor.from_pretrained("llava-hf/llava-onevision-qwen2-0.5b-ov-hf")

>>> # If video processor files are in a directory (e.g. video processor was saved using *save_pretrained('./test/saved_model/')*)
>>> # video_processor = AutoVideoProcessor.from_pretrained("./test/saved_model/")

register

< >

config_class video_processor_class exist_ok = False

引數

為此類註冊一個新的影片處理器。

AutoProcessor

class transformers.AutoProcessor

< >

( )

這是一個通用的處理器類,當使用 AutoProcessor.from_pretrained() 類方法建立時,它將被例項化為庫中的一個處理器類。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_pretrained

< >

( pretrained_model_name_or_path **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 字串,即 huggingface.co 上模型倉庫中託管的預訓練 feature_extractor 的 model id
    • 包含使用 save_pretrained() 方法儲存的處理器檔案的目錄路徑,例如 ./my_model_directory/
  • cache_dir (str or os.PathLike, optional) — 如果不應使用標準快取,則為下載的預訓練模型特徵提取器應快取到的目錄路徑。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載特徵提取器檔案並覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • token (str or bool, optional) — 用於遠端檔案的 HTTP Bearer 授權的令牌。如果為 True,將使用執行 huggingface-cli login 時生成的令牌(儲存在 ~/.huggingface 中)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • return_unused_kwargs (bool, optional, defaults to False) — 如果為 False,則此函式僅返回最終的特徵提取器物件。如果為 True,則此函式返回一個 Tuple(feature_extractor, unused_kwargs),其中 unused_kwargs 是一個字典,包含其鍵不是特徵提取器屬性的鍵/值對:即 kwargs 中未用於更新 feature_extractor 且在其他情況下被忽略的部分。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型檔案中定義模型。此選項只應為你信任且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • kwargs (dict[str, Any], optional) — kwargs 中任何鍵是特徵提取器屬性的值將用於覆蓋載入的值。對於鍵不是特徵提取器屬性的鍵/值對的行為由 return_unused_kwargs 關鍵字引數控制。

從預訓練模型詞彙表中例項化庫中的一個處理器類。

要例項化的處理器類是根據 config 物件的 model_type 屬性選擇的(可以作為引數傳遞,也可以從 pretrained_model_name_or_path 載入)。

當您想使用私有模型時,需要傳遞 token=True

示例

>>> from transformers import AutoProcessor

>>> # Download processor from huggingface.co and cache.
>>> processor = AutoProcessor.from_pretrained("facebook/wav2vec2-base-960h")

>>> # If processor files are in a directory (e.g. processor was saved using *save_pretrained('./test/saved_model/')*)
>>> # processor = AutoProcessor.from_pretrained("./test/saved_model/")

register

< >

config_class processor_class exist_ok = False

引數

為此類註冊一個新的處理器。

通用模型類

以下自動類可用於例項化沒有特定頭部的基礎模型類。

AutoModel

class transformers.AutoModel

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個基礎模型類。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

透過配置例項化一個庫中的基礎模型類。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModel

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModel.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 *TensorFlow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是由庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供一個本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了一個名為 config.json 的配置 JSON 檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 用於替代從已儲存權重檔案載入的狀態字典。

    如果你想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,你應該檢查使用 save_pretrained()from_pretrained() 是否是更簡單的選擇。

  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,用於快取下載的預訓練模型配置的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 用於按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的、存在於其自身建模檔案中的自定義模型。此選項只應為受信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則指定要用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如 output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個基礎模型類。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModel

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModel.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModel.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModel.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModel

class transformers.TFAutoModel

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個基礎模型類。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

透過配置例項化一個庫中的基礎模型類。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModel

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModel.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 字串,即 huggingface.co 上模型倉庫中託管的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案的路徑或 URL(例如 ./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應透過 config 引數提供一個配置物件。與使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型相比,這種載入路徑較慢。
  • model_args (額外的位置引數,可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用且被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應對您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果 Hub 上的程式碼與模型的其餘部分位於不同的倉庫中,要使用的特定程式碼版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數,可選) — 可用於更新配置物件(載入後)和初始化模型(例如 output_attentions=True)。其行為取決於是否提供了 config 或自動載入配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應於任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個基礎模型類。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModel

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModel.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModel.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModel.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModel

class transformers.FlaxAutoModel

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個基礎模型類。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

透過配置例項化一個庫中的基礎模型類。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModel

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModel.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 字串,即 huggingface.co 上模型倉庫中託管的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案的路徑或 URL(例如 ./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應透過 config 引數提供一個配置物件。與使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型相比,這種載入路徑較慢。
  • model_args (額外的位置引數,可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用且被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應對您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果 Hub 上的程式碼與模型的其餘部分位於不同的倉庫中,要使用的特定程式碼版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數,可選) — 可用於更新配置物件(載入後)和初始化模型(例如 output_attentions=True)。其行為取決於是否提供了 config 或自動載入配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應於任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個基礎模型類。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModel

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModel.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModel.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModel.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

通用預訓練類

以下自動類可用於例項化帶有預訓練頭的模型。

AutoModelForPreTraining

class transformers.AutoModelForPreTraining

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有預訓練頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有預訓練頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForPreTraining

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForPreTraining.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 TensorFlow 索引檢查點檔案的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 一個狀態字典,用於替代從儲存的權重檔案載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 Git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 Git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上存在的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 Git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 Git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為根據是否提供 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個與配置屬性對應的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有預訓練頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForPreTraining

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForPreTraining.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForPreTraining.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForPreTraining.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForPreTraining

class transformers.TFAutoModelForPreTraining

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有預訓練頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有預訓練頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForPreTraining

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForPreTraining.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,應將 from_pt 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都預設支援斷點續傳。此引數將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個字典,用於指定按協議或端點使用的代理伺服器,例如:{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將被傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有預訓練頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForPreTraining

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForPreTraining.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForPreTraining.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForPreTraining.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForPreTraining

class transformers.FlaxAutoModelForPreTraining

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有預訓練頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有預訓練頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForPreTraining

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForPreTraining.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到名為 *config.json* 的配置 JSON 檔案。
  • cache_dir (str or os.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都預設支援斷點續傳。此引數將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個字典,用於指定按協議或端點使用的代理伺服器,例如:{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將被傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有預訓練頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForPreTraining

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForPreTraining.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForPreTraining.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForPreTraining.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

自然語言處理

以下自動類可用於以下自然語言處理任務。

AutoModelForCausalLM

class transformers.AutoModelForCausalLM

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有因果語言建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置例項化庫中的一個模型類(帶有因果語言建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForCausalLM

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForCausalLM.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個*tensorflow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後再載入 PyTorch 模型要慢。
  • model_args (其他位置引數, 可選) — 將傳遞給底層模型 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供一個本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能時都會預設續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用於每個請求。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型檔案中定義模型。此選項只應為受信任的且您已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有因果語言建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForCausalLM

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForCausalLM.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForCausalLM.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForCausalLM.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForCausalLM

class transformers.TFAutoModelForCausalLM

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有因果語言建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置例項化庫中的一個模型類(帶有因果語言建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForCausalLM

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForCausalLM.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的 *model id*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 *PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應透過 config 引數提供一個配置物件。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數,可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(透過預訓練模型的 *model id* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄來重新載入。
    • 透過將本地目錄作為 pretrained_model_name_or_path 提供來載入模型,並且在目錄中找到了名為 *config.json* 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,用於快取下載的預訓練模型配置的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為你信任且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則使用 Hub 上程式碼的特定版本。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數,可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有因果語言建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForCausalLM

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForCausalLM.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForCausalLM.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForCausalLM.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForCausalLM

class transformers.FlaxAutoModelForCausalLM

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有因果語言建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置例項化庫中的一個模型類(帶有因果語言建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForCausalLM

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForCausalLM.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的 *model id*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 *PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應透過 config 引數提供一個配置物件。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數,可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(透過預訓練模型的 *model id* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄來重新載入。
    • 透過將本地目錄作為 pretrained_model_name_or_path 提供來載入模型,並且在目錄中找到了名為 *config.json* 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,用於快取下載的預訓練模型配置的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為你信任且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則使用 Hub 上程式碼的特定版本。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數,可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有因果語言建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForCausalLM

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForCausalLM.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForCausalLM.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForCausalLM.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForMaskedLM

class transformers.AutoModelForMaskedLM

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有掩碼語言建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有掩碼語言建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForMaskedLM

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForMaskedLM.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的 *model id*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 *tensorflow index checkpoint file* 的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應透過 config 引數提供一個配置物件。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將沿底層模型的 __init__() 方法傳遞。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而非自動載入的配置。配置可以在以下情況下自動載入:

    • 該模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 該模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 用於替代從已儲存權重檔案載入的狀態字典的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,則可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項僅應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上存在的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則要為 Hub 上的程式碼使用的特定修訂版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有掩碼語言建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForMaskedLM

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForMaskedLM.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForMaskedLM.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForMaskedLM.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForMaskedLM

class transformers.TFAutoModelForMaskedLM

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有掩碼語言建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有掩碼語言建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForMaskedLM

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForMaskedLM.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將沿底層模型的 __init__() 方法傳遞。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而非自動載入的配置。配置可以在以下情況下自動載入:

    • 該模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 該模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項僅應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上存在的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則要為 Hub 上的程式碼使用的特定修訂版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有掩碼語言建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForMaskedLM

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForMaskedLM.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForMaskedLM.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForMaskedLM.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForMaskedLM

class transformers.FlaxAutoModelForMaskedLM

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有掩碼語言建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有掩碼語言建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForMaskedLM

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForMaskedLM.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將沿底層模型的 __init__() 方法傳遞。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而非自動載入的配置。配置可以在以下情況下自動載入:

    • 該模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 該模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • cache_dir (stros.PathLike可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool可選,預設為 False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool可選,預設為 False) — 是否強制(重新)下載模型權重和配置檔案,如果存在快取版本則覆蓋它們。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下預設恢復。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str]可選) — 用於按協議或端點使用的代理伺服器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選,預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str可選,預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool可選,預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上存在的程式碼。
  • code_revision (str可選,預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定在 Hub 上使用的程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數,可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有掩碼語言建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForMaskedLM

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForMaskedLM.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForMaskedLM.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForMaskedLM.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForMaskGeneration

class transformers.AutoModelForMaskGeneration

< >

*args **kwargs

TFAutoModelForMaskGeneration

class transformers.TFAutoModelForMaskGeneration

< >

*args **kwargs

AutoModelForSeq2SeqLM

class transformers.AutoModelForSeq2SeqLM

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有序列到序列語言建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有序列到序列語言建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForSeq2SeqLM

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-t5/t5-base")
>>> model = AutoModelForSeq2SeqLM.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的模型 ID
    • 包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • tensorflow 索引檢查點檔案的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後再載入 PyTorch 模型要慢。
  • model_args (附加位置引數,可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的模型 ID 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到了名為 config.json 的配置 JSON 檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否是更簡單的選項。

  • cache_dir (stros.PathLike可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_tf (bool可選,預設為 False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool可選,預設為 False) — 是否強制(重新)下載模型權重和配置檔案,如果存在快取版本則覆蓋它們。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下預設恢復。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str]可選) — 用於按協議或端點使用的代理伺服器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選,預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str可選,預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool可選,預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上存在的程式碼。
  • code_revision (str可選,預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定在 Hub 上使用的程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數,可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有序列到序列語言建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForSeq2SeqLM

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base")

>>> # Update configuration during loading
>>> model = AutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/t5_tf_model_config.json")
>>> model = AutoModelForSeq2SeqLM.from_pretrained(
...     "./tf_model/t5_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForSeq2SeqLM

class transformers.TFAutoModelForSeq2SeqLM

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有序列到序列語言建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有序列到序列語言建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForSeq2SeqLM

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-t5/t5-base")
>>> model = TFAutoModelForSeq2SeqLM.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即 huggingface.co 上模型倉庫中託管的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的 目錄 路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案 的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,應將 from_pt 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,以代替自動載入的配置。在以下情況下可以自動載入配置:

    • 該模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, 可選) — 當不應使用標準快取時,下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用且被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 一個根據協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理伺服器用於每個請求。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為你信任且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如 output_attentions=True)。行為因是否提供 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設對配置的所有相關更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有序列到序列語言建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForSeq2SeqLM

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base")

>>> # Update configuration during loading
>>> model = TFAutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/t5_pt_model_config.json")
>>> model = TFAutoModelForSeq2SeqLM.from_pretrained(
...     "./pt_model/t5_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForSeq2SeqLM

class transformers.FlaxAutoModelForSeq2SeqLM

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有序列到序列語言建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有序列到序列語言建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForSeq2SeqLM

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-t5/t5-base")
>>> model = FlaxAutoModelForSeq2SeqLM.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即 huggingface.co 上模型倉庫中託管的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的 目錄 路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案 的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,應將 from_pt 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,以代替自動載入的配置。在以下情況下可以自動載入配置:

    • 該模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, 可選) — 當不應使用標準快取時,下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用且被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 一個根據協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理伺服器用於每個請求。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為你信任且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如 output_attentions=True)。行為因是否提供 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設對配置的所有相關更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有序列到序列語言建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForSeq2SeqLM

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForSeq2SeqLM.from_pretrained("google-t5/t5-base", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/t5_pt_model_config.json")
>>> model = FlaxAutoModelForSeq2SeqLM.from_pretrained(
...     "./pt_model/t5_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForSequenceClassification

class transformers.AutoModelForSequenceClassification

< >

*args **kwargs

這是一個通用的模型類,在使用 from_pretrained() 類方法或 from_config() 類方法建立時,將被例項化為庫中的某個模型類(帶序列分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有序列分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForSequenceClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForSequenceClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向*TensorFlow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 `from_tf` 設定為 `True`,並應提供一個配置物件作為 `config` 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後再載入 PyTorch 模型要慢。
  • model_args (其他位置引數,可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供一個本地目錄作為 `pretrained_model_name_or_path` 來載入模型,並且在目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 `pretrained_model_name_or_path` 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 `revision` 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許 Hub 上自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 `True`,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定要用於 Hub 上程式碼的特定修訂版。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 `revision` 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數,可選) — 可用於更新配置物件(載入後)和初始化模型(例如,`output_attentions=True`)。其行為根據是否提供了 `config` 或自動載入而有所不同:

    • 如果透過 `config` 提供了配置,`**kwargs` 將直接傳遞給底層模型的 `__init__` 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,`kwargs` 將首先傳遞給配置類的初始化函式(from_pretrained())。`kwargs` 中與配置屬性對應的每個鍵將用於使用提供的 `kwargs` 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 `__init__` 函式。

從預訓練模型例項化庫中的一個模型類(帶有序列分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForSequenceClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForSequenceClassification.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForSequenceClassification

class transformers.TFAutoModelForSequenceClassification

< >

*args **kwargs

這是一個通用的模型類,在使用 from_pretrained() 類方法或 from_config() 類方法建立時,將被例項化為庫中的某個模型類(帶序列分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有序列分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForSequenceClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForSequenceClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的模型 ID
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應透過 config 引數提供一個配置物件。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (額外的 positional 引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,以替代自動載入的配置。在以下情況下可以自動載入配置:

    • 該模型是庫提供的模型(使用預訓練模型的模型 ID字串載入)。
    • 該模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置 JSON 檔案。
  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,用於快取下載的預訓練模型配置的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定修訂版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入了配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有序列分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForSequenceClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForSequenceClassification.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForSequenceClassification

class transformers.FlaxAutoModelForSequenceClassification

< >

*args **kwargs

這是一個通用的模型類,在使用 from_pretrained() 類方法或 from_config() 類方法建立時,將被例項化為庫中的某個模型類(帶序列分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有序列分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForSequenceClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForSequenceClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的模型 ID
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應透過 config 引數提供一個配置物件。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (額外的 positional 引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,以替代自動載入的配置。在以下情況下可以自動載入配置:

    • 該模型是庫提供的模型(使用預訓練模型的模型 ID字串載入)。
    • 該模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置 JSON 檔案。
  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,用於快取下載的預訓練模型配置的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定修訂版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入了配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有序列分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForSequenceClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForSequenceClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForSequenceClassification.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForMultipleChoice

class transformers.AutoModelForMultipleChoice

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有選擇題頭部)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置中例項化庫中的一個模型類(帶有多項選擇頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForMultipleChoice

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForMultipleChoice.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個*tensorflow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後再載入 PyTorch 模型要慢。
  • model_args (其他位置引數, optional) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是更簡單的選項。

  • cache_dir (str or os.PathLike, optional) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_tf (bool, optional, defaults to False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能時都會預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用於每個請求。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上自定義模型定義在其自己的建模檔案中。此選項只應為 True 設定為您信任且已閱讀其程式碼的倉庫,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的儲存庫中,則用於 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 `revision` 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有多項選擇頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForMultipleChoice

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForMultipleChoice.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForMultipleChoice

class transformers.TFAutoModelForMultipleChoice

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有選擇題頭部)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置中例項化庫中的一個模型類(帶有多項選擇頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForMultipleChoice

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForMultipleChoice.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型,然後再載入 TensorFlow 模型要慢。
  • model_args (其他位置引數, optional) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • cache_dir (str or os.PathLike, optional) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, optional, defaults to False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能時都會預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用於每個請求。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上自定義模型定義在其自己的建模檔案中。此選項只應為 True 設定為您信任且已閱讀其程式碼的倉庫,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的儲存庫中,則用於 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 `revision` 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有多項選擇頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForMultipleChoice

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForMultipleChoice.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForMultipleChoice

class transformers.FlaxAutoModelForMultipleChoice

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有選擇題頭部)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置中例項化庫中的一個模型類(帶有多項選擇頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForMultipleChoice

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForMultipleChoice.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的*模型ID*。
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個指向*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (其他位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而非自動載入的配置。當滿足以下條件時,可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, 可選) — 如果不想使用標準快取,可以指定一個目錄路徑,用於快取下載的預訓練模型配置。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個協議或端點使用的代理伺服器字典,例如:{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。可以是一個分支名、一個標籤名或一個提交ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型檔案中定義模型。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則使用 Hub 上的特定程式碼版本。可以是一個分支名、一個標籤名或一個提交ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為方式取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有多項選擇頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForMultipleChoice

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForMultipleChoice.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForMultipleChoice.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForNextSentencePrediction

class transformers.AutoModelForNextSentencePrediction

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有下一句預測頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有下一句預測頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForNextSentencePrediction

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForNextSentencePrediction.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的*模型ID*。
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個指向*tensorflow索引檢查點檔案*的路徑或URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型後再載入 PyTorch 模型要慢。
  • model_args (其他位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而非自動載入的配置。當滿足以下條件時,可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (str or os.PathLike, 可選) — 如果不想使用標準快取,可以指定一個目錄路徑,用於快取下載的預訓練模型配置。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個協議或端點使用的代理伺服器字典,例如:{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。可以是一個分支名、一個標籤名或一個提交ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型檔案中定義模型。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則使用 Hub 上的特定程式碼版本。可以是一個分支名、一個標籤名或一個提交ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為方式取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有下一句預測頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForNextSentencePrediction

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForNextSentencePrediction.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForNextSentencePrediction.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForNextSentencePrediction.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForNextSentencePrediction

class transformers.TFAutoModelForNextSentencePrediction

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有下一句預測頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有下一句預測頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForNextSentencePrediction

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForNextSentencePrediction.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的*模型ID*。
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個指向*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (其他位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而非自動載入的配置。當滿足以下條件時,可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, 可選) — 如果不想使用標準快取,可以指定一個目錄路徑,用於快取下載的預訓練模型配置。
  • from_pt (bool, optional, defaults to False) — 是否從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,如果存在快取版本則覆蓋它們。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用於每個請求。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (additional keyword arguments, optional) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為因是否提供了 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已經完成)
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有下一句預測頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForNextSentencePrediction

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForNextSentencePrediction.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForNextSentencePrediction.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForNextSentencePrediction.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForNextSentencePrediction

class transformers.FlaxAutoModelForNextSentencePrediction

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有下一句預測頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有下一句預測頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForNextSentencePrediction

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForNextSentencePrediction.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的 模型 ID
    • 一個包含使用 save_pretrained() 儲存的模型權重的 目錄 路徑,例如 ./my_model_directory/
    • 一個 PyTorch state_dict 儲存檔案 的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (additional positional arguments, optional) — 將傳遞給底層模型 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。配置可以在以下情況下自動載入:

    • 該模型是庫提供的模型(使用預訓練模型的 模型 ID 字串載入)。
    • 該模型使用 save_pretrained() 儲存,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, optional) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_pt (bool, optional, defaults to False) — 是否從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,如果存在快取版本則覆蓋它們。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用於每個請求。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (additional keyword arguments, optional) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為因是否提供了 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已經完成)
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有下一句預測頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForNextSentencePrediction

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForNextSentencePrediction.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForNextSentencePrediction.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForNextSentencePrediction.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForTokenClassification

class transformers.AutoModelForTokenClassification

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有詞元分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的某個模型類(帶有詞元分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForTokenClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForTokenClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的 模型 ID
    • 一個包含使用 save_pretrained() 儲存的模型權重的 目錄 路徑,例如 ./my_model_directory/
    • 一個 tensorflow 索引檢查點檔案 的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後載入 PyTorch 模型要慢。
  • model_args (additional positional arguments, optional) — 將傳遞給底層模型 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 該模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄進行重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果你想從預訓練的配置中建立一個模型,但載入自己的權重,可以使用此選項。但在這種情況下,你應該檢查使用 save_pretrained()from_pretrained() 是否是更簡單的選擇。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能時都預設支援斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用於每個請求。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為你信任的且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,要使用的 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果未提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有詞元分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForTokenClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForTokenClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForTokenClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForTokenClassification.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForTokenClassification

class transformers.TFAutoModelForTokenClassification

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有詞元分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的某個模型類(帶有詞元分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForTokenClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForTokenClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 字串,即託管在 huggingface.co 上模型倉庫中的預訓練模型的*模型 ID*。
    • 包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 指向*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,應將 from_pt 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型並隨後載入 TensorFlow 模型要慢。
  • model_args (附加位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 該模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄進行重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能時都預設支援斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用於每個請求。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為你信任的且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,要使用的 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果未提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有詞元分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForTokenClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForTokenClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForTokenClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForTokenClassification.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForTokenClassification

class transformers.FlaxAutoModelForTokenClassification

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有詞元分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的某個模型類(帶有詞元分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForTokenClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForTokenClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應該設定為 True,並且應該提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型,然後載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, optional) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • cache_dir (stros.PathLike, optional) — 如果不想使用標準快取,可以指定一個目錄路徑,用於快取下載的預訓練模型配置。
  • from_pt (bool, optional, defaults to False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], optional) — 一個按協議或端點使用的代理伺服器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為你信任且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定用於 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, optional) — 可用於更新配置物件(在載入後)並初始化模型(例如,output_attentions=True)。行為會根據是否提供 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有詞元分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForTokenClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForTokenClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForTokenClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForTokenClassification.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForQuestionAnswering

class transformers.AutoModelForQuestionAnswering

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有問答頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有問答頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForQuestionAnswering

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForQuestionAnswering.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個 *TensorFlow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應該設定為 True,並且應該提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, optional) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 一個狀態字典,用於代替從儲存的權重檔案中載入的狀態字典。

    如果你想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,你應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, optional) — 如果不想使用標準快取,可以指定一個目錄路徑,用於快取下載的預訓練模型配置。
  • from_tf (bool, optional, defaults to False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], optional) — 一個按協議或端點使用的代理伺服器字典,例如,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統來儲存 huggingface.co 上的模型和其他構件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,要用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統來儲存 huggingface.co 上的模型和其他構件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為方式取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有問答頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForQuestionAnswering

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForQuestionAnswering.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForQuestionAnswering.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForQuestionAnswering.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForQuestionAnswering

class transformers.TFAutoModelForQuestionAnswering

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有問答頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有問答頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForQuestionAnswering

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForQuestionAnswering.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案 的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (附加位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。配置可以在以下情況下自動載入:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能時都預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理用於每個請求。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統來儲存 huggingface.co 上的模型和其他構件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,要用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統來儲存 huggingface.co 上的模型和其他構件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為方式取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有問答頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForQuestionAnswering

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForQuestionAnswering.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForQuestionAnswering.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForQuestionAnswering.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForQuestionAnswering

class transformers.FlaxAutoModelForQuestionAnswering

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有問答頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有問答頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForQuestionAnswering

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForQuestionAnswering.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案 的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (附加位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。配置可以在以下情況下自動載入:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, optional) — 目錄路徑,如果不想使用標準快取,則下載的預訓練模型配置將快取到此目錄中。
  • from_pt (bool, optional, defaults to False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], optional) — 代理伺服器字典,用於按協議或端點指定代理,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。每次請求都會使用這些代理。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許 Hub 上自定義模型在其自己的建模檔案中定義。此選項只應為你信任且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則指定要用於 Hub 上程式碼的特定修訂版。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設對配置的所有相關更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應於任何配置屬性的其餘鍵將被傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有問答頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForQuestionAnswering

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForQuestionAnswering.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForQuestionAnswering.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForQuestionAnswering.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForTextEncoding

class transformers.AutoModelForTextEncoding

< >

*args **kwargs

TFAutoModelForTextEncoding

class transformers.TFAutoModelForTextEncoding

< >

*args **kwargs

計算機視覺

以下 auto 類可用於以下計算機視覺任務。

AutoModelForDepthEstimation

class transformers.AutoModelForDepthEstimation

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有深度估計頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有深度估計頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForDepthEstimation

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForDepthEstimation.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 tensorflow index checkpoint file 的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後再載入 PyTorch 模型要慢。
  • model_args (附加位置引數, optional) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。配置可以在以下情況下自動載入:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型使用 save_pretrained() 儲存,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 一個狀態字典,用於代替從儲存的權重檔案中載入的狀態字典。

    如果你想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,你應該檢查使用 save_pretrained()from_pretrained() 是否是更簡單的選擇。

  • cache_dir (str or os.PathLike, optional) — 目錄路徑,如果不想使用標準快取,則下載的預訓練模型配置將快取到此目錄中。
  • from_tf (bool, optional, defaults to False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], optional) — 代理伺服器字典,用於按協議或端點指定代理,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。每次請求都會使用這些代理。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許 Hub 上自定義模型在其自己的建模檔案中定義。此選項只應為你信任且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則指定要用於 Hub 上程式碼的特定修訂版。它可以是分支名、標籤名或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設對配置的所有相關更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應於任何配置屬性的其餘鍵將被傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有深度估計頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForDepthEstimation

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForDepthEstimation.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForDepthEstimation.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForDepthEstimation.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForImageClassification

class transformers.AutoModelForImageClassification

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有影像分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置中例項化庫中的一個模型類(帶有影像分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForImageClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForImageClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的模型 ID
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 TensorFlow 索引檢查點檔案的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而非自動載入的配置。配置可以在以下情況下自動載入:

    • 該模型是庫提供的模型(使用預訓練模型的模型 ID 字串載入)。
    • 該模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 一個狀態字典,用於代替從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型檔案中定義自定義模型。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則要使用的 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從一個預訓練模型中例項化庫中的一個模型類(帶有影像分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForImageClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForImageClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForImageClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForImageClassification.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForImageClassification

class transformers.TFAutoModelForImageClassification

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有影像分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置中例項化庫中的一個模型類(帶有影像分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForImageClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForImageClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的模型 ID
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,應將 from_pt 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型,然後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而非自動載入的配置。配置可以在以下情況下自動載入:

    • 該模型是庫提供的模型(使用預訓練模型的模型 ID 字串載入)。
    • 該模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型檔案中定義自定義模型。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則要使用的 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從一個預訓練模型中例項化庫中的一個模型類(帶有影像分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForImageClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForImageClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForImageClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForImageClassification.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForImageClassification

class transformers.FlaxAutoModelForImageClassification

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有影像分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置中例項化庫中的一個模型類(帶有影像分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForImageClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForImageClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 *PyTorch state_dict 儲存檔案* 的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(透過預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,用於快取下載的預訓練模型配置的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用且被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則使用 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為取決於是否提供 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從一個預訓練模型中例項化庫中的一個模型類(帶有影像分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForImageClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForImageClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForImageClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForImageClassification.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForVideoClassification

class transformers.AutoModelForVideoClassification

< >

*args **kwargs

這是一個通用模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有影片分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置例項化庫中的某個模型類(帶有影片分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForVideoClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForVideoClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 *tensorflow 索引檢查點檔案* 的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(透過預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,用於快取下載的預訓練模型配置的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用且被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則使用 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為取決於是否提供 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有影片分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForVideoClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForVideoClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForVideoClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForVideoClassification.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForKeypointDetection

class transformers.AutoModelForKeypointDetection

< >

*args **kwargs

AutoModelForMaskedImageModeling

class transformers.AutoModelForMaskedImageModeling

< >

*args **kwargs

這是一個通用模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有掩碼影像建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置例項化庫中的某個模型類(帶有掩碼影像建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForMaskedImageModeling

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForMaskedImageModeling.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 *tensorflow 索引檢查點檔案* 的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(透過預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 當不應使用標準快取時,用於快取下載的預訓練模型配置的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 是否從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個根據協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其他部分位於不同的倉庫中,指定要用於 Hub 上程式碼的特定修訂版。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為方式取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有掩碼影像建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForMaskedImageModeling

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForMaskedImageModeling.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForMaskedImageModeling.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForMaskedImageModeling.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForMaskedImageModeling

class transformers.TFAutoModelForMaskedImageModeling

< >

*args **kwargs

這是一個通用模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有掩碼影像建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置例項化庫中的某個模型類(帶有掩碼影像建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForMaskedImageModeling

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForMaskedImageModeling.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個 PyTorch state_dict 儲存檔案的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (附加位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型使用 save_pretrained() 儲存,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 是否從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個根據協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其他部分位於不同的倉庫中,指定要用於 Hub 上程式碼的特定修訂版。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為方式取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有掩碼影像建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForMaskedImageModeling

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForMaskedImageModeling.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForMaskedImageModeling.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForMaskedImageModeling.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForObjectDetection

class transformers.AutoModelForObjectDetection

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有目標檢測頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有目標檢測頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForObjectDetection

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForObjectDetection.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個 TensorFlow 索引檢查點檔案的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後載入 PyTorch 模型要慢。
  • model_args (附加位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型使用 save_pretrained() 儲存,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是更簡單的選項。

  • cache_dir (str or os.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 是否從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個根據協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其他部分位於不同的倉庫中,指定要用於 Hub 上程式碼的特定修訂版。它可以是分支名、標籤名或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為方式取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有目標檢測頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForObjectDetection

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForObjectDetection.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForObjectDetection.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForObjectDetection.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForImageSegmentation

class transformers.AutoModelForImageSegmentation

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有影像分割頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

  • config (PretrainedConfig) — 要例項化的模型類是根據配置類選擇的:

  • attn_implementation (str, 可選) — 在模型中使用的注意力實現(如果相關)。可以是 "eager"(注意力的手動實現)、"sdpa"(使用 F.scaled_dot_product_attention)或 "flash_attention_2"(使用 Dao-AILab/flash-attention)中的任何一種。預設情況下,如果可用,SDPA 將用於 torch>=2.1.1。否則,預設是手動的 "eager" 實現。

根據配置例項化庫中的一個模型類(帶有影像分割頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForImageSegmentation

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForImageSegmentation.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的 model id
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的目錄的路徑,例如 ./my_model_directory/
    • 一個指向 tensorflow 索引檢查點檔案的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後再載入 PyTorch 模型要慢。
  • model_args (其他位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過將本地目錄作為 pretrained_model_name_or_path 提供來載入模型,並且在目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為那些您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類初始化函式 (from_pretrained())。每個與配置屬性對應的 kwargs 鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有影像分割頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForImageSegmentation

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForImageSegmentation.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForImageSegmentation.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForImageSegmentation.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForImageToImage

class transformers.AutoModelForImageToImage

< >

*args **kwargs

AutoModelForSemanticSegmentation

class transformers.AutoModelForSemanticSegmentation

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有語義分割頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有語義分割頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForSemanticSegmentation

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForSemanticSegmentation.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的 model id
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的目錄的路徑,例如 ./my_model_directory/
    • 一個指向 tensorflow 索引檢查點檔案的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後再載入 PyTorch 模型要慢。
  • model_args (其他位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過將本地目錄作為 pretrained_model_name_or_path 提供來載入模型,並且在目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為那些您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類初始化函式 (from_pretrained())。每個與配置屬性對應的 kwargs 鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有語義分割頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForSemanticSegmentation

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForSemanticSegmentation.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForSemanticSegmentation.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForSemanticSegmentation.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForSemanticSegmentation

class transformers.TFAutoModelForSemanticSegmentation

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有語義分割頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有語義分割頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForSemanticSegmentation

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForSemanticSegmentation.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的*模型 ID*。
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個指向 *PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, 可選) — 下載的預訓練模型配置應快取的目錄路徑,如果不應使用標準快取。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。可以是一個分支名、一個標籤名或一個提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用它們自己的建模檔案。此選項只應為那些您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果 Hub 上的程式碼位於與模型其餘部分不同的倉庫中,則使用該程式碼的特定修訂版。可以是一個分支名、一個標籤名或一個提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(在載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有語義分割頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForSemanticSegmentation

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForSemanticSegmentation.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForSemanticSegmentation.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForSemanticSegmentation.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForInstanceSegmentation

class transformers.AutoModelForInstanceSegmentation

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有例項分割頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有例項分割頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForInstanceSegmentation

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForInstanceSegmentation.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的*模型 ID*。
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個指向 *tensorflow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 一個狀態字典,用於替代從儲存的權重檔案載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否是更簡單的選擇。

  • cache_dir (str or os.PathLike, 可選) — 下載的預訓練模型配置應快取的目錄路徑,如果不應使用標準快取。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。可以是一個分支名、一個標籤名或一個提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用它們自己的建模檔案。此選項只應為那些您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果 Hub 上的程式碼位於與模型其餘部分不同的倉庫中,則使用該程式碼的特定修訂版。可以是一個分支名、一個標籤名或一個提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(在載入後)和初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入配置:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有例項分割頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForInstanceSegmentation

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForInstanceSegmentation.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForInstanceSegmentation.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForInstanceSegmentation.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForUniversalSegmentation

class transformers.AutoModelForUniversalSegmentation

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有通用影像分割頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有通用影像分割頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForUniversalSegmentation

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForUniversalSegmentation.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的*模型 ID*。
    • 一個指向包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如:./my_model_directory/
    • 一個指向 *tensorflow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 一個狀態字典,用於替代從儲存的權重檔案載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否是更簡單的選擇。

  • cache_dir (str or os.PathLike, 可選) — 下載的預訓練模型配置應快取的目錄路徑,如果不應使用標準快取。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都預設支援斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許 Hub 上自定義模型在其自己的建模檔案中定義。此選項只應為受信任且已閱讀程式碼的倉庫設定為 True,因為它將在本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則指定 Hub 上要使用的特定程式碼版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如 output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於以提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有通用影像分割頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForUniversalSegmentation

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForUniversalSegmentation.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForUniversalSegmentation.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForUniversalSegmentation.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForZeroShotImageClassification

class transformers.AutoModelForZeroShotImageClassification

< >

*args **kwargs

這是一個通用模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有零樣本影像分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置中例項化庫中的一個模型類(帶有零樣本影像分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForZeroShotImageClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForZeroShotImageClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個*tensorflow 索引檢查點檔案*的路徑或 URL(例如 ./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 模型的配置,用於替代自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 *config.json* 的配置 JSON 檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 一個狀態字典,用於代替從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (str or os.PathLike, 可選) — 一個目錄的路徑,用於快取下載的預訓練模型配置,如果不想使用標準快取目錄。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都預設支援斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許 Hub 上自定義模型在其自己的建模檔案中定義。此選項只應為受信任且已閱讀程式碼的倉庫設定為 True,因為它將在本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則指定 Hub 上要使用的特定程式碼版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如 output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於以提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有零樣本影像分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForZeroShotImageClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForZeroShotImageClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForZeroShotImageClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForZeroShotImageClassification.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForZeroShotImageClassification

class transformers.TFAutoModelForZeroShotImageClassification

< >

*args **kwargs

這是一個通用模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有零樣本影像分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

  • config (PretrainedConfig) — 要例項化的模型類是根據配置類選擇的:

  • attn_implementation (str, 可選) — 在模型中使用的注意力實現(如果相關)。可以是 "eager"(注意力的手動實現)、"sdpa"(使用 F.scaled_dot_product_attention)或 "flash_attention_2"(使用 Dao-AILab/flash-attention)中的任何一種。預設情況下,如果可用,SDPA 將用於 torch>=2.1.1。否則,預設是手動的 "eager" 實現。

從配置中例項化庫中的一個模型類(帶有零樣本影像分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForZeroShotImageClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForZeroShotImageClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個*PyTorch state_dict 儲存檔案*的路徑或 URL(例如 ./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型,然後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 模型的配置,用於替代自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 *config.json* 的配置 JSON 檔案。
  • cache_dir (str or os.PathLike, 可選) — 一個目錄的路徑,用於快取下載的預訓練模型配置,如果不想使用標準快取目錄。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都預設支援斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許 Hub 上自定義模型在其自己的建模檔案中定義。此選項只應為受信任且已閱讀程式碼的倉庫設定為 True,因為它將在本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則指定 Hub 上要使用的特定程式碼版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如 output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於以提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有零樣本影像分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForZeroShotImageClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForZeroShotImageClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForZeroShotImageClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForZeroShotImageClassification.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForZeroShotObjectDetection

class transformers.AutoModelForZeroShotObjectDetection

< >

*args **kwargs

這是一個通用模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有零樣本目標檢測頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置中例項化一個庫中的模型類(帶有零樣本物件檢測頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForZeroShotObjectDetection

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForZeroShotObjectDetection.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向 *tensorflow 索引檢查點檔案* 的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 模型的配置,用於替代自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 用於代替從已儲存的權重檔案中載入的狀態字典。

    如果你想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,你應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。可以是一個分支名、一個標籤名或一個提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為你信任的且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定修訂版。可以是一個分支名、一個標籤名或一個提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(在載入後)和初始化模型(例如,output_attentions=True)。其行為因是否提供 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個與配置屬性對應的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化一個庫中的模型類(帶有零樣本物件檢測頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForZeroShotObjectDetection

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForZeroShotObjectDetection.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForZeroShotObjectDetection.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForZeroShotObjectDetection.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

音訊

以下自動類可用於以下音訊任務。

AutoModelForAudioClassification

class transformers.AutoModelForAudioClassification

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中帶有音訊分類頭的模型類之一。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置中例項化一個庫中的模型類(帶有音訊分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForAudioClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForAudioClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向 *tensorflow 索引檢查點檔案* 的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 模型的配置,用於替代自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 用於代替從已儲存的權重檔案中載入的狀態字典。

    如果你想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,你應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。可以是一個分支名、一個標籤名或一個提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為你信任的且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地機器上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定修訂版。可以是一個分支名、一個標籤名或一個提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(在載入後)和初始化模型(例如,output_attentions=True)。其行為因是否提供 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中每個與配置屬性對應的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化一個庫中的模型類(帶有音訊分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForAudioClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForAudioClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForAudioClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForAudioClassification.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForAudioFrameClassification

class transformers.TFAutoModelForAudioClassification

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中帶有音訊分類頭的模型類之一。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置中例項化一個庫中的模型類(帶有音訊分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForAudioClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForAudioClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向 *PyTorch state_dict 儲存檔案* 的路徑或 URL(例如 ./pt_model/pytorch_model.bin)。在這種情況下,應將 from_pt 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型,然後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 模型的配置,用於替代自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • cache_dir (str or os.PathLike, optional) — 目錄路徑,如果不想使用標準快取,下載的預訓練模型配置將快取到該目錄中。
  • from_pt (bool, optional, defaults to False) — 從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,若存在快取版本則覆蓋它們。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都預設支援斷點續傳。將在 Transformers v5 中移除。
  • proxies (dict[str, str], optional) — 一個字典,用於指定按協議或端點使用的代理伺服器,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將用於每個請求。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型檔案中定義模型。此選項只應在你信任的且已閱讀過程式碼的倉庫中設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果 Hub 上的程式碼與模型的其餘部分不在同一個倉庫中,要使用的特定程式碼版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (additional keyword arguments, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為因是否提供 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化一個庫中的模型類(帶有音訊分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForAudioClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForAudioClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForAudioClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForAudioClassification.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

TFAutoModelForAudioFrameClassification

class transformers.AutoModelForAudioFrameClassification

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有音訊幀(詞元)分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有音訊幀(詞元)分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForAudioFrameClassification

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForAudioFrameClassification.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 字串,即 huggingface.co 上模型倉庫中託管的預訓練模型的 *模型 ID*。
    • 包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • *tensorflow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後載入 PyTorch 模型要慢。
  • model_args (additional positional arguments, optional) — 將傳遞給底層模型 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。配置可以在以下情況下自動載入:

    • 模型是庫提供的模型(使用預訓練模型的 *模型 ID* 字串載入)。
    • 模型使用 save_pretrained() 儲存,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到名為 *config.json* 的配置 JSON 檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 一個狀態字典,用於代替從已儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是更簡單的選擇。

  • cache_dir (str or os.PathLike, optional) — 目錄路徑,如果不想使用標準快取,下載的預訓練模型配置將快取到該目錄中。
  • from_tf (bool, optional, defaults to False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,若存在快取版本則覆蓋它們。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都預設支援斷點續傳。將在 Transformers v5 中移除。
  • proxies (dict[str, str], optional) — 一個字典,用於指定按協議或端點使用的代理伺服器,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將用於每個請求。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型檔案中定義模型。此選項只應在你信任的且已閱讀過程式碼的倉庫中設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果 Hub 上的程式碼與模型的其餘部分不在同一個倉庫中,要使用的特定程式碼版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (additional keyword arguments, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為因是否提供 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有音訊幀(詞元)分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForAudioFrameClassification

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForAudioFrameClassification.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForAudioFrameClassification.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForAudioFrameClassification.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForCTC

class transformers.AutoModelForCTC

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的某個模型類(帶有連線時序分類頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有連線時序分類頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForCTC

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForCTC.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 字串,即 huggingface.co 上模型倉庫中託管的預訓練模型的 *模型 ID*。
    • 包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • *tensorflow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後載入 PyTorch 模型要慢。
  • model_args (additional positional arguments, optional) — 將傳遞給底層模型 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。配置可以在以下情況下自動載入:

    • 模型是庫提供的模型(使用預訓練模型的 *模型 ID* 字串載入)。
    • 模型使用 save_pretrained() 儲存,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到名為 *config.json* 的配置 JSON 檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 一個狀態字典,用於代替從已儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是更簡單的選擇。

  • cache_dir (str or os.PathLike, optional) — 目錄路徑,如果不想使用標準快取,下載的預訓練模型配置將快取到該目錄中。
  • from_tf (bool, optional, defaults to False) — 是否從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,如果存在快取版本則覆蓋它們。
  • resume_download — 已棄用並忽略。現在,所有下載在可能的情況下都會預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則指定用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (additional keyword arguments, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為方式根據是否提供 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有連線主義時間分類頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForCTC

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForCTC.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForCTC.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForCTC.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForSpeechSeq2Seq

class transformers.AutoModelForSpeechSeq2Seq

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有序列到序列語音轉文字建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置例項化庫中的一個模型類(帶有序列到序列語音轉文字建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForSpeechSeq2Seq

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForSpeechSeq2Seq.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向*tensorflow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後再載入 PyTorch 模型要慢。
  • model_args (additional positional arguments, optional) — 將傳遞給底層模型 __init__() 方法的其他位置引數。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。配置可以在以下情況下自動載入:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型使用 save_pretrained() 儲存,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 要使用的狀態字典,而不是從儲存的權重檔案載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,則可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是更簡單的選項。

  • cache_dir (str or os.PathLike, optional) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_tf (bool, optional, defaults to False) — 是否從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,如果存在快取版本則覆蓋它們。
  • resume_download — 已棄用並忽略。現在,所有下載在可能的情況下都會預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則指定用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (additional keyword arguments, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為方式根據是否提供 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有序列到序列語音轉文字建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForSpeechSeq2Seq

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForSpeechSeq2Seq.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForSpeechSeq2Seq.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForSpeechSeq2Seq.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForSpeechSeq2Seq

class transformers.TFAutoModelForSpeechSeq2Seq

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有序列到序列語音轉文字建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置例項化庫中的一個模型類(帶有序列到序列語音轉文字建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForSpeechSeq2Seq

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForSpeechSeq2Seq.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型,然後再載入 TensorFlow 模型要慢。
  • model_args (additional positional arguments, optional) — 將傳遞給底層模型 __init__() 方法的其他位置引數。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。配置可以在以下情況下自動載入:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型使用 save_pretrained() 儲存,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到了名為 *config.json* 的配置檔案。
  • cache_dir (str or os.PathLike, optional) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, optional, defaults to False) — 是否從 PyTorch 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,如果存在快取版本則覆蓋它們。
  • resume_download — 已棄用並忽略。現在,所有下載在可能的情況下都會預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 一個用於按協議或端點指定代理伺服器的字典,例如:{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交ID,因為我們使用基於git的系統在huggingface.co上儲存模型和其他工件,所以 revision 可以是git允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為受信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定要用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交ID,因為我們使用基於git的系統在huggingface.co上儲存模型和其他工件,所以 revision 可以是git允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為方式根據是否提供 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已經完成)
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於覆蓋該屬性的值。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有序列到序列語音轉文字建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForSpeechSeq2Seq

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForSpeechSeq2Seq.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForSpeechSeq2Seq.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForSpeechSeq2Seq.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForSpeechSeq2Seq

class transformers.FlaxAutoModelForSpeechSeq2Seq

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有序列到序列語音轉文字建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從配置例項化庫中的一個模型類(帶有序列到序列語音轉文字建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForSpeechSeq2Seq

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForSpeechSeq2Seq.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個*目錄*的路徑,該目錄包含使用 save_pretrained() 儲存的模型權重,例如 ./my_model_directory/
    • 一個*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (附加位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (str or os.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個用於按協議或端點指定代理伺服器的字典,例如:{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交ID,因為我們使用基於git的系統在huggingface.co上儲存模型和其他工件,所以 revision 可以是git允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為受信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定要用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交ID,因為我們使用基於git的系統在huggingface.co上儲存模型和其他工件,所以 revision 可以是git允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為方式根據是否提供 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已經完成)
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於覆蓋該屬性的值。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有序列到序列語音轉文字建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForSpeechSeq2Seq

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForSpeechSeq2Seq.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForSpeechSeq2Seq.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForSpeechSeq2Seq.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForAudioXVector

class transformers.AutoModelForAudioXVector

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有透過 x-vector 進行音訊檢索的頭部)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有透過 x-vector 進行音訊檢索的頭部)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForAudioXVector

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForAudioXVector.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個*目錄*的路徑,該目錄包含使用 save_pretrained() 儲存的模型權重,例如 ./my_model_directory/
    • 一個*tensorflow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,from_tf 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型後再載入 PyTorch 模型要慢。
  • model_args (附加位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 用於替代從已儲存權重檔案載入的狀態字典的狀態字典。

    如果您想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (str or os.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個用於按協議或端點指定代理伺服器的字典,例如:{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交ID,因為我們使用基於git的系統在huggingface.co上儲存模型和其他工件,所以 revision 可以是git允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為受信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則指定要用於 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交ID,因為我們使用基於git的系統在huggingface.co上儲存模型和其他工件,所以 revision 可以是git允許的任何識別符號。
  • kwargs (附加關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為方式根據是否提供 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有對配置的相關更新已經完成)
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式 (from_pretrained())。kwargs 中與配置屬性對應的每個鍵都將用於覆蓋該屬性的值。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有透過 x-vector 進行音訊檢索的頭部)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForAudioXVector

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForAudioXVector.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForAudioXVector.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForAudioXVector.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForTextToSpectrogram

class transformers.AutoModelForTextToSpectrogram

< >

*args **kwargs

AutoModelForTextToWaveform

class transformers.AutoModelForTextToWaveform

< >

*args **kwargs

AutoModelForAudioTokenization

class transformers.AutoModelForAudioTokenization

< >

*args **kwargs

這是一個通用的模型類,當透過 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有一個透過碼本進行音訊分詞的頭部)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

  • config (PretrainedConfig) — 要例項化的模型類是根據配置類選擇的:

  • attn_implementation (str, optional) — 在模型中使用的注意力實現(如果相關)。可以是 "eager"(注意力的手動實現)、"sdpa"(使用 F.scaled_dot_product_attention)或 "flash_attention_2"(使用 Dao-AILab/flash-attention)中的任何一種。預設情況下,如果可用,對於 torch>=2.1.1,將使用 SDPA。否則,預設是手動的 "eager" 實現。

從配置中例項化庫中的一個模型類(帶有一個透過碼本進行音訊分詞的頭部)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForAudioTokenization

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForAudioTokenization.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個 *TensorFlow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, optional) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供一個本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 一個狀態字典,用於代替從儲存的權重檔案中載入的狀態字典。

    如果你想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,你應該檢查使用 save_pretrained()from_pretrained() 是否是更簡單的選項。

  • cache_dir (str or os.PathLike, optional) — 下載的預訓練模型配置應快取的目錄路徑,如果不應使用標準快取。
  • from_tf (bool, optional, defaults to False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為你信任的且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則用於 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有一個透過碼本進行音訊分詞的頭部)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForAudioTokenization

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForAudioTokenization.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForAudioTokenization.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForAudioTokenization.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

多模態

以下自動類可用於以下多模態任務。

AutoModelForTableQuestionAnswering

class transformers.AutoModelForTableQuestionAnswering

< >

*args **kwargs

這是一個通用的模型類,當透過 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有一個表格問答頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

  • config (PretrainedConfig) — 要例項化的模型類是根據配置類選擇的:

  • attn_implementation (str, optional) — 在模型中使用的注意力實現(如果相關)。可以是 "eager"(注意力的手動實現)、"sdpa"(使用 F.scaled_dot_product_attention)或 "flash_attention_2"(使用 Dao-AILab/flash-attention)中的任何一種。預設情況下,如果可用,對於 torch>=2.1.1,將使用 SDPA。否則,預設是手動的 "eager" 實現。

從配置中例項化庫中的一個模型類(帶有一個表格問答頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForTableQuestionAnswering

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google/tapas-base-finetuned-wtq")
>>> model = AutoModelForTableQuestionAnswering.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個 *TensorFlow 索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型,然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, optional) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型透過提供一個本地目錄作為 pretrained_model_name_or_path 載入,並且在該目錄中找到了名為 *config.json* 的配置檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 一個狀態字典,用於代替從儲存的權重檔案中載入的狀態字典。

    如果你想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,你應該檢查使用 save_pretrained()from_pretrained() 是否是更簡單的選項。

  • cache_dir (str or os.PathLike, optional) — 下載的預訓練模型配置應快取的目錄路徑,如果不應使用標準快取。
  • from_tf (bool, optional, defaults to False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每次請求時使用。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為你信任的且已閱讀其程式碼的倉庫設定為 True,因為它將在你的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼與模型的其餘部分位於不同的倉庫中,則用於 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為根據是否提供了 config 或自動載入而有所不同:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中每個對應於配置屬性的鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有一個表格問答頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForTableQuestionAnswering

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForTableQuestionAnswering.from_pretrained("google/tapas-base-finetuned-wtq")

>>> # Update configuration during loading
>>> model = AutoModelForTableQuestionAnswering.from_pretrained("google/tapas-base-finetuned-wtq", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/tapas_tf_model_config.json")
>>> model = AutoModelForTableQuestionAnswering.from_pretrained(
...     "./tf_model/tapas_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForTableQuestionAnswering

class transformers.TFAutoModelForTableQuestionAnswering

< >

*args **kwargs

這是一個通用的模型類,當透過 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有一個表格問答頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

  • config (PretrainedConfig) — 要例項化的模型類是根據配置類選擇的:

  • attn_implementation (str, optional) — 在模型中使用的注意力實現(如果相關)。可以是 "eager"(注意力的手動實現)、"sdpa"(使用 F.scaled_dot_product_attention)或 "flash_attention_2"(使用 Dao-AILab/flash-attention)中的任何一種。預設情況下,如果可用,對於 torch>=2.1.1,將使用 SDPA。否則,預設是手動的 "eager" 實現。

從配置中例項化庫中的一個模型類(帶有一個表格問答頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForTableQuestionAnswering

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google/tapas-base-finetuned-wtq")
>>> model = TFAutoModelForTableQuestionAnswering.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,應將 from_pt 設定為 True,並且應透過 config 引數提供一個配置物件。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,以替代自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用且被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個字典,包含按協議或端點使用的代理伺服器,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則使用 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為因是否提供了 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型中例項化庫中的一個模型類(帶有一個表格問答頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForTableQuestionAnswering

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForTableQuestionAnswering.from_pretrained("google/tapas-base-finetuned-wtq")

>>> # Update configuration during loading
>>> model = TFAutoModelForTableQuestionAnswering.from_pretrained("google/tapas-base-finetuned-wtq", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/tapas_pt_model_config.json")
>>> model = TFAutoModelForTableQuestionAnswering.from_pretrained(
...     "./pt_model/tapas_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForDocumentQuestionAnswering

class transformers.AutoModelForDocumentQuestionAnswering

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有文件問答頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有文件問答頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForDocumentQuestionAnswering

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("impira/layoutlm-document-qa", revision="52e01b3")
>>> model = AutoModelForDocumentQuestionAnswering.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 tensorflow 索引檢查點檔案的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並且應透過 config 引數提供一個配置物件。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,以替代自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 一個狀態字典,用於替代從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否是更簡單的選擇。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用且被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個字典,包含按協議或端點使用的代理伺服器,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則使用 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。行為因是否提供了 config 或自動載入而異:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新都已完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有文件問答頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForDocumentQuestionAnswering

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForDocumentQuestionAnswering.from_pretrained("impira/layoutlm-document-qa", revision="52e01b3")

>>> # Update configuration during loading
>>> model = AutoModelForDocumentQuestionAnswering.from_pretrained("impira/layoutlm-document-qa", revision="52e01b3", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/layoutlm_tf_model_config.json")
>>> model = AutoModelForDocumentQuestionAnswering.from_pretrained(
...     "./tf_model/layoutlm_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForDocumentQuestionAnswering

class transformers.TFAutoModelForDocumentQuestionAnswering

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有文件問答頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有文件問答頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForDocumentQuestionAnswering

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("impira/layoutlm-document-qa", revision="52e01b3")
>>> model = TFAutoModelForDocumentQuestionAnswering.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 模型倉庫中的預訓練模型的 model id
    • 一個包含使用 save_pretrained() 儲存的模型權重的目錄路徑,例如 ./my_model_directory/
    • 一個指向 PyTorch state_dict 儲存檔案的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,應將 from_pt 設定為 True,並且應透過 config 引數提供一個配置物件。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型後再載入 TensorFlow 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,以替代自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的 model id 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在該目錄中找到了名為 config.json 的配置檔案。
  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(參見 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用且被忽略。現在所有下載在可能的情況下都會預設斷點續傳。將在 Transformers v5 版本中移除。
  • proxies (dict[str, str], 可選) — 一個字典,包含按協議或端點使用的代理伺服器,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理將在每個請求中使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上定義的自定義模型使用其自己的建模檔案。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則使用 Hub 上程式碼的特定版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如 output_attentions=True)。其行為因是否提供了 `config` 或自動載入而異:

    • 如果透過 `config` 提供了配置,`**kwargs` 將直接傳遞給底層模型的 `__init__` 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,`kwargs` 將首先傳遞給配置類的初始化函式 (from_pretrained())。`kwargs` 中與配置屬性對應的每個鍵都將用於使用提供的 `kwargs` 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將被傳遞給底層模型的 `__init__` 函式。

從預訓練模型例項化庫中的一個模型類(帶有文件問答頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForDocumentQuestionAnswering

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForDocumentQuestionAnswering.from_pretrained("impira/layoutlm-document-qa", revision="52e01b3")

>>> # Update configuration during loading
>>> model = TFAutoModelForDocumentQuestionAnswering.from_pretrained("impira/layoutlm-document-qa", revision="52e01b3", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/layoutlm_pt_model_config.json")
>>> model = TFAutoModelForDocumentQuestionAnswering.from_pretrained(
...     "./pt_model/layoutlm_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForVisualQuestionAnswering

class transformers.AutoModelForVisualQuestionAnswering

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中帶有視覺問答頭的模型類之一。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有視覺問答頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForVisualQuestionAnswering

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
>>> model = AutoModelForVisualQuestionAnswering.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 *tensorflow 索引檢查點檔案*的路徑或 URL(例如 ./tf_model/model.ckpt.index)。在這種情況下,`from_tf` 應設定為 `True`,並且應提供一個配置物件作為 `config` 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 `__init__()` 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 該模型是由庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 該模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 該模型透過提供一個本地目錄作為 `pretrained_model_name_or_path` 來載入,並且在該目錄中找到了一個名為 *config.json* 的配置 JSON 檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄的路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 `pretrained_model_name_or_path` 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋現有的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 `revision` 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 `True`,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 `revision` 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如 output_attentions=True)。其行為因是否提供了 `config` 或自動載入而異:

    • 如果透過 `config` 提供了配置,`**kwargs` 將直接傳遞給底層模型的 `__init__` 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,`kwargs` 將首先傳遞給配置類的初始化函式 (from_pretrained())。`kwargs` 中與配置屬性對應的每個鍵都將用於使用提供的 `kwargs` 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將被傳遞給底層模型的 `__init__` 函式。

從預訓練模型例項化庫中的一個模型類(帶有視覺問答頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForVisualQuestionAnswering

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForVisualQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")

>>> # Update configuration during loading
>>> model = AutoModelForVisualQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/vilt_tf_model_config.json")
>>> model = AutoModelForVisualQuestionAnswering.from_pretrained(
...     "./tf_model/vilt_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

AutoModelForVision2Seq

class transformers.AutoModelForVision2Seq

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中帶有視覺到文字建模頭的模型類之一。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有視覺到文字建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForVision2Seq

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForVision2Seq.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向 *tensorflow 索引檢查點檔案*的路徑或 URL(例如 ./tf_model/model.ckpt.index)。在這種情況下,`from_tf` 應設定為 `True`,並且應提供一個配置物件作為 `config` 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後再載入 PyTorch 模型要慢。
  • model_args (額外的位置引數, 可選) — 將傳遞給底層模型的 `__init__()` 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 該模型是由庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 該模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 該模型透過提供一個本地目錄作為 `pretrained_model_name_or_path` 來載入,並且在該目錄中找到了一個名為 *config.json* 的配置 JSON 檔案。
  • state_dict (dict[str, torch.Tensor], 可選) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練的配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取到的目錄的路徑。
  • from_tf (bool, 可選, 預設為 False) — 從 TensorFlow 檢查點儲存檔案中載入模型權重(請參閱 `pretrained_model_name_or_path` 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋現有的快取版本。
  • resume_download — 已棄用並被忽略。現在所有下載在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 `revision` 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 `True`,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則用於 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們在 huggingface.co 上使用基於 git 的系統來儲存模型和其他工件,所以 `revision` 可以是 git 允許的任何識別符號。
  • kwargs (額外的關鍵字引數, 可選) — 可用於更新配置物件(載入後)並初始化模型(例如 output_attentions=True)。其行為因是否提供了 `config` 或自動載入而異:

    • 如果透過 `config` 提供了配置,`**kwargs` 將直接傳遞給底層模型的 `__init__` 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,`kwargs` 將首先傳遞給配置類的初始化函式 (from_pretrained())。`kwargs` 中與配置屬性對應的每個鍵都將用於使用提供的 `kwargs` 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將被傳遞給底層模型的 `__init__` 函式。

從預訓練模型例項化庫中的一個模型類(帶有視覺到文字建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForVision2Seq

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForVision2Seq.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForVision2Seq.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForVision2Seq.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

TFAutoModelForVision2Seq

class transformers.TFAutoModelForVision2Seq

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中帶有視覺到文字建模頭的模型類之一。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有視覺到文字建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, TFAutoModelForVision2Seq

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = TFAutoModelForVision2Seq.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (其他位置引數, 可選) — 將傳遞給底層模型的 `__init__()` 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型是透過提供本地目錄作為 pretrained_model_name_or_path 載入的,並且在目錄中找到了名為 *config.json* 的配置 JSON 檔案。
  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 `pretrained_model_name_or_path` 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能時預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型檔案中定義自定義模型。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果 Hub 上的程式碼與模型的其餘部分位於不同的倉庫中,則使用特定的程式碼修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,`output_attentions=True`)。行為因是否提供 `config` 或自動載入而異:

    • 如果使用 `config` 提供了配置,`**kwargs` 將直接傳遞給底層模型的 `__init__` 方法(我們假設所有對配置的相關更新已經完成)
    • 如果沒有提供配置,`kwargs` 將首先傳遞給配置類的初始化函式(from_pretrained())。`kwargs` 中與配置屬性對應的每個鍵將用於使用提供的 `kwargs` 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 `__init__` 函式。

從預訓練模型例項化庫中的一個模型類(帶有視覺到文字建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, TFAutoModelForVision2Seq

>>> # Download model and configuration from huggingface.co and cache.
>>> model = TFAutoModelForVision2Seq.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = TFAutoModelForVision2Seq.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = TFAutoModelForVision2Seq.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

FlaxAutoModelForVision2Seq

class transformers.FlaxAutoModelForVision2Seq

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中帶有視覺到文字建模頭的模型類之一。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

根據配置例項化庫中的一個模型類(帶有視覺到文字建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForVision2Seq

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = FlaxAutoModelForVision2Seq.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (stros.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個包含使用 save_pretrained() 儲存的模型權重的*目錄*路徑,例如 ./my_model_directory/
    • 一個指向*PyTorch state_dict 儲存檔案*的路徑或 URL(例如,./pt_model/pytorch_model.bin)。在這種情況下,from_pt 應設定為 True,並且應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 PyTorch 模型轉換為 TensorFlow 模型然後載入 TensorFlow 模型要慢。
  • model_args (其他位置引數, 可選) — 將傳遞給底層模型的 `__init__()` 方法。
  • config (PretrainedConfig, 可選) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID* 字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 模型是透過提供本地目錄作為 pretrained_model_name_or_path 載入的,並且在目錄中找到了名為 *config.json* 的配置 JSON 檔案。
  • cache_dir (stros.PathLike, 可選) — 如果不應使用標準快取,則為下載的預訓練模型配置應快取的目錄路徑。
  • from_pt (bool, 可選, 預設為 False) — 從 PyTorch 檢查點儲存檔案中載入模型權重(請參閱 `pretrained_model_name_or_path` 引數的文件字串)。
  • force_download (bool, 可選, 預設為 False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。所有下載現在在可能時預設恢復。將在 Transformers v5 中移除。
  • proxies (dict[str, str], 可選) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, 可選, 預設為 False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤訊息的字典。
  • local_files_only(bool, 可選, 預設為 False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, 可選, 預設為 "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, 可選, 預設為 False) — 是否允許在 Hub 上的自定義模型檔案中定義自定義模型。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上的程式碼。
  • code_revision (str, 可選, 預設為 "main") — 如果 Hub 上的程式碼與模型的其餘部分位於不同的倉庫中,則使用特定的程式碼修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, 可選) — 可用於更新配置物件(載入後)和初始化模型(例如,`output_attentions=True`)。行為因是否提供 `config` 或自動載入而異:

    • 如果使用 `config` 提供了配置,`**kwargs` 將直接傳遞給底層模型的 `__init__` 方法(我們假設所有對配置的相關更新已經完成)
    • 如果沒有提供配置,`kwargs` 將首先傳遞給配置類的初始化函式(from_pretrained())。`kwargs` 中與配置屬性對應的每個鍵將用於使用提供的 `kwargs` 值覆蓋該屬性。不對應任何配置屬性的其餘鍵將傳遞給底層模型的 `__init__` 函式。

從預訓練模型例項化庫中的一個模型類(帶有視覺到文字建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

示例

>>> from transformers import AutoConfig, FlaxAutoModelForVision2Seq

>>> # Download model and configuration from huggingface.co and cache.
>>> model = FlaxAutoModelForVision2Seq.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = FlaxAutoModelForVision2Seq.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a PyTorch checkpoint file instead of a TensorFlow model (slower)
>>> config = AutoConfig.from_pretrained("./pt_model/bert_pt_model_config.json")
>>> model = FlaxAutoModelForVision2Seq.from_pretrained(
...     "./pt_model/bert_pytorch_model.bin", from_pt=True, config=config
... )

AutoModelForImageTextToText

class transformers.AutoModelForImageTextToText

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有影像-文字到文字建模頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

從一個配置例項化庫中的一個模型類(帶有影像-文字到文字建模頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForImageTextToText

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForImageTextToText.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個指向使用 save_pretrained() 儲存的模型權重*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向*tensorflow索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後載入 PyTorch 模型要慢。
  • model_args (其他位置引數, optional) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到名為 *config.json* 的配置 JSON 檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (str or os.PathLike, optional) — 下載的預訓練模型配置應快取的目錄路徑,如果不應使用標準快取。
  • from_tf (bool, optional, defaults to False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上存在的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則要使用的 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有影像-文字到文字建模頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForImageTextToText

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForImageTextToText.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForImageTextToText.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForImageTextToText.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )

時間序列

AutoModelForTimeSeriesPrediction

class transformers.AutoModelForTimeSeriesPrediction

< >

*args **kwargs

這是一個通用的模型類,當使用 from_pretrained() 類方法或 from_config() 類方法建立時,它將被例項化為庫中的一個模型類(帶有時間序列預測頭)。

這個類不能直接使用 __init__() 進行例項化(會丟擲錯誤)。

from_config

< >

**kwargs

引數

  • config (PretrainedConfig) — 要例項化的模型類是根據配置類選擇的:

  • attn_implementation (str, optional) — 要在模型中使用的注意力實現(如果相關)。可以是 "eager"(注意力的手動實現)、"sdpa"(使用 F.scaled_dot_product_attention)或 "flash_attention_2"(使用 Dao-AILab/flash-attention)中的任何一種。預設情況下,如果可用,SDPA 將用於 torch>=2.1.1。否則,預設值為手動的 "eager" 實現。

從一個配置例項化庫中的一個模型類(帶有時間序列預測頭)。

注意:從其配置檔案載入模型並不會載入模型權重。它隻影響模型的配置。請使用 from_pretrained() 來載入模型權重。

示例

>>> from transformers import AutoConfig, AutoModelForTimeSeriesPrediction

>>> # Download configuration from huggingface.co and cache.
>>> config = AutoConfig.from_pretrained("google-bert/bert-base-cased")
>>> model = AutoModelForTimeSeriesPrediction.from_config(config)

from_pretrained

< >

( *model_args **kwargs )

引數

  • pretrained_model_name_or_path (str or os.PathLike) — 可以是以下之一:

    • 一個字串,即託管在 huggingface.co 上的模型倉庫中的預訓練模型的*模型 ID*。
    • 一個指向使用 save_pretrained() 儲存的模型權重*目錄*的路徑,例如 ./my_model_directory/
    • 一個指向*tensorflow索引檢查點檔案*的路徑或 URL(例如,./tf_model/model.ckpt.index)。在這種情況下,應將 from_tf 設定為 True,並應提供一個配置物件作為 config 引數。這種載入路徑比使用提供的轉換指令碼將 TensorFlow 檢查點轉換為 PyTorch 模型然後載入 PyTorch 模型要慢。
  • model_args (其他位置引數, optional) — 將傳遞給底層模型的 __init__() 方法。
  • config (PretrainedConfig, optional) — 用於模型的配置,而不是自動載入的配置。在以下情況下可以自動載入配置:

    • 模型是庫提供的模型(使用預訓練模型的*模型 ID*字串載入)。
    • 模型是使用 save_pretrained() 儲存的,並透過提供儲存目錄重新載入。
    • 透過提供本地目錄作為 pretrained_model_name_or_path 載入模型,並且在目錄中找到名為 *config.json* 的配置 JSON 檔案。
  • state_dict (dict[str, torch.Tensor], optional) — 要使用的狀態字典,而不是從儲存的權重檔案中載入的狀態字典。

    如果您想從預訓練配置建立模型但載入自己的權重,可以使用此選項。但在這種情況下,您應該檢查使用 save_pretrained()from_pretrained() 是否不是一個更簡單的選項。

  • cache_dir (str or os.PathLike, optional) — 下載的預訓練模型配置應快取的目錄路徑,如果不應使用標準快取。
  • from_tf (bool, optional, defaults to False) — 從 TensorFlow 檢查點儲存檔案載入模型權重(請參閱 pretrained_model_name_or_path 引數的文件字串)。
  • force_download (bool, optional, defaults to False) — 是否強制(重新)下載模型權重和配置檔案,覆蓋已存在的快取版本。
  • resume_download — 已棄用並忽略。現在所有下載在可能的情況下都會預設恢復。將在 Transformers 的 v5 版本中移除。
  • proxies (dict[str, str], optional) — 按協議或端點使用的代理伺服器字典,例如 {'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}。代理在每個請求上使用。
  • output_loading_info(bool, optional, defaults to False) — 是否同時返回一個包含缺失鍵、意外部索引鍵和錯誤資訊的字典。
  • local_files_only(bool, optional, defaults to False) — 是否只檢視本地檔案(例如,不嘗試下載模型)。
  • revision (str, optional, defaults to "main") — 要使用的特定模型版本。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • trust_remote_code (bool, optional, defaults to False) — 是否允許在 Hub 上的自定義模型在其自己的建模檔案中定義。此選項只應為您信任且已閱讀其程式碼的倉庫設定為 True,因為它將在您的本地計算機上執行 Hub 上存在的程式碼。
  • code_revision (str, optional, defaults to "main") — 如果程式碼位於與模型其餘部分不同的倉庫中,則要使用的 Hub 上程式碼的特定修訂版。它可以是分支名稱、標籤名稱或提交 ID,因為我們使用基於 git 的系統在 huggingface.co 上儲存模型和其他工件,所以 revision 可以是 git 允許的任何識別符號。
  • kwargs (其他關鍵字引數, optional) — 可用於更新配置物件(載入後)並初始化模型(例如,output_attentions=True)。其行為取決於是否提供了 config 或自動載入:

    • 如果透過 config 提供了配置,**kwargs 將直接傳遞給底層模型的 __init__ 方法(我們假設所有相關的配置更新已經完成)。
    • 如果沒有提供配置,kwargs 將首先傳遞給配置類的初始化函式(from_pretrained())。kwargs 中與配置屬性對應的每個鍵將用於使用提供的 kwargs 值覆蓋該屬性。不對應任何配置屬性的剩餘鍵將傳遞給底層模型的 __init__ 函式。

從預訓練模型例項化庫中的一個模型類(帶有時間序列預測頭)。

要例項化的模型類是根據配置物件的 model_type 屬性選擇的(可以作為引數傳遞,或者如果可能的話從 pretrained_model_name_or_path 載入),或者在缺失時,透過對 pretrained_model_name_or_path 進行模式匹配來選擇。

預設情況下,模型透過 model.eval() 設定為評估模式(例如,dropout 模組被停用)。要訓練模型,您應該首先使用 model.train() 將其設定回訓練模式。

示例

>>> from transformers import AutoConfig, AutoModelForTimeSeriesPrediction

>>> # Download model and configuration from huggingface.co and cache.
>>> model = AutoModelForTimeSeriesPrediction.from_pretrained("google-bert/bert-base-cased")

>>> # Update configuration during loading
>>> model = AutoModelForTimeSeriesPrediction.from_pretrained("google-bert/bert-base-cased", output_attentions=True)
>>> model.config.output_attentions
True

>>> # Loading from a TF checkpoint file instead of a PyTorch model (slower)
>>> config = AutoConfig.from_pretrained("./tf_model/bert_tf_model_config.json")
>>> model = AutoModelForTimeSeriesPrediction.from_pretrained(
...     "./tf_model/bert_tf_checkpoint.ckpt.index", from_tf=True, config=config
... )
< > 在 GitHub 上更新

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