Buckets:
요약
요약(Summarization)은 문서나 기사의 중요한 정보를 모두 담아 더 짧은 버전으로 만드는 작업입니다. 번역과 함께, 시퀀스-투-시퀀스(sequence-to-sequence) 작업으로 정의할 수 있는 또 다른 예시입니다. 요약은 다음과 같은 유형이 있습니다:
- 추출형: 문서에서 가장 관련성이 높은 정보를 추출합니다.
- Abstractive: 가장 관련성이 높은 정보를 포착하는 새로운 텍스트를 생성합니다.
이 가이드에서는 다음 내용을 보여 드립니다:
- BillSum 데이터셋의 캘리포니아 주 법안 하위 집합을 사용하여 T5를 추상적 요약(abstractive summarization)을 위해 fine-tune합니다.
- 파인튜닝한 모델을 추론에 사용하세요.
이 작업과 호환되는 모든 아키텍처와 체크포인트를 확인하려면 task-page를 확인하는 것을 권장합니다.
시작하기 전에 필요한 라이브러리가 모두 설치되어 있는지 확인하세요:
pip install transformers datasets evaluate rouge_score
모델을 업로드하고 커뮤니티와 공유할 수 있도록 Hugging Face 계정에 로그인하는 것을 권장합니다. 프롬프트가 표시되면 로그인할 토큰을 입력하세요:
>>> from huggingface_hub import notebook_login
>>> notebook_login()
BillSum 데이터셋 로드
🤗 Datasets 라이브러리에서 BillSum 데이터셋의 더 작은 California 주 법안 하위 집합을 로드하는 것으로 시작합니다:
>>> from datasets import load_dataset
>>> billsum = load_dataset("FiscalNote/billsum", split="ca_test")
train_test_split 메서드를 사용하여 데이터셋을 학습 세트와 테스트 세트로 나눕니다:
>>> billsum = billsum.train_test_split(test_size=0.2)
다음은 예시입니다:
>>> billsum["train"][0]
{'summary': 'Existing law authorizes state agencies to enter into contracts for the acquisition of goods or services upon approval by the Department of General Services. Existing law sets forth various requirements and prohibitions for those contracts, including, but not limited to, a prohibition on entering into contracts for the acquisition of goods or services of $100,000 or more with a contractor that discriminates between spouses and domestic partners or same-sex and different-sex couples in the provision of benefits. Existing law provides that a contract entered into in violation of those requirements and prohibitions is void and authorizes the state or any person acting on behalf of the state to bring a civil action seeking a determination that a contract is in violation and therefore void. Under existing law, a willful violation of those requirements and prohibitions is a misdemeanor.\nThis bill would also prohibit a state agency from entering into contracts for the acquisition of goods or services of $100,000 or more with a contractor that discriminates between employees on the basis of gender identity in the provision of benefits, as specified. By expanding the scope of a crime, this bill would impose a state-mandated local program.\nThe California Constitution requires the state to reimburse local agencies and school districts for certain costs mandated by the state. Statutory provisions establish procedures for making that reimbursement.\nThis bill would provide that no reimbursement is required by this act for a specified reason.',
'text': 'The people of the State of California do enact as follows:\n\n\nSECTION 1.\nSection 10295.35 is added to the Public Contract Code, to read:\n10295.35.\n(a) (1) Notwithstanding any other law, a state agency shall not enter into any contract for the acquisition of goods or services in the amount of one hundred thousand dollars ($100,000) or more with a contractor that, in the provision of benefits, discriminates between employees on the basis of an employee’s or dependent’s actual or perceived gender identity, including, but not limited to, the employee’s or dependent’s identification as transgender.\n(2) For purposes of this section, “contract” includes contracts with a cumulative amount of one hundred thousand dollars ($100,000) or more per contractor in each fiscal year.\n(3) For purposes of this section, an employee health plan is discriminatory if the plan is not consistent with Section 1365.5 of the Health and Safety Code and Section 10140 of the Insurance Code.\n(4) The requirements of this section shall apply only to those portions of a contractor’s operations that occur under any of the following conditions:\n(A) Within the state.\n(B) On real property outside the state if the property is owned by the state or if the state has a right to occupy the property, and if the contractor’s presence at that location is connected to a contract with the state.\n(C) Elsewhere in the United States where work related to a state contract is being performed.\n(b) Contractors shall treat as confidential, to the maximum extent allowed by law or by the requirement of the contractor’s insurance provider, any request by an employee or applicant for employment benefits or any documentation of eligibility for benefits submitted by an employee or applicant for employment.\n(c) After taking all reasonable measures to find a contractor that complies with this section, as determined by the state agency, the requirements of this section may be waived under any of the following circumstances:\n(1) There is only one prospective contractor willing to enter into a specific contract with the state agency.\n(2) The contract is necessary to respond to an emergency, as determined by the state agency, that endangers the public health, welfare, or safety, or the contract is necessary for the provision of essential services, and no entity that complies with the requirements of this section capable of responding to the emergency is immediately available.\n(3) The requirements of this section violate, or are inconsistent with, the terms or conditions of a grant, subvention, or agreement, if the agency has made a good faith attempt to change the terms or conditions of any grant, subvention, or agreement to authorize application of this section.\n(4) The contractor is providing wholesale or bulk water, power, or natural gas, the conveyance or transmission of the same, or ancillary services, as required for ensuring reliable services in accordance with good utility practice, if the purchase of the same cannot practically be accomplished through the standard competitive bidding procedures and the contractor is not providing direct retail services to end users.\n(d) (1) A contractor shall not be deemed to discriminate in the provision of benefits if the contractor, in providing the benefits, pays the actual costs incurred in obtaining the benefit.\n(2) If a contractor is unable to provide a certain benefit, despite taking reasonable measures to do so, the contractor shall not be deemed to discriminate in the provision of benefits.\n(e) (1) Every contract subject to this chapter shall contain a statement by which the contractor certifies that the contractor is in compliance with this section.\n(2) The department or other contracting agency shall enforce this section pursuant to its existing enforcement powers.\n(3) (A) If a contractor falsely certifies that it is in compliance with this section, the contract with that contractor shall be subject to Article 9 (commencing with Section 10420), unless, within a time period specified by the department or other contracting agency, the contractor provides to the department or agency proof that it has complied, or is in the process of complying, with this section.\n(B) The application of the remedies or penalties contained in Article 9 (commencing with Section 10420) to a contract subject to this chapter shall not preclude the application of any existing remedies otherwise available to the department or other contracting agency under its existing enforcement powers.\n(f) Nothing in this section is intended to regulate the contracting practices of any local jurisdiction.\n(g) This section shall be construed so as not to conflict with applicable federal laws, rules, or regulations. In the event that a court or agency of competent jurisdiction holds that federal law, rule, or regulation invalidates any clause, sentence, paragraph, or section of this code or the application thereof to any person or circumstances, it is the intent of the state that the court or agency sever that clause, sentence, paragraph, or section so that the remainder of this section shall remain in effect.\nSEC. 2.\nSection 10295.35 of the Public Contract Code shall not be construed to create any new enforcement authority or responsibility in the Department of General Services or any other contracting agency.\nSEC. 3.\nNo reimbursement is required by this act pursuant to Section 6 of Article XIII\u2009B of the California Constitution because the only costs that may be incurred by a local agency or school district will be incurred because this act creates a new crime or infraction, eliminates a crime or infraction, or changes the penalty for a crime or infraction, within the meaning of Section 17556 of the Government Code, or changes the definition of a crime within the meaning of Section 6 of Article XIII\u2009B of the California Constitution.',
'title': 'An act to add Section 10295.35 to the Public Contract Code, relating to public contracts.'}
사용할 두 가지 필드가 있습니다:
text: 모델의 입력이 될 청구서의 텍스트.summary:text의 압축된 버전으로, 모델의 학습 대상이 됩니다.
전처리
다음 단계는 text와 summary를 처리하기 위해 T5 토크나이저를 로드하는 것입니다:
>>> from transformers import AutoTokenizer
>>> checkpoint = "google-t5/t5-small"
>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint)
만들려는 전처리 함수는 다음을 수행해야 합니다:
- 입력 앞에 프롬프트를 붙여 T5가 이 작업이 요약 작업임을 알 수 있도록 합니다. 여러 NLP 작업을 수행할 수 있는 일부 모델은 특정 작업을 위해 프롬프팅이 필요합니다.
- 레이블을 토큰화할 때는
text_target키워드 인수를 사용하세요. max_length파라미터로 설정된 최대 길이를 초과하지 않도록 시퀀스를 잘라냅니다.
>>> prefix = "summarize: "
>>> def preprocess_function(examples):
... inputs = [prefix + doc for doc in examples["text"]]
... model_inputs = tokenizer(inputs, max_length=1024, truncation=True)
... labels = tokenizer(text_target=examples["summary"], max_length=128, truncation=True)
... model_inputs["labels"] = labels["input_ids"]
... return model_inputs
전체 데이터셋에 전처리 함수를 적용하려면 🤗 Datasets의 map 메서드를 사용합니다. batched=True를 설정하여 데이터셋의 여러 요소를 한 번에 처리하면 map 함수의 속도를 높일 수 있습니다:
>>> tokenized_billsum = billsum.map(preprocess_function, batched=True)
이제 DataCollatorForSeq2Seq를 사용하여 예시 배치(batch)를 생성합니다. 데이터셋 전체를 최대 길이에 맞게 패딩하는 대신, collation 단계에서 배치 내 가장 긴 길이에 맞춰 문장을 동적으로 패딩하는 것이 더 효율적입니다.
>>> from transformers import DataCollatorForSeq2Seq
>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint)
평가
학습 중에 메트릭을 포함하는 것은 모델 성능을 평가하는 데 도움이 되는 경우가 많습니다. 🤗 Evaluate 라이브러리를 사용하면 평가 방법을 빠르게 로드할 수 있습니다. 이 작업에서는 ROUGE 메트릭을 로드합니다(메트릭을 로드하고 계산하는 방법에 대해 자세히 알아보려면 🤗 Evaluate quick tour를 참고하세요):
>>> import evaluate
>>> rouge = evaluate.load("rouge")
이후 예측값과 라벨을 compute에 전달하여 ROUGE 지표를 계산하는 함수를 만듭니다:
>>> import numpy as np
>>> def compute_metrics(eval_pred):
... predictions, labels = eval_pred
... decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True)
... labels = np.where(labels != -100, labels, tokenizer.pad_token_id)
... decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
... result = rouge.compute(predictions=decoded_preds, references=decoded_labels, use_stemmer=True)
... prediction_lens = [np.count_nonzero(pred != tokenizer.pad_token_id) for pred in predictions]
... result["gen_len"] = np.mean(prediction_lens)
... return {k: round(v, 4) for k, v in result.items()}
compute_metrics 함수가 이제 준비되었습니다. 학습을 설정할 때 다시 이 함수를 사용하게 됩니다.
학습
Trainer를 사용하여 모델을 fine-tuning하는 방법에 익숙하지 않다면, 여기의 기본 튜토리얼을 참고하세요!
이제 모델 학습을 시작할 준비가 되었습니다! AutoModelForSeq2SeqLM으로 T5를 로드하세요:
>>> from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer
>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
이 시점에서 남은 단계는 단 세 가지입니다:
- Seq2SeqTrainingArguments에서 학습 하이퍼파라미터를 정의합니다. 필수 파라미터는 모델 저장 위치를 지정하는
output_dir하나뿐입니다.push_to_hub=True를 설정하여 이 모델을 Hub에 업로드할 수 있습니다(모델 업로드를 위해 Hugging Face에 로그인되어 있어야 합니다). 각 에포크가 끝날 때마다 Trainer는 ROUGE 메트릭을 평가하고 학습 체크포인트를 저장합니다. - 모델, 데이터셋, 토크나이저, 데이터 콜레이터,
compute_metrics함수와 함께 학습 인수를 Seq2SeqTrainer에 전달합니다. - 모델을 파인튜닝하려면 train()을 호출하세요.
>>> training_args = Seq2SeqTrainingArguments(
... output_dir="my_awesome_billsum_model",
... eval_strategy="epoch",
... learning_rate=2e-5,
... per_device_train_batch_size=16,
... per_device_eval_batch_size=16,
... weight_decay=0.01,
... save_total_limit=3,
... num_train_epochs=4,
... predict_with_generate=True,
... fp16=True, #change to bf16=True for XPU
... push_to_hub=True,
... )
>>> trainer = Seq2SeqTrainer(
... model=model,
... args=training_args,
... train_dataset=tokenized_billsum["train"],
... eval_dataset=tokenized_billsum["test"],
... processing_class=tokenizer,
... data_collator=data_collator,
... compute_metrics=compute_metrics,
... )
>>> trainer.train()
학습이 완료되면 push_to_hub() 메서드를 사용하여 모델을 Hub에 공유하여 누구나 모델을 사용할 수 있도록 하세요:
>>> trainer.push_to_hub()
요약 작업을 위해 모델을 fine-tune하는 방법에 대한 더 자세한 예제는 해당 PyTorch notebook을 참고하세요.
추론
좋습니다. 이제 모델을 fine-tuning 했으므로 추론에 사용할 수 있습니다!
요약하고 싶은 텍스트를 작성해 보세요. T5의 경우, 작업 유형에 따라 입력 앞에 접두사를 붙여야 합니다. 요약 작업의 경우, 아래에 표시된 대로 입력 앞에 접두사를 붙여야 합니다:
>>> text = "summarize: The Inflation Reduction Act lowers prescription drug costs, health care costs, and energy costs. It's the most aggressive action on tackling the climate crisis in American history, which will lift up American workers and create good-paying, union jobs across the country. It'll lower the deficit and ask the ultra-wealthy and corporations to pay their fair share. And no one making under $400,000 per year will pay a penny more in taxes."
텍스트를 토큰화하고 input_ids를 PyTorch 텐서로 반환합니다:
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("username/my_awesome_billsum_model")
>>> inputs = tokenizer(text, return_tensors="pt").input_ids
요약 생성에는 generate() 메서드를 사용합니다. 다양한 텍스트 생성 전략 및 생성을 제어하는 매개변수에 대한 자세한 내용은 Text Generation API를 참고하세요.
>>> from transformers import AutoModelForSeq2SeqLM
>>> model = AutoModelForSeq2SeqLM.from_pretrained("username/my_awesome_billsum_model")
>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=False)
생성된 토큰 id를 다시 텍스트로 디코딩합니다:
>>> tokenizer.decode(outputs[0], skip_special_tokens=True)
'the inflation reduction act lowers prescription drug costs, health care costs, and energy costs. it's the most aggressive action on tackling the climate crisis in american history. it will ask the ultra-wealthy and corporations to pay their fair share.'
Xet Storage Details
- Size:
- 18.7 kB
- Xet hash:
- f9d76b64c8890e2f94ac11a7572acb11ac7ae3b5e9089cd9c84e8cad28d54639
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.