Buckets:
| # Trainer APIతో మోడల్ ఫైన్-ట్యూనింగ్[[fine-tuning-a-model-with-the-trainer-api]] | |
| 🤗 Transformers లైబ్రరీలోని `Trainer` క్లాస్ మీ డేటాసెట్పై ప్రీ-ట్రైన్డ్ మోడల్ను సులభంగా ఫైన్-ట్యూన్ చేయడానికి ఉపయోగపడుతుంది. | |
| ముందు సెక్షన్లో డేటా ప్రీ-ప్రాసెసింగ్ పూర్తయిన తర్వాత, Trainer నిర్వచించడానికి కేవలం కొన్ని స్టెప్స్ మాత్రమే మిగిలి ఉంటాయి. | |
| CPUలో `Trainer.train()` చాలా నెమ్మదిగా రన్ అవుతుంది. GPU లేకపోతే [Google Colab](https://colab.research.google.com/)లో ఉచిత GPU/TPU ఉపయోగించవచ్చు. | |
| > [!TIP] | |
| > 📚 **శిక్షణ రిసోర్సెస్**: ప్రారంభించే ముందు [🤗 Transformers శిక్షణ గైడ్](https://huggingface.co/docs/transformers/main/en/training) మరియు [ఫైన్-ట్యూనింగ్ కుక్బుక్](https://huggingface.co/learn/cookbook/en/fine_tuning_code_llm_on_single_gpu) చూడండి. | |
| ముందు సెక్షన్లో రన్ చేసిన కోడ్ రీక్యాప్: | |
| ```py | |
| from datasets import load_dataset | |
| from transformers import AutoTokenizer, DataCollatorWithPadding | |
| raw_datasets = load_dataset("glue", "mrpc") | |
| checkpoint = "bert-base-uncased" | |
| tokenizer = AutoTokenizer.from_pretrained(checkpoint) | |
| def tokenize_function(example): | |
| return tokenizer(example["sentence1"], example["sentence2"], truncation=True) | |
| tokenized_datasets = raw_datasets.map(tokenize_function, batched=True) | |
| data_collator = DataCollatorWithPadding(tokenizer=tokenizer) | |
| ``` | |
| ### శిక్షణ[[training]] | |
| Trainer నిర్వచించే ముందు, `TrainingArguments` క్లాస్ సృష్టించాలి. ఇది శిక్షణ & ఎవాల్యుయేషన్ కోసం అన్ని హైపర్పారామీటర్స్ కలిగి ఉంటుంది. | |
| ```py | |
| from transformers import TrainingArguments | |
| training_args = TrainingArguments("test-trainer") | |
| ``` | |
| `push_to_hub=True` ఇస్తే శిక్షణ సమయంలోనే మోడల్ Hugging Face Hubకి ఆటోమేటిక్గా అప్లోడ్ అవుతుంది. | |
| > [!TIP] | |
| > 🚀 **అడ్వాన్స్డ్ కాన్ఫిగరేషన్**: అన్ని ఆప్షన్స్ కోసం [TrainingArguments డాక్యుమెంటేషన్](https://huggingface.co/docs/transformers/main/en/main_classes/trainer#transformers.TrainingArguments) చూడండి. | |
| మోడల్ నిర్వచించడం: | |
| ```py | |
| from transformers import AutoModelForSequenceClassification | |
| model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) | |
| ``` | |
| > వార్నింగ్ వస్తుంది – ఎందుకంటే BERT ప్రీ-ట్రైన్డ్ కాదు sentence pair classification కోసం. పాత హెడ్ తీసేసి, కొత్త sequence classification హెడ్ జోడించారు. ఇప్పుడు శిక్షణ ప్రారంభించాలి. | |
| Trainer సృష్టించడం: | |
| ```py | |
| from transformers import Trainer | |
| trainer = Trainer( | |
| model, | |
| training_args, | |
| train_dataset=tokenized_datasets["train"], | |
| eval_dataset=tokenized_datasets["validation"], | |
| data_collator=data_collator, | |
| processing_class=tokenizer, # కొత్త ఫీచర్ – ఏ టోకెనైజర్ వాడాలో చెప్పడానికి | |
| ) | |
| ``` | |
| `processing_class` ఇవ్వడం ద్వారా `data_collator` ఆటోమేటిక్గా `DataCollatorWithPadding` అవుతుంది. | |
| శిక్షణ ప్రారంభించడానికి: | |
| ```py | |
| trainer.train() | |
| ``` | |
| **ఇది శిక్షణ ప్రారంభిస్తుంది**, కానీ ఎవాల్యుయేషన్ మెట్రిక్స్ రావు ఎందుకంటే: | |
| 1. `eval_strategy` సెట్ చేయలేదు | |
| 2. `compute_metrics` ఫంక్షన్ ఇవ్వలేదు | |
| --- | |
| ### ఎవాల్యుయేషన్[[evaluation]] | |
| ```py | |
| predictions = trainer.predict(tokenized_datasets["validation"]) | |
| print(predictions.predictions.shape, predictions.label_ids.shape) | |
| # → (408, 2) (408,) | |
| ``` | |
| ```py | |
| import numpy as np | |
| preds = np.argmax(predictions.predictions, axis=-1) | |
| ``` | |
| ```py | |
| import evaluate | |
| metric = evaluate.load("glue", "mrpc") | |
| metric.compute(predictions=preds, references=predictions.label_ids) | |
| # → {'accuracy': 0.8578, 'f1': 0.8996} | |
| ``` | |
| పూర్తి `compute_metrics` ఫంక్షన్: | |
| ```py | |
| def compute_metrics(eval_preds): | |
| metric = evaluate.load("glue", "mrpc") | |
| logits, labels = eval_preds | |
| predictions = np.argmax(logits, axis=-1) | |
| return metric.compute(predictions=predictions, references=labels) | |
| ``` | |
| --- | |
| ### కొత్త Trainerతో metricsతో శిక్షణ | |
| ```py | |
| training_args = TrainingArguments("test-trainer", eval_strategy="epoch") | |
| model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2) | |
| trainer = Trainer( | |
| model, | |
| training_args, | |
| train_dataset=tokenized_datasets["train"], | |
| eval_dataset=tokenized_datasets["validation"], | |
| data_collator=data_collator, | |
| processing_class=tokenizer, | |
| compute_metrics=compute_metrics, | |
| ) | |
| trainer.train() | |
| ``` | |
| ఇప్పుడు ప్రతి ఎపాక్ చివర validation loss + metrics కనిపిస్తాయి! | |
| --- | |
| ### అడ్వాన్స్డ్ శిక్షణ ఫీచర్స్[[advanced-training-features]] | |
| **మిక్స్డ్ ప్రెసిషన్ (fp16)** – వేగం + మెమరీ ఆదా: | |
| ```py | |
| training_args = TrainingArguments( | |
| "test-trainer", | |
| eval_strategy="epoch", | |
| fp16=True, # మిక్స్డ్ ప్రెసిషన్ ఆన్ | |
| ) | |
| ``` | |
| **గ్రేడియంట్ అక్యుములేషన్** – చిన్న GPUలో ఎక్కువ effective batch size: | |
| ```py | |
| training_args = TrainingArguments( | |
| "test-trainer", | |
| per_device_train_batch_size=4, | |
| gradient_accumulation_steps=4, # effective batch size = 16 | |
| ) | |
| ``` | |
| **లెర్నింగ్ రేట్ షెడ్యూలర్**: | |
| ```py | |
| training_args = TrainingArguments( | |
| "test-trainer", | |
| learning_rate=2e-5, | |
| lr_scheduler_type="cosine", | |
| ) | |
| ``` | |
| > [!TIP] | |
| > 🎯 **పర్ఫార్మెన్స్ ఆప్టిమైజేషన్**: డిస్ట్రిబ్యూటెడ్ ట్రైనింగ్, మెమరీ ఆప్టిమైజేషన్ కోసం [🤗 Transformers performance guide](https://huggingface.co/docs/transformers/main/en/performance) చూడండి. | |
| Trainer మల్టీ-GPU/TPUలో ఔట్-ఆఫ్-ది-బాక్స్ పని చేస్తుంది – చాప్టర్ 10లో వివరంగా. | |
| --- | |
| ## సెక్షన్ క్విజ్[[section-quiz]] | |
| ### 1. Trainerలో `processing_class` పరామీటర్ దేనికి? | |
| ### 2. ఎవాల్యుయేషన్ ఎప్పుడు జరగాలో ఏ పారామీటర్ నిర్ణయిస్తుంది? | |
| ### 3. `fp16=True` ఏమి చేస్తుంది? | |
| ### 4. `compute_metrics` ఫంక్షన్ పాత్ర ఏమిటి? | |
| ### 5. `eval_dataset` ఇవ్వకపోతే ఏమవుతుంది? | |
| ### 6. గ్రేడియంట్ అక్యుములేషన్ అంటే ఏమిటి? | |
| > [!TIP] | |
| > 💡 **ముఖ్య పాయింట్లు**: | |
| > | |
| > * `Trainer` API అతి సులభమైన, శక్తివంతమైన ఇంటర్ఫేస్ | |
| > * `processing_class` ద్వారా టోకెనైజర్ స్పష్టంగా చెప్పండి | |
| > * `TrainingArguments`లో learning rate, batch size, fp16, gradient accumulation సెట్ చేయండి | |
| > * `compute_metrics` ద్వారా కావలసిన metrics చూడవచ్చు | |
| > * ఆధునిక optimizations (fp16, gradient accumulation) ఒక్క లైన్లో ఆన్ చేయవచ్చు | |
Xet Storage Details
- Size:
- 8.81 kB
- Xet hash:
- 7694d7593023b7f77ed716d355b10fe5449fc7cc18f6b06d51c7e0f82eefc7b3
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.