Movatterモバイル変換


[0]ホーム

URL:


Hugging Face's logoHugging Face

Transformers documentation

Auto Classes

Transformers

You are viewingmain version, which requiresinstallation from source. If you'd likeregular pip install, checkout the latest stable version (v4.57.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

Collaborate on models, datasets and Spaces
Faster examples with accelerated inference
Switch between documentation themes

to get started

Auto Classes

In many cases, the architecture you want to use can be guessed from the name or the path of the pretrained model youare supplying to thefrom_pretrained() method. AutoClasses are here to do this job for you so that youautomatically retrieve the relevant model given the name/path to the pretrained weights/config/vocabulary.

Instantiating one ofAutoConfig,AutoModel, andAutoTokenizer will directly create a class of the relevant architecture. For instance

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

will create a model that is an instance ofBertModel.

There is one class ofAutoModel for each task.

Extending the Auto Classes

Each of the auto classes has a method to be extended with your custom classes. For instance, if you have defined acustom class of modelNewModel, make sure you have aNewModelConfig then you can add those to the autoclasses like this:

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

You will then be able to use the auto classes like you would usually do!

If yourNewModelConfig is a subclass ofPreTrainedConfig, make sure itsmodel_type attribute is set to the same key you use when registering the config (here"new-model").

Likewise, if yourNewModel is a subclass ofPreTrainedModel, make sure itsconfig_class attribute is set to the same class you use when registering the model (hereNewModelConfig).

AutoConfig

classtransformers.AutoConfig

<source>

()

This is a generic configuration class that will be instantiated as one of the configuration classes of the librarywhen created with thefrom_pretrained() class method.

This class cannot be instantiated directly using__init__() (throws an error).

from_pretrained

<source>

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

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model configuration hosted inside a model repo onhuggingface.co.
    • A path to adirectory containing a configuration file saved using thesave_pretrained() method, or thesave_pretrained() method,e.g.,./my_model_directory/.
    • A path or url to a saved configuration JSONfile, e.g.,./my_model_directory/configuration.json.
  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download the model weights and configuration files and override thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • return_unused_kwargs (bool,optional, defaults toFalse) —IfFalse, then this function returns just the final configuration object.

    IfTrue, then this functions returns aTuple(config, unused_kwargs) whereunused_kwargs is adictionary consisting of the key/value pairs whose keys are not configuration attributes: i.e., thepart ofkwargs which has not been used to updateconfig and is otherwise ignored.

  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • kwargs(additional keyword arguments,optional) —The values in kwargs of any keys which are configuration attributes will be used to override the loadedvalues. Behavior concerning key/value pairs whose keys arenot configuration attributes is controlledby thereturn_unused_kwargs keyword parameter.

Instantiate one of the configuration classes of the library from a pretrained model configuration.

The configuration class to instantiate is selected based on themodel_type property of the config object thatis loaded, or when it’s missing, by falling back to using pattern matching onpretrained_model_name_or_path:

Examples:

>>>from transformersimport 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_attentionsTrue>>>config, unused_kwargs = AutoConfig.from_pretrained(..."google-bert/bert-base-uncased", output_attentions=True, foo=False, return_unused_kwargs=True...)>>>config.output_attentionsTrue>>>unused_kwargs{'foo':False}

register

<source>

(model_typeconfigexist_ok = False)

Parameters

  • model_type (str) — The model type like “bert” or “gpt”.
  • config (PreTrainedConfig) — The config to register.

Register a new configuration for this class.

AutoTokenizer

classtransformers.AutoTokenizer

<source>

()

This is a generic tokenizer class that will be instantiated as one of the tokenizer classes of the library whencreated with theAutoTokenizer.from_pretrained() class method.

This class cannot be instantiated directly using__init__() (throws an error).

from_pretrained

<source>

