30 KiB
Quick tour
ð€ Transformersã䜿ãå§ããŸãããïŒ éçºè
ã§ããããšãæ¥åžžçãªãŠãŒã¶ãŒã§ããããšããã®ã¯ã€ãã¯ãã¢ãŒã¯
åããŠå§ããã®ãæ¯æŽãã[pipeline
]ã䜿ã£ãæšè«æ¹æ³ãAutoClassã§äºååŠç¿æžã¿ã¢ãã«ãšããªããã»ããµãããŒãããæ¹æ³ã
ãããŠPyTorchãŸãã¯TensorFlowã§çŽ æ©ãã¢ãã«ããã¬ãŒãã³ã°ããæ¹æ³ã瀺ããŸãã åå¿è
ã®å Žåãããã§çŽ¹ä»ãããã³ã³ã»ããã®è©³çŽ°ãªèª¬æãæäŸãã
ãã¥ãŒããªã¢ã«ãŸãã¯ã³ãŒã¹ã次ã«åç
§ããããšããå§ãããŸãã
å§ããåã«ãå¿ èŠãªã©ã€ãã©ãªããã¹ãŠã€ã³ã¹ããŒã«ãããŠããããšã確èªããŠãã ããïŒ
!pip install transformers datasets evaluate accelerate
ããªãã¯ãŸãã奜ããªæ©æ¢°åŠç¿ãã¬ãŒã ã¯ãŒã¯ãã€ã³ã¹ããŒã«ããå¿ èŠããããŸã:
pip install torch
pip install tensorflow
Pipeline
[pipeline
] ã¯ãäºååŠç¿æžã¿ã¢ãã«ãæšè«ã«æãç°¡åã§é«éãªæ¹æ³ã§ãã
[pipeline
] ã䜿çšããããšã§ãããŸããŸãªã¢ããªãã£ã«ãããå€ãã®ã¿ã¹ã¯ã«å¯ŸããŠå³åº§ã«äœ¿çšã§ããŸãã
ããã€ãã®ã¿ã¹ã¯ã¯ä»¥äžã®è¡šã«ç€ºãããŠããŸãïŒ
䜿çšå¯èœãªã¿ã¹ã¯ã®å®å šãªäžèŠ§ã«ã€ããŠã¯ãpipeline API ãªãã¡ã¬ã³ã¹ã確èªããŠãã ããã
ã¿ã¹ã¯ | 説æ | ã¢ããªã㣠| ãã€ãã©ã€ã³èå¥å |
---|---|---|---|
ããã¹ãåé¡ | ããã¹ãã®ã·ãŒã±ã³ã¹ã«ã©ãã«ãå²ãåœãŠã | NLP | pipeline(task="sentiment-analysis") |
ããã¹ãçæ | ããã³ãããæå®ããŠããã¹ããçæãã | NLP | pipeline(task="text-generation") |
èŠçŽ | ããã¹ããŸãã¯ããã¥ã¡ã³ãã®èŠçŽãçæãã | NLP | pipeline(task="summarization") |
ç»ååé¡ | ç»åã«ã©ãã«ãå²ãåœãŠã | ã³ã³ãã¥ãŒã¿ããžã§ã³ | pipeline(task="image-classification") |
ç»åã»ã°ã¡ã³ããŒã·ã§ã³ | ç»åã®ååå¥ã®ãã¯ã»ã«ã«ã©ãã«ãå²ãåœãŠãïŒã»ãã³ãã£ãã¯ãããããã£ãã¯ãããã³ã€ã³ã¹ã¿ã³ã¹ã»ã°ã¡ã³ããŒã·ã§ã³ããµããŒãïŒ | ã³ã³ãã¥ãŒã¿ããžã§ã³ | pipeline(task="image-segmentation") |
ãªããžã§ã¯ãæ€åº | ç»åå ã®ãªããžã§ã¯ãã®å¢çããã¯ã¹ãšã¯ã©ã¹ãäºæž¬ãã | ã³ã³ãã¥ãŒã¿ããžã§ã³ | pipeline(task="object-detection") |
ãªãŒãã£ãªåé¡ | ãªãŒãã£ãªããŒã¿ã«ã©ãã«ãå²ãåœãŠã | ãªãŒãã£ãª | pipeline(task="audio-classification") |
èªåé³å£°èªè | é³å£°ãããã¹ãã«å€æãã | ãªãŒãã£ãª | pipeline(task="automatic-speech-recognition") |
ããžã¥ã¢ã«ã¯ãšã¹ãã§ã³å¿ç | ç»åãšè³ªåãäžããããå Žåã«ãç»åã«é¢ãã質åã«åçãã | ãã«ãã¢ãŒãã« | pipeline(task="vqa") |
ããã¥ã¡ã³ãã¯ãšã¹ãã§ã³å¿ç | ããã¥ã¡ã³ããšè³ªåãäžããããå Žåã«ãããã¥ã¡ã³ãã«é¢ãã質åã«åçãã | ãã«ãã¢ãŒãã« | pipeline(task="document-question-answering") |
ç»åãã£ãã·ã§ãã³ã° | äžããããç»åã«ãã£ãã·ã§ã³ãçæãã | ãã«ãã¢ãŒãã« | pipeline(task="image-to-text") |
ãŸãã[pipeline
] ã®ã€ã³ã¹ã¿ã³ã¹ãäœæãã䜿çšãããã¿ã¹ã¯ãæå®ããŸãã
ãã®ã¬ã€ãã§ã¯ãã»ã³ãã¡ã³ãåæã®ããã« [pipeline
] ã䜿çšããäŸã瀺ããŸãïŒ
>>> from transformers import pipeline
>>> classifier = pipeline("sentiment-analysis")
[pipeline
]ã¯ãææ
åæã®ããã®ããã©ã«ãã®äºååŠç¿æžã¿ã¢ãã«ãšããŒã¯ãã€ã¶ãããŠã³ããŒãããŠãã£ãã·ã¥ãã䜿çšã§ããããã«ãªããŸãã
ããã§ãclassifier
ã察象ã®ããã¹ãã«äœ¿çšã§ããŸãïŒ
>>> classifier("ç§ãã¡ã¯ð€ Transformersã©ã€ãã©ãªããèŠãã§ããŠãšãŠãå¬ããã§ãã")
[{'label': 'POSITIVE', 'score': 0.9998}]
è€æ°ã®å
¥åãããå Žåã¯ã[pipeline
]ã«å
¥åããªã¹ããšããŠæž¡ããŠãèŸæžã®ãªã¹ããè¿ããŸãïŒ
>>> results = classifier(["ð€ Transformersã©ã€ãã©ãªãã玹ä»ã§ããŠéåžžã«å¬ããã§ãã", "å«ãã«ãªããªãã§ã»ããã§ãã"])
>>> for result in results:
... print(f"label: {result['label']}, ã¹ã³ã¢: {round(result['score'], 4)}")
label: POSITIVE, ã¹ã³ã¢: 0.9998
label: NEGATIVE, ã¹ã³ã¢: 0.5309
[pipeline
]ã¯ãä»»æã®ã¿ã¹ã¯ã«å¯ŸããŠããŒã¿ã»ããå
šäœãç¹°ãè¿ãåŠçããããšãã§ããŸãããã®äŸã§ã¯ãèªåé³å£°èªèãã¿ã¹ã¯ãšããŠéžã³ãŸãããïŒ
>>> import torch
>>> from transformers import pipeline
>>> speech_recognizer = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
ãªãŒãã£ãªããŒã¿ã»ãããããŒãããŸãïŒè©³çŽ°ã«ã€ããŠã¯ð€ Datasets ã¯ã€ãã¯ã¹ã¿ãŒããåç §ããŠãã ããïŒã ããšãã°ãMInDS-14ããŒã¿ã»ãããããŒãããŸãïŒ
>>> from datasets import load_dataset, Audio
>>> dataset = load_dataset("PolyAI/minds14", name="en-US", split="train") # doctest: +IGNORE_RESULT
ããŒã¿ã»ããã®ãµã³ããªã³ã°ã¬ãŒããfacebook/wav2vec2-base-960h
ããã¬ãŒãã³ã°ããããµã³ããªã³ã°ã¬ãŒããšäžèŽããããšã確èªããŠãã ããïŒ
>>> dataset = dataset.cast_column("audio", Audio(sampling_rate=speech_recognizer.feature_extractor.sampling_rate))
"audio"åãåŒã³åºããšããªãŒãã£ãªãã¡ã€ã«ã¯èªåçã«ããŒãããããªãµã³ããªã³ã°ãããŸããæåã®4ã€ã®ãµã³ãã«ããçã®æ³¢åœ¢é åãæœåºããããããã€ãã©ã€ã³ã«ãªã¹ããšããŠæž¡ããŸãã
>>> result = speech_recognizer(dataset[:4]["audio"])
>>> print([d["text"] for d in result])
['I WOULD LIKE TO SET UP A JOINT ACCOUNT WITH MY PARTNER HOW DO I PROCEED WITH DOING THAT', "FONDERING HOW I'D SET UP A JOIN TO HELL T WITH MY WIFE AND WHERE THE AP MIGHT BE", "I I'D LIKE TOY SET UP A JOINT ACCOUNT WITH MY PARTNER I'M NOT SEEING THE OPTION TO DO IT ON THE APSO I CALLED IN TO GET SOME HELP CAN I JUST DO IT OVER THE PHONE WITH YOU AND GIVE YOU THE INFORMATION OR SHOULD I DO IT IN THE AP AN I'M MISSING SOMETHING UQUETTE HAD PREFERRED TO JUST DO IT OVER THE PHONE OF POSSIBLE THINGS", 'HOW DO I FURN A JOINA COUT']
倧èŠæš¡ãªããŒã¿ã»ããã§ãå ¥åã倧ããå ŽåïŒé³å£°ãç»åãªã©ïŒããã¹ãŠã®å ¥åãã¡ã¢ãªã«èªã¿èŸŒã代ããã«ããªã¹ãã§ã¯ãªããžã§ãã¬ãŒã¿ãæž¡ãããšããå§ãã§ãã詳现ã«ã€ããŠã¯ãã€ãã©ã€ã³APIãªãã¡ã¬ã³ã¹ãåç §ããŠãã ããã
Use another model and tokenizer in the pipeline
[pipeline
]ã¯Hubããã®ä»»æã®ã¢ãã«ãå容ã§ããä»ã®ãŠãŒã¹ã±ãŒã¹ã«[pipeline
]ãé©å¿ãããããšã容æã§ããããšãã°ããã©ã³ã¹èªã®ããã¹ããåŠçã§ããã¢ãã«ãå¿
èŠãªå ŽåãHubã®ã¿ã°ã䜿çšããŠé©åãªã¢ãã«ããã£ã«ã¿ãªã³ã°ã§ããŸãããããã®ãã£ã«ã¿ãªã³ã°ãããçµæã¯ããã©ã³ã¹èªã®ããã¹ãã«äœ¿çšã§ããææ
åæçšã«èª¿æŽãããå€èšèªã®BERTã¢ãã«ãè¿ããŸãïŒ
>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
[`AutoModelForSequenceClassification`]ãš[`AutoTokenizer`]ã䜿çšããŠäºååŠç¿æžã¿ã¢ãã«ãšããã«é¢é£ããããŒã¯ãã€ã¶ãããŒãããŸãïŒæ¬¡ã®ã»ã¯ã·ã§ã³ã§`AutoClass`ã«ã€ããŠè©³ãã説æããŸãïŒïŒ
>>> from transformers import AutoTokenizer, AutoModelForSequenceClassification
>>> model = AutoModelForSequenceClassification.from_pretrained(model_name)
>>> tokenizer = AutoTokenizer.from_pretrained(model_name)
以äžã®ã³ãŒãã¯ã[`TFAutoModelForSequenceClassification`]ããã³[`AutoTokenizer`]ã䜿çšããŠãäºååŠç¿æžã¿ã¢ãã«ãšãã®é¢é£ããããŒã¯ãã€ã¶ãããŒãããæ¹æ³ã瀺ããŠããŸãïŒ`TFAutoClass`ã«ã€ããŠã¯æ¬¡ã®ã»ã¯ã·ã§ã³ã§è©³ãã説æããŸãïŒïŒ
>>> from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
>>> model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
>>> tokenizer = AutoTokenizer.from_pretrained(model_name)
æå®ããã¢ãã«ãšããŒã¯ãã€ã¶ã[pipeline
]ã«èšå®ããä»åºŠã¯ãã©ã³ã¹èªã®ããã¹ãã«classifier
ãé©çšã§ããŸãïŒ
>>> classifier = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)
>>> classifier("Nous sommes trÚs heureux de vous présenter la bibliothÚque ð€ Transformers.")
[{'label': '5 stars', 'score': 0.7273}]
ãããããªãã®ãŠãŒã¹ã±ãŒã¹ã«é©ããã¢ãã«ãèŠã€ãããªãå ŽåãäºååŠç¿æžã¿ã¢ãã«ãããªãã®ããŒã¿ã§ãã¡ã€ã³ãã¥ãŒãã³ã°ããå¿ èŠããããŸãã ãã¡ã€ã³ãã¥ãŒãã³ã°ã®æ¹æ³ã«ã€ããŠã¯ããã¡ã€ã³ãã¥ãŒãã³ã°ã®ãã¥ãŒããªã¢ã«ãã芧ãã ããã æåŸã«ããã¡ã€ã³ãã¥ãŒãã³ã°ããäºååŠç¿æžã¿ã¢ãã«ãå ±æããã³ãã¥ããã£ãšå ±æããã§å ±æããããšãæ€èšããŠãã ãããããã«ãããæ©æ¢°åŠç¿ãæ°äž»åããæå©ããã§ããŸãïŒ ð€
AutoClass
[AutoModelForSequenceClassification
] ããã³ [AutoTokenizer
] ã¯ã©ã¹ã¯ãäžèšã§äœ¿çšãã [pipeline
] ãé§åããããã«ååããŠåäœããŸãã
AutoClass ã¯ãäºååŠç¿æžã¿ã¢ãã«ã®ã¢ãŒããã¯ãã£ããã®ååãŸãã¯ãã¹ããèªåçã«ååŸããã·ã§ãŒãã«ããã§ãã
é©å㪠AutoClass
ãéžæããããã«é¢é£ããååŠçã¯ã©ã¹ãéžæããã ãã§æžã¿ãŸãã
åã®ã»ã¯ã·ã§ã³ããã®äŸã«æ»ããAutoClass
ã䜿çšã㊠[pipeline
] ã®çµæãåçŸããæ¹æ³ãèŠãŠã¿ãŸãããã
AutoTokenizer
ããŒã¯ãã€ã¶ã¯ããã¹ããã¢ãã«ã®å ¥åãšããŠäœ¿çšã§ããæ°å€ã®é åã«ååŠçãã圹å²ãæãããŸãã ããŒã¯ãã€ãŒãŒã·ã§ã³ããã»ã¹ã«ã¯ãåèªãã©ã®ããã«åå²ãããããåèªãã©ã®ã¬ãã«ã§åå²ããããšãã£ãå€ãã®ã«ãŒã«ããããŸã ïŒããŒã¯ãã€ãŒãŒã·ã§ã³ã«ã€ããŠã®è©³çŽ°ã¯ ããŒã¯ãã€ã¶ãµããªãŒ ãã芧ãã ããïŒã æãéèŠãªããšã¯ãã¢ãã«ãäºååŠç¿æžã¿ã«ãªã£ããšããšåãããŒã¯ãã€ãŒãŒã·ã§ã³ã«ãŒã«ã䜿çšããããã«ãåãã¢ãã«åã§ããŒã¯ãã€ã¶ãã€ã³ã¹ã¿ã³ã¹åããå¿ èŠãããããšã§ãã
[AutoTokenizer
] ã䜿çšããŠããŒã¯ãã€ã¶ãããŒãããŸãïŒ
>>> from transformers import AutoTokenizer
>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
>>> tokenizer = AutoTokenizer.from_pretrained(model_name)
Pass your text to the tokenizer:
>>> encoding = tokenizer("ç§ãã¡ã¯ð€ Transformersã©ã€ãã©ãªããèŠãã§ããŠãšãŠãå¬ããã§ãã")
>>> print(encoding)
{'input_ids': [101, 11312, 10320, 12495, 19308, 10114, 11391, 10855, 10103, 100, 58263, 13299, 119, 102],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}
ããŒã¯ãã€ã¶ã¯ã次ã®æ å ±ãå«ãèŸæžãè¿ããŸãïŒ
- input_ids: ããŒã¯ã³ã®æ°å€è¡šçŸã
- attention_mask: ã©ã®ããŒã¯ã³ã«ã¢ãã³ã·ã§ã³ãåãããã瀺ããŸãã
ããŒã¯ãã€ã¶ã¯ãŸããå ¥åã®ãªã¹ããåãå ¥ããäžæ§ãªé·ãã®ããããè¿ãããã«ããã¹ããããã£ã³ã°ããã³åãè©°ããããšãã§ããŸãã
>>> pt_batch = tokenizer(
... ["ð€ Transformersã©ã€ãã©ãªããèŠãã§ããŠéåžžã«å¬ããã§ãã", "å«ãã§ã¯ãªãããšãé¡ã£ãŠããŸãã"],
... padding=True,
... truncation=True,
... max_length=512,
... return_tensors="pt",
... )
>>> tf_batch = tokenizer(
... ["We are very happy to show you the ð€ Transformers library.", "We hope you don't hate it."],
... padding=True,
... truncation=True,
... max_length=512,
... return_tensors="tf",
... )
ååŠçãã¥ãŒããªã¢ã«ãã芧ããã ããããŒã¯ãã€ãŒãŒã·ã§ã³ã®è©³çŽ°ãã[AutoImageProcessor
]ã[AutoFeatureExtractor
]ã[AutoProcessor
]ã䜿çšããŠç»åããªãŒãã£ãªãããã³ãã«ãã¢ãŒãã«å
¥åãååŠçããæ¹æ³ã«ã€ããŠè©³ãã説æãããŠããããŒãžãã芧ãã ããã
AutoModel
ð€ Transformersã¯äºååŠç¿æžã¿ã€ã³ã¹ã¿ã³ã¹ãç°¡åã«çµ±äžçã«ããŒãããæ¹æ³ãæäŸããŸãã ããã¯ã[`AutoTokenizer`]ãããŒãããã®ãšåãããã«[`AutoModel`]ãããŒãã§ããããšãæå³ããŸãã ã¿ã¹ã¯ã«é©ãã[`AutoModel`]ãéžæãã以å€ã®éãã¯ãããŸããã ããã¹ãïŒãŸãã¯ã·ãŒã±ã³ã¹ïŒåé¡ã®å Žåã[`AutoModelForSequenceClassification`]ãããŒãããå¿ èŠããããŸãïŒ>>> from transformers import AutoModelForSequenceClassification
>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
>>> pt_model = AutoModelForSequenceClassification.from_pretrained(model_name)
[AutoModel
]ã¯ã©ã¹ã§ãµããŒããããŠããã¿ã¹ã¯ã«é¢ãã詳现ã«ã€ããŠã¯ãã¿ã¹ã¯ã®æŠèŠãåç
§ããŠãã ããã
ä»ãååŠçæžã¿ã®ããããçŽæ¥ã¢ãã«ã«æž¡ããŸããèŸæžãå±éããã ãã§ã**
ãè¿œå ããå¿
èŠããããŸãïŒ
>>> pt_outputs = pt_model(**pt_batch)
ã¢ãã«ã¯ãlogits
å±æ§ã«æçµçãªã¢ã¯ãã£ããŒã·ã§ã³ãåºåããŸãã logits
ã«softmaxé¢æ°ãé©çšããŠç¢ºçãååŸããŸãïŒ
>>> from torch import nn
>>> pt_predictions = nn.functional.softmax(pt_outputs.logits, dim=-1)
>>> print(pt_predictions)
tensor([[0.0021, 0.0018, 0.0115, 0.2121, 0.7725],
[0.2084, 0.1826, 0.1969, 0.1755, 0.2365]], grad_fn=<SoftmaxBackward0>)
ð€ Transformersã¯äºååŠç¿æžã¿ã€ã³ã¹ã¿ã³ã¹ãããŒãããããã®ã·ã³ãã«ã§çµ±äžãããæ¹æ³ãæäŸããŸãã
ããã¯ã[`TFAutoModel`]ã[`AutoTokenizer`]ãããŒãããã®ãšåãããã«ããŒãã§ããããšãæå³ããŸãã
å¯äžã®éãã¯ãã¿ã¹ã¯ã«é©ãã[`TFAutoModel`]ãéžæããããšã§ãã
ããã¹ãïŒãŸãã¯ã·ãŒã±ã³ã¹ïŒåé¡ã®å Žåã[`TFAutoModelForSequenceClassification`]ãããŒãããå¿
èŠããããŸãïŒ
>>> from transformers import TFAutoModelForSequenceClassification
>>> model_name = "nlptown/bert-base-multilingual-uncased-sentiment"
>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(model_name)
詳现ã«ã€ããŠã¯ã[AutoModel
]ã¯ã©ã¹ã§ãµããŒããããŠããã¿ã¹ã¯ã«é¢ããæ
å ±ã¯ãã¿ã¹ã¯ã®æŠèŠãåç
§ããŠãã ããã
次ã«ãååŠçæžã¿ã®ããããçŽæ¥ã¢ãã«ã«æž¡ããŸãããã³ãœã«ããã®ãŸãŸæž¡ãããšãã§ããŸãïŒ
>>> tf_outputs = tf_model(tf_batch)
ã¢ãã«ã¯logits
å±æ§ã«æçµçãªã¢ã¯ãã£ããŒã·ã§ã³ãåºåããŸããlogits
ã«ãœããããã¯ã¹é¢æ°ãé©çšããŠç¢ºçãååŸããŸãïŒ
>>> import tensorflow as tf
>>> tf_predictions = tf.nn.softmax(tf_outputs.logits, axis=-1)
>>> tf_predictions # doctest: +IGNORE_RESULT
ð€ Transformersã®ãã¹ãŠã®ã¢ãã«ïŒPyTorchãŸãã¯TensorFlowïŒã¯ãæçµçãªæŽ»æ§åé¢æ°ïŒsoftmaxãªã©ïŒåã®ãã³ãœã«ãåºåããŸãã æçµçãªæŽ»æ§åé¢æ°ã¯ããã°ãã°æ倱ãšçµåãããŠããããã§ããã¢ãã«ã®åºåã¯ç¹å¥ãªããŒã¿ã¯ã©ã¹ã§ããããã®å±æ§ã¯IDEã§èªåè£å®ãããŸãã ã¢ãã«ã®åºåã¯ãã¿ãã«ãŸãã¯èŸæžã®ããã«åäœããŸãïŒæŽæ°ãã¹ã©ã€ã¹ããŸãã¯æååã§ã€ã³ããã¯ã¹ãä»ããããšãã§ããŸãïŒã ãã®å ŽåãNoneã§ããå±æ§ã¯ç¡èŠãããŸãã
Save a Model
ã¢ãã«ããã¡ã€ã³ãã¥ãŒãã³ã°ãããã[`PreTrainedModel.save_pretrained`]ã䜿çšããŠããŒã¯ãã€ã¶ãšå ±ã«ä¿åã§ããŸãïŒ>>> pt_save_directory = "./pt_save_pretrained"
>>> tokenizer.save_pretrained(pt_save_directory) # doctest: +IGNORE_RESULT
>>> pt_model.save_pretrained(pt_save_directory)
åã³ã¢ãã«ã䜿çšããæºåãã§ãããã[PreTrainedModel.from_pretrained
]ã䜿çšããŠå床ããŒãããŸãïŒ
>>> pt_model = AutoModelForSequenceClassification.from_pretrained("./pt_save_pretrained")
ã¢ãã«ããã¡ã€ã³ãã¥ãŒãã³ã°ãããããã®ããŒã¯ãã€ã¶ã䜿çšããŠã¢ãã«ãä¿åã§ããŸãã[`TFPreTrainedModel.save_pretrained`]ã䜿çšããŸãïŒ
>>> tf_save_directory = "./tf_save_pretrained"
>>> tokenizer.save_pretrained(tf_save_directory) # doctest: +IGNORE_RESULT
>>> tf_model.save_pretrained(tf_save_directory)
ã¢ãã«ãå床䜿çšããæºåãã§ãããã[TFPreTrainedModel.from_pretrained
]ã䜿çšããŠå床ããŒãããŸãïŒ
>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained("./tf_save_pretrained")
ð€ Transformersã®ç¹ã«çŽ æŽãããæ©èœã®äžã€ã¯ãã¢ãã«ãä¿åãããããPyTorchã¢ãã«ãŸãã¯TensorFlowã¢ãã«ãšããŠåããŒãã§ããããšã§ãã from_pt
ãŸãã¯from_tf
ãã©ã¡ãŒã¿ã䜿çšããŠã¢ãã«ããã¬ãŒã ã¯ãŒã¯éã§å€æã§ããŸãïŒ
>>> from transformers import AutoModel
>>> tokenizer = AutoTokenizer.from_pretrained(tf_save_directory)
>>> pt_model = AutoModelForSequenceClassification.from_pretrained(tf_save_directory, from_tf=True)
>>> from transformers import TFAutoModel
>>> tokenizer = AutoTokenizer.from_pretrained(pt_save_directory)
>>> tf_model = TFAutoModelForSequenceClassification.from_pretrained(pt_save_directory, from_pt=True)
Custom model builds
ã¢ãã«ãæ§ç¯æ¹æ³ãå€æŽããã«ã¯ãã¢ãã«ã®èšå®ã¯ã©ã¹ãå€æŽã§ããŸããèšå®ã¯ã¢ãã«ã®å±æ§ãæå®ããŸããäŸãã°ãé ãå±€ã®æ°ãã¢ãã³ã·ã§ã³ãããã®æ°ãªã©ãããã«å«ãŸããŸããã«ã¹ã¿ã èšå®ã¯ã©ã¹ããã¢ãã«ãåæåããéã«ã¯ããŒãããå§ããŸããã¢ãã«ã®å±æ§ã¯ã©ã³ãã ã«åæåãããææ矩ãªçµæãåŸãããã«ã¢ãã«ããã¬ãŒãã³ã°ããå¿ èŠããããŸãã
æåã«[AutoConfig
]ãã€ã³ããŒãããå€æŽãããäºååŠç¿æžã¿ã¢ãã«ãããŒãããŸãã[AutoConfig.from_pretrained
]å
ã§ãå€æŽãããå±æ§ïŒäŸïŒã¢ãã³ã·ã§ã³ãããã®æ°ïŒãæå®ã§ããŸãïŒ
>>> from transformers import AutoConfig
>>> my_config = AutoConfig.from_pretrained("distilbert/distilbert-base-uncased", n_heads=12)
[`AutoModel.from_config`]ã䜿çšããŠã«ã¹ã¿ã èšå®ããã¢ãã«ãäœæããŸãïŒ
>>> from transformers import AutoModel
>>> my_model = AutoModel.from_config(my_config)
ã«ã¹ã¿ã æ§æããã¢ãã«ãäœæããã«ã¯ã[`TFAutoModel.from_config`]ã䜿çšããŸãïŒ
>>> from transformers import TFAutoModel
>>> my_model = TFAutoModel.from_config(my_config)
ã«ã¹ã¿ã ã¢ãŒããã¯ãã£ãäœæã¬ã€ããåç §ããŠãã«ã¹ã¿ã æ§æã®è©³çŽ°æ å ±ã確èªããŠãã ããã
Trainer - PyTorchåãã®æé©åããããã¬ãŒãã³ã°ã«ãŒã
ãã¹ãŠã®ã¢ãã«ã¯æšæºã®torch.nn.Module
ã§ãããããéåžžã®ãã¬ãŒãã³ã°ã«ãŒãã§äœ¿çšã§ããŸãã
ç¬èªã®ãã¬ãŒãã³ã°ã«ãŒããäœæã§ããŸãããð€ Transformersã¯PyTorchåãã«[Trainer
]ã¯ã©ã¹ãæäŸããŠãããåºæ¬çãªãã¬ãŒãã³ã°ã«ãŒãã«å ããŠã
åæ£ãã¬ãŒãã³ã°ãæ··å粟床ãªã©ã®æ©èœã®è¿œå ãè¡ã£ãŠããŸãã
ã¿ã¹ã¯ã«å¿ããŠãéåžžã¯[Trainer
]ã«ä»¥äžã®ãã©ã¡ãŒã¿ãæž¡ããŸãïŒ
-
[
PreTrainedModel
]ãŸãã¯torch.nn.Module
ããå§ããŸãïŒ>>> from transformers import AutoModelForSequenceClassification >>> model = AutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased")
-
[
TrainingArguments
]ã«ã¯ãå€æŽã§ããã¢ãã«ã®ãã€ããŒãã©ã¡ãŒã¿ãå«ãŸããŠãããåŠç¿çãããããµã€ãºããã¬ãŒãã³ã°ãšããã¯æ°ãªã©ãå€æŽã§ããŸããæå®ããªãå Žåãããã©ã«ãå€ã䜿çšãããŸãïŒ>>> from transformers import TrainingArguments >>> training_args = TrainingArguments( ... output_dir="path/to/save/folder/", ... learning_rate=2e-5, ... per_device_train_batch_size=8, ... per_device_eval_batch_size=8, ... num_train_epochs=2, ... )
-
ããŒã¯ãã€ã¶ãç»åããã»ããµãç¹åŸŽéæœåºåšããŸãã¯ããã»ããµã®ãããªååŠçã¯ã©ã¹ãããŒãããŸãïŒ
>>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased")
-
ããŒã¿ã»ãããããŒããã:
>>> from datasets import load_dataset >>> dataset = load_dataset("rotten_tomatoes") # doctest: +IGNORE_RESULT
-
ããŒã¿ã»ãããããŒã¯ã³åããããã®é¢æ°ãäœæããŸãïŒ
>>> def tokenize_dataset(dataset): ... return tokenizer(dataset["text"])
ãã®åŸã[
~datasets.Dataset.map
]ã䜿çšããŠããŒã¿ã»ããå šäœã«é©çšããŸãïŒ>>> dataset = dataset.map(tokenize_dataset, batched=True)
-
ããŒã¿ã»ããããã®äŸã®ããããäœæããããã® [
DataCollatorWithPadding
]ïŒ>>> from transformers import DataCollatorWithPadding >>> data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
次ã«ããããã®ã¯ã©ã¹ã[Trainer
]ã«ãŸãšããŸãïŒ
>>> from transformers import Trainer
>>> trainer = Trainer(
... model=model,
... args=training_args,
... train_dataset=dataset["train"],
... eval_dataset=dataset["test"],
... tokenizer=tokenizer,
... data_collator=data_collator,
... ) # doctest: +SKIP
èšç·Žãéå§ããæºåãã§ãããã[~Trainer.train
]ãåŒã³åºããŠãã¬ãŒãã³ã°ãéå§ããŸãïŒ
>>> trainer.train() # doctest: +SKIP
翻蚳ãèŠçŽãªã©ãã·ãŒã±ã³ã¹éã¢ãã«ã䜿çšããã¿ã¹ã¯ã«ã¯ã代ããã«[Seq2SeqTrainer
]ãš[Seq2SeqTrainingArguments
]ã¯ã©ã¹ã䜿çšããŠãã ããã
[Trainer
]å
ã®ã¡ãœããããµãã¯ã©ã¹åããããšã§ããã¬ãŒãã³ã°ã«ãŒãã®åäœãã«ã¹ã¿ãã€ãºã§ããŸããããã«ãããæ倱é¢æ°ããªããã£ãã€ã¶ãã¹ã±ãžã¥ãŒã©ãªã©ã®æ©èœãã«ã¹ã¿ãã€ãºã§ããŸãããµãã¯ã©ã¹åã§ããã¡ãœããã®äžèŠ§ã«ã€ããŠã¯ã[Trainer
]ãªãã¡ã¬ã³ã¹ãã芧ãã ããã
ãã¬ãŒãã³ã°ã«ãŒããã«ã¹ã¿ãã€ãºããå¥ã®æ¹æ³ã¯ãCallbacksã䜿çšããããšã§ããã³ãŒã«ããã¯ã䜿çšããŠä»ã®ã©ã€ãã©ãªãšçµ±åãããã¬ãŒãã³ã°ã«ãŒããç£èŠããŠé²æç¶æ³ãå ±åãããããã¬ãŒãã³ã°ãæ©æã«åæ¢ãããã§ããŸããã³ãŒã«ããã¯ã¯ãã¬ãŒãã³ã°ã«ãŒãèªäœã«ã¯äœãå€æŽãå ããŸãããæ倱é¢æ°ãªã©ã®ã«ã¹ã¿ãã€ãºãè¡ãå Žåã¯ã[Trainer
]ããµãã¯ã©ã¹åããå¿
èŠããããŸãã
Train with TensorFlow
ãã¹ãŠã®ã¢ãã«ã¯æšæºã®tf.keras.Model
ã§ãããããKeras APIã䜿çšããŠTensorFlowã§ãã¬ãŒãã³ã°ã§ããŸãã
ð€ Transformersã¯ãããŒã¿ã»ãããtf.data.Dataset
ãšããŠç°¡åã«ããŒãã§ããããã«ãã[~TFPreTrainedModel.prepare_tf_dataset
]ã¡ãœãããæäŸããŠãããKerasã®compile
ããã³fit
ã¡ãœããã䜿çšããŠãã¬ãŒãã³ã°ãããã«éå§ã§ããŸãã
-
[
TFPreTrainedModel
]ãŸãã¯tf.keras.Model
ããå§ããŸãïŒ>>> from transformers import TFAutoModelForSequenceClassification >>> model = TFAutoModelForSequenceClassification.from_pretrained("distilbert/distilbert-base-uncased")
-
ããŒã¯ãã€ã¶ãç»åããã»ããµãç¹åŸŽéæœåºåšããŸãã¯ããã»ããµã®ãããªååŠçã¯ã©ã¹ãããŒãããŸãïŒ
>>> from transformers import AutoTokenizer >>> tokenizer = AutoTokenizer.from_pretrained("distilbert/distilbert-base-uncased")
-
ããŒã¿ã»ãããããŒã¯ãã€ãºããããã®é¢æ°ãäœæããŸãïŒ
>>> def tokenize_dataset(dataset): ... return tokenizer(dataset["text"]) # doctest: +SKIP
-
[
~datasets.Dataset.map
]ã䜿çšããŠããŒã¿ã»ããå šäœã«ããŒã¯ãã€ã¶ãé©çšããããŒã¿ã»ãããšããŒã¯ãã€ã¶ã[~TFPreTrainedModel.prepare_tf_dataset
]ã«æž¡ããŸããããããµã€ãºãå€æŽããããŒã¿ã»ãããã·ã£ããã«ããããšãã§ããŸãã>>> dataset = dataset.map(tokenize_dataset) # doctest: +SKIP >>> tf_dataset = model.prepare_tf_dataset( ... dataset["train"], batch_size=16, shuffle=True, tokenizer=tokenizer ... ) # doctest: +SKIP
-
æºåãã§ãããã
compile
ãšfit
ãåŒã³åºããŠãã¬ãŒãã³ã°ãéå§ã§ããŸãã Transformersã¢ãã«ã¯ãã¹ãŠããã©ã«ãã®ã¿ã¹ã¯ã«é¢é£ããæ倱é¢æ°ãæã£ãŠãããããæå®ããªãéããæ倱é¢æ°ãæå®ããå¿ èŠã¯ãããŸããã>>> from tensorflow.keras.optimizers import Adam >>> model.compile(optimizer=Adam(3e-5)) # æ倱é¢æ°ã®åŒæ°ã¯äžèŠã§ãïŒ >>> model.fit(tf
What's next?
ð€ Transformersã®ã¯ã€ãã¯ãã¢ãŒãå®äºããããã¬ã€ãããã§ãã¯ããŠãã«ã¹ã¿ã ã¢ãã«ã®äœæãã¿ã¹ã¯ã®ããã®ãã¡ã€ã³ãã¥ãŒãã³ã°ãã¹ã¯ãªããã䜿çšããã¢ãã«ã®ãã¬ãŒãã³ã°ãªã©ãããå ·äœçãªããšãåŠã¶ããšãã§ããŸããð€ Transformersã®ã³ã¢ã³ã³ã»ããã«ã€ããŠãã£ãšè©³ããç¥ãããå Žåã¯ãã³ã³ã»ããã¥ã¢ã«ã¬ã€ããèªãã§ã¿ãŠãã ããïŒ