|
169 | 169 | "cell_type": "markdown", |
170 | 170 | "metadata": {}, |
171 | 171 | "source": [ |
172 | | - "### ***Importing required modules for model training and evaluation***" |
| 172 | + "### ***Loading the dataset for English to Spanish translation task***" |
173 | 173 | ] |
174 | 174 | }, |
175 | 175 | { |
176 | 176 | "cell_type": "code", |
177 | | - "execution_count": null, |
178 | | - "metadata": { |
179 | | - "id": "AWfPzkluGKuk" |
180 | | - }, |
181 | | - "outputs": [], |
182 | | - "source": [ |
183 | | - "from transformers import AutoTokenizer\n", |
184 | | - "from transformers import AutoModelForSeq2SeqLM\n", |
185 | | - "from transformers import Seq2SeqTrainer\n", |
186 | | - "from transformers import Seq2SeqTrainingArguments\n", |
187 | | - "from transformers import DataCollatorForSeq2Seq\n", |
188 | | - "from datasets import load_dataset\n", |
189 | | - "from peft import get_peft_model\n", |
190 | | - "from peft import LoraConfig\n", |
191 | | - "from peft import TaskType\n", |
192 | | - "from peft import prepare_model_for_kbit_training\n", |
193 | | - "import evaluate\n", |
194 | | - "import numpy as np\n", |
195 | | - "import torch" |
196 | | - ] |
197 | | - }, |
198 | | - { |
199 | | - "cell_type": "markdown", |
| 177 | + "execution_count": 1, |
200 | 178 | "metadata": {}, |
| 179 | + "outputs": [ |
| 180 | + { |
| 181 | + "name": "stderr", |
| 182 | + "output_type": "stream", |
| 183 | + "text": [ |
| 184 | + "c:\\Users\\emon1\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n", |
| 185 | + " from .autonotebook import tqdm as notebook_tqdm\n" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
201 | 189 | "source": [ |
202 | | - "### ***Loading the dataset for English to Spanish translation task***" |
| 190 | + "from datasets import load_dataset" |
203 | 191 | ] |
204 | 192 | }, |
205 | 193 | { |
|
316 | 304 | "### ***Loading pre-trained model and tokenizer from Hugging Face Model Hub***" |
317 | 305 | ] |
318 | 306 | }, |
| 307 | + { |
| 308 | + "cell_type": "code", |
| 309 | + "execution_count": null, |
| 310 | + "metadata": {}, |
| 311 | + "outputs": [], |
| 312 | + "source": [ |
| 313 | + "from transformers import AutoTokenizer" |
| 314 | + ] |
| 315 | + }, |
319 | 316 | { |
320 | 317 | "cell_type": "code", |
321 | 318 | "execution_count": null, |
|
560 | 557 | "### ***Load Pretrained Model***" |
561 | 558 | ] |
562 | 559 | }, |
| 560 | + { |
| 561 | + "cell_type": "code", |
| 562 | + "execution_count": null, |
| 563 | + "metadata": {}, |
| 564 | + "outputs": [], |
| 565 | + "source": [ |
| 566 | + "from transformers import AutoModelForSeq2SeqLM" |
| 567 | + ] |
| 568 | + }, |
563 | 569 | { |
564 | 570 | "cell_type": "code", |
565 | 571 | "execution_count": null, |
|
636 | 642 | "### ***Apply PEFT + LoRA Configuration***" |
637 | 643 | ] |
638 | 644 | }, |
| 645 | + { |
| 646 | + "cell_type": "code", |
| 647 | + "execution_count": null, |
| 648 | + "metadata": {}, |
| 649 | + "outputs": [], |
| 650 | + "source": [ |
| 651 | + "from peft import LoraConfig, TaskType" |
| 652 | + ] |
| 653 | + }, |
639 | 654 | { |
640 | 655 | "cell_type": "code", |
641 | 656 | "execution_count": null, |
|
647 | 662 | "lora_config = LoraConfig(\n", |
648 | 663 | " r=8,\n", |
649 | 664 | " lora_alpha=32,\n", |
650 | | - " target_modules=[\"q_proj\", \"v_proj\"], # Adjust for model type\n", |
| 665 | + " target_modules=[\"q_proj\", \"v_proj\"],\n", |
651 | 666 | " lora_dropout=0.1,\n", |
652 | 667 | " bias=\"none\",\n", |
653 | 668 | " task_type=TaskType.SEQ_2_SEQ_LM\n", |
654 | 669 | ")" |
655 | 670 | ] |
656 | 671 | }, |
| 672 | + { |
| 673 | + "cell_type": "code", |
| 674 | + "execution_count": null, |
| 675 | + "metadata": {}, |
| 676 | + "outputs": [], |
| 677 | + "source": [ |
| 678 | + "from peft import get_peft_model" |
| 679 | + ] |
| 680 | + }, |
657 | 681 | { |
658 | 682 | "cell_type": "code", |
659 | 683 | "execution_count": 8, |
|
685 | 709 | "### ***Training Arguments***" |
686 | 710 | ] |
687 | 711 | }, |
| 712 | + { |
| 713 | + "cell_type": "code", |
| 714 | + "execution_count": null, |
| 715 | + "metadata": {}, |
| 716 | + "outputs": [], |
| 717 | + "source": [ |
| 718 | + "from transformers import Seq2SeqTrainingArguments\n", |
| 719 | + "import torch" |
| 720 | + ] |
| 721 | + }, |
688 | 722 | { |
689 | 723 | "cell_type": "code", |
690 | 724 | "execution_count": null, |
|
705 | 739 | " logging_steps=100,\n", |
706 | 740 | " save_strategy=\"epoch\",\n", |
707 | 741 | " predict_with_generate=True,\n", |
708 | | - " fp16=torch.cuda.is_available(), # Optional: if CUDA available\n", |
| 742 | + " fp16=torch.cuda.is_available(),\n", |
709 | 743 | ")" |
710 | 744 | ] |
711 | 745 | }, |
|
716 | 750 | "### ***Data Collator***" |
717 | 751 | ] |
718 | 752 | }, |
| 753 | + { |
| 754 | + "cell_type": "code", |
| 755 | + "execution_count": null, |
| 756 | + "metadata": {}, |
| 757 | + "outputs": [], |
| 758 | + "source": [ |
| 759 | + "from transformers import DataCollatorForSeq2Seq" |
| 760 | + ] |
| 761 | + }, |
719 | 762 | { |
720 | 763 | "cell_type": "code", |
721 | 764 | "execution_count": null, |
|
734 | 777 | "### ***Evaluation Metric: BLEU***" |
735 | 778 | ] |
736 | 779 | }, |
| 780 | + { |
| 781 | + "cell_type": "code", |
| 782 | + "execution_count": null, |
| 783 | + "metadata": {}, |
| 784 | + "outputs": [], |
| 785 | + "source": [ |
| 786 | + "import evaluate\n", |
| 787 | + "import numpy as np" |
| 788 | + ] |
| 789 | + }, |
737 | 790 | { |
738 | 791 | "cell_type": "code", |
739 | 792 | "execution_count": null, |
|
753 | 806 | " decoded_preds = tokenizer.batch_decode(preds, skip_special_tokens=True)\n", |
754 | 807 | " decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)\n", |
755 | 808 | "\n", |
756 | | - " # Important: BLEU expects list of strings for preds and list of list of strings for refs\n", |
| 809 | + " # BLEU expects list of strings for preds and list of list of strings for refs\n", |
757 | 810 | " # So wrap references inside list\n", |
758 | 811 | " references = [[ref] for ref in decoded_labels]\n", |
759 | 812 | "\n", |
|
769 | 822 | "### ***Trainer Setup***" |
770 | 823 | ] |
771 | 824 | }, |
| 825 | + { |
| 826 | + "cell_type": "code", |
| 827 | + "execution_count": null, |
| 828 | + "metadata": {}, |
| 829 | + "outputs": [], |
| 830 | + "source": [ |
| 831 | + "from transformers import Seq2SeqTrainer" |
| 832 | + ] |
| 833 | + }, |
772 | 834 | { |
773 | 835 | "cell_type": "code", |
774 | 836 | "execution_count": null, |
|
1406 | 1468 | "name": "python3" |
1407 | 1469 | }, |
1408 | 1470 | "language_info": { |
1409 | | - "name": "python" |
| 1471 | + "codemirror_mode": { |
| 1472 | + "name": "ipython", |
| 1473 | + "version": 3 |
| 1474 | + }, |
| 1475 | + "file_extension": ".py", |
| 1476 | + "mimetype": "text/x-python", |
| 1477 | + "name": "python", |
| 1478 | + "nbconvert_exporter": "python", |
| 1479 | + "pygments_lexer": "ipython3", |
| 1480 | + "version": "3.11.9" |
1410 | 1481 | }, |
1411 | 1482 | "widgets": { |
1412 | 1483 | "application/vnd.jupyter.widget-state+json": { |
|
0 commit comments