Transformers와 TensorFlow의 겉 핥아보기
아래의 것들을 설치하자.
pip3이 없다면 pip으로 하면 됩니다.
cGlwMyBpbnN0YWxsIC0tdXBncmFkZSBwaXANCnBpcDMgaW5zdGFsbCB0ZW5zb3JmbG93LWNwdQ0KcGlwMyBpbnN0YWxsIHRyYW5zZm9ybWVycw0KcGlwMyBpbnN0YWxsIHRlbnNvcmZsb3c9PTIuMy4wDQo=
pip3 install --upgrade pip
pip3 install tensorflow-cpu
pip3 install transformers
pip3 install tensorflow==2.3.0
다 설치했으면 아래 코드를 test.py 파일에 담아서 저장하고
python3 test.py 해서 실행해봅니다
코드상에 넣은 text 의 내용에 대해서 questions 배열에 질문을 담으면 애가 글을 읽고 맞는 답을 내어줍니다
ZnJvbSB0cmFuc2Zvcm1lcnMgaW1wb3J0IEF1dG9Ub2tlbml6ZXIsIFRGQXV0b01vZGVsRm9yUXVlc3Rpb25BbnN3ZXJpbmcNCmltcG9ydCB0ZW5zb3JmbG93IGFzIHRmDQp0b2tlbml6ZXIgPSBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZCgiYmVydC1sYXJnZS11bmNhc2VkLXdob2xlLXdvcmQtbWFza2luZy1maW5ldHVuZWQtc3F1YWQiKQ0KbW9kZWwgPSBURkF1dG9Nb2RlbEZvclF1ZXN0aW9uQW5zd2VyaW5nLmZyb21fcHJldHJhaW5lZCgiYmVydC1sYXJnZS11bmNhc2VkLXdob2xlLXdvcmQtbWFza2luZy1maW5ldHVuZWQtc3F1YWQiKQ0KdGV4dCA9IHIiIiINClRoZSBFbXBpcmUgU3RhdGUgQnVpbGRpbmcgaXMgYSAxMDItc3RvcnkgQXJ0IERlY28gc2t5c2NyYXBlciBpbiBNaWR0b3duIE1hbmhhdHRhbiBpbiBOZXcgWW9yayBDaXR5Lg0KSXQgd2FzIGRlc2lnbmVkIGJ5IFNocmV2ZSwgTGFtYiAmIEhhcm1vbiBhbmQgYnVpbHQgZnJvbSAxOTMwIHRvIDE5MzEuDQp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gNCiAgICAgICAgdGYuYXJnbWF4KGFuc3dlcl9lbmRfc2NvcmVzLCBheGlzPTEpICsgMQ0KICAgICkubnVtcHkoKVswXSAgIyBHZXQgdGhlIG1vc3QgbGlrZWx5IGVuZCBvZiBhbnN3ZXIgd2l0aCB0aGUgYXJnbWF4IG9mIHRoZSBzY29yZQ0KICAgIGFuc3dlciA9IHRva2VuaXplci5jb252ZXJ0X3Rva2Vuc190b19zdHJpbmcodG9rZW5pemVyLmNvbnZlcnRfaWRzX3RvX3Rva2VucyhpbnB1dF9pZHNbYW5zd2VyX3N0YXJ0OmFuc3dlcl9lbmRdKSkNCiAgICBwcmludChmIlF1ZXN0aW9uOiB7cXVlc3Rpb259IikNCiAgICBwcmludChmIkFuc3dlcjoge2Fuc3dlcn0iKQ==
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"""
The Empire State Building is a 102-story Art Deco skyscraper in Midtown Manhattan in New York City.
It was designed by Shreve, Lamb & Harmon and built from 1930 to 1931.
Its name is derived from "Empire State", the nickname of the state of New York.
The building has a roof height of 1,250 feet (380 m) and stands a total of 1,454 feet (443.2 m) tall, including its antenna.
"""
questions = [
"What's the location of the building?",
"What's the name of the building?",
"How tall is this?"
]
for question in questions:
inputs = tokenizer(question, text, add_special_tokens=True, return_tensors="tf")
input_ids = inputs["input_ids"].numpy()[0]
text_tokens = tokenizer.convert_ids_to_tokens(input_ids)
answer_start_scores, answer_end_scores = model(inputs)
answer_start = tf.argmax(
answer_start_scores, axis=1
).numpy()[0] # Get the most likely beginning of answer with the argmax of the score
answer_end = (
tf.argmax(answer_end_scores, axis=1) + 1
).numpy()[0] # Get the most likely end of answer with the argmax of the score
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}")
애의 답은 다음과 같다
UXVlc3Rpb246IFdoYXQncyB0aGUgbG9jYXRpb24gb2YgdGhlIGJ1aWxkaW5nPw0KQW5zd2VyOiBtaWR0b3duIG1hbmhhdHRhbg0KUXVlc3Rpb246IFdoYXQncyB0aGUgbmFtZSBvZiB0aGUgYnVpbGRpbmc/DQpBbnN3ZXI6IHRoZSBlbXBpcmUgc3RhdGUgYnVpbGRpbmcNClF1ZXN0aW9uOiBIb3cgdGFsbCBpcyB0aGlzPw0KQW5zd2VyOiAxICwgNDU0IGZlZXQ=
Question: What's the location of the building?
Answer: midtown manhattan
Question: What's the name of the building?
Answer: the empire state building
Question: How tall is this?
Answer: 1 , 454 feet