(pretrained_model_name_or_path*inputs**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a predefined tokenizer hosted inside a model repo on huggingface.co.
    • A path to adirectory containing vocabulary files required by the tokenizer, for instance savedusing thesave_pretrained() method, e.g.,./my_model_directory/.
    • A path or url to a single saved vocabulary file if and only if the tokenizer only requires asingle vocabulary file (like Bert or XLNet), e.g.:./my_model_directory/vocab.txt. (Notapplicable to all derived classes)
  • inputs (additional positional arguments,optional) —Will be passed along to the Tokenizer__init__() method.
  • config (PreTrainedConfig,optional) —The configuration object used to determine the tokenizer class to instantiate.
  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download the model weights and configuration files and override thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • subfolder (str,optional) —In case the relevant files are located inside a subfolder of the model repo on huggingface.co (e.g. forfacebook/rag-token-base), specify it here.
  • use_fast (bool,optional, defaults toTrue) —Use afast Rust-based tokenizer if it is supported fora given model. If a fast tokenizer is not available for a given model, a normal Python-based tokenizeris returned instead.
  • tokenizer_type (str,optional) —Tokenizer type to be loaded.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • kwargs (additional keyword arguments,optional) —Will be passed to the Tokenizer__init__() method. Can be used to set special tokens likebos_token,eos_token,unk_token,sep_token,pad_token,cls_token,mask_token,additional_special_tokens. See parameters in the__init__() for more details.

Instantiate one of the tokenizer classes of the library from a pretrained model vocabulary.

The tokenizer class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

Examples:

>>>from transformersimport 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

<source>

(config_classslow_tokenizer_class = Nonefast_tokenizer_class = Noneexist_ok = False)

Parameters

  • config_class (PreTrainedConfig) —The configuration corresponding to the model to register.
  • slow_tokenizer_class (PretrainedTokenizer,optional) —The slow tokenizer to register.
  • fast_tokenizer_class (PretrainedTokenizerFast,optional) —The fast tokenizer to register.

Register a new tokenizer in this mapping.

AutoFeatureExtractor

classtransformers.AutoFeatureExtractor

<source>

()

This is a generic feature extractor class that will be instantiated as one of the feature extractor classes of thelibrary when created with theAutoFeatureExtractor.from_pretrained() class method.

This class cannot be instantiated directly using__init__() (throws an error).

from_pretrained

<source>

(pretrained_model_name_or_path**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —This can be either:

    • a string, themodel id of a pretrained feature_extractor hosted inside a model repo onhuggingface.co.
    • a path to adirectory containing a feature extractor file saved using thesave_pretrained() method, e.g.,./my_model_directory/.
    • a path or url to a saved feature extractor JSONfile, e.g.,./my_model_directory/preprocessor_config.json.
  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model feature extractor should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force to (re-)download the feature extractor files and override the cached versionsif they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • token (str orbool,optional) —The token to use as HTTP bearer authorization for remote files. IfTrue, will use the token generatedwhen runninghf auth login (stored in~/.huggingface).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • return_unused_kwargs (bool,optional, defaults toFalse) —IfFalse, then this function returns just the final feature extractor object. IfTrue, then thisfunctions returns aTuple(feature_extractor, unused_kwargs) whereunused_kwargs is a dictionaryconsisting of the key/value pairs whose keys are not feature extractor attributes: i.e., the part ofkwargs which has not been used to updatefeature_extractor and is otherwise ignored.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • kwargs (dict[str, Any],optional) —The values in kwargs of any keys which are feature extractor attributes will be used to override theloaded values. Behavior concerning key/value pairs whose keys arenot feature extractor attributes iscontrolled by thereturn_unused_kwargs keyword parameter.

Instantiate one of the feature extractor classes of the library from a pretrained model vocabulary.

The feature extractor class to instantiate is selected based on themodel_type property of the config object(either passed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’smissing, by falling back to using pattern matching onpretrained_model_name_or_path:

Passingtoken=True is required when you want to use a private model.

Examples:

>>>from transformersimport 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

<source>

(config_classfeature_extractor_classexist_ok = False)

Parameters

  • config_class (PreTrainedConfig) —The configuration corresponding to the model to register.
  • feature_extractor_class (FeatureExtractorMixin) — The feature extractor to register.

Register a new feature extractor for this class.

AutoImageProcessor

classtransformers.AutoImageProcessor

<source>

()

This is a generic image processor class that will be instantiated as one of the image processor classes of thelibrary when created with theAutoImageProcessor.from_pretrained() class method.

This class cannot be instantiated directly using__init__() (throws an error).

from_pretrained

<source>

(pretrained_model_name_or_path*inputs**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —This can be either:

    • a string, themodel id of a pretrained image_processor hosted inside a model repo onhuggingface.co.
    • a path to adirectory containing a image processor file saved using thesave_pretrained() method, e.g.,./my_model_directory/.
    • a path or url to a saved image processor JSONfile, e.g.,./my_model_directory/preprocessor_config.json.
  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model image processor should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force to (re-)download the image processor files and override the cached versions ifthey exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • token (str orbool,optional) —The token to use as HTTP bearer authorization for remote files. IfTrue, will use the token generatedwhen runninghf auth login (stored in~/.huggingface).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • use_fast (bool,optional, defaults toFalse) —Use a fast torchvision-base image processor if it is supported for a given model.If a fast image processor is not available for a given model, a normal numpy-based image processoris returned instead.
  • return_unused_kwargs (bool,optional, defaults toFalse) —IfFalse, then this function returns just the final image processor object. IfTrue, then thisfunctions returns aTuple(image_processor, unused_kwargs) whereunused_kwargs is a dictionaryconsisting of the key/value pairs whose keys are not image processor attributes: i.e., the part ofkwargs which has not been used to updateimage_processor and is otherwise ignored.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • image_processor_filename (str,optional, defaults to"config.json") —The name of the file in the model directory to use for the image processor config.
  • kwargs (dict[str, Any],optional) —The values in kwargs of any keys which are image processor attributes will be used to override theloaded values. Behavior concerning key/value pairs whose keys arenot image processor attributes iscontrolled by thereturn_unused_kwargs keyword parameter.

Instantiate one of the image processor classes of the library from a pretrained model vocabulary.

The image processor class to instantiate is selected based on themodel_type property of the config object(either passed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’smissing, by falling back to using pattern matching onpretrained_model_name_or_path:

Passingtoken=True is required when you want to use a private model.

Examples:

>>>from transformersimport 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

<source>

(config_classimage_processor_class = Noneslow_image_processor_class = Nonefast_image_processor_class = Noneexist_ok = False)

Parameters

Register a new image processor for this class.

AutoVideoProcessor

classtransformers.AutoVideoProcessor

<source>

()

This is a generic video processor class that will be instantiated as one of the video processor classes of thelibrary when created with theAutoVideoProcessor.from_pretrained() class method.

This class cannot be instantiated directly using__init__() (throws an error).

from_pretrained

<source>

(pretrained_model_name_or_path*inputs**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —This can be either:

    • a string, themodel id of a pretrained video_processor hosted inside a model repo onhuggingface.co.
    • a path to adirectory containing a video processor file saved using thesave_pretrained() method, e.g.,./my_model_directory/.
    • a path or url to a saved video processor JSONfile, e.g.,./my_model_directory/preprocessor_config.json.
  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model video processor should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force to (re-)download the video processor files and override the cached versions ifthey exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • token (str orbool,optional) —The token to use as HTTP bearer authorization for remote files. IfTrue, will use the token generatedwhen runninghf auth login (stored in~/.huggingface).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • return_unused_kwargs (bool,optional, defaults toFalse) —IfFalse, then this function returns just the final video processor object. IfTrue, then thisfunctions returns aTuple(video_processor, unused_kwargs) whereunused_kwargs is a dictionaryconsisting of the key/value pairs whose keys are not video processor attributes: i.e., the part ofkwargs which has not been used to updatevideo_processor and is otherwise ignored.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • kwargs (dict[str, Any],optional) —The values in kwargs of any keys which are video processor attributes will be used to override theloaded values. Behavior concerning key/value pairs whose keys arenot video processor attributes iscontrolled by thereturn_unused_kwargs keyword parameter.

Instantiate one of the video processor classes of the library from a pretrained model vocabulary.

The video processor class to instantiate is selected based on themodel_type property of the config object(either passed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’smissing, by falling back to using pattern matching onpretrained_model_name_or_path:

Passingtoken=True is required when you want to use a private model.

Examples:

>>>from transformersimport 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

<source>

(config_classvideo_processor_classexist_ok = False)

Parameters

  • config_class (PreTrainedConfig) —The configuration corresponding to the model to register.
  • video_processor_class (BaseVideoProcessor) —The video processor to register.

Register a new video processor for this class.

AutoProcessor

classtransformers.AutoProcessor

<source>

()

This is a generic processor class that will be instantiated as one of the processor classes of the library whencreated with theAutoProcessor.from_pretrained() class method.

This class cannot be instantiated directly using__init__() (throws an error).

from_pretrained

<source>

(pretrained_model_name_or_path**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —This can be either:

    • a string, themodel id of a pretrained feature_extractor hosted inside a model repo onhuggingface.co.
    • a path to adirectory containing a processor files saved using thesave_pretrained() method,e.g.,./my_model_directory/.
  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model feature extractor should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force to (re-)download the feature extractor files and override the cached versionsif they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • token (str orbool,optional) —The token to use as HTTP bearer authorization for remote files. IfTrue, will use the token generatedwhen runninghf auth login (stored in~/.huggingface).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • return_unused_kwargs (bool,optional, defaults toFalse) —IfFalse, then this function returns just the final feature extractor object. IfTrue, then thisfunctions returns aTuple(feature_extractor, unused_kwargs) whereunused_kwargs is a dictionaryconsisting of the key/value pairs whose keys are not feature extractor attributes: i.e., the part ofkwargs which has not been used to updatefeature_extractor and is otherwise ignored.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • kwargs (dict[str, Any],optional) —The values in kwargs of any keys which are feature extractor attributes will be used to override theloaded values. Behavior concerning key/value pairs whose keys arenot feature extractor attributes iscontrolled by thereturn_unused_kwargs keyword parameter.

Instantiate one of the processor classes of the library from a pretrained model vocabulary.

The processor class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible):

Passingtoken=True is required when you want to use a private model.

Examples:

>>>from transformersimport 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

<source>

(config_classprocessor_classexist_ok = False)

Parameters

  • config_class (PreTrainedConfig) —The configuration corresponding to the model to register.
  • processor_class (ProcessorMixin) — The processor to register.

Register a new processor for this class.

Generic model classes

The following auto classes are available for instantiating a base model class without a specific head.

AutoModel

classtransformers.AutoModel

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the base model classes of the library when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the base model classes of the library from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the base model classes of the library from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

Generic pretraining classes

The following auto classes are available for instantiating a model with a pretraining head.

AutoModelForPreTraining

classtransformers.AutoModelForPreTraining

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a pretraining head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a pretraining head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a pretraining head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

Natural Language Processing

The following auto classes are available for the following natural language processing tasks.

AutoModelForCausalLM

classtransformers.AutoModelForCausalLM

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a causal language modeling head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a causal language modeling head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a causal language modeling head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForMaskedLM

classtransformers.AutoModelForMaskedLM

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a masked language modeling head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a masked language modeling head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a masked language modeling head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForMaskGeneration

classtransformers.AutoModelForMaskGeneration

<source>

(*args**kwargs)

AutoModelForSeq2SeqLM

classtransformers.AutoModelForSeq2SeqLM

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a sequence-to-sequence language modeling head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a sequence-to-sequence language modeling head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

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

from_pretrained

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a sequence-to-sequence language modeling head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForSequenceClassification

classtransformers.AutoModelForSequenceClassification

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a sequence classification head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a sequence classification head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a sequence classification head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForMultipleChoice

classtransformers.AutoModelForMultipleChoice

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a multiple choice head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a multiple choice head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a multiple choice head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForNextSentencePrediction

classtransformers.AutoModelForNextSentencePrediction

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a next sentence prediction head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a next sentence prediction head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a next sentence prediction head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForTokenClassification

classtransformers.AutoModelForTokenClassification

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a token classification head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a token classification head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a token classification head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForQuestionAnswering

classtransformers.AutoModelForQuestionAnswering

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a question answering head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a question answering head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a question answering head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForTextEncoding

classtransformers.AutoModelForTextEncoding

<source>

(*args**kwargs)

Computer vision

The following auto classes are available for the following computer vision tasks.

AutoModelForDepthEstimation

classtransformers.AutoModelForDepthEstimation

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a depth estimation head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a depth estimation head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a depth estimation head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForImageClassification

classtransformers.AutoModelForImageClassification

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a image classification head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a image classification head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a image classification head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForVideoClassification

classtransformers.AutoModelForVideoClassification

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a video classification head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a video classification head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a video classification head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForKeypointDetection

classtransformers.AutoModelForKeypointDetection

<source>

(*args**kwargs)

AutoModelForKeypointMatching

classtransformers.AutoModelForKeypointMatching

<source>

(*args**kwargs)

AutoModelForMaskedImageModeling

classtransformers.AutoModelForMaskedImageModeling

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a masked image modeling head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a masked image modeling head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a masked image modeling head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForObjectDetection

classtransformers.AutoModelForObjectDetection

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a object detection head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a object detection head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a object detection head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForImageSegmentation

classtransformers.AutoModelForImageSegmentation

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a image segmentation head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

  • config (PreTrainedConfig) —The model class to instantiate is selected based on the configuration class:

  • attn_implementation (str,optional) —The attention implementation to use in the model (if relevant). Can be any of"eager" (manual implementation of the attention),"sdpa" (usingF.scaled_dot_product_attention), or"flash_attention_2" (usingDao-AILab/flash-attention). By default, if available, SDPA will be used for torch>=2.1.1. The default is otherwise the manual"eager" implementation.

Instantiates one of the model classes of the library (with a image segmentation head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a image segmentation head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForImageToImage

classtransformers.AutoModelForImageToImage

<source>

(*args**kwargs)

AutoModelForSemanticSegmentation

classtransformers.AutoModelForSemanticSegmentation

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a semantic segmentation head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a semantic segmentation head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a semantic segmentation head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForInstanceSegmentation

classtransformers.AutoModelForInstanceSegmentation

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a instance segmentation head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a instance segmentation head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a instance segmentation head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForUniversalSegmentation

classtransformers.AutoModelForUniversalSegmentation

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a universal image segmentation head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a universal image segmentation head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a universal image segmentation head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForZeroShotImageClassification

classtransformers.AutoModelForZeroShotImageClassification

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a zero-shot image classification head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a zero-shot image classification head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a zero-shot image classification head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForZeroShotObjectDetection

classtransformers.AutoModelForZeroShotObjectDetection

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a zero-shot object detection head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a zero-shot object detection head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a zero-shot object detection head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

Audio

The following auto classes are available for the following audio tasks.

AutoModelForAudioClassification

classtransformers.AutoModelForAudioClassification

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a audio classification head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a audio classification head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a audio classification head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForAudioFrameClassification

classtransformers.AutoModelForAudioFrameClassification

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a audio frame (token) classification head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a audio frame (token) classification head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a audio frame (token) classification head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForCTC

classtransformers.AutoModelForCTC

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a connectionist temporal classification head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a connectionist temporal classification head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a connectionist temporal classification head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForSpeechSeq2Seq

classtransformers.AutoModelForSpeechSeq2Seq

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a sequence-to-sequence speech-to-text modeling head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a sequence-to-sequence speech-to-text modeling head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a sequence-to-sequence speech-to-text modeling head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForAudioXVector

classtransformers.AutoModelForAudioXVector

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a audio retrieval via x-vector head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a audio retrieval via x-vector head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a audio retrieval via x-vector head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForTextToSpectrogram

classtransformers.AutoModelForTextToSpectrogram

<source>

(*args**kwargs)

AutoModelForTextToWaveform

classtransformers.AutoModelForTextToWaveform

<source>

(*args**kwargs)

AutoModelForAudioTokenization

classtransformers.AutoModelForAudioTokenization

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a audio tokenization through codebooks head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

  • config (PreTrainedConfig) —The model class to instantiate is selected based on the configuration class:

  • attn_implementation (str,optional) —The attention implementation to use in the model (if relevant). Can be any of"eager" (manual implementation of the attention),"sdpa" (usingF.scaled_dot_product_attention), or"flash_attention_2" (usingDao-AILab/flash-attention). By default, if available, SDPA will be used for torch>=2.1.1. The default is otherwise the manual"eager" implementation.

Instantiates one of the model classes of the library (with a audio tokenization through codebooks head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a audio tokenization through codebooks head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

Multimodal

The following auto classes are available for the following multimodal tasks.

AutoModelForTableQuestionAnswering

classtransformers.AutoModelForTableQuestionAnswering

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a table question answering head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

  • config (PreTrainedConfig) —The model class to instantiate is selected based on the configuration class:

  • attn_implementation (str,optional) —The attention implementation to use in the model (if relevant). Can be any of"eager" (manual implementation of the attention),"sdpa" (usingF.scaled_dot_product_attention), or"flash_attention_2" (usingDao-AILab/flash-attention). By default, if available, SDPA will be used for torch>=2.1.1. The default is otherwise the manual"eager" implementation.

Instantiates one of the model classes of the library (with a table question answering head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a table question answering head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForDocumentQuestionAnswering

classtransformers.AutoModelForDocumentQuestionAnswering

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a document question answering head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a document question answering head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a document question answering head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForVisualQuestionAnswering

classtransformers.AutoModelForVisualQuestionAnswering

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a visual question answering head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a visual question answering head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a visual question answering head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

AutoModelForVision2Seq

classtransformers.AutoModelForVision2Seq

<source>

(*args**kwargs)

AutoModelForImageTextToText

classtransformers.AutoModelForImageTextToText

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a image-text-to-text modeling head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

Instantiates one of the model classes of the library (with a image-text-to-text modeling head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a image-text-to-text modeling head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue

Time Series

AutoModelForTimeSeriesPrediction

classtransformers.AutoModelForTimeSeriesPrediction

<source>

(*args**kwargs)

This is a generic model class that will be instantiated as one of the model classes of the library (with a time-series prediction head) when createdwith thefrom_pretrained() class method or thefrom_config() classmethod.

This class cannot be instantiated directly using__init__() (throws an error).

from_config

<source>

(**kwargs)

Parameters

  • config (PreTrainedConfig) —The model class to instantiate is selected based on the configuration class:

  • attn_implementation (str,optional) —The attention implementation to use in the model (if relevant). Can be any of"eager" (manual implementation of the attention),"sdpa" (usingF.scaled_dot_product_attention), or"flash_attention_2" (usingDao-AILab/flash-attention). By default, if available, SDPA will be used for torch>=2.1.1. The default is otherwise the manual"eager" implementation.

Instantiates one of the model classes of the library (with a time-series prediction head) from a configuration.

Note:Loading a model from its configuration file doesnot load the model weights. It only affects themodel’s configuration. Usefrom_pretrained() to load the model weights.

Examples:

>>>from transformersimport 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

<source>

(*model_args**kwargs)

Parameters

  • pretrained_model_name_or_path (str oros.PathLike) —Can be either:

    • A string, themodel id of a pretrained model hosted inside a model repo on huggingface.co.
    • A path to adirectory containing model weights saved usingsave_pretrained(), e.g.,./my_model_directory/.
  • model_args (additional positional arguments,optional) —Will be passed along to the underlying model__init__() method.
  • config (PreTrainedConfig,optional) —Configuration for the model to use instead of an automatically loaded configuration. Configuration canbe automatically loaded when:

    • The model is a model provided by the library (loaded with themodel id string of a pretrainedmodel).
    • The model was saved usingsave_pretrained() and is reloaded by supplying thesave directory.
    • The model is loaded by supplying a local directory aspretrained_model_name_or_path and aconfiguration JSON file namedconfig.json is found in the directory.
  • state_dict (dict[str, torch.Tensor],optional) —A state dictionary to use instead of a state dictionary loaded from saved weights file.

    This option can be used if you want to create a model from a pretrained configuration but load your ownweights. In this case though, you should check if usingsave_pretrained() andfrom_pretrained() is not a simpler option.

  • cache_dir (str oros.PathLike,optional) —Path to a directory in which a downloaded pretrained model configuration should be cached if thestandard cache should not be used.
  • force_download (bool,optional, defaults toFalse) —Whether or not to force the (re-)download of the model weights and configuration files, overriding thecached versions if they exist.
  • proxies (dict[str, str],optional) —A dictionary of proxy servers to use by protocol or endpoint, e.g.,{'http': 'foo.bar:3128', 'http://hostname': 'foo.bar:4012'}. The proxies are used on each request.
  • output_loading_info(bool,optional, defaults toFalse) —Whether ot not to also return a dictionary containing missing keys, unexpected keys and error messages.
  • local_files_only(bool,optional, defaults toFalse) —Whether or not to only look at local files (e.g., not try downloading the model).
  • revision (str,optional, defaults to"main") —The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use agit-based system for storing models and other artifacts on huggingface.co, sorevision can be anyidentifier allowed by git.
  • trust_remote_code (bool,optional, defaults toFalse) —Whether or not to allow for custom models defined on the Hub in their own modeling files. This optionshould only be set toTrue for repositories you trust and in which you have read the code, as it willexecute code present on the Hub on your local machine.
  • code_revision (str,optional, defaults to"main") —The specific revision to use for the code on the Hub, if the code leaves in a different repository thanthe rest of the model. It can be a branch name, a tag name, or a commit id, since we use a git-basedsystem for storing models and other artifacts on huggingface.co, sorevision can be any identifierallowed by git.
  • kwargs (additional keyword arguments,optional) —Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,output_attentions=True). Behaves differently depending on whether aconfig is provided orautomatically loaded:

    • If a configuration is provided withconfig,**kwargs will be directly passed to theunderlying model’s__init__ method (we assume all relevant updates to the configuration havealready been done)
    • If a configuration is not provided,kwargs will be first passed to the configuration classinitialization function (from_pretrained()). Each key ofkwargs thatcorresponds to a configuration attribute will be used to override said attribute with thesuppliedkwargs value. Remaining keys that do not correspond to any configuration attributewill be passed to the underlying model’s__init__ function.

Instantiate one of the model classes of the library (with a time-series prediction head) from a pretrained model.

The model class to instantiate is selected based on themodel_type property of the config object (eitherpassed as an argument or loaded frompretrained_model_name_or_path if possible), or when it’s missing, byfalling back to using pattern matching onpretrained_model_name_or_path:

The model is set in evaluation mode by default usingmodel.eval() (so for instance, dropout modules aredeactivated). To train the model, you should first set it back in training mode withmodel.train()

Examples:

>>>from transformersimport 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_attentionsTrue
Update on GitHub


[8]ページ先頭

©2009-2025 Movatter.jp