π Docs >> Using Transformers >> Summary of the tasks
μ΄ νμ΄μ§μλ λΌμ΄λΈλ¬λ¦¬ μ¬μ© μ κ°μ₯ λ§μ΄ μ μ©λλ μ¬λ‘κ° μκ°λμ΄ μμ΅λλ€. νκΉ νμ΄μ€ νΈλμ€ν¬λ¨Έμ λͺ¨λΈλ€μ λ€μν ꡬμ±κ³Ό μ¬μ© μ¬λ‘λ₯Ό μ§μν©λλ€. κ°μ₯ κ°λ¨ν κ²μ μ§λ¬Έ λ΅λ³(question answering), μνμ€ λΆλ₯(sequence classification), κ°μ²΄λͺ μΈμ(named entity recognition) λ±κ³Ό κ°μ μμ μ λν μ¬λ‘λ€μ λλ€.
μ΄λ¬ν μμ μμλ μ€ν λͺ¨λΈ(auto-models)μ νμ©ν©λλ€. μ€ν λͺ¨λΈμ μ£Όμ΄μ§ 체ν¬ν¬μΈνΈμ λ°λΌ λͺ¨λΈμ μΈμ€ν΄μ€ννκ³ μ¬λ°λ₯Έ λͺ¨λΈ μν€ν μ²λ₯Ό μλμΌλ‘ μ ννλ ν΄λμ€μ λλ€. μμΈν λ΄μ©μ AutoModel λ¬Έμλ₯Ό μ°Έμ‘°νμμμ€. λ¬Έμλ₯Ό μ°Έμ‘°νμ¬ μ½λλ₯Ό λ ꡬ체μ μΌλ‘ μμ νκ³ , νΉμ μ¬μ© μ¬λ‘μ λ§κ² μμ λ‘κ² μ‘°μ ν μ μμ΅λλ€.
λͺ¨λΈμ΄ μ μ€νλλ €λ©΄ ν΄λΉ νμ€ν¬μ ν΄λΉνλ 체ν¬ν¬μΈνΈμμ λ‘λλμ΄μΌ ν©λλ€. μ΄λ¬ν 체ν¬ν¬μΈνΈλ μΌλ°μ μΌλ‘ λκ·λͺ¨ λ°μ΄ν° μ§ν©μ μ¬μ©νμ¬ ν리νΈλ μΈλκ³ νΉμ νμ€ν¬μ λν΄ νμΈνλ λ©λλ€. μ΄λ μλμ κ°μ΅λλ€.
- λͺ¨λ λͺ¨λΈμ΄ λͺ¨λ νμ€ν¬μ λν΄ νμΈνλλ κ²μ μλλλ€. νΉμ νμ€ν¬μμ λͺ¨λΈμ νμΈνλνλ €λ©΄ μμ λλ ν 리μ run_$TASK.pyμ€ν¬λ¦½νΈλ₯Ό νμ©ν μ μμ΅λλ€.
- νμΈνλλ λͺ¨λΈμ νΉμ λ°μ΄ν°μ μ μ¬μ©νμ¬ νμΈνλλμμ΅λλ€. μ΄ λ°μ΄ν°μ μ μ¬μ© μμ λ° λλ©μΈκ³Ό κ΄λ ¨μ΄ μμ μ μμ§λ§, κ·Έλ μ§ μμ μλ μμ΅λλ€. μμ μΈκΈνλ―μ΄ μμ μ€ν¬λ¦½νΈλ₯Ό νμ©νμ¬ λͺ¨λΈμ νμΈνλνκ±°λ λͺ¨λΈ νμ΅μ μ¬μ©ν μ€ν¬λ¦½νΈλ₯Ό μ§μ μμ±ν μ μμ΅λλ€.
μΆλ‘ νμ€ν¬λ₯Ό μν΄ λΌμ΄λΈλ¬λ¦¬μμ λͺ κ°μ§ λ©μ»€λμ¦μ μ¬μ©ν μ μμ΅λλ€.
- νμ΄νλΌμΈ : μ¬μ©νκΈ° λ§€μ° μ¬μ΄ λ°©μμΌλ‘, λ μ€μ μ½λλ‘ μ¬μ©μ΄ κ°λ₯ν©λλ€.
- μ§μ λͺ¨λΈ μ¬μ©νκΈ° : μΆμνκ° λ λμ§λ§, ν ν¬λμ΄μ (νμ΄ν μΉ/ν μνλ‘μ°)μ μ§μ μ‘μΈμ€ν μ μλ€λ μ μμ μ μ°μ±κ³Ό μ±λ₯μ΄ ν₯μλ©λλ€.
μ¬κΈ°μ λ κ°μ§ μ κ·Ό λ°©μμ΄ λͺ¨λ μ μλμ΄ μμ΅λλ€.
π μ£Όμ
μ¬κΈ°μ μ μλ λͺ¨λ νμ€ν¬μμλ νΉμ νμ€ν¬μ λ§κ² νμΈνλλ ν리νΈλ μΈ μ²΄ν¬ν¬μΈνΈλ₯Ό νμ©ν©λλ€. νΉμ μμ μμ νμΈνλ λμ§ μμ 체ν¬ν¬μΈνΈλ₯Ό λ‘λνλ©΄ νμ€ν¬μ μ¬μ©λλ μΆκ° ν€λκ° μλ κΈ°λ³Έ νΈλμ€ν¬λ¨Έ λ μ΄μ΄λ§ λ‘λλμ΄ ν΄λΉ ν€λμ κ°μ€μΉκ° 무μμλ‘ μ΄κΈ°νλ©λλ€. μ΄λ κ² νλ©΄ λλ€μΌλ‘ μΆλ ₯μ΄ μμ±λ©λλ€.
μνμ€ λΆλ₯(Sequence Classification)
μνμ€ λΆλ₯λ μ£Όμ΄μ§ ν΄λμ€ μμ λ°λΌ μνμ€λ₯Ό λΆλ₯νλ νμ€ν¬μ λλ€. μνμ€ λΆλ₯μ μμλ‘λ μ΄ νμ€ν¬λ₯Ό κΈ°λ°μΌλ‘ νλ GLUE λ°μ΄ν°μ μ΄ μμ΅λλ€. GLUE μνμ€ λΆλ₯ νμ€ν¬μμ λͺ¨λΈμ νμΈνλ νλ €λ©΄ run_glue.py, run_tf_glue.py, run_tf_classification.py λλ run_xnli.py μ€ν¬λ¦½νΈλ₯Ό νμ©ν μ μμ΅λλ€.
λ€μμ νμ΄νλΌμΈμ μ¬μ©νμ¬ μνμ€κ° κΈμ μΈμ§ λΆμ μΈμ§λ₯Ό μλ³νμ¬ κ°μ±λΆμμ μννλ μμ λλ€. GLUE νμ€ν¬μΈ sst2μμ νμΈνλλ λͺ¨λΈμ νμ©ν©λλ€.
μ΄λ κ² νλ©΄ λ€μκ³Ό κ°μ΄ μ€μ½μ΄μ ν¨κ» λΌλ²¨(POSITIVE-κΈμ or NEGATIVE-λΆμ )μ΄ λ°νλ©λλ€.
from transformers import pipeline
classifier = pipeline("sentiment-analysis")
result = classifier("I hate you")[0]
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
result = classifier("I love you")[0]
print(f"label: {result['label']}, with score: {round(result['score'], 4)}")
λ€μμ λͺ¨λΈμ μ¬μ©νμ¬ λ μνμ€κ° μλ‘ κ°μ μλ―Έμ λ€λ₯Έ λ¬Έμ₯μΈμ§μ μ¬λΆ(paraphrase or not)λ₯Ό κ²°μ νλ μνμ€ λΆλ₯μ μμ λλ€. νλ‘μΈμ€λ λ€μκ³Ό κ°μ΅λλ€.
- 체ν¬ν¬μΈνΈ μ΄λ¦μμ ν ν¬λμ΄μ λ° λͺ¨λΈμ μΈμ€ν΄μ€νν©λλ€. λͺ¨λΈμ BERT λͺ¨λΈλ‘μ μλ³λλ©° 체ν¬ν¬μΈνΈμ μ μ₯λ κ°μ€μΉλ‘ λ‘λλ©λλ€.
- μ¬λ°λ₯Έ λͺ¨λΈλ³ κ΅¬λΆ κΈ°νΈ, ν ν° μ ν ID λ° μ΄ν μ λ§μ€ν¬(ν ν¬λμ΄μ μ μν΄ μλμΌλ‘ μμ±λ¨)λ₯Ό μ¬μ©νμ¬ λ λ¬Έμ₯μ μνμ€λ₯Ό μμ±ν©λλ€.
- λͺ¨λΈμ ν΅ν΄ μ΄ μνμ€λ₯Ό μ λ¬νκ³ μ¬μ© κ°λ₯ν λ ν΄λμ€ μ€ νλμΈ 0(no paraphrase)κ³Ό 1(paraphrase) μ€ νλλ‘ λΆλ₯ν©λλ€.
- ν΄λμ€ λΆλ₯μ λν νλ₯ μ κ³μ°νκΈ° μν΄ κ²°κ³Όμ μννΈλ§₯μ€ ν¨μλ₯Ό μ μ©νμ¬ κ³μ°ν©λλ€.
- κ²°κ³Όλ₯Ό νλ¦°νΈν©λλ€.
# Pytorch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased-finetuned-mrpc")
model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased-finetuned-mrpc")
classes = ["not paraphrase", "is paraphrase"]
sequence_0 = "The company HuggingFace is based in New York City"
sequence_1 = "Apples are especially bad for your health"
sequence_2 = "HuggingFace's headquarters are situated in Manhattan"
# The tokenizer will automatically add any model specific separators (i.e. <CLS> and <SEP>) and tokens to
# the sequence, as well as compute the attention masks.
paraphrase = tokenizer(sequence_0, sequence_2, return_tensors="pt")
not_paraphrase = tokenizer(sequence_0, sequence_1, return_tensors="pt")
paraphrase_classification_logits = model(**paraphrase).logits
not_paraphrase_classification_logits = model(**not_paraphrase).logits
paraphrase_results = torch.softmax(paraphrase_classification_logits, dim=1).tolist()[0]
not_paraphrase_results = torch.softmax(not_paraphrase_classification_logits, dim=1).tolist()[0]
# Should be paraphrase
for i in range(len(classes)):
print(f"{classes[i]}: {int(round(paraphrase_results[i] * 100))}%")
"""
not paraphrase: 10%
is paraphrase: 90%
"""
# Should not be paraphrase
for i in range(len(classes)):
print(f"{classes[i]}: {int(round(not_paraphrase_results[i] * 100))}%")
"""
not paraphrase: 94%
is paraphrase: 6%
"""
# Tensorflow
from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
import tensorflow as tf
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased-finetuned-mrpc")
model = TFAutoModelForSequenceClassification.from_pretrained("bert-base-cased-finetuned-mrpc")
classes = ["not paraphrase", "is paraphrase"]
sequence_0 = "The company HuggingFace is based in New York City"
sequence_1 = "Apples are especially bad for your health"
sequence_2 = "HuggingFace's headquarters are situated in Manhattan"
# The tokenizer will automatically add any model specific separators (i.e. <CLS> and <SEP>) and tokens to
# the sequence, as well as compute the attention masks.
paraphrase = tokenizer(sequence_0, sequence_2, return_tensors="tf")
not_paraphrase = tokenizer(sequence_0, sequence_1, return_tensors="tf")
paraphrase_classification_logits = model(paraphrase).logits
not_paraphrase_classification_logits = model(not_paraphrase).logits
paraphrase_results = tf.nn.softmax(paraphrase_classification_logits, axis=1).numpy()[0]
not_paraphrase_results = tf.nn.softmax(not_paraphrase_classification_logits, axis=1).numpy()[0]
# Should be paraphrase
for i in range(len(classes)):
print(f"{classes[i]}: {int(round(paraphrase_results[i] * 100))}%")
"""
not paraphrase: 10%
is paraphrase: 90%
"""
# Should not be paraphrase
for i in range(len(classes)):
print(f"{classes[i]}: {int(round(not_paraphrase_results[i] * 100))}%")
"""
not paraphrase: 94%
is paraphrase: 6%
"""
μΆμΆ μ§μμλ΅(Extractive Question Answering)
μΆμΆ μ§μμλ΅μ μ£Όμ΄μ§ μ§λ¬Έ ν μ€νΈμμ λ΅μ μΆμΆνλ μμ μ λλ€. μ§λ¬Έ λ΅λ³ λ°μ΄ν°μ μ μλ‘λ ν΄λΉ μμ μ κΈ°λ°μΌλ‘ νλ SQuAD λ°μ΄ν°μ μ΄ μμ΅λλ€. SQuAD μμ μμ λͺ¨λΈμ νμΈνλνλ €λ©΄ run_qa.py λ° run_tf_squad.py μ€ν¬λ¦½νΈλ₯Ό νμ©ν μ μμ΅λλ€.
λ€μμ νμ΄νλΌμΈμ μ¬μ©νμ¬ μ£Όμ΄μ§ μ§λ¬Έ ν μ€νΈμμ λ΅λ³μ μΆμΆνλ μ§μμλ΅μ μννλ μμ λλ€. SQuAD λ°μ΄ν°μ μ ν΅ν΄ νμΈνλλ λͺ¨λΈμ νμ©ν©λλ€.
from transformers import pipeline
question_answerer = pipeline("question-answering")
context = r"""
Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a
question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune
a model on a SQuAD task, you may leverage the examples/pytorch/question-answering/run_squad.py script.
"""
μ΄λ κ² νλ©΄ ν μ€νΈμμ μΆμΆλ λ΅λ³κ³Ό **μ λ’° μ μ(confidence score)**κ° ν μ€νΈμμ μΆμΆλ λ΅λ³μ μμΉμΈ 'μμ' λ° 'μ’ λ£' κ°κ³Ό ν¨κ» λ°νλ©λλ€.
result = question_answerer(question="What is extractive question answering?", context=context)
print(f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}")
result = question_answerer(question="What is a good example of a question answering dataset?", context=context)
print(f"Answer: '{result['answer']}', score: {round(result['score'], 4)}, start: {result['start']}, end: {result['end']}")
λͺ¨λΈ λ° ν ν¬λμ΄μ λ₯Ό μ¬μ©νμ¬ μ§λ¬Έμ λλ΅νλ μμ λλ€. νλ‘μΈμ€λ λ€μκ³Ό κ°μ΅λλ€.
- 체ν¬ν¬μΈνΈ μ΄λ¦μμ ν ν¬λμ΄μ λ° λͺ¨λΈμ μΈμ€ν΄μ€νν©λλ€. λͺ¨λΈμ BERT λͺ¨λΈλ‘ μλ³λλ©° 체ν¬ν¬μΈνΈμ μ μ₯λ κ°μ€μΉλ‘ λ‘λλ©λλ€.
- ν μ€νΈμ λͺ κ°μ§ μ§λ¬Έμ μ μν©λλ€.
- μ§λ¬Έμ λ°λ³΅νκ³ μ¬λ°λ₯Έ λͺ¨λΈλ³ μλ³μ ν ν° νμ ID λ° μ΄ν μ λ§μ€ν¬λ₯Ό μ¬μ©νμ¬ ν μ€νΈμ νμ¬ μ§λ¬Έμ μνμ€λ₯Ό μμ±ν©λλ€.
- μ΄ μνμ€λ₯Ό λͺ¨λΈμ μ λ¬ν©λλ€. κ·Έλ¬λ©΄ μμ μμΉμ λ μμΉ λͺ¨λμ λν΄ μ 체 μνμ€ ν ν°(μ§λ¬Έκ³Ό ν μ€νΈ)μ κ±Έμ³ λ€μν μ μκ° μΆλ ₯λ©λλ€.
- ν ν°μ λν νλ₯ μ μ»κΈ° μν΄ κ²°κ³Όκ°μ μννΈλ§₯μ€ ν¨μλ₯Ό μ·¨ν©λλ€.
- μλ³λ μμ λ° λ μμΉμμ ν ν°μ κ°μ Έμ λ¬Έμμ΄λ‘ λ³νν©λλ€.
- κ²°κ³Όλ₯Ό νλ¦°νΈν©λλ€.
# Pytorch
from transformers import AutoTokenizer, AutoModelForQuestionAnswering
import torch
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
model = AutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
text = r"""
π€ Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides general-purpose
architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet…) for Natural Language Understanding (NLU) and Natural
Language Generation (NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between
TensorFlow 2.0 and PyTorch.
"""
questions = [
"How many pretrained models are available in π€ Transformers?",
"What does π€ Transformers provide?",
"π€ Transformers provides interoperability between which frameworks?",
]
for question in questions:
inputs = tokenizer(question, text, add_special_tokens=True, return_tensors="pt")
input_ids = inputs["input_ids"].tolist()[0]
outputs = model(**inputs)
answer_start_scores = outputs.start_logits
answer_end_scores = outputs.end_logits
# Get the most likely beginning of answer with the argmax of the score
answer_start = torch.argmax(answer_start_scores)
# Get the most likely end of answer with the argmax of the score
answer_end = torch.argmax(answer_end_scores) + 1
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
print(f"Question: {question}")
print(f"Answer: {answer}")
"""
Question: How many pretrained models are available in π€ Transformers?
Answer: over 32 +
Question: What does π€ Transformers provide?
Answer: general - purpose architectures
Question: π€ Transformers provides interoperability between which frameworks?
Answer: tensorflow 2. 0 and pytorch
"""
# Tensorflow
from transformers import AutoTokenizer, TFAutoModelForQuestionAnswering
import tensorflow as tf
tokenizer = AutoTokenizer.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
model = TFAutoModelForQuestionAnswering.from_pretrained("bert-large-uncased-whole-word-masking-finetuned-squad")
text = r"""
π€ Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides general-purpose
architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet…) for Natural Language Understanding (NLU) and Natural
Language Generation (NLG) with over 32+ pretrained models in 100+ languages and deep interoperability between
TensorFlow 2.0 and PyTorch.
"""
questions = [
"How many pretrained models are available in π€ Transformers?",
"What does π€ Transformers provide?",
"π€ Transformers provides interoperability between which frameworks?",
]
for question in questions:
inputs = tokenizer(question, text, add_special_tokens=True, return_tensors="tf")
input_ids = inputs["input_ids"].numpy()[0]
outputs = model(inputs)
answer_start_scores = outputs.start_logits
answer_end_scores = outputs.end_logits
# Get the most likely beginning of answer with the argmax of the score
answer_start = tf.argmax(answer_start_scores, axis=1).numpy()[0]
# Get the most likely end of answer with the argmax of the score
answer_end = tf.argmax(answer_end_scores, axis=1).numpy()[0] + 1
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(input_ids[answer_start:answer_end]))
print(f"Question: {question}")
print(f"Answer: {answer}")
"""
Question: How many pretrained models are available in π€ Transformers?
Answer: over 32 +
Question: What does π€ Transformers provide?
Answer: general - purpose architectures
Question: π€ Transformers provides interoperability between which frameworks?
Answer: tensorflow 2. 0 and pytorch
"""
μΈμ΄ λͺ¨λΈλ§(Language Modeling)
μΈμ΄ λͺ¨λΈλ§μ λͺ¨λΈμ μ½νΌμ€μ λ§μΆλ μμ μ΄λ©°, νΉμ λλ©μΈμ νΉνμν¬ μ μμ΅λλ€. λͺ¨λ νΈλμ€ν¬λ¨Έ κΈ°λ° λͺ¨λΈμ μΈμ΄ λͺ¨λΈλ§μ λ³ν(μ: λ§μ€ν¬λ μΈμ΄ λͺ¨λΈλ§μ μ¬μ©ν BERT, μΌμ μΈμ΄ λͺ¨λΈλ§μ μ¬μ©ν GPT-2)νμ¬ νλ ¨λ©λλ€.
μΈμ΄ λͺ¨λΈλ§μ ν리νΈλ μ΄λ μ΄μΈμλ λͺ¨λΈ λ°°ν¬λ₯Ό κ° λλ©μΈμ λ§κ² νΉνμν€κΈ° μν΄ μ μ©νκ² μ¬μ©λ μ μμ΅λλ€. μλ₯Ό λ€μ΄, λμ©λ μ½νΌμ€λ₯Ό ν΅ν΄ νλ ¨λ μΈμ΄ λͺ¨λΈμ μ¬μ©ν λ€μ λ΄μ€ λ°μ΄ν°μ λλ κ³Όν λ Όλ¬Έ λ°μ΄ν°μ (μ : LysandreJik/arxiv-nlp)μΌλ‘ νμΈνλνλ κ²μ λλ€.
λ§μ€ν¬λ μΈμ΄ λͺ¨λΈλ§(Masked Language Modeling)
λ§μ€ν¬λ μΈμ΄ λͺ¨λΈλ§μ λ§μ€νΉ ν ν°μ μ¬μ©νμ¬ μμλλ‘ ν ν°μ λ§μ€νΉνκ³ λͺ¨λΈμ΄ ν΄λΉ λ§μ€ν¬λ₯Ό μ μ ν ν ν°μΌλ‘ μ±μ°λλ‘ μμ²νλ μμ μ λλ€. λ°λΌμ λͺ¨λΈμ΄ μ€λ₯Έμͺ½ 컨ν μ€νΈ(λ§μ€ν¬ μ€λ₯Έμͺ½μ ν ν°)μ μΌμͺ½ 컨ν μ€νΈ(λ§μ€ν¬ μΌμͺ½μ ν ν°)λ₯Ό λͺ¨λ μ΄ν΄λ³Ό μ μμ΅λλ€. μ΄λ¬ν νλ ¨μ SQuAD(μ§μμλ΅, Lewis, Lui, Goyal et al, ννΈ 4.2)μ κ°μ μλ°©ν₯ 컨ν μ€νΈλ₯Ό νμλ‘ νλ λ€μ΄μ€νΈλ¦Ό μμ μ λν κ°λ ₯ν κΈ°μ΄ λͺ¨λΈμ λ§λλλ€. λ§μ€νΉλ μΈμ΄ λͺ¨λΈλ§ μμ μμ λͺ¨λΈμ νμΈνλνλ €λ©΄ run_mlm.py μ€ν¬λ¦½νΈλ₯Ό νμ©ν μ μμ΅λλ€.
λ€μμ νμ΄νλΌμΈμ μ¬μ©νμ¬ μνμ€μμ λ§μ€ν¬λ₯Ό κ΅μ²΄νλ μμ λλ€.
from transformers import pipeline
unmasker = pipeline("fill-mask")
κ·Έλ¬λ©΄ λ§μ€ν¬κ° μ±μμ§ μνμ€, μ€μ½μ΄ λ° ν ν°IDκ° ν ν¬λμ΄μ λ₯Ό ν΅ν΄ μΆλ ₯λ©λλ€.
from pprint import pprint
pprint(unmasker(f"HuggingFace is creating a {unmasker.tokenizer.mask_token} that the community uses to solve NLP tasks."))
[{'score': 0.1793,
'sequence': 'HuggingFace is creating a tool that the community uses to solve '
'NLP tasks.',
'token': 3944,
'token_str': ' tool'},
{'score': 0.1135,
'sequence': 'HuggingFace is creating a framework that the community uses to '
'solve NLP tasks.',
'token': 7208,
'token_str': ' framework'},
{'score': 0.0524,
'sequence': 'HuggingFace is creating a library that the community uses to '
'solve NLP tasks.',
'token': 5560,
'token_str': ' library'},
{'score': 0.0349,
'sequence': 'HuggingFace is creating a database that the community uses to '
'solve NLP tasks.',
'token': 8503,
'token_str': ' database'},
{'score': 0.0286,
'sequence': 'HuggingFace is creating a prototype that the community uses to '
'solve NLP tasks.',
'token': 17715,
'token_str': ' prototype'}]
λ€μμ λͺ¨λΈ λ° ν ν¬λμ΄μ λ₯Ό μ¬μ©νμ¬ λ§μ€νΉλ μΈμ΄ λͺ¨λΈλ§μ μννλ μμ λλ€. νλ‘μΈμ€λ λ€μκ³Ό κ°μ΅λλ€.
- 체ν¬ν¬μΈνΈ μ΄λ¦μμ ν ν¬λΌμ΄μ λ° λͺ¨λΈμ μΈμ€ν΄μ€νν©λλ€. μ¬κΈ°μλ DistilBERT λͺ¨λΈμ μ¬μ©ν κ²μ΄κ³ , κ°μ€μΉκ° 체ν¬ν¬μΈνΈμ μ μ₯λ©λλ€.
- λ¨μ΄ λμ tokenizer.mask_tokenμ λ°°μΉνμ¬ λ§μ€νΉλ ν ν°μΌλ‘ μνμ€λ₯Ό μ μν©λλ€.
- ν΄λΉ μνμ€λ₯Ό ID λͺ©λ‘μΌλ‘ μΈμ½λ©νκ³ ν΄λΉ λͺ©λ‘μμ λ§μ€νΉλ ν ν°μ μμΉλ₯Ό μ°Ύμ΅λλ€.
- λ§μ€νΉλ ν ν°μ μΈλ±μ€μμ μμΈ‘κ°μ κ²μν©λλ€. μ΄ ν μλ μ΄νμ ν¬κΈ°κ° κ°κ³ , κ°μ κ° ν ν°μ κ·μλλ μ μμ λλ€. μ΄ λͺ¨λΈμ κ·Έλ° λ§₯λ½μμ κ°λ₯μ±μ΄ λλ€κ³ μκ°λλ ν ν°μ λ λμ μ μλ₯Ό λΆμ¬ν©λλ€.
- PyTorch topk λλ TensorFlow top_k λ©μλλ₯Ό μ¬μ©νμ¬ μμ 5κ°μ ν ν°μ κ²μν©λλ€.
- λ§μ€νΉλ ν ν°μ ν ν°μΌλ‘ λ°κΎΈκ³ κ²°κ³Όλ₯Ό νλ¦°νΈν©λλ€.
# Pytorch
from transformers import AutoModelForMaskedLM, AutoTokenizer
import torch
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-cased")
model = AutoModelForMaskedLM.from_pretrained("distilbert-base-cased")
sequence = "Distilled models are smaller than the models they mimic. Using them instead of the large " \
f"versions would help {tokenizer.mask_token} our carbon footprint."
inputs = tokenizer(sequence, return_tensors="pt")
mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1]
token_logits = model(**inputs).logits
mask_token_logits = token_logits[0, mask_token_index, :]
top_5_tokens = torch.topk(mask_token_logits, 5, dim=1).indices[0].tolist()
for token in top_5_tokens:
print(sequence.replace(tokenizer.mask_token, tokenizer.decode([token])))
"""
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help reduce our carbon footprint.
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help increase our carbon footprint.
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help decrease our carbon footprint.
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help offset our carbon footprint.
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help improve our carbon footprint.
"""
# Tensorflow
from transformers import TFAutoModelForMaskedLM, AutoTokenizer
import tensorflow as tf
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-cased")
model = TFAutoModelForMaskedLM.from_pretrained("distilbert-base-cased")
sequence = "Distilled models are smaller than the models they mimic. Using them instead of the large " \
f"versions would help {tokenizer.mask_token} our carbon footprint."
inputs = tokenizer(sequence, return_tensors="tf")
mask_token_index = tf.where(inputs["input_ids"] == tokenizer.mask_token_id)[0, 1]
token_logits = model(**inputs).logits
mask_token_logits = token_logits[0, mask_token_index, :]
top_5_tokens = tf.math.top_k(mask_token_logits, 5).indices.numpy()
for token in top_5_tokens:
print(sequence.replace(tokenizer.mask_token, tokenizer.decode([token])))
"""
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help reduce our carbon footprint.
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help increase our carbon footprint.
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help decrease our carbon footprint.
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help offset our carbon footprint.
Distilled models are smaller than the models they mimic. Using them instead of the large versions would help improve our carbon footprint.
"""
λͺ¨λΈμμ μμΈ‘ν μμ 5κ°μ ν ν°λ€μΌλ‘ μ΄λ£¨μ΄μ§ 5κ°μ μνμ€κ° νλ¦°νΈλ©λλ€.
μΈκ³Ό μΈμ΄ λͺ¨λΈλ§(Causal Language Modeling)
μΈκ³Ό μΈμ΄ λͺ¨λΈλ§μ ν ν° μμμ λ°λΌ λ€μ ν ν°μ μμΈ‘νλ μμ μ λλ€. μ΄ κ³Όμ μμλ λͺ¨λΈμ΄ μΌμͺ½ 컨ν μ€νΈ(λ§μ€ν¬ μΌμͺ½μ μλ ν ν°)μλ§ μ§μ€νκ² λ©λλ€. μ΄λ¬ν νμ΅ κ³Όμ μ λ¬Έμ₯ μμ± μμ κ³Ό νΉν μ°κ΄μ΄ μμ΅λλ€. μΈκ³Ό μΈμ΄ λͺ¨λΈλ§ μμ μμ λͺ¨λΈμ νμΈνλνλ €λ©΄ run_clm.py μ€ν¬λ¦½νΈλ₯Ό νμ©ν μ μμ΅λλ€.
μΌλ°μ μΌλ‘ λ€μ ν ν°μ λͺ¨λΈμ΄ μ λ ₯ μνμ€μμ μμ±νλ λ§μ§λ§ νλ λ μ΄μ΄μ logitμμ μνλ§λμ΄ μμΈ‘λ©λλ€.
λ€μμ ν ν¬λμ΄μ μ λͺ¨λΈμ μ¬μ©νκ³ top_k_top_p_filtering() λ©μλλ₯Ό νμ©νμ¬ μΈν ν ν° μνμ€μ λ°λΌ λ€μ ν ν°μ μνλ§νλ μμ λλ€.
# Pytorch
from transformers import AutoModelForCausalLM, AutoTokenizer, top_k_top_p_filtering
import torch
from torch import nn
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = AutoModelForCausalLM.from_pretrained("gpt2")
sequence = f"Hugging Face is based in DUMBO, New York City, and"
inputs = tokenizer(sequence, return_tensors="pt")
input_ids = inputs["input_ids"]
# get logits of last hidden state
next_token_logits = model(**inputs).logits[:, -1, :]
# filter
filtered_next_token_logits = top_k_top_p_filtering(next_token_logits, top_k=50, top_p=1.0)
# sample
probs = nn.functional.softmax(filtered_next_token_logits, dim=-1)
next_token = torch.multinomial(probs, num_samples=1)
generated = torch.cat([input_ids, next_token], dim=-1)
resulting_string = tokenizer.decode(generated.tolist()[0])
print(resulting_string)
"""
Hugging Face is based in DUMBO, New York City, and ...
"""
# Tensorflow
from transformers import TFAutoModelForCausalLM, AutoTokenizer, tf_top_k_top_p_filtering
import tensorflow as tf
tokenizer = AutoTokenizer.from_pretrained("gpt2")
model = TFAutoModelForCausalLM.from_pretrained("gpt2")
sequence = f"Hugging Face is based in DUMBO, New York City, and"
inputs = tokenizer(sequence, return_tensors="tf")
input_ids = inputs["input_ids"]
# get logits of last hidden state
next_token_logits = model(**inputs).logits[:, -1, :]
# filter
filtered_next_token_logits = tf_top_k_top_p_filtering(next_token_logits, top_k=50, top_p=1.0)
# sample
next_token = tf.random.categorical(filtered_next_token_logits, dtype=tf.int32, num_samples=1)
generated = tf.concat([input_ids, next_token], axis=1)
resulting_string = tokenizer.decode(generated.numpy().tolist()[0])
print(resulting_string)
"""
Hugging Face is based in DUMBO, New York City, and ...
"""
μ΄λ κ² νλ©΄ μλμ μμμ λ°λΌ μΌκ΄μ± μλ λ€μ ν ν°μ΄ μΆλ ₯λ©λλ€. μ΄ ν ν°μ μ°λ¦¬μ κ²½μ° λ¨μ΄ λλ νΉμ§μ λλ€.
λ€μ μΉμ μμλ ν λ²μ νλμ ν ν°μ΄ μλλΌ μ§μ λ κΈΈμ΄λ‘ μ¬λ¬ ν ν°μ μμ±νλ λ° *generate()*λ₯Ό μ¬μ©νλ λ°©λ²μ λ³΄μ¬ μ€λλ€.
ν μ€νΈ μμ±(Text Generation)
ν μ€νΈ μμ±(κ°λ°©ν ν μ€νΈ μμ±μ΄λΌκ³ λ ν¨)μ λͺ©νλ μ£Όμ΄μ§ Contextμ μΌκ΄λκ² μ΄μ΄μ§λ ν μ€νΈλ₯Ό λ§λλ κ²μ λλ€. λ€μ μλ νμ΄νλΌμΈμμ GPT-2λ₯Ό μ¬μ©νμ¬ ν μ€νΈλ₯Ό μμ±νλ λ°©λ²μ 보μ¬μ€λλ€. κΈ°λ³Έμ μΌλ‘ λͺ¨λ λͺ¨λΈμ νμ΄νλΌμΈμμ μ¬μ©ν λ κ° Configμμ μ€μ ν λλ‘ Top-K μνλ§μ μ μ©ν©λλ€(μμ : gpt-2 config μ°Έμ‘°).
from transformers import pipeline
text_generator = pipeline("text-generation")
print(text_generator("As far as I am concerned, I will", max_length=50, do_sample=False))
"""
[{'generated_text': 'As far as I am concerned, I will be the first to admit that I am not a fan of the idea of a
"free market." I think that the idea of a free market is a bit of a stretch. I think that the idea'}]
"""
μ¬κΈ°μ λͺ¨λΈμ "As far as I am concerned, I will"λΌλ Contextμμ μ΄ μ΅λ κΈΈμ΄ 50κ°μ ν ν°μ κ°μ§ μμμ ν μ€νΈλ₯Ό μμ±ν©λλ€. λ°±κ·ΈλΌμ΄λμμ νμ΄νλΌμΈ κ°μ²΄λ generate() λ©μλλ₯Ό νΈμΆνμ¬ ν μ€νΈλ₯Ό μμ±ν©λλ€. max_length λ° do_sample μΈμμ κ°μ΄ μ΄ λ©μλμ κΈ°λ³Έ μΈμλ νμ΄νλΌμΈμμ μ¬μ μν μ μμ΅λλ€.
λ€μμ XLNet λ° ν΄λΉ ν ν¬λμ΄μ λ₯Ό μ¬μ©ν ν μ€νΈ μμ± μμ μ΄λ©°, generate() λ©μλλ₯Ό ν¬ν¨νκ³ μμ΅λλ€.
# Pytorch
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("xlnet-base-cased")
tokenizer = AutoTokenizer.from_pretrained("xlnet-base-cased")
# Padding text helps XLNet with short prompts - proposed by Aman Rusia in <https://github.com/rusiaaman/XLNet-gen#methodology>
PADDING_TEXT = """In 1991, the remains of Russian Tsar Nicholas II and his family
(except for Alexei and Maria) are discovered.
The voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the
remainder of the story. 1883 Western Siberia,
a young Grigori Rasputin is asked by his father and a group of men to perform magic.
Rasputin has a vision and denounces one of the men as a horse thief. Although his
father initially slaps him for making such an accusation, Rasputin watches as the
man is chased outside and beaten. Twenty years later, Rasputin sees a vision of
the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous,
with people, even a bishop, begging for his blessing. """
prompt = "Today the weather is really nice and I am planning on "
inputs = tokenizer(PADDING_TEXT + prompt, add_special_tokens=False, return_tensors="pt")["input_ids"]
prompt_length = len(tokenizer.decode(inputs[0]))
outputs = model.generate(inputs, max_length=250, do_sample=True, top_p=0.95, top_k=60)
generated = prompt + tokenizer.decode(outputs[0])[prompt_length+1:]
print(generated)
"""
Today the weather is really nice and I am planning ...
"""
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("xlnet-base-cased")
tokenizer = AutoTokenizer.from_pretrained("xlnet-base-cased")
# Padding text helps XLNet with short prompts - proposed by Aman Rusia in <https://github.com/rusiaaman/XLNet-gen#methodology>
PADDING_TEXT = """In 1991, the remains of Russian Tsar Nicholas II and his family
(except for Alexei and Maria) are discovered.
The voice of Nicholas's young son, Tsarevich Alexei Nikolaevich, narrates the
remainder of the story. 1883 Western Siberia,
a young Grigori Rasputin is asked by his father and a group of men to perform magic.
Rasputin has a vision and denounces one of the men as a horse thief. Although his
father initially slaps him for making such an accusation, Rasputin watches as the
man is chased outside and beaten. Twenty years later, Rasputin sees a vision of
the Virgin Mary, prompting him to become a priest. Rasputin quickly becomes famous,
with people, even a bishop, begging for his blessing. """
prompt = "Today the weather is really nice and I am planning on "
inputs = tokenizer(PADDING_TEXT + prompt, add_special_tokens=False, return_tensors="pt")["input_ids"]
prompt_length = len(tokenizer.decode(inputs[0]))
outputs = model.generate(inputs, max_length=250, do_sample=True, top_p=0.95, top_k=60)
generated = prompt + tokenizer.decode(outputs[0])[prompt_length+1:]
print(generated)
"""
Today the weather is really nice and I am planning ...
"""
ν μ€νΈ μμ±μ νμ¬ PyTorchμ GPT-2, OpenAi-GPT, CTRL, XLNet, Transpo-XL λ° Reformerμ Tensorflowμ λλΆλΆμ λͺ¨λΈμμλ κ°λ₯ν©λλ€. μμ μμμ λ³Ό μ μλ―μ΄, XLNet λ° Transpo-XLμ΄ μ λλ‘ μλνλ €λ©΄ ν¨λ©μ΄ νμν κ²½μ°κ° λ§μ΅λλ€. GPT-2λ μΈκ³Ό μΈμ΄ λͺ¨λΈλ§ λͺ©μ μΌλ‘ μλ°±λ§ κ°μ μΉ νμ΄μ§λ₯Ό ν΅ν΄ νμ΅λμκΈ° λλ¬Έμ μΌλ°μ μΌλ‘ κ°λ°©ν ν μ€νΈ μμ±μ μ ν©ν©λλ€.
ν μ€νΈ μμ±μ μν΄ λ€μν λμ½λ© μ λ΅μ μ μ©νλ λ°©λ²μ λν μμΈν λ΄μ©μ ν μ€νΈ μμ± λΈλ‘κ·Έ κ²μλ¬Όμ μ°Έμ‘°νμμμ€.
κ°μ²΄λͺ μΈμ(Named Entity Recognition)
κ°μ²΄λͺ μΈμ(NER)μ κ°μΈ, κΈ°κ΄ λλ μ₯μμ μ΄λ¦ λ±μΌλ‘ μλ³ κ°λ₯ν ν΄λμ€μ λ°λΌ ν ν°μ λΆλ₯νλ μμ μ λλ€. κ°μ²΄λͺ μΈμ λ°μ΄ν°μ μ μλ‘λ CoNLL-2003 λ°μ΄ν°μ μ΄ μμ΅λλ€. NER μμ μμ λͺ¨λΈμ νμΈνλνλ €λ κ²½μ° run_ner.py μ€ν¬λ¦½νΈλ₯Ό νμ©ν μ μμ΅λλ€.
λ€μμ νμ΄νλΌμΈμ μ¬μ©νμ¬ κ°μ²΄λͺ μΈμμΌλ‘ ν ν°μ 9κ° ν΄λμ€ μ€ νλμ μνλλ‘ μμΈ‘νλ μμμ λλ€(BIO νν).
O, κ°μ²΄λͺ μ΄ μλ λΆλΆ
B-MIS, κΈ°ν μν°ν°κ° μμλλ λΆλΆ
I-MIS, κΈ°ν μν°ν°
B-PER, μ¬λμ μ΄λ¦μ΄ μμλλ λΆλΆ
I-PER, μ¬λμ μ΄λ¦
B-ORG, κΈ°κ΄λͺ μ΄ μμλλ λΆλΆ
I-ORG, κΈ°κ΄λͺ
B-LOC, μ₯μλͺ μ΄ μμλλ λΆλΆ
I-LOC, μ₯μλͺ
CoNLL-2003μ νμΈνλ λͺ¨λΈμ μ¬μ©νμμΌλ©°, dbmdzμ @stefan-itμ μν΄ νμΈνλ λμμ΅λλ€.
from transformers import pipeline
ner_pipe = pipeline("ner")
sequence = """Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO,
therefore very close to the Manhattan Bridge which is visible from the window."""
μ΄λ κ² νλ©΄ μμμ μ μν 9κ° ν΄λμ€μ μν°ν° μ€ νλλ‘ μλ³λ λͺ¨λ λ¨μ΄ λͺ©λ‘μ΄ μΆλ ₯λ©λλ€. μμλλ κ²°κ³Όλ λ€μκ³Ό κ°μ΅λλ€.
for entity in ner_pipe(sequence):
print(entity)
"""
{'entity': 'I-ORG', 'score': 0.9996, 'index': 1, 'word': 'Hu', 'start': 0, 'end': 2}
{'entity': 'I-ORG', 'score': 0.9910, 'index': 2, 'word': '##gging', 'start': 2, 'end': 7}
{'entity': 'I-ORG', 'score': 0.9982, 'index': 3, 'word': 'Face', 'start': 8, 'end': 12}
{'entity': 'I-ORG', 'score': 0.9995, 'index': 4, 'word': 'Inc', 'start': 13, 'end': 16}
{'entity': 'I-LOC', 'score': 0.9994, 'index': 11, 'word': 'New', 'start': 40, 'end': 43}
{'entity': 'I-LOC', 'score': 0.9993, 'index': 12, 'word': 'York', 'start': 44, 'end': 48}
{'entity': 'I-LOC', 'score': 0.9994, 'index': 13, 'word': 'City', 'start': 49, 'end': 53}
{'entity': 'I-LOC', 'score': 0.9863, 'index': 19, 'word': 'D', 'start': 79, 'end': 80}
{'entity': 'I-LOC', 'score': 0.9514, 'index': 20, 'word': '##UM', 'start': 80, 'end': 82}
{'entity': 'I-LOC', 'score': 0.9337, 'index': 21, 'word': '##BO', 'start': 82, 'end': 84}
{'entity': 'I-LOC', 'score': 0.9762, 'index': 28, 'word': 'Manhattan', 'start': 114, 'end': 123}
{'entity': 'I-LOC', 'score': 0.9915, 'index': 29, 'word': 'Bridge', 'start': 124, 'end': 130}
"""
μ΄λ»κ² "Huggingface" μνμ€μ ν ν°μ΄ κΈ°κ΄λͺ μΌλ‘ μλ³λκ³ "New York City", "DUMBO" λ° "Manhattan Bridge"κ° μ₯μλͺ μΌλ‘ μλ³λλμ§μ μ£Όμν΄μ 보μμμ€.
λ€μμ λͺ¨λΈ λ° ν ν¬λμ΄μ λ₯Ό μ¬μ©νμ¬ κ°μ²΄λͺ μΈμμ μννλ μμμ λλ€. νλ‘μΈμ€λ λ€μκ³Ό κ°μ΅λλ€.
- 체ν¬ν¬μΈνΈμμ ν ν¬λμ΄μ λ° λͺ¨λΈμ μΈμ€ν΄μ€νν©λλ€. BERT λͺ¨λΈμ μ¬μ©νκ³ , 체ν¬ν¬μΈνΈμ μ μ₯λ κ°μ€μΉλ₯Ό λ‘λν©λλ€.
- κ° μνμ€μ μν°ν°λ₯Ό μ μν©λλ€. μλ₯Ό λ€μ΄ "Hugging Face"λ₯Ό κΈ°κ΄λͺ μΌλ‘, "New York City"λ₯Ό μ₯μλͺ μΌλ‘ μ μν μ μμ΅λλ€.
- λ¨μ΄λ₯Ό ν ν°μΌλ‘ λΆν νμ¬ μμΈ‘μ 맀νν μ μλλ‘ ν©λλ€. μ°λ¦¬λ λ¨Όμ μνμ€λ₯Ό μμ ν μΈμ½λ©νκ³ λμ½λ©νμ¬ νΉλ³ν ν ν°μ΄ ν¬ν¨λ λ¬Έμμ΄μ λ¨κ²¨λλλ‘ ν©λλ€.
- ν΄λΉ μνμ€λ₯Ό IDλ‘ μΈμ½λ©ν©λλ€(νΉμ ν ν°μ΄ μλμΌλ‘ μΆκ°λ¨).
- μ λ ₯ ν ν°μ λͺ¨λΈμ μ λ¬νκ³ , 첫 λ²μ§Έ μΆλ ₯μ κ°μ Έμμ μμΈ‘μ μνν©λλ€. μ΄ κ²°κ³Όλ₯Ό κ° ν ν°μ λν΄ λ§€μΉ κ°λ₯ν 9κ° ν΄λμ€μ λμ‘°ν©λλ€. κ° ν ν°μ λν΄ κ°μ₯ κ°λ₯μ±μ΄ λμ ν΄λμ€λ₯Ό κ²μνκΈ° μν΄ argmax ν¨μλ₯Ό μ¬μ©ν©λλ€.
- κ°κ°μ ν ν°μ μμΈ‘ κ²°κ³Όμ λ¬Άμ΄ νλ¦°νΈν©λλ€.
# Pytorch
from transformers import AutoModelForTokenClassification, AutoTokenizer
import torch
model = AutoModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
sequence = "Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO, " \\
"therefore very close to the Manhattan Bridge."
inputs = tokenizer(sequence, return_tensors="pt")
tokens = inputs.tokens()
outputs = model(**inputs).logits
predictions = torch.argmax(outputs, dim=2)
# Tensorflow
from transformers import TFAutoModelForTokenClassification, AutoTokenizer
import tensorflow as tf
model = TFAutoModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
tokenizer = AutoTokenizer.from_pretrained("bert-base-cased")
sequence = "Hugging Face Inc. is a company based in New York City. Its headquarters are in DUMBO, " \\
"therefore very close to the Manhattan Bridge."
inputs = tokenizer(sequence, return_tensors="tf")
tokens = inputs.tokens()
outputs = model(**inputs)[0]
predictions = tf.argmax(outputs, axis=2)
ν΄λΉ μμΈ‘ κ²°κ³Όλ‘ λ§€νλ κ° ν ν° λͺ©λ‘μ μΆλ ₯ν©λλ€. νμ΄νλΌμΈκ³Ό λ¬λ¦¬ λͺ¨λ ν ν°μ μμΈ‘ κ²°κ³Όκ° λμ€κ² λλλ°, μν°ν°κ° μλ ν ν°μΈ ν΄λμ€ 0μ κ²½μ°λ₯Ό μ κ±°νμ§ μμκΈ° λλ¬Έμ λλ€.
μμ μμμμ μμΈ‘ κ²°κ³Όλ μ μλ‘ ννλ©λλ€. μλ κ·Έλ¦Όκ³Ό κ°μ΄ μ μ ννμ ν΄λμ€ λ²νΈλ₯Ό ν΄λμ€ μ΄λ¦μΌλ‘ λ°κΎΈκΈ° μν΄ model.config.id2label μμ±μ μ¬μ©ν μ μμ΅λλ€.
for token, prediction in zip(tokens, predictions[0].numpy()):
print((token, model.config.id2label[prediction]))
"""
('[CLS]', 'O')
('Hu', 'I-ORG')
('##gging', 'I-ORG')
('Face', 'I-ORG')
('Inc', 'I-ORG')
('.', 'O')
('is', 'O')
('a', 'O')
('company', 'O')
('based', 'O')
('in', 'O')
('New', 'I-LOC')
('York', 'I-LOC')
('City', 'I-LOC')
('.', 'O')
('Its', 'O')
('headquarters', 'O')
('are', 'O')
('in', 'O')
('D', 'I-LOC')
('##UM', 'I-LOC')
('##BO', 'I-LOC')
(',', 'O')
('therefore', 'O')
('very', 'O')
('close', 'O')
('to', 'O')
('the', 'O')
('Manhattan', 'I-LOC')
('Bridge', 'I-LOC')
('.', 'O')
('[SEP]', 'O')
"""
μμ½(Summarization)
μμ½μ λ¬Έμλ κΈ°μ¬λ₯Ό λ 짧μ ν μ€νΈλ‘ μ€μ΄λ μμ μ λλ€. μμ½ μμ μμ λͺ¨λΈμ νμΈνλνλ €λ©΄ run_summarization.pyλ₯Ό νμ©ν μ μμ΅λλ€.
μμ½ λ°μ΄ν°μ μλ‘λ CNN / Daily Mail λ°μ΄ν°μ μ΄ μμ΅λλ€. μ΄ λ°μ΄ν°μ μ κΈ΄ λ΄μ€ κΈ°μ¬λ‘ ꡬμ±λμ΄ μμΌλ©° μμ½ μμ μ μν΄ λ§λ€μ΄μ‘μ΅λλ€. μμ½ μμ μμ λͺ¨λΈμ νμΈνλνλ €λ©΄, μ΄ λ¬Έμμμ λ€μν μ κ·Ό λ°©μμ λ°°μΈ μ μμ΅λλ€.
λ€μμ νμ΄νλΌμΈμ μ¬μ©νμ¬ μμ½μ μννλ μμ λλ€. CNN/Daily Mail λ°μ΄ν°μ μΌλ‘ νμΈνλλ Bart λͺ¨λΈμ νμ©ν©λλ€.
from transformers import pipeline
summarizer = pipeline("summarization")
ARTICLE = """ New York (CNN)When Liana Barrientos was 23 years old, she got married in Westchester County, New York.
A year later, she got married again in Westchester County, but to a different man and without divorcing her first husband.
Only 18 days after that marriage, she got hitched yet again. Then, Barrientos declared "I do" five more times, sometimes only within two weeks of each other.
In 2010, she married once more, this time in the Bronx. In an application for a marriage license, she stated it was her "first and only" marriage.
Barrientos, now 39, is facing two criminal counts of "offering a false instrument for filing in the first degree," referring to her false statements on the
2010 marriage license application, according to court documents.
Prosecutors said the marriages were part of an immigration scam.
On Friday, she pleaded not guilty at State Supreme Court in the Bronx, according to her attorney, Christopher Wright, who declined to comment further.
After leaving court, Barrientos was arrested and charged with theft of service and criminal trespass for allegedly sneaking into the New York subway through an emergency exit, said Detective
Annette Markowski, a police spokeswoman. In total, Barrientos has been married 10 times, with nine of her marriages occurring between 1999 and 2002.
All occurred either in Westchester County, Long Island, New Jersey or the Bronx. She is believed to still be married to four men, and at one time, she was married to eight men at once, prosecutors say.
Prosecutors said the immigration scam involved some of her husbands, who filed for permanent residence status shortly after the marriages.
Any divorces happened only after such filings were approved. It was unclear whether any of the men will be prosecuted.
The case was referred to the Bronx District Attorney\\'s Office by Immigration and Customs Enforcement and the Department of Homeland Security\\'s
Investigation Division. Seven of the men are from so-called "red-flagged" countries, including Egypt, Turkey, Georgia, Pakistan and Mali.
Her eighth husband, Rashid Rajput, was deported in 2006 to his native Pakistan after an investigation by the Joint Terrorism Task Force.
If convicted, Barrientos faces up to four years in prison. Her next court appearance is scheduled for May 18.
"""
μμ½ νμ΄νλΌμΈμ PreTrainedModel.generate() λ©μλμ μμ‘΄νλ―λ‘ μλμ κ°μ΄ νμ΄νλΌμΈμμ max_length λ° min_lengthμ λν *PreTrainedModel.generate()*μ κΈ°λ³Έ μΈμλ₯Ό μ§μ μ¬μ μν μ μμ΅λλ€. μ΄λ κ² νλ©΄ λ€μκ³Ό κ°μ μμ½ κ²°κ³Όκ° μΆλ ₯λ©λλ€.
print(summarizer(ARTICLE, max_length=130, min_length=30, do_sample=False))
"""
[{'summary_text': ' Liana Barrientos, 39, is charged with two counts of "offering a false instrument for filing in
the first degree" In total, she has been married 10 times, with nine of her marriages occurring between 1999 and
2002 . At one time, she was married to eight men at once, prosecutors say .'}]
"""
λ€μμ λͺ¨λΈκ³Ό ν ν¬λμ΄μ λ₯Ό μ¬μ©νμ¬ μμ½μ μννλ μμμ λλ€. νλ‘μΈμ€λ λ€μκ³Ό κ°μ΅λλ€.
- 체ν¬ν¬μΈνΈμμ ν ν¬λμ΄μ λ° λͺ¨λΈμ μΈμ€ν΄μ€νν©λλ€. μΌλ°μ μΌλ‘ Bart λλ T5μ κ°μ μΈμ½λ-λμ½λ λͺ¨λΈμ μ¬μ©νμ¬ μνν©λλ€.
- μμ½ν΄μΌ ν λ¬Έμλ₯Ό μ μν©λλ€.
- T5μ νΉμν μ λμ¬μΈ "summarize: "λ₯Ό μΆκ°ν©λλ€.
- μμ½λ¬Έ μμ±μ μν΄ PreTrainedModel.generate() λ©μλλ₯Ό μ¬μ©ν©λλ€.
μ΄ μμμμλ Googleμ T5 λͺ¨λΈμ μ¬μ©ν©λλ€. λ€μ€ μμ νΌν© λ°μ΄ν°μ (CNN/Daily Mail ν¬ν¨)μμλ§ ν리νΈλ μΈμ νμμλ λΆκ΅¬νκ³ λ§€μ° μ’μ κ²°κ³Όλ₯Ό μ»μ μ μμ΅λλ€.
# Pytorch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
tokenizer = AutoTokenizer.from_pretrained("t5-base")
# T5 uses a max_length of 512 so we cut the article to 512 tokens.
inputs = tokenizer("summarize: " + ARTICLE, return_tensors="pt", max_length=512, truncation=True)
outputs = model.generate(
inputs["input_ids"], max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True
)
print(tokenizer.decode(outputs[0]))
"""
<pad> prosecutors say the marriages were part of an immigration scam. if convicted, barrientos faces two criminal
counts of "offering a false instrument for filing in the first degree" she has been married 10 times, nine of them
between 1999 and 2002.</s>
"""
λ²μ(Translation)
λ²μμ ν μΈμ΄μμ λ€λ₯Έ μΈμ΄λ‘ ν μ€νΈλ₯Ό λ°κΎΈλ μμ μ λλ€. λ²μ μμ μμ λͺ¨λΈμ νμΈνλ νλ €λ©΄ run_translation.py μ€ν¬λ¦½νΈλ₯Ό νμ©ν μ μμ΅λλ€.
λ²μ λ°μ΄ν°μ μ μλ‘λ WMT English to German λ°μ΄ν°μ μ΄ μλλ°, μ΄ λ°μ΄ν°μ μλ μμ΄λ‘ λ λ¬Έμ₯μ΄ μ λ ₯ λ°μ΄ν°λ‘, λ μΌμ΄λ‘ λ λ¬Έμ₯μ΄ νκ² λ°μ΄ν°λ‘ ν¬ν¨λμ΄ μμ΅λλ€. λ²μ μμ μμ λͺ¨λΈμ νμΈνλνλ €λ κ²½μ°μ λν΄ μ΄ λ¬Έμμμλ λ€μν μ κ·Ό λ°©μμ μ€λͺ ν©λλ€.
λ€μμ νμ΄νλΌμΈμ μ¬μ©νμ¬ λ²μμ μννλ μμ λλ€. λ€μ€ μμ νΌν© λ°μ΄ν° μΈνΈ(WMT ν¬ν¨)μμ ν리νΈλ μΈλ T5 λͺ¨λΈμ νμ©νμ¬ λ²μ κ²°κ³Όλ₯Ό μ 곡ν©λλ€.
from transformers import pipeline
translator = pipeline("translation_en_to_de")
print(translator("Hugging Face is a technology company based in New York and Paris", max_length=40))
"""
[{'translation_text': 'Hugging Face ist ein Technologieunternehmen mit Sitz in New York und Paris.'}]
"""
λ³μ νμ΄νλΌμΈμ PreTrainedModel.generate() λ©μλμ μμ‘΄νλ―λ‘ μμ κ°μ΄ νμ΄νλΌμΈμμ max_lengthμ λν *PreTrainedModel.generate()*μ κΈ°λ³Έ μΈμλ₯Ό μ§μ μ¬μ μν μ μμ΅λλ€.
λ€μμ λͺ¨λΈκ³Ό ν ν¬λμ΄μ λ₯Ό μ¬μ©νμ¬ λ²μμ μννλ μμμ λλ€. νλ‘μΈμ€λ λ€μκ³Ό κ°μ΅λλ€.
- 체ν¬ν¬μΈνΈμμ ν ν¬λμ΄μ λ° λͺ¨λΈμ μΈμ€ν΄μ€νν©λλ€. μΌλ°μ μΌλ‘ Bart λλ T5μ κ°μ μΈμ½λ-λμ½λ λͺ¨λΈμ μ¬μ©νμ¬ μνν©λλ€.
- λ²μν΄μΌ ν λ¬Έμλ₯Ό μ μν©λλ€.
- T5μ νΉμν μ λμ¬μΈ "translate English to German:“μ μΆκ°ν©λλ€.
- λ²μλ¬Έ μμ±μ μν΄ PreTrainedModel.generate() λ©μλλ₯Ό μ¬μ©ν©λλ€.
# Pytorch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
tokenizer = AutoTokenizer.from_pretrained("t5-base")
inputs = tokenizer(
"translate English to German: Hugging Face is a technology company based in New York and Paris",
return_tensors="pt"
)
outputs = model.generate(inputs["input_ids"], max_length=40, num_beams=4, early_stopping=True)
print(tokenizer.decode(outputs[0]))
"""
<pad> Hugging Face ist ein Technologieunternehmen mit Sitz in New York und Paris.</s>
"""
# Tensorflow
from transformers import TFAutoModelForSeq2SeqLM, AutoTokenizer
model = TFAutoModelForSeq2SeqLM.from_pretrained("t5-base")
tokenizer = AutoTokenizer.from_pretrained("t5-base")
inputs = tokenizer(
"translate English to German: Hugging Face is a technology company based in New York and Paris",
return_tensors="tf"
)
outputs = model.generate(inputs["input_ids"], max_length=40, num_beams=4, early_stopping=True)
print(tokenizer.decode(outputs[0]))
"""
<pad> Hugging Face ist ein Technologieunternehmen mit Sitz in New York und Paris.
"""
μμ μμμ κ°μ΄ λ²μλ¬Έμ΄ μΆλ ₯λ©λλ€.