Spaces:
Paused
Paused
File size: 24,797 Bytes
ee6e328 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
<!--Copyright 2022 The HuggingFace Team. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
โ ๏ธ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
rendered properly in your Markdown viewer.
-->
# ์์ฝ[[summarization]]
[[open-in-colab]]
<Youtube id="yHnr5Dk2zCI"/>
์์ฝ์ ๋ฌธ์๋ ๊ธฐ์ฌ์์ ์ค์ํ ์ ๋ณด๋ฅผ ๋ชจ๋ ํฌํจํ๋ ์งง๊ฒ ๋ง๋๋ ์ผ์
๋๋ค.
๋ฒ์ญ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก, ์ํ์ค-ํฌ-์ํ์ค ๋ฌธ์ ๋ก ๊ตฌ์ฑํ ์ ์๋ ๋ํ์ ์ธ ์์
์ค ํ๋์
๋๋ค.
์์ฝ์๋ ์๋์ ๊ฐ์ด ์ ํ์ด ์์ต๋๋ค:
- ์ถ์ถ(Extractive) ์์ฝ: ๋ฌธ์์์ ๊ฐ์ฅ ๊ด๋ จ์ฑ ๋์ ์ ๋ณด๋ฅผ ์ถ์ถํฉ๋๋ค.
- ์์ฑ(Abstractive) ์์ฝ: ๊ฐ์ฅ ๊ด๋ จ์ฑ ๋์ ์ ๋ณด๋ฅผ ํฌ์ฐฉํด๋ด๋ ์๋ก์ด ํ
์คํธ๋ฅผ ์์ฑํฉ๋๋ค.
์ด ๊ฐ์ด๋์์ ์๊ฐํ ๋ด์ฉ์ ์๋์ ๊ฐ์ต๋๋ค:
1. ์์ฑ ์์ฝ์ ์ํ [BillSum](https://huggingface.co/datasets/billsum) ๋ฐ์ดํฐ์
์ค ์บ๋ฆฌํฌ๋์ ์ฃผ ๋ฒ์ ํ์ ์งํฉ์ผ๋ก [T5](https://huggingface.co/t5-small)๋ฅผ ํ์ธํ๋ํฉ๋๋ค.
2. ํ์ธํ๋๋ ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์ถ๋ก ํฉ๋๋ค.
<Tip>
์ด ํํ ๋ฆฌ์ผ์์ ์ค๋ช
ํ๋ ์์
์ ๋ค์ ๋ชจ๋ธ ์ํคํ
์ฒ์์ ์ง์๋ฉ๋๋ค:
<!--This tip is automatically generated by `make fix-copies`, do not fill manually!-->
[BART](../model_doc/bart), [BigBird-Pegasus](../model_doc/bigbird_pegasus), [Blenderbot](../model_doc/blenderbot), [BlenderbotSmall](../model_doc/blenderbot-small), [Encoder decoder](../model_doc/encoder-decoder), [FairSeq Machine-Translation](../model_doc/fsmt), [GPTSAN-japanese](../model_doc/gptsan-japanese), [LED](../model_doc/led), [LongT5](../model_doc/longt5), [M2M100](../model_doc/m2m_100), [Marian](../model_doc/marian), [mBART](../model_doc/mbart), [MT5](../model_doc/mt5), [MVP](../model_doc/mvp), [NLLB](../model_doc/nllb), [NLLB-MOE](../model_doc/nllb-moe), [Pegasus](../model_doc/pegasus), [PEGASUS-X](../model_doc/pegasus_x), [PLBart](../model_doc/plbart), [ProphetNet](../model_doc/prophetnet), [SwitchTransformers](../model_doc/switch_transformers), [T5](../model_doc/t5), [XLM-ProphetNet](../model_doc/xlm-prophetnet)
<!--End of the generated tip-->
</Tip>
์์ํ๊ธฐ ์ ์ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ชจ๋ ์ค์น๋์ด ์๋์ง ํ์ธํ์ธ์:
```bash
pip install transformers datasets evaluate rouge_score
```
Hugging Face ๊ณ์ ์ ๋ก๊ทธ์ธํ๋ฉด ๋ชจ๋ธ์ ์
๋ก๋ํ๊ณ ์ปค๋ฎค๋ํฐ์ ๊ณต์ ํ ์ ์์ต๋๋ค.
ํ ํฐ์ ์
๋ ฅํ์ฌ ๋ก๊ทธ์ธํ์ธ์.
```py
>>> from huggingface_hub import notebook_login
>>> notebook_login()
```
## BillSum ๋ฐ์ดํฐ์
๊ฐ์ ธ์ค๊ธฐ[[load-billsum-dataset]]
๐ค Datasets ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ BillSum ๋ฐ์ดํฐ์
์ ์์ ๋ฒ์ ์ธ ์บ๋ฆฌํฌ๋์ ์ฃผ ๋ฒ์ ํ์ ์งํฉ์ ๊ฐ์ ธ์ค์ธ์:
```py
>>> from datasets import load_dataset
>>> billsum = load_dataset("billsum", split="ca_test")
```
[`~datasets.Dataset.train_test_split`] ๋ฉ์๋๋ก ๋ฐ์ดํฐ์
์ ํ์ต์ฉ์ ํ
์คํธ์ฉ์ผ๋ก ๋๋์ธ์:
```py
>>> billsum = billsum.train_test_split(test_size=0.2)
```
๊ทธ๋ฐ ๋ค์ ์์๋ฅผ ํ๋ ์ดํด๋ณด์ธ์:
```py
>>> 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`์ ๊ฐ๋ตํ ๋ฒ์ ์ผ๋ก ๋ชจ๋ธ์ ํ๊ฒ์ด ๋ฉ๋๋ค.
## ์ ์ฒ๋ฆฌ[[preprocess]]
๋ค์์ผ๋ก `text`์ `summary`๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํ T5 ํ ํฌ๋์ด์ ๋ฅผ ๊ฐ์ ธ์ต๋๋ค:
```py
>>> from transformers import AutoTokenizer
>>> checkpoint = "t5-small"
>>> tokenizer = AutoTokenizer.from_pretrained(checkpoint)
```
์์ฑํ๋ ค๋ ์ ์ฒ๋ฆฌ ํจ์๋ ์๋ ์กฐ๊ฑด์ ๋ง์กฑํด์ผ ํฉ๋๋ค:
1. ์
๋ ฅ ์์ ํ๋กฌํํธ๋ฅผ ๋ถ์ฌ T5๊ฐ ์์ฝ ์์
์์ ์ธ์ํ ์ ์๋๋ก ํฉ๋๋ค. ์ฌ๋ฌ NLP ์์
์ ์ํํ ์ ์๋ ์ผ๋ถ ๋ชจ๋ธ์ ํน์ ์์
์ ๋ํ ํ๋กฌํํธ๊ฐ ํ์ํฉ๋๋ค.
2. ๋ ์ด๋ธ์ ํ ํฐํํ ๋ `text_target` ์ธ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
3. `max_length` ๋งค๊ฐ๋ณ์๋ก ์ค์ ๋ ์ต๋ ๊ธธ์ด๋ฅผ ๋์ง ์๋๋ก ๊ธด ์ํ์ค๋ฅผ ์๋ผ๋
๋๋ค.
```py
>>> 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์ [`~datasets.Dataset.map`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ธ์.
`batched=True`๋ก ์ค์ ํ์ฌ ๋ฐ์ดํฐ์
์ ์ฌ๋ฌ ์์๋ฅผ ํ ๋ฒ์ ์ฒ๋ฆฌํ๋ฉด `map` ํจ์์ ์๋๋ฅผ ๋์ผ ์ ์์ต๋๋ค.
```py
>>> tokenized_billsum = billsum.map(preprocess_function, batched=True)
```
์ด์ [`DataCollatorForSeq2Seq`]๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๋ฐฐ์น๋ฅผ ๋ง๋์ธ์.
์ ์ฒด ๋ฐ์ดํฐ์
์ ์ต๋ ๊ธธ์ด๋ก ํจ๋ฉํ๋ ๊ฒ๋ณด๋ค ๋ฐฐ์น๋ง๋ค ๊ฐ์ฅ ๊ธด ๋ฌธ์ฅ ๊ธธ์ด์ ๋ง์ถฐ *๋์ ํจ๋ฉ*ํ๋ ๊ฒ์ด ๋ ํจ์จ์ ์
๋๋ค.
<frameworkcontent>
<pt>
```py
>>> from transformers import DataCollatorForSeq2Seq
>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint)
```
</pt>
<tf>
```py
>>> from transformers import DataCollatorForSeq2Seq
>>> data_collator = DataCollatorForSeq2Seq(tokenizer=tokenizer, model=checkpoint, return_tensors="tf")
```
</tf>
</frameworkcontent>
## ํ๊ฐ[[evaluate]]
ํ์ต ์ค์ ํ๊ฐ ์งํ๋ฅผ ํฌํจํ๋ฉด ๋ชจ๋ธ์ ์ฑ๋ฅ์ ํ๊ฐํ๋ ๋ฐ ๋์์ด ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
๐ค [Evaluate](https://huggingface.co/docs/evaluate/index) ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ฉด ํ๊ฐ ๋ฐฉ๋ฒ์ ๋น ๋ฅด๊ฒ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค.
์ด ์์
์์๋ [ROUGE](https://huggingface.co/spaces/evaluate-metric/rouge) ํ๊ฐ ์งํ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
(ํ๊ฐ ์งํ๋ฅผ ๋ถ๋ฌ์ค๊ณ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ๐ค Evaluate [๋๋ฌ๋ณด๊ธฐ](https://huggingface.co/docs/evaluate/a_quick_tour)๋ฅผ ์ฐธ์กฐํ์ธ์.)
```py
>>> import evaluate
>>> rouge = evaluate.load("rouge")
```
๊ทธ๋ฐ ๋ค์ ์์ธก๊ฐ๊ณผ ๋ ์ด๋ธ์ [`~evaluate.EvaluationModule.compute`]์ ์ ๋ฌํ์ฌ ROUGE ์งํ๋ฅผ ๊ณ์ฐํ๋ ํจ์๋ฅผ ๋ง๋ญ๋๋ค:
```py
>>> 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` ํจ์๋ฅผ ์ฌ์ฉํ ์ค๋น๊ฐ ๋์์ผ๋ฉฐ, ํ์ต์ ์ค์ ํ ๋ ์ด ํจ์๋ก ๋๋์์ฌ ๊ฒ์
๋๋ค.
## ํ์ต[[train]]
<frameworkcontent>
<pt>
<Tip>
๋ชจ๋ธ์ [`Trainer`]๋ก ํ์ธํ๋ ํ๋ ๊ฒ์ด ์ต์ํ์ง ์๋ค๋ฉด, [์ฌ๊ธฐ](../training#train-with-pytorch-trainer)์์ ๊ธฐ๋ณธ ํํ ๋ฆฌ์ผ์ ํ์ธํด๋ณด์ธ์!
</Tip>
์ด์ ๋ชจ๋ธ ํ์ต์ ์์ํ ์ค๋น๊ฐ ๋์์ต๋๋ค! [`AutoModelForSeq2SeqLM`]๋ก T5๋ฅผ ๊ฐ์ ธ์ค์ธ์:
```py
>>> from transformers import AutoModelForSeq2SeqLM, Seq2SeqTrainingArguments, Seq2SeqTrainer
>>> model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
```
์ด์ ์ธ ๋จ๊ณ๋ง ๋จ์์ต๋๋ค:
1. [`Seq2SeqTrainingArguments`]์์ ํ์ต ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ ์ํ์ธ์.
์ ์ผํ ํ์ ๋งค๊ฐ๋ณ์๋ ๋ชจ๋ธ์ ์ ์ฅํ ์์น๋ฅผ ์ง์ ํ๋ `output_dir`์
๋๋ค.
`push_to_hub=True`๋ฅผ ์ค์ ํ์ฌ ์ด ๋ชจ๋ธ์ Hub์ ํธ์ํ ์ ์์ต๋๋ค(๋ชจ๋ธ์ ์
๋ก๋ํ๋ ค๋ฉด Hugging Face์ ๋ก๊ทธ์ธํด์ผ ํฉ๋๋ค.)
[`Trainer`]๋ ๊ฐ ์ํญ์ด ๋๋ ๋๋ง๋ค ROUGE ์งํ๋ฅผ ํ๊ฐํ๊ณ ํ์ต ์ฒดํฌํฌ์ธํธ๋ฅผ ์ ์ฅํฉ๋๋ค.
2. ๋ชจ๋ธ, ๋ฐ์ดํฐ์
, ํ ํฌ๋์ด์ , ๋ฐ์ดํฐ ์ฝ๋ ์ดํฐ ๋ฐ `compute_metrics` ํจ์์ ํจ๊ป ํ์ต ์ธ์๋ฅผ [`Seq2SeqTrainer`]์ ์ ๋ฌํ์ธ์.
3. [`~Trainer.train`]์ ํธ์ถํ์ฌ ๋ชจ๋ธ์ ํ์ธํ๋ํ์ธ์.
```py
>>> training_args = Seq2SeqTrainingArguments(
... output_dir="my_awesome_billsum_model",
... evaluation_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,
... push_to_hub=True,
... )
>>> trainer = Seq2SeqTrainer(
... model=model,
... args=training_args,
... train_dataset=tokenized_billsum["train"],
... eval_dataset=tokenized_billsum["test"],
... tokenizer=tokenizer,
... data_collator=data_collator,
... compute_metrics=compute_metrics,
... )
>>> trainer.train()
```
ํ์ต์ด ์๋ฃ๋๋ฉด, ๋๊ตฌ๋ ๋ชจ๋ธ์ ์ฌ์ฉํ ์ ์๋๋ก [`~transformers.Trainer.push_to_hub`] ๋ฉ์๋๋ก Hub์ ๊ณต์ ํฉ๋๋ค:
```py
>>> trainer.push_to_hub()
```
</pt>
<tf>
<Tip>
Keras๋ก ๋ชจ๋ธ ํ์ธํ๋์ ํ๋ ๊ฒ์ด ์ต์ํ์ง ์๋ค๋ฉด, [์ฌ๊ธฐ](../training#train-a-tensorflow-model-with-keras)์์ ๊ธฐ๋ณธ์ ์ธ ํํ ๋ฆฌ์ผ์ ํ์ธํ์ธ์!
</Tip>
TensorFlow์์ ๋ชจ๋ธ์ ํ์ธํ๋ํ๋ ค๋ฉด, ๋จผ์ ์ตํฐ๋ง์ด์ , ํ์ต๋ฅ ์ค์ผ์ค ๊ทธ๋ฆฌ๊ณ ๋ช ๊ฐ์ง ํ์ต ํ์ดํผํ๋ผ๋ฏธํฐ๋ฅผ ์ค์ ํ์ธ์:
```py
>>> from transformers import create_optimizer, AdamWeightDecay
>>> optimizer = AdamWeightDecay(learning_rate=2e-5, weight_decay_rate=0.01)
```
๊ทธ๋ฐ ๋ค์ [`TFAutoModelForSeq2SeqLM`]์ ์ฌ์ฉํ์ฌ T5๋ฅผ ๊ฐ์ ธ์ค์ธ์:
```py
>>> from transformers import TFAutoModelForSeq2SeqLM
>>> model = TFAutoModelForSeq2SeqLM.from_pretrained(checkpoint)
```
[`~transformers.TFPreTrainedModel.prepare_tf_dataset`]์ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ์
์ `tf.data.Dataset` ํ์์ผ๋ก ๋ณํํ์ธ์:
```py
>>> tf_train_set = model.prepare_tf_dataset(
... tokenized_billsum["train"],
... shuffle=True,
... batch_size=16,
... collate_fn=data_collator,
... )
>>> tf_test_set = model.prepare_tf_dataset(
... tokenized_billsum["test"],
... shuffle=False,
... batch_size=16,
... collate_fn=data_collator,
... )
```
[`compile`](https://keras.io/api/models/model_training_apis/#compile-method)์ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ์ ํ์ตํ ์ ์๋๋ก ๊ตฌ์ฑํ์ธ์:
```py
>>> import tensorflow as tf
>>> model.compile(optimizer=optimizer)
```
ํ์ต์ ์์ํ๊ธฐ ์ ์ ์ค์ ํด์ผ ํ ๋ง์ง๋ง ๋ ๊ฐ์ง๋ ์์ธก์์ ROUGE ์ ์๋ฅผ ๊ณ์ฐํ๊ณ ๋ชจ๋ธ์ Hub์ ํธ์ํ๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ ๊ฒ์
๋๋ค.
๋ ์์
๋ชจ๋ [Keras callbacks](../main_classes/keras_callbacks)์ผ๋ก ์ํํ ์ ์์ต๋๋ค.
[`~transformers.KerasMetricCallback`]์ `compute_metrics` ํจ์๋ฅผ ์ ๋ฌํ์ธ์:
```py
>>> from transformers.keras_callbacks import KerasMetricCallback
>>> metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_validation_set)
```
[`~transformers.PushToHubCallback`]์์ ๋ชจ๋ธ๊ณผ ํ ํฌ๋์ด์ ๋ฅผ ํธ์ํ ์์น๋ฅผ ์ง์ ํ์ธ์:
```py
>>> from transformers.keras_callbacks import PushToHubCallback
>>> push_to_hub_callback = PushToHubCallback(
... output_dir="my_awesome_billsum_model",
... tokenizer=tokenizer,
... )
```
๊ทธ๋ฐ ๋ค์ ์ฝ๋ฐฑ์ ๋ฒ๋ค๋ก ๋ฌถ์ด์ค๋๋ค:
```py
>>> callbacks = [metric_callback, push_to_hub_callback]
```
๋๋์ด ๋ชจ๋ธ ํ์ต์ ์์ํ ์ค๋น๊ฐ ๋์์ต๋๋ค!
ํ์ต ๋ฐ ๊ฒ์ฆ ๋ฐ์ดํฐ์
, ์ํญ ์ ๋ฐ ์ฝ๋ฐฑ๊ณผ ํจ๊ป [`fit`](https://keras.io/api/models/model_training_apis/#fit-method)์ ํธ์ถํ์ฌ ๋ชจ๋ธ์ ํ์ธํ๋ํ์ธ์.
```py
>>> model.fit(x=tf_train_set, validation_data=tf_test_set, epochs=3, callbacks=callbacks)
```
ํ์ต์ด ์๋ฃ๋๋ฉด ๋ชจ๋ธ์ด ์๋์ผ๋ก Hub์ ์
๋ก๋๋์ด ๋๊ตฌ๋ ์ฌ์ฉํ ์ ์๊ฒ ๋ฉ๋๋ค!
</tf>
</frameworkcontent>
<Tip>
์์ฝ์ ์ํด ๋ชจ๋ธ์ ํ์ธํ๋ํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๋ ์์ธํ ์์ ๋ฅผ ๋ณด๋ ค๋ฉด [PyTorch notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization.ipynb)
๋๋ [TensorFlow notebook](https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/summarization-tf.ipynb)์ ์ฐธ๊ณ ํ์ธ์.
</Tip>
## ์ถ๋ก [[inference]]
์ข์์, ์ด์ ๋ชจ๋ธ์ ํ์ธํ๋ํ์ผ๋ ์ถ๋ก ์ ์ฌ์ฉํ ์ ์์ต๋๋ค!
์์ฝํ ํ
์คํธ๋ฅผ ์์ฑํด๋ณด์ธ์. T5์ ๊ฒฝ์ฐ ์์
์ ๋ฐ๋ผ ์
๋ ฅ ์์ ์ ๋์ฌ๋ฅผ ๋ถ์ฌ์ผ ํฉ๋๋ค. ์์ฝ์ ๊ฒฝ์ฐ, ์๋์ ๊ฐ์ ์ ๋์ฌ๋ฅผ ์
๋ ฅ ์์ ๋ถ์ฌ์ผ ํฉ๋๋ค:
```py
>>> 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."
```
์ถ๋ก ์ ์ํด ํ์ธํ๋ํ ๋ชจ๋ธ์ ์ํํด ๋ณด๋ ๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ [`pipeline`]์์ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค.
๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์์ฝ์ ์ํํ [`pipeline`]์ ์ธ์คํด์คํํ๊ณ ํ
์คํธ๋ฅผ ์ ๋ฌํ์ธ์:
```py
>>> from transformers import pipeline
>>> summarizer = pipeline("summarization", model="stevhliu/my_awesome_billsum_model")
>>> summarizer(text)
[{"summary_text": "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."}]
```
์ํ๋ค๋ฉด ์๋์ผ๋ก ๋ค์๊ณผ ๊ฐ์ ์์
์ ์ํํ์ฌ [`pipeline`]์ ๊ฒฐ๊ณผ์ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค:
<frameworkcontent>
<pt>
ํ
์คํธ๋ฅผ ํ ํฌ๋์ด์ฆํ๊ณ `input_ids`๋ฅผ PyTorch ํ
์๋ก ๋ฐํํฉ๋๋ค:
```py
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_billsum_model")
>>> inputs = tokenizer(text, return_tensors="pt").input_ids
```
์์ฝ๋ฌธ์ ์์ฑํ๋ ค๋ฉด [`~transformers.generation_utils.GenerationMixin.generate`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ธ์.
ํ
์คํธ ์์ฑ์ ๋ํ ๋ค์ํ ์ ๋ต๊ณผ ์์ฑ์ ์ ์ดํ๊ธฐ ์ํ ๋งค๊ฐ๋ณ์์ ๋ํ ์์ธํ ๋ด์ฉ์ [ํ
์คํธ ์์ฑ](../main_classes/text_generation) API๋ฅผ ์ฐธ์กฐํ์ธ์.
```py
>>> from transformers import AutoModelForSeq2SeqLM
>>> model = AutoModelForSeq2SeqLM.from_pretrained("stevhliu/my_awesome_billsum_model")
>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=False)
```
์์ฑ๋ ํ ํฐ ID๋ฅผ ํ
์คํธ๋ก ๋์ฝ๋ฉํฉ๋๋ค:
```py
>>> 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.'
```
</pt>
<tf>
ํ
์คํธ๋ฅผ ํ ํฌ๋์ด์ฆํ๊ณ `input_ids`๋ฅผ TensorFlow ํ
์๋ก ๋ฐํํฉ๋๋ค:
```py
>>> from transformers import AutoTokenizer
>>> tokenizer = AutoTokenizer.from_pretrained("stevhliu/my_awesome_billsum_model")
>>> inputs = tokenizer(text, return_tensors="tf").input_ids
```
์์ฝ๋ฌธ์ ์์ฑํ๋ ค๋ฉด [`~transformers.generation_tf_utils.TFGenerationMixin.generate`] ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ธ์.
ํ
์คํธ ์์ฑ์ ๋ํ ๋ค์ํ ์ ๋ต๊ณผ ์์ฑ์ ์ ์ดํ๊ธฐ ์ํ ๋งค๊ฐ๋ณ์์ ๋ํ ์์ธํ ๋ด์ฉ์ [ํ
์คํธ ์์ฑ](../main_classes/text_generation) API๋ฅผ ์ฐธ์กฐํ์ธ์.
```py
>>> from transformers import TFAutoModelForSeq2SeqLM
>>> model = TFAutoModelForSeq2SeqLM.from_pretrained("stevhliu/my_awesome_billsum_model")
>>> outputs = model.generate(inputs, max_new_tokens=100, do_sample=False)
```
์์ฑ๋ ํ ํฐ ID๋ฅผ ํ
์คํธ๋ก ๋์ฝ๋ฉํฉ๋๋ค:
```py
>>> 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.'
```
</tf>
</frameworkcontent>
|