ð€ Transformersã«ãããŠå¶çŽä»ãããŒã ãµãŒããçšããããã¹ãçæã®ã¬ã€ã
'ð€ Transformersã®å¶çŽä»ãããŒã ãµãŒãã¬ã€ã'
ã€ã³ãããã¯ã·ã§ã³
ãã®ããã°æçš¿ã§ã¯ããã©ã³ã¹ãã©ãŒããŒã䜿çšããèšèªçæã®ããã®ç°ãªããã³ãŒãã£ã³ã°æ¹æ³ã«ã€ããŠèª¬æããããã°æçš¿ãããã¹ãçææ¹æ³: ãã©ã³ã¹ãã©ãŒããŒã䜿çšããç°ãªããã³ãŒãã£ã³ã°æ¹æ³ãã§èª¬æãããŠããããã«ãèªè ãããŒã ãµãŒãã®ç°ãªãããªã¢ã³ãã䜿çšããããã¹ãçææ¹æ³ã«ç²ŸéããŠããããšãåæãšããŠããŸãã
éåžžã®ããŒã ãµãŒããšã¯ç°ãªããå¶çŽä»ãããŒã ãµãŒãã§ã¯ããã¹ãçæã®åºåã«å¯ŸããŠå¶åŸ¡ãè¡ãããšãã§ããŸããããã¯ãåºåå ã«æ£ç¢ºã«äœãå«ãããããç¥ã£ãŠããå Žåã«åœ¹ç«ã¡ãŸããããšãã°ããã¥ãŒã©ã«æ©æ¢°ç¿»èš³ã®ã¿ã¹ã¯ã§ã¯ãèŸæžæ€çŽ¢ã䜿çšããŠæçµçãªç¿»èš³ã«å«ãŸããå¿ èŠãããåèªãç¥ã£ãŠãããããããŸãããèšèªã¢ãã«ã«ãšã£ãŠã»ãŒåããããå¯èœæ§ãããçæåºåã§ããç¹å®ã®æèã«ãããŠãšã³ããŠãŒã¶ãŒã«ãšã£ãŠã¯åããããæãŸãããªãå ŽåããããŸãããããã®ç¶æ³ã¯ããŠãŒã¶ãŒãã¢ãã«ã«æçµåºåã«å«ãŸããå¿ èŠã®ããåèªãæ瀺ããããšã§è§£æ±ºã§ããŸãã
ãªãé£ããã®ã
ããããããã¯éåžžã«éèªæãªåé¡ã§ããããã¯ãçæãããããã¹ãã®æçµåºåã®ã©ããã§ãç¹å®ã®éšåæååã®çæã匷å¶ããå¿ èŠãããããã§ãã
- Hugging Face TransformersãšAWS Inferentiaã䜿çšããŠãBERTæšè«ãé«éåãã
- Hugging Faceã§ã®Decision Transformersã®çŽ¹ä» ð€
- ~èªåèªèº«ã~ ç¹°ãè¿ããªã
äŸãã°ãããŒã¯ã³ t 1 , t 2 t_1, t_2 t 1 â , t 2 â ãé çªã«å«ãæ S
ãçæããããšããŸããäºæž¬ãããæ S
ã¯æ¬¡ã®ããã«å®çŸ©ãããŸãïŒ
S e x p e c t e d = { s 1 , s 2 , . . . , s k , t 1 , t 2 , s k + 1 , . . . , s n } S_{expected} = \{ s_1, s_2, …, s_k, t_1, t_2, s_{k+1}, …, s_n \} S e x p e c t e d â = { s 1 â , s 2 â , . . . , s k â , t 1 â , t 2 â , s k + 1 â , . . . , s n â }
åé¡ã¯ãããŒã ãµãŒããããŒã¯ã³ã1ã€ãã€çæããããšã§ããããŒã ãµãŒããé¢æ° B ( s 0 : i ) = s i + 1 B(\mathbf{s}_{0:i}) = s_{i+1} B ( s 0 : i â ) = s i + 1 â ãšèããããšãã§ããŸãããã®é¢æ°ã¯ãçŸåšçæãããŠããããŒã¯ã³ã®ã·ãŒã±ã³ã¹ s 0 : i s_{0:i} s 0 : i â ïŒ0 0 0 ãã i i i ãŸã§ïŒãèŠãŠã次ã®ããŒã¯ã³ã i + 1 i+1 i + 1 ã§äºæž¬ããŸããããããä»»æã®ã¹ããã i < k i < k i < k ã§ããã®é¢æ°ãããŒã¯ã³ãå°æ¥ã®ã¹ããã k k k ã§çæãããå¿ èŠãããããšãã©ã®ããã«ç¥ãããšãã§ããŸããïŒãŸããã¹ããã i = k i=k i = k ã®å ŽåããããããŒã¯ã³ã匷å¶ããã®ã«æé©ãªå Žæã§ãããå°æ¥ã®ã¹ããã i > k i>k i > k ã§ã¯ãªãããšã確å®ã«ç¥ãããšãã§ããŸããïŒ
ããã«ãç°ãªãèŠä»¶ãæã€è€æ°ã®å¶çŽãããå Žåã¯ã©ããªããŸããïŒãã¬ãŒãº p 1 = { t 1 , t 2 } p_1=\{ t_1, t_2 \} p 1 â = { t 1 â , t 2 â } ãšãã¬ãŒãº p 2 = { t 3 , t 4 , t 5 , t 6 } p_2=\{ t_3, t_4, t_5, t_6\} p 2 â = { t 3 â , t 4 â , t 5 â , t 6 â } ã匷å¶ãããå Žåã¯ã©ããªããŸããïŒ2ã€ã®ãã¬ãŒãºã®éã§éžæããããå Žåã¯ã©ããªããŸããïŒãã¬ãŒãº p 1 p_1 p 1 â ã匷å¶ãããã¬ãŒãºã®ãªã¹ã { p 21 , p 22 , p 23 } \{p_{21}, p_{22}, p_{23}\} { p 2 1 â , p 2 2 â , p 2 3 â } ã®äžãã1ã€ã®ãã¬ãŒãºã匷å¶ãããå Žåã¯ã©ããªããŸããïŒ
äžèšã®äŸã¯å®éã«éåžžã«åççãªãŠãŒã¹ã±ãŒã¹ã§ãã以äžã§ç€ºãããããã«ãæ°ããå¶çŽä»ãããŒã ãµãŒãæ©èœã§ã¯ãããããã¹ãŠãå¯èœã«ãªããŸãïŒ
ãã®æçš¿ã§ã¯ãæ°ããå¶çŽä»ãããŒã ãµãŒãæ©èœãããªãã«äœãã§ããããç°¡åã«èª¬æãããã®ä»çµã¿ã«ã€ããŠãã詳现ã«èª¬æããŸãã
äŸ1ïŒåèªã匷å¶ãã
ãHow old are you?ãããã€ãèªã«ç¿»èš³ããããšããŠãããšããŸãã
éå ¬åŒãªå Žé¢ã§ã¯ãWie alt bist du?ããšèšããŸãããå ¬åŒãªå Žé¢ã§ã¯ãWie alt sind Sie?ããšèšããŸãã
ãããŠãæèã«ãã£ãŠã¯ãäžæ¹ã®åœ¢åŒã®æ¹ãä»æ¹ããã奜ãŸããå ŽåããããŸãããã¢ãã«ã«ãããäŒããã«ã¯ã©ãããã°ããã§ããããïŒ
åŸæ¥ã®ããŒã ãµãŒã
以äžã¯ãåŸæ¥ã®ããŒã ãµãŒãèšå®ã§ã®ããã¹ã翻蚳ã®æ¹æ³ã§ãã
!pip install -q git+https://github.com/huggingface/transformers.git
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("t5-base")
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
encoder_input_str = "translate English to German: How old are you?"
input_ids = tokenizer(encoder_input_str, return_tensors="pt").input_ids
outputs = model.generate(
input_ids,
num_beams=10,
num_return_sequences=1,
no_repeat_ngram_size=1,
remove_invalid_values=True,
)
print("Output:\n" + 100 * '-')
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Output:
----------------------------------------------------------------------------------------------------
Wie alt bist du?
å¶çŽä»ãããŒã ãµãŒãã䜿çšããå Žå
ããããéå ¬åŒãªãã®ã§ã¯ãªãå ¬åŒãªåºåãæããšããã£ãŠããå Žåã¯ã©ãã§ããããïŒä»¥åã¯ãããã€ãã®å¯èœãªåºåãçæããèŠä»¶ã«åããã®ããã£ã«ã¿ãªã³ã°ããå¿ èŠããããŸãããããããä»ã§ã¯çæã¹ããŒãžã§ãããè¡ãããšãã§ããŸãã
model.generate()
ã® force_words_ids
ããŒã¯ãŒãåŒæ°ã䜿çšãããšã次ã®ãããªããšãå¯èœã§ãïŒ
tokenizer = AutoTokenizer.from_pretrained("t5-base")
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
encoder_input_str = "translate English to German: How old are you?"
force_words = ["Sie"]
input_ids = tokenizer(encoder_input_str, return_tensors="pt").input_ids
force_words_ids = tokenizer(force_words, add_special_tokens=False).input_ids
outputs = model.generate(
input_ids,
force_words_ids=force_words_ids,
num_beams=5,
num_return_sequences=1,
no_repeat_ngram_size=1,
remove_invalid_values=True,
)
print("Output:\n" + 100 * '-')
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Output:
----------------------------------------------------------------------------------------------------
Wie alt sind Sie?
ã芧ã®ããã«ãæãŸããåºåã«é¢ããäºåç¥èã䜿çšããŠçæãèªå°ããããšãã§ããŸããã以åã¯ãå¯èœãªåºåãçæãããã®åŸãèŠä»¶ã«åããã®ããã£ã«ã¿ãªã³ã°ããå¿ èŠããããŸãããããããä»ã§ã¯çæã¹ããŒãžã§ãããè¡ãããšãã§ããŸãã
äŸ2ïŒéžæå¶çŽ
åè¿°ã®ããã«ãæçµçãªåºåã«å«ãŸããŠã»ããåèªãäºåã«ç¥ã£ãŠãããŠãŒã¹ã±ãŒã¹ãæããŸãããããã¯ããã¥ãŒã©ã«æ©æ¢°ç¿»èš³äžã«èŸæžæ€çŽ¢ã䜿çšããå Žåã®äŸã§ãã
ããããåºåã«äœ¿çšããåèªåœ¢åŒãç¥ããªãå ŽåãäŸãã° ["raining", "rained", "rains", ...]
ã®ãããªåºåãåããããå¯èœã§ããå Žåã¯ã©ãã§ããããïŒäžè¬çã«ã¯ãæåéãã®åèªã ãã§ãªããé¢é£ããä»ã®å¯èœæ§ãèæ
®ããå ŽåããããŸãã
ãã®ãããªæ¯ãèããèš±å¯ããå¶çŽã¯ãéžæå¶çŽ ã§ãããŠãŒã¶ãŒã¯ãçæã®éã«æçµçãªåºåã«å«ãŸããå¿ èŠãããåèªã®ãªã¹ããå ¥åããããšãã§ããŸãã
以äžã¯ãäžèšã®2ã€ã®å¶çŽã¿ã€ããçµã¿åãããäŸã§ãïŒ
from transformers import GPT2LMHeadModel, GPT2Tokenizer
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
force_word = "scared"
force_flexible = ["scream", "screams", "screaming", "screamed"]
force_words_ids = [
tokenizer([force_word], add_prefix_space=True, add_special_tokens=False).input_ids,
tokenizer(force_flexible, add_prefix_space=True, add_special_tokens=False).input_ids,
]
starting_text = ["The soldiers", "The child"]
input_ids = tokenizer(starting_text, return_tensors="pt").input_ids
outputs = model.generate(
input_ids,
force_words_ids=force_words_ids,
num_beams=10,
num_return_sequences=1,
no_repeat_ngram_size=1,
remove_invalid_values=True,
)
print("Output:\n" + 100 * '-')
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
print(tokenizer.decode(outputs[1], skip_special_tokens=True))
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.
Output:
----------------------------------------------------------------------------------------------------
The soldiers, who were all scared and screaming at each other as they tried to get out of the
The child was taken to a local hospital where she screamed and scared for her life, police said.
ã芧ã®éããæåã®åºåã§ã¯"screaming"
ã䜿çšããã2çªç®ã®åºåã§ã¯"screamed"
ã䜿çšãããäž¡æ¹ã®åºåã§"scared"
ããã®ãŸãŸäœ¿çšãããŠããŸããéžæãªã¹ã["screaming", "screamed", ...]
ã¯åèªåœ¢åŒã§ããå¿
èŠã¯ãããŸããããã®ãªã¹ãã¯ãåã«ãªã¹ããã1ã€ã ãå¿
èŠãªå Žåã«äœ¿çšã§ããŸãã
åŸæ¥ã®ããŒã ãµãŒã
以äžã¯ã以åã®ããã°èšäºããåŒçšããåŸæ¥ã®ããŒã ãµãŒãã®äŸã§ãã
ã°ãªãŒãã£ãµãŒããšã¯ç°ãªããããŒã ãµãŒãã¯ããé·ã仮説ã®ãªã¹ããä¿æããããšã«ãã£ãŠæ©èœããŸããäžèšã®ç»åã§ã¯ãåã¹ãããã§çæå¯èœãªæ¬¡ã®ããŒã¯ã³ã3ã€è¡šç€ºããŠããŸãã
以äžã®äŸã§ã¯ãnum_beams=3
ã®å Žåã®ããŒã ãµãŒãã®æåã®ã¹ããããå¥ã®èŠç¹ããèŠãŠã¿ãŸãããã
貪欲ãªæ€çŽ¢ãè¡ãããã«"The dog"
ã®ã¿ãéžæããã®ã§ã¯ãªããããŒã ãµãŒãã§ã¯"The nice"
ãš"The car"
ã®èæ
®ã蚱容ãããŸãã
次ã®ã¹ãããã§ã¯ãåã®ã¹ãããã§äœæãã3ã€ã®æããããã«å¯ŸããŠæ¬¡ã®å¯èœãªããŒã¯ã³ãèæ ®ããŸãã
num_beams
以äžã®åºåãèæ
®ããããšã«ãªããŸãããã¹ãããã®æåŸã«ããããnum_beams
ã«æžãããŸããç¡å¶éã«æåãããç¶ããããšã¯ã§ããŸãããæåãããããã³ã«è¿œè·¡ããå¿
èŠã®ããbeams
ã®æ°ã¯ãéåžžã«æ©ãéåžžã«å€§ãããªããŸãïŒ10ã¹ãããåŸã«ã¯10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000ã®10,000,000,000
éè¡ã¯ãå¶çŽãæºããããšãšåççãªåºåãäœæããããšã®ãã©ã³ã¹ãäœãåºãããšã§ããã®åé¡ã解決ããŸãã
éè¡ n n n ã¯ãå¶çŽãæºããããã« n n n ã¹ãããé²ãã ããŒã ã®ãªã¹ããæããŸãããã¹ãŠã®å¯èœãªããŒã ãåéè¡ã«åé¡ããåŸãã©ãŠã³ãããã³éžæãè¡ããŸããäžèšã®äŸã§ã¯ãBank 2 ããæã確çã®é«ãåºåã次㫠Bank 1 ããæã確çã®é«ãåºåãBank 0 ãã 1 ã€ãBank 2 ãã 2 çªç®ã«ç¢ºçã®é«ãåºåãBank 1 ãã 2 çªç®ã«ç¢ºçã®é«ãåºåããšããããã«éžæããŸãã num_beams=3
ã䜿çšããŠãããããäžèšã®ããã»ã¹ã 3 åç¹°ãè¿ãããšã§ã["The is fast", "The dog is", "The dog and"]
ã«ãªããŸãã
ãã®ããã«ãæåã§æãŸããããŒã¯ã³ãè¿œå ããæãèæ
®ããããã«ã¢ãã«ã«åŒ·å¶ããäžæ¹ã§ãããæå³ã®ããã·ãŒã±ã³ã¹ã远跡ããããšãã§ããŸãã "The is fast"
ã¯å¶çŽãå®å
šã«æºãããŠããŸãããéåžžã«æå³ã®ãããã¬ãŒãºã§ã¯ãããŸããã幞ãã«ããæªæ¥ã®ã¹ãããã§äœæ¥ããããã« "The dog is"
ãš "The dog and"
ã䜿çšã§ããŸããããã«ãããåŸã®æ®µéã§ããæå³ã®ããåºåã«ã€ãªããããšãæåŸ
ããŠããŸãã
ãã®åäœã¯ãäžèšã®äŸã®ç¬¬ 3 ã¹ãããã§ç€ºãããŠããŸã:
"The is fast"
ã¯æåã§å¶çŽããŒã¯ã³ãè¿œå ããå¿
èŠããªããããæ¢ã«æºããããŠããããšã«æ³šæããŠãã ããïŒã€ãŸãããã¬ãŒãº "is fast"
ãå«ãã§ããŸãïŒããŸãã"The dog is slow"
ã "The dog is mad"
ã®ãããªããŒã ã¯ãå®éã«ã¯ Bank 0 ã«ãããŸãããªããªããããŒã¯ã³ "is"
ãå«ãã§ããã«ããããããã"is fast"
ãçæããããã«æåããããçŽãå¿
èŠãããããã§ãã "is"
ã®åŸã« "slow"
ã®ãããªãã®ãè¿œå ããããšã§ãå¹æçã«é²æããªã»ãããããŸãã
ãããŠæåŸã«ãå¶çŽãã¬ãŒãºãå«ãæå³ã®ããåºåã«ãã©ãçããããšã«æ³šæããŠãã ãã: "The dog is fast"
ã§ãã
æå㯠"The is fast"
ã®ãããªæå³ã®ãªããã¬ãŒãºãåŸãããå¯èœæ§ããããããå¿é
ããŠããŸãããããããéè¡ããã®ã©ãŠã³ãããã³éžæã䜿çšããããšã§ãããæå³ã®ããåºåãåªå
ããããã«æå³ã®ãªãåºåãæé»çã«åãé€ãããšãã§ããŸããã
Constraint
ã¯ã©ã¹ãšã«ã¹ã¿ã å¶çŽã«ã€ããŠã®è©³çŽ°
説æããåŸãããäž»ãªãã€ã³ãã¯æ¬¡ã®ããã«èŠçŽããããšãã§ããŸããåã¹ãããã§ãå¶çŽãæºããããŒã¯ã³ãèæ ®ããããã«ã¢ãã«ã«èŠæ±ãç¶ããªãããèŠæ±ãæºãããªãããŒã ã远跡ããæçµçã«æãŸãããã¬ãŒãºãå«ãæ¯èŒçé«ã確çã®ã·ãŒã±ã³ã¹ã«å°éããŸãã
ãããã£ãŠããã®å®è£
ãèšèšããããã®ååçãªæ¹æ³ã¯ãåå¶çŽã Constraint
ãªããžã§ã¯ããšããŠè¡šçŸãããã®é²æã远跡ããããŒã ãµãŒãã«æ¬¡ã«çæããããŒã¯ã³ãäŒããããšã§ããã model.generate()
ã®ããŒã¯ãŒãåŒæ° force_words_ids
ãæäŸããŠããŸãããããã¯ãšã³ãã§å®éã«è¡ãããããšã¯æ¬¡ã®ãšããã§ã:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, PhrasalConstraint
tokenizer = AutoTokenizer.from_pretrained("t5-base")
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
encoder_input_str = "translate English to German: How old are you?"
constraints = [
PhrasalConstraint(
tokenizer("Sie", add_special_tokens=False).input_ids
)
]
input_ids = tokenizer(encoder_input_str, return_tensors="pt").input_ids
outputs = model.generate(
input_ids,
constraints=constraints,
num_beams=10,
num_return_sequences=1,
no_repeat_ngram_size=1,
remove_invalid_values=True,
)
print("Output:\n" + 100 * '-')
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
Output:
----------------------------------------------------------------------------------------------------
Wie alt sind Sie?
èªåã§å®çŸ©ããŠãŠããŒã¯ãªå¶çŽãèšèšããããã«ãconstraints
ããŒã¯ãŒãåŒæ°ã«ãããå
¥åããããšãã§ããŸãã Constraint
ã®æœè±¡ã€ã³ã¿ãŒãã§ãŒã¹ã¯ã©ã¹ã®ãµãã¯ã©ã¹ãäœæãããã®èŠä»¶ã«åŸã£ãŠãã ããã Constraint
ã®å®çŸ©ã«é¢ãã詳现ãªæ
å ±ã¯ããã¡ãã§èŠã€ããããšãã§ããŸãã
äžéšæªå®è£
ã®ãŠããŒã¯ãªã¢ã€ãã¢ïŒãè©Šãããã ãããããããŸããïŒïŒã«ã¯ãOrderedConstraints
ãTemplateConstraints
ãªã©ã®å¶çŽãè¿œå ãããå¯èœæ§ããããŸããçŸåšãçæã¯åºåã®ã©ãã«ã§ãã·ãŒã±ã³ã¹ãå«ããããšã§å®çŸãããŠããŸããäŸãã°ã以åã®äŸã§ã¯ãscared -> screamingã®1ã€ã®ã·ãŒã±ã³ã¹ãšãscreamed -> scaredã®ãã1ã€ã®ã·ãŒã±ã³ã¹ããããŸããã OrderedConstraints
ã§ã¯ããããã®å¶çŽãå®çŸãããé åºããŠãŒã¶ãŒãæå®ããããšãã§ããŸãã
TemplateConstraints
ã§ã¯ãããããããªäœ¿ãæ¹ãå¯èœã§ãç®çã¯æ¬¡ã®ãããªãã®ã«ãªããŸã:
starting_text = "The woman"
template = ["the", "", "School of", "", "in"]
possible_outputs == [
"The woman attended the Ross School of Business in Michigan.",
"The woman was the administrator for the Harvard School of Business in MA."
]
ãŸãã¯:
starting_text = "The woman"
template = ["the", "", "", "University", "", "in"]
possible_outputs == [
"The woman attended the Carnegie Mellon University in Pittsburgh.",
]
impossible_outputs == [
"The woman attended the Harvard University in MA."
]
ãããã¯ããŠãŒã¶ãŒã2ã€ã®åèªã®éã«æ¿å
¥ã§ããããŒã¯ã³ã®æ°ã«é¢å¿ãæããªãå ŽåãOrderedConstraint
ã䜿çšããããšãã§ããŸãã
çµè«
å¶çŽä»ãããŒã ãµãŒãã¯ãããã¹ãçæã«å€éšã®ç¥èãèŠä»¶ãæè»ã«æ³šå
¥ããæ段ãæäŸããŸãã以åã¯ãã¢ãã«ã«å¯ŸããŠä»¥äžã®æ瀺ãç°¡åã«äŒããæ¹æ³ããããŸããã§ããïŒ1. ã·ãŒã±ã³ã¹ã®ãªã¹ããå«ããããšã2. ãã®ãã¡ã®ããã€ãã¯ãªãã·ã§ã³ã§ãããããã€ãã¯å¿
é ã§ããã3. ããããã·ãŒã±ã³ã¹å
ã®é©åãªäœçœ®ã«çæãããããã«ããããšãä»ã§ã¯ãConstraint
ãªããžã§ã¯ãã®ããŸããŸãªãµãã¯ã©ã¹ã®çµã¿åããã§ãçæã«å®å
šãªå¶åŸ¡ãå¯èœã§ãïŒ
ãã®æ°æ©èœã¯ãäž»ã«ä»¥äžã®è«æã«åºã¥ããŠããŸãïŒ
- Guided Open Vocabulary Image Captioning with Constrained Beam Search
- Fast Lexically Constrained Decoding with Dynamic Beam Allocation for Neural Machine Translation
- Improved Lexically Constrained Decoding for Translation and Monolingual Rewriting
- Guided Generation of Cause and Effect
äžèšã®ããã«ãå€ãã®æ°ããç 究è«æã§ã¯ãå€éšã®ç¥èïŒäŸïŒKGãKBïŒã䜿çšããŠå€§èŠæš¡ãªæ·±å±€åŠç¿ã¢ãã«ã®åºåãèªå°ããæ¹æ³ãæ¢çŽ¢ããŠããŸãããã®å¶çŽä»ãããŒã ãµãŒãæ©èœããã®ç®çãéæããããã®ããäžã€ã®å¹æçãªæ段ãšãªãããšãé¡ã£ãŠããŸãã
ãã®æ©èœã®è²¢ç®ã«ã¬ã€ãã³ã¹ãæäŸããŠããããã¹ãŠã®æ¹ã ã«æè¬ããŸãïŒåæã®åé¡ããæçµPRãŸã§é¢äžããŠãããPatrick von Platenæ°ãããã³ã³ãŒãã«å¯Ÿãã詳现ãªãã£ãŒãããã¯ãæäŸããŠãããNarsil Patryæ°ã
ãã®æçš¿ã®ãµã ãã€ã«ã¯ã以äžã®å±æ§ãæã€ã¢ã€ã³ã³ã䜿çšããŠããŸãïŒShorthand icons created by Freepik – Flaticon
We will continue to update VoAGI; if you have any questions or suggestions, please contact us!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- Habana LabsãšHugging FaceãææºããTransformerã¢ãã«ã®ãã¬ãŒãã³ã°ãå éåãã
- CO2æåºéãšð€ããïŒãªãŒãã£ã³ã°ã»ã¶ã»ãã£ãŒãž
- æè²ã®ããã®Hugging Faceãã玹ä»ããŸã ð€
- Habana Gaudiã§ã®Transformersã®å§ãæ¹
- KiliãšHuggingFace AutoTrainã䜿çšããæèŠåé¡
- Hugging Faceãããžãfastaiãããæè¿ããŸã
- æé©ãªãã€ãã©ã€ã³ãšãã©ã³ã¹ãã©ãŒããŒãã€ãã©ã€ã³ã«ããé«éæšè«