Transformers ライブラリでは、プロセッサは 2 つの異なる意味を持ちます。
マルチモーダル モデルでは、オブジェクトが複数のモダリティ (テキスト、 視覚と音声)。これは、2 つ以上の処理オブジェクトをグループ化するプロセッサーと呼ばれるオブジェクトによって処理されます。 トークナイザー (テキスト モダリティ用)、画像プロセッサー (視覚用)、特徴抽出器 (オーディオ用) など。
これらのプロセッサは、保存およびロード機能を実装する次の基本クラスを継承します。
This is a mixin used to provide saving/loading functionality for all processor classes.
( pretrained_model_name_or_path: typing.Union[str, os.PathLike] cache_dir: typing.Union[str, os.PathLike, NoneType] = None force_download: bool = False local_files_only: bool = False token: typing.Union[str, bool, NoneType] = None revision: str = 'main' **kwargs )
Parameters
str
or os.PathLike
) —
This can be either:
bert-base-uncased
, or
namespaced under a user or organization name, like dbmdz/bert-base-german-cased
../my_model_directory/
../my_model_directory/preprocessor_config.json
.
**kwargs —
Additional keyword arguments passed along to both
from_pretrained() and
~tokenization_utils_base.PreTrainedTokenizer.from_pretrained
.Instantiate a processor associated with a pretrained model.
This class method is simply calling the feature extractor
from_pretrained(), image processor
ImageProcessingMixin and the tokenizer
~tokenization_utils_base.PreTrainedTokenizer.from_pretrained
methods. Please refer to the docstrings of the
methods above for more information.
( repo_id: str use_temp_dir: typing.Optional[bool] = None commit_message: typing.Optional[str] = None private: typing.Optional[bool] = None token: typing.Union[bool, str, NoneType] = None max_shard_size: typing.Union[int, str, NoneType] = '5GB' create_pr: bool = False safe_serialization: bool = True revision: str = None commit_description: str = None **deprecated_kwargs )
Parameters
str
) —
The name of the repository you want to push your processor to. It should contain your organization name
when pushing to a given organization. bool
, optional) —
Whether or not to use a temporary directory to store the files saved before they are pushed to the Hub.
Will default to True
if there is no directory named like repo_id
, False
otherwise. str
, optional) —
Message to commit while pushing. Will default to "Upload processor"
. bool
, optional) —
Whether or not the repository created should be private. bool
or str
, optional) —
The token to use as HTTP bearer authorization for remote files. If True
, will use the token generated
when running huggingface-cli login
(stored in ~/.huggingface
). Will default to True
if repo_url
is not specified. int
or str
, optional, defaults to "5GB"
) —
Only applicable for models. The maximum size for a checkpoint before being sharded. Checkpoints shard
will then be each of size lower than this size. If expressed as a string, needs to be digits followed
by a unit (like "5MB"
). We default it to "5GB"
so that users can easily load models on free-tier
Google Colab instances without any CPU OOM issues. bool
, optional, defaults to False
) —
Whether or not to create a PR with the uploaded files or directly commit. bool
, optional, defaults to True
) —
Whether or not to convert the model weights in safetensors format for safer serialization. str
, optional) —
Branch to push the uploaded files to. str
, optional) —
The description of the commit that will be created Upload the processor files to the 🤗 Model Hub.
Examples:
from transformers import AutoProcessor
processor = AutoProcessor.from_pretrained("bert-base-cased")
# Push the processor to your namespace with the name "my-finetuned-bert".
processor.push_to_hub("my-finetuned-bert")
# Push the processor to an organization with the name "my-finetuned-bert".
processor.push_to_hub("huggingface/my-finetuned-bert")
( auto_class = 'AutoProcessor' )
Register this class with a given auto class. This should only be used for custom feature extractors as the ones
in the library are already mapped with AutoProcessor
.
This API is experimental and may have some slight breaking changes in the next releases.
( save_directory push_to_hub: bool = False **kwargs )
Parameters
str
or os.PathLike
) —
Directory where the feature extractor JSON file and the tokenizer files will be saved (directory will
be created if it does not exist). bool
, optional, defaults to False
) —
Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the
repository you want to push to with repo_id
(will default to the name of save_directory
in your
namespace). Dict[str, Any]
, optional) —
Additional key word arguments passed along to the push_to_hub() method. Saves the attributes of this processor (feature extractor, tokenizer…) in the specified directory so that it can be reloaded using the from_pretrained() method.
This class method is simply calling save_pretrained() and save_pretrained(). Please refer to the docstrings of the methods above for more information.
すべてのプロセッサは、同じアーキテクチャに従っています。
DataProcessor。プロセッサは次のリストを返します。
InputExample。これら
InputExample は次のように変換できます。
~data.processors.utils.Input features
をモデルにフィードします。
Base class for data converters for sequence classification data sets.
Gets a collection of InputExample for the dev set.
Gets an example from a dict with tensorflow tensors.
Gets the list of labels for this data set.
Gets a collection of InputExample for the test set.
Gets a collection of InputExample for the train set.
Some tensorflow_datasets datasets are not formatted the same way the GLUE datasets are. This method converts examples to the correct format.
( guid: str text_a: str text_b: typing.Optional[str] = None label: typing.Optional[str] = None )
A single training/test example for simple sequence classification.
Serializes this instance to a JSON string.
( input_ids: typing.List[int] attention_mask: typing.Optional[typing.List[int]] = None token_type_ids: typing.Optional[typing.List[int]] = None label: typing.Union[int, float, NoneType] = None )
A single set of features of data. Property names are the same names as the corresponding inputs to a model.
Serializes this instance to a JSON string.
一般言語理解評価 (GLUE) は、 既存の NLU タスクの多様なセットにわたるモデルのパフォーマンス。紙と同時発売された GLUE: A 自然言語理解のためのマルチタスクベンチマークおよび分析プラットフォーム
このライブラリは、MRPC、MNLI、MNLI (不一致)、CoLA、SST2、STSB、 QQP、QNLI、RTE、WNLI。
それらのプロセッサは次のとおりです。
~data.processors.utils.MrpcProcessor
~data.processors.utils.MnliProcessor
~data.processors.utils.MnliMismatchedProcessor
~data.processors.utils.Sst2Processor
~data.processors.utils.StsbProcessor
~data.processors.utils.QqpProcessor
~data.processors.utils.QnliProcessor
~data.processors.utils.RteProcessor
~data.processors.utils.WnliProcessor
さらに、次のメソッドを使用して、データ ファイルから値をロードし、それらをリストに変換することができます。 InputExample。
( examples: typing.Union[typing.List[transformers.data.processors.utils.InputExample], ForwardRef('tf.data.Dataset')] tokenizer: PreTrainedTokenizer max_length: typing.Optional[int] = None task = None label_list = None output_mode = None )
Loads a data file into a list of InputFeatures
クロスリンガル NLI コーパス (XNLI) は、 言語を超えたテキスト表現の品質。 XNLI は、MultiNLI に基づくクラウドソースのデータセットです。テキストのペアには、15 個のテキスト含意アノテーションがラベル付けされています。 さまざまな言語 (英語などの高リソース言語とスワヒリ語などの低リソース言語の両方を含む)。
論文 XNLI: Evaluating Cross-lingual Sentence Representations と同時にリリースされました。
このライブラリは、XNLI データをロードするプロセッサをホストします。
~data.processors.utils.XnliProcessor
テストセットにはゴールドラベルが付いているため、評価はテストセットで行われますのでご了承ください。
これらのプロセッサを使用する例は、run_xnli.py スクリプトに示されています。
The Stanford Question Answering Dataset (SQuAD) は、次のベンチマークです。 質問応答に関するモデルのパフォーマンスを評価します。 v1.1 と v2.0 の 2 つのバージョンが利用可能です。最初のバージョン (v1.1) は、論文 SQuAD: 100,000+ question for Machine Comprehension of Text とともにリリースされました。 2 番目のバージョン (v2.0) は、論文 Know What You Don’t と同時にリリースされました。 知っておくべき: SQuAD の答えられない質問。
このライブラリは、次の 2 つのバージョンのそれぞれのプロセッサをホストします。
それらのプロセッサは次のとおりです。
~data.processors.utils.SquadV1Processor
~data.processors.utils.SquadV2Processor
どちらも抽象クラス ~data.processors.utils.SquadProcessor
を継承しています。
Processor for the SQuAD data set. overridden by SquadV1Processor and SquadV2Processor, used by the version 1.1 and version 2.0 of SQuAD, respectively.
Returns the evaluation example from the data directory.
Creates a list of SquadExample
using a TFDS dataset.
Returns the training examples from the data directory.
さらに、次のメソッドを使用して、SQuAD の例を次の形式に変換できます。
モデルの入力として使用できる ~data.processors.utils.SquadFeatures
。
( examples tokenizer max_seq_length doc_stride max_query_length is_training padding_strategy = 'max_length' return_dataset = False threads = 1 tqdm_enabled = True )
Converts a list of examples into a list of features that can be directly given as input to a model. It is model-dependant and takes advantage of many of the tokenizer’s features to create the model’s inputs.
Example:
processor = SquadV2Processor()
examples = processor.get_dev_examples(data_dir)
features = squad_convert_examples_to_features(
examples=examples,
tokenizer=tokenizer,
max_seq_length=args.max_seq_length,
doc_stride=args.doc_stride,
max_query_length=args.max_query_length,
is_training=not evaluate,
)
これらのプロセッサと前述の方法は、データを含むファイルだけでなく、 tensorflow_datasets パッケージ。以下に例を示します。
以下にプロセッサを使用した例と、データ ファイルを使用した変換方法を示します。
# Loading a V2 processor
processor = SquadV2Processor()
examples = processor.get_dev_examples(squad_v2_data_dir)
# Loading a V1 processor
processor = SquadV1Processor()
examples = processor.get_dev_examples(squad_v1_data_dir)
features = squad_convert_examples_to_features(
examples=examples,
tokenizer=tokenizer,
max_seq_length=max_seq_length,
doc_stride=args.doc_stride,
max_query_length=max_query_length,
is_training=not evaluate,
)
tensorflow_datasets の使用は、データ ファイルを使用するのと同じくらい簡単です。
# tensorflow_datasets only handle Squad V1.
tfds_examples = tfds.load("squad")
examples = SquadV1Processor().get_examples_from_dataset(tfds_examples, evaluate=evaluate)
features = squad_convert_examples_to_features(
examples=examples,
tokenizer=tokenizer,
max_seq_length=max_seq_length,
doc_stride=args.doc_stride,
max_query_length=max_query_length,
is_training=not evaluate,
)
これらのプロセッサを使用する別の例は、run_squad.py スクリプトに示されています。