File size: 52,343 Bytes
3e66b7c |
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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 |
{
"cells": [
{
"cell_type": "markdown",
"id": "be94e6d6-4096-4d1a-aa58-5afd89f33bff",
"metadata": {},
"source": [
"# Fine-tuning Sandbox\n",
"\n",
"Code authored by: Shawhin Talebi <br>\n",
"Blog link: https://medium.com/towards-data-science/fine-tuning-large-language-models-llms-23473d763b91"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4ef8ea85-d04d-4217-99a3-21c446bf2ffa",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python39\\site-packages\\keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n",
"\n"
]
}
],
"source": [
"from datasets import load_dataset, DatasetDict, Dataset\n",
"\n",
"from transformers import (\n",
" AutoTokenizer,\n",
" AutoConfig, \n",
" AutoModelForSequenceClassification,\n",
" DataCollatorWithPadding,\n",
" TrainingArguments,\n",
" Trainer)\n",
"# PEFT的全称是Parameter-Efficient Fine-Tuning,是transform开发的一个参数高效微调的库\n",
"from peft import PeftModel, PeftConfig, get_peft_model, LoraConfig\n",
"import evaluate\n",
"import torch\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "aa6a4484-07d8-49dd-81ef-672105f53ebe",
"metadata": {},
"source": [
"### dataset"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fa9722d3-0609-4aea-9585-9aa2cfc1fc9a",
"metadata": {
"jupyter": {
"source_hidden": true
}
},
"outputs": [],
"source": [
"# # how dataset was generated\n",
"\n",
"# # load imdb data\n",
"# imdb_dataset = load_dataset(\"imdb\")\n",
"\n",
"# # define subsample size\n",
"# N = 1000 \n",
"# # generate indexes for random subsample\n",
"# rand_idx = np.random.randint(24999, size=N) \n",
"\n",
"# # extract train and test data\n",
"# x_train = imdb_dataset['train'][rand_idx]['text']\n",
"# y_train = imdb_dataset['train'][rand_idx]['label']\n",
"\n",
"# x_test = imdb_dataset['test'][rand_idx]['text']\n",
"# y_test = imdb_dataset['test'][rand_idx]['label']\n",
"\n",
"# # create new dataset\n",
"# dataset = DatasetDict({'train':Dataset.from_dict({'label':y_train,'text':x_train}),\n",
"# 'validation':Dataset.from_dict({'label':y_test,'text':x_test})})"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "de226234-c521-4577-802c-0e7079ef4364",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['label', 'text'],\n",
" num_rows: 1000\n",
" })\n",
" validation: Dataset({\n",
" features: ['label', 'text'],\n",
" num_rows: 1000\n",
" })\n",
" test: Dataset({\n",
" features: ['label', 'text'],\n",
" num_rows: 1000\n",
" })\n",
"})"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 加载数据集 训练 验证 测试\n",
"dataset = load_dataset('shawhin/imdb-truncated')\n",
"dataset"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d5625faa-5fea-4334-bd38-b77de983d8a8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 得出训练集标签的平均值\n",
"np.array(dataset['train']['label']).sum()/len(dataset['train']['label'])"
]
},
{
"cell_type": "markdown",
"id": "3644c68d-9adf-48a4-90a2-8fd89555a302",
"metadata": {},
"source": [
"### model"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a60dd1fe-8144-4678-b018-20891e49237a",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Some weights of DistilBertForSequenceClassification were not initialized from the model checkpoint at distilbert-base-uncased and are newly initialized: ['classifier.bias', 'classifier.weight', 'pre_classifier.bias', 'pre_classifier.weight']\n",
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n"
]
}
],
"source": [
"model_checkpoint = 'distilbert-base-uncased'\n",
"\n",
"# 类别的映射关系\n",
"id2label = {0: \"Negative\", 1: \"Positive\"}\n",
"label2id = {\"Negative\":0, \"Positive\":1}\n",
"\n",
"# 加载预训练的权重 num_labels指明是二分类任务 model_checkpoint 预训练模型的名称\n",
"model = AutoModelForSequenceClassification.from_pretrained(\n",
" model_checkpoint, num_labels=2, id2label=id2label, label2id=label2id)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "853002f8-d39c-4bc4-8d07-e44a47de3b47",
"metadata": {},
"outputs": [],
"source": [
"# display architecture\n",
"model = model.cuda()"
]
},
{
"cell_type": "markdown",
"id": "4bc98609-873d-455c-bac4-155632cda484",
"metadata": {},
"source": [
"### 预处理数据"
]
},
{
"cell_type": "raw",
"id": "93e728f3-9e12-400d-950e-f7f2e29fe19e",
"metadata": {},
"source": [
"add_prefix_space参数告诉 tokenizer 在处理单词和标点符号之间添加一个前缀空格 前缀空格(表示为 Ġ)\n",
"# 原始句子\n",
"sentence = \"Hello, world!\"\n",
"['ĠHello', ',', 'Ġworld', '!']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "7fe08707-657f-4e66-aa72-84899c54bf8d",
"metadata": {},
"outputs": [],
"source": [
"# 创建分词器\n",
"tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, add_prefix_space=True)\n",
"\n",
"# 判断是否有填充标记 通过 resize_token_embeddings 方法调整模型的 token embeddings,以包含新添加的 pad token。\n",
"if tokenizer.pad_token is None:\n",
" tokenizer.add_special_tokens({'pad_token': '[PAD]'})\n",
" model.resize_token_embeddings(len(tokenizer))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "20f4adb9-ce8f-4f54-9b94-300c9daae1b8",
"metadata": {},
"outputs": [],
"source": [
"# 创建分词器函数\n",
"def tokenize_function(examples):\n",
" # 提取文本\n",
" text = examples[\"text\"]\n",
"\n",
" # 设置 tokenizer 的截断位置为左侧。这意味着如果文本超过指定的 max_length,则在左侧截断。这是为了确保重要的文本内容被保留下来。\n",
" tokenizer.truncation_side = \"left\"\n",
" tokenized_inputs = tokenizer(\n",
" text,\n",
" # 返回numpy 类型\n",
" return_tensors=\"np\",\n",
" # 是否进行文本截断\n",
" truncation=True,\n",
" max_length=512\n",
" )\n",
"\n",
" return tokenized_inputs"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b7600bcd-7e93-4fb4-bd8d-ffc76bed1ac2",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c029f605df0e4e3c9484aa97af255052",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/1000 [00:00<?, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"DatasetDict({\n",
" train: Dataset({\n",
" features: ['label', 'text', 'input_ids', 'attention_mask'],\n",
" num_rows: 1000\n",
" })\n",
" validation: Dataset({\n",
" features: ['label', 'text', 'input_ids', 'attention_mask'],\n",
" num_rows: 1000\n",
" })\n",
" test: Dataset({\n",
" features: ['label', 'text', 'input_ids', 'attention_mask'],\n",
" num_rows: 1000\n",
" })\n",
"})"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# tokenize training and validation datasets\n",
"tokenized_dataset = dataset.map(tokenize_function, batched=True)\n",
"tokenized_dataset"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "3f8e85f9-1804-4f49-a783-4da59580ea1e",
"metadata": {},
"outputs": [],
"source": [
"# 创建数据收集器\n",
"data_collator = DataCollatorWithPadding(tokenizer=tokenizer)"
]
},
{
"cell_type": "markdown",
"id": "3cd9a120-580d-470c-a981-7c7e22604865",
"metadata": {},
"source": [
"### evaluation"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "2a894819-2e9c-4a53-9790-32130c182bca",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Using the latest cached version of the module from C:\\Users\\Administrator\\.cache\\huggingface\\modules\\evaluate_modules\\metrics\\evaluate-metric--accuracy\\f887c0aab52c2d38e1f8a215681126379eca617f96c447638f751434e8e65b14 (last modified on Fri Mar 15 09:54:33 2024) since it couldn't be found locally at evaluate-metric--accuracy, or remotely on the Hugging Face Hub.\n"
]
}
],
"source": [
"# import accuracy evaluation metric\n",
"accuracy = evaluate.load(\"accuracy\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "c07b9be2-a3f6-4b38-b9e8-6a2bc8aa945a",
"metadata": {},
"outputs": [],
"source": [
"# define an evaluation function to pass into trainer later\n",
"def compute_metrics(p):\n",
" predictions, labels = p\n",
" predictions = np.argmax(predictions, axis=1)\n",
" # 计算预测结果和真实标签 返回准确率\n",
" return {\"accuracy\": accuracy.compute(predictions=predictions, references=labels)}"
]
},
{
"cell_type": "markdown",
"id": "47500035-a555-46e0-83dc-440586d96b7e",
"metadata": {},
"source": [
"### Apply untrained model to text"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "8f3761c1-a297-45c8-882e-d74856259810",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Untrained model predictions:\n",
"----------------------------\n",
"I'm sorry. - Negative\n",
"You areedespicable person - Negative\n",
"Better than the first one. - Negative\n",
"This is not worth watching even once. - Negative\n",
"This one is a pass. - Negative\n"
]
}
],
"source": [
"# define list of examples\n",
"text_list = [\"I'm sorry.\", \"You areedespicable person\", \"Better than the first one.\", \"This is not worth watching even once.\", \"This one is a pass.\"]\n",
"\n",
"print(\"Untrained model predictions:\")\n",
"print(\"----------------------------\")\n",
"for text in text_list:\n",
" # 将文本转化为可以理解的编码 并返回pytorch张量\n",
" inputs = tokenizer.encode(text, return_tensors=\"pt\")\n",
" # 计算对数\n",
" logits = model(inputs.cuda()).logits\n",
" # convert logits to label\n",
" predictions = torch.argmax(logits)\n",
"\n",
" print(text + \" - \" + id2label[predictions.tolist()])"
]
},
{
"cell_type": "markdown",
"id": "ff356f78-c9fd-4f2b-8f5b-097cf29c1c08",
"metadata": {},
"source": [
"### Train model"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e4dde538-cd7f-4ab5-a96d-c30f3003822e",
"metadata": {},
"outputs": [],
"source": [
"peft_config = LoraConfig(task_type=\"SEQ_CLS\", # 序列分类任务\n",
" r = 4, # 递归深度\n",
" lora_alpha = 32, # alpha 值表示 LORA 模块的影响更大。\n",
" lora_dropout = 0.01,\n",
" target_modules = ['q_lin'])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "f1391303-1e16-4d5c-b2b4-799997eff9f8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"LoraConfig(peft_type=<PeftType.LORA: 'LORA'>, auto_mapping=None, base_model_name_or_path=None, revision=None, task_type='SEQ_CLS', inference_mode=False, r=4, target_modules={'q_lin'}, lora_alpha=32, lora_dropout=0.01, fan_in_fan_out=False, bias='none', use_rslora=False, modules_to_save=None, init_lora_weights=True, layers_to_transform=None, layers_pattern=None, rank_pattern={}, alpha_pattern={}, megatron_config=None, megatron_core='megatron.core', loftq_config={}, use_dora=False)"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"peft_config"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "3e0d9408-9fc4-4bd3-8d35-4d8217fe01e2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"trainable params: 628,994 || all params: 67,584,004 || trainable%: 0.9306847223789819\n"
]
}
],
"source": [
"# 对模型进行配置\n",
"model = get_peft_model(model, peft_config)\n",
"model.print_trainable_parameters()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "5db78059-e5ae-4807-89db-b58ef6abedd1",
"metadata": {},
"outputs": [],
"source": [
"# hyperparameters\n",
"lr = 1e-3\n",
"batch_size = 4\n",
"num_epochs = 10"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "9244ed55-65a4-4c66-8388-55efd87bceb8",
"metadata": {},
"outputs": [],
"source": [
"# define training arguments\n",
"training_args = TrainingArguments(\n",
" output_dir= model_checkpoint + \"-lora-text-classification\",\n",
" learning_rate=lr,\n",
" per_device_train_batch_size=batch_size,\n",
" per_device_eval_batch_size=batch_size,\n",
" num_train_epochs=num_epochs,\n",
" weight_decay=0.01, # 权重衰减,一种正则化技术,用于控制模型参数的大小。\n",
" evaluation_strategy=\"epoch\",\n",
" save_strategy=\"epoch\",\n",
" load_best_model_at_end=True, # 是否在训练结束加载最佳模型\n",
")"
]
},
{
"cell_type": "markdown",
"id": "6e21aa23-a366-4606-b13b-ad22e4639272",
"metadata": {},
"source": [
"### "
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "fc8bc705-5dd7-4305-a797-399b2b0fa2c7",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"D:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\accelerate\\accelerator.py:432: FutureWarning: Passing the following arguments to `Accelerator` is deprecated and will be removed in version 1.0 of Accelerate: dict_keys(['dispatch_batches', 'split_batches', 'even_batches', 'use_seedable_sampler']). Please pass an `accelerate.DataLoaderConfiguration` instead: \n",
"dataloader_config = DataLoaderConfiguration(dispatch_batches=None, split_batches=False, even_batches=True, use_seedable_sampler=True)\n",
" warnings.warn(\n",
"\u001b[34m\u001b[1mwandb\u001b[0m: Currently logged in as: \u001b[33m1321416285\u001b[0m (\u001b[33mxuuuu\u001b[0m). Use \u001b[1m`wandb login --relogin`\u001b[0m to force relogin\n"
]
},
{
"data": {
"text/html": [
"wandb version 0.16.4 is available! To upgrade, please run:\n",
" $ pip install wandb --upgrade"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Tracking run with wandb version 0.15.12"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Run data is saved locally in <code>D:\\software\\Anaconda\\jupyterfile\\AIfinetuning\\wandb\\run-20240315_211852-07azjtzv</code>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"Syncing run <strong><a href='https://wandb.ai/xuuuu/huggingface/runs/07azjtzv' target=\"_blank\">fast-firefly-2</a></strong> to <a href='https://wandb.ai/xuuuu/huggingface' target=\"_blank\">Weights & Biases</a> (<a href='https://wandb.me/run' target=\"_blank\">docs</a>)<br/>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
" View project at <a href='https://wandb.ai/xuuuu/huggingface' target=\"_blank\">https://wandb.ai/xuuuu/huggingface</a>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
" View run at <a href='https://wandb.ai/xuuuu/huggingface/runs/07azjtzv' target=\"_blank\">https://wandb.ai/xuuuu/huggingface/runs/07azjtzv</a>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
" <div>\n",
" \n",
" <progress value='2500' max='2500' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
" [2500/2500 02:44, Epoch 10/10]\n",
" </div>\n",
" <table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: left;\">\n",
" <th>Epoch</th>\n",
" <th>Training Loss</th>\n",
" <th>Validation Loss</th>\n",
" <th>Accuracy</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <td>1</td>\n",
" <td>No log</td>\n",
" <td>0.438809</td>\n",
" <td>{'accuracy': 0.855}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>2</td>\n",
" <td>0.427600</td>\n",
" <td>0.648398</td>\n",
" <td>{'accuracy': 0.859}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>3</td>\n",
" <td>0.427600</td>\n",
" <td>0.637398</td>\n",
" <td>{'accuracy': 0.877}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>4</td>\n",
" <td>0.218100</td>\n",
" <td>0.689158</td>\n",
" <td>{'accuracy': 0.889}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>5</td>\n",
" <td>0.218100</td>\n",
" <td>0.774748</td>\n",
" <td>{'accuracy': 0.897}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>6</td>\n",
" <td>0.073100</td>\n",
" <td>0.846054</td>\n",
" <td>{'accuracy': 0.887}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>7</td>\n",
" <td>0.073100</td>\n",
" <td>0.946100</td>\n",
" <td>{'accuracy': 0.894}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>8</td>\n",
" <td>0.015500</td>\n",
" <td>0.941895</td>\n",
" <td>{'accuracy': 0.901}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>9</td>\n",
" <td>0.015500</td>\n",
" <td>0.994161</td>\n",
" <td>{'accuracy': 0.898}</td>\n",
" </tr>\n",
" <tr>\n",
" <td>10</td>\n",
" <td>0.006700</td>\n",
" <td>0.999837</td>\n",
" <td>{'accuracy': 0.897}</td>\n",
" </tr>\n",
" </tbody>\n",
"</table><p>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Trainer is attempting to log a value of \"{'accuracy': 0.855}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-250 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n",
"Trainer is attempting to log a value of \"{'accuracy': 0.859}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-500 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n",
"Trainer is attempting to log a value of \"{'accuracy': 0.877}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-750 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n",
"Trainer is attempting to log a value of \"{'accuracy': 0.889}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-1000 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n",
"Trainer is attempting to log a value of \"{'accuracy': 0.897}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-1250 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n",
"Trainer is attempting to log a value of \"{'accuracy': 0.887}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-1500 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n",
"Trainer is attempting to log a value of \"{'accuracy': 0.894}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-1750 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n",
"Trainer is attempting to log a value of \"{'accuracy': 0.901}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-2000 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n",
"Trainer is attempting to log a value of \"{'accuracy': 0.898}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-2250 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n",
"Trainer is attempting to log a value of \"{'accuracy': 0.897}\" of type <class 'dict'> for key \"eval/accuracy\" as a scalar. This invocation of Tensorboard's writer.add_scalar() is incorrect so we dropped this attribute.\n",
"Checkpoint destination directory distilbert-base-uncased-lora-text-classification\\checkpoint-2500 already exists and is non-empty. Saving will proceed but saved results may be invalid.\n"
]
},
{
"data": {
"text/plain": [
"TrainOutput(global_step=2500, training_loss=0.14819346437454223, metrics={'train_runtime': 174.6372, 'train_samples_per_second': 57.262, 'train_steps_per_second': 14.315, 'total_flos': 1112883852759936.0, 'train_loss': 0.14819346437454223, 'epoch': 10.0})"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creater trainer object\n",
"trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" train_dataset=tokenized_dataset[\"train\"],\n",
" eval_dataset=tokenized_dataset[\"validation\"],\n",
" tokenizer=tokenizer,\n",
" data_collator=data_collator, # this will dynamically pad examples in each batch to be equal length\n",
" compute_metrics=compute_metrics, \n",
")\n",
"\n",
"# train model\n",
"trainer.train()"
]
},
{
"cell_type": "markdown",
"id": "6f5664d1-9bd2-4ce1-bc24-cab5adf80f49",
"metadata": {},
"source": [
"### Generate prediction"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e5dc029e-1c16-491d-a3f1-715f9e0adf52",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Trained model predictions:\n",
"--------------------------\n",
"I'm sorry. - Negative\n",
"You areedespicable person - Positive\n",
"Better than the first one. - Positive\n",
"This is not worth watching even once. - Negative\n",
"This one is a pass. - Negative\n"
]
}
],
"source": [
"model.to('cuda') # moving to mps for Mac (can alternatively do 'cpu')\n",
"\n",
"print(\"Trained model predictions:\")\n",
"print(\"--------------------------\")\n",
"for text in text_list:\n",
" inputs = tokenizer.encode(text, return_tensors=\"pt\").to(\"cuda\") # moving to mps for Mac (can alternatively do 'cpu')\n",
"\n",
" logits = model(inputs).logits\n",
" predictions = torch.max(logits,1).indices\n",
"\n",
" print(text + \" - \" + id2label[predictions.tolist()[0]])"
]
},
{
"cell_type": "markdown",
"id": "c084bd9e-f7b1-4979-b753-73335ee0cede",
"metadata": {},
"source": [
"### Optional: push model to hub"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "159eb49a-dd0d-4c9e-b9ab-27e06585fd84",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a0e23e8a27634de78c21c18041cd010f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"VBox(children=(HTML(value='<center> <img\\nsrc=https://huggingface.co/front/assets/huggingface_logo-noborder.sv…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# option 1: notebook login\n",
"from huggingface_hub import notebook_login\n",
"notebook_login() # ensure token gives write access\n",
"\n",
"# # option 2: key login\n",
"# from huggingface_hub import login\n",
"# write_key = 'hf_' # paste token here\n",
"# login(write_key)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "09496307-e253-47e3-a46f-3f28a84c89a7",
"metadata": {},
"outputs": [],
"source": [
"hf_name = 'shawhin' # your hf username or org name\n",
"model_id = hf_name + \"/\" + model_checkpoint + \"-lora-text-classification\" # you can name the model whatever you want"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "c56ea581-0ea3-45f3-af21-362e9093ee37",
"metadata": {},
"outputs": [
{
"ename": "HfHubHTTPError",
"evalue": "403 Client Error: Forbidden for url: https://huggingface.co/shawhin/distilbert-base-uncased-lora-text-classification.git/info/lfs/objects/batch (Request ID: Root=1-65f44b6d-3a7059390bd0f46b3618a6e6;b93e4a6f-c6a2-4179-8d62-ec4b3235048e)\n\nAuthorization error.",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mHTTPError\u001b[0m Traceback (most recent call last)",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\utils\\_errors.py:304\u001b[0m, in \u001b[0;36mhf_raise_for_status\u001b[1;34m(response, endpoint_name)\u001b[0m\n\u001b[0;32m 303\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m--> 304\u001b[0m \u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 305\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m HTTPError \u001b[38;5;28;01mas\u001b[39;00m e:\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\requests\\models.py:943\u001b[0m, in \u001b[0;36mResponse.raise_for_status\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 942\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m http_error_msg:\n\u001b[1;32m--> 943\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HTTPError(http_error_msg, response\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n",
"\u001b[1;31mHTTPError\u001b[0m: 403 Client Error: Forbidden for url: https://huggingface.co/shawhin/distilbert-base-uncased-lora-text-classification.git/info/lfs/objects/batch",
"\nThe above exception was the direct cause of the following exception:\n",
"\u001b[1;31mHfHubHTTPError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[1;32mIn[23], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[43mmodel\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpush_to_hub\u001b[49m\u001b[43m(\u001b[49m\u001b[43mmodel_id\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\transformers\\utils\\hub.py:894\u001b[0m, in \u001b[0;36mPushToHubMixin.push_to_hub\u001b[1;34m(self, repo_id, use_temp_dir, commit_message, private, token, max_shard_size, create_pr, safe_serialization, revision, commit_description, tags, **deprecated_kwargs)\u001b[0m\n\u001b[0;32m 891\u001b[0m \u001b[38;5;66;03m# Update model card if needed:\u001b[39;00m\n\u001b[0;32m 892\u001b[0m model_card\u001b[38;5;241m.\u001b[39msave(os\u001b[38;5;241m.\u001b[39mpath\u001b[38;5;241m.\u001b[39mjoin(work_dir, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mREADME.md\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n\u001b[1;32m--> 894\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_upload_modified_files\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 895\u001b[0m \u001b[43m \u001b[49m\u001b[43mwork_dir\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 896\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 897\u001b[0m \u001b[43m \u001b[49m\u001b[43mfiles_timestamps\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 898\u001b[0m \u001b[43m \u001b[49m\u001b[43mcommit_message\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_message\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 899\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 900\u001b[0m \u001b[43m \u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcreate_pr\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 901\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 902\u001b[0m \u001b[43m \u001b[49m\u001b[43mcommit_description\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_description\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 903\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\transformers\\utils\\hub.py:758\u001b[0m, in \u001b[0;36mPushToHubMixin._upload_modified_files\u001b[1;34m(self, working_dir, repo_id, files_timestamps, commit_message, token, create_pr, revision, commit_description)\u001b[0m\n\u001b[0;32m 755\u001b[0m create_branch(repo_id\u001b[38;5;241m=\u001b[39mrepo_id, branch\u001b[38;5;241m=\u001b[39mrevision, token\u001b[38;5;241m=\u001b[39mtoken, exist_ok\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[0;32m 757\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUploading the following files to \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mrepo_id\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m,\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(modified_files)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m--> 758\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcreate_commit\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 759\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 760\u001b[0m \u001b[43m \u001b[49m\u001b[43moperations\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moperations\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 761\u001b[0m \u001b[43m \u001b[49m\u001b[43mcommit_message\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_message\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 762\u001b[0m \u001b[43m \u001b[49m\u001b[43mcommit_description\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcommit_description\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 763\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 764\u001b[0m \u001b[43m \u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcreate_pr\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 765\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 766\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\utils\\_validators.py:118\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.<locals>._inner_fn\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 115\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m check_use_auth_token:\n\u001b[0;32m 116\u001b[0m kwargs \u001b[38;5;241m=\u001b[39m smoothly_deprecate_use_auth_token(fn_name\u001b[38;5;241m=\u001b[39mfn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m, has_token\u001b[38;5;241m=\u001b[39mhas_token, kwargs\u001b[38;5;241m=\u001b[39mkwargs)\n\u001b[1;32m--> 118\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m fn(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\hf_api.py:1227\u001b[0m, in \u001b[0;36mfuture_compatible.<locals>._inner\u001b[1;34m(self, *args, **kwargs)\u001b[0m\n\u001b[0;32m 1224\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mrun_as_future(fn, \u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m 1226\u001b[0m \u001b[38;5;66;03m# Otherwise, call the function normally\u001b[39;00m\n\u001b[1;32m-> 1227\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m fn(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\hf_api.py:3762\u001b[0m, in \u001b[0;36mHfApi.create_commit\u001b[1;34m(self, repo_id, operations, commit_message, commit_description, token, repo_type, revision, create_pr, num_threads, parent_commit, run_as_future)\u001b[0m\n\u001b[0;32m 3759\u001b[0m \u001b[38;5;66;03m# If updating twice the same file or update then delete a file in a single commit\u001b[39;00m\n\u001b[0;32m 3760\u001b[0m _warn_on_overwriting_operations(operations)\n\u001b[1;32m-> 3762\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpreupload_lfs_files\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 3763\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 3764\u001b[0m \u001b[43m \u001b[49m\u001b[43madditions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43madditions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 3765\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 3766\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 3767\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43munquoted_revision\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# first-class methods take unquoted revision\u001b[39;49;00m\n\u001b[0;32m 3768\u001b[0m \u001b[43m \u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcreate_pr\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 3769\u001b[0m \u001b[43m \u001b[49m\u001b[43mnum_threads\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mnum_threads\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 3770\u001b[0m \u001b[43m \u001b[49m\u001b[43mfree_memory\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;66;43;03m# do not remove `CommitOperationAdd.path_or_fileobj` on LFS files for \"normal\" users\u001b[39;49;00m\n\u001b[0;32m 3771\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 3772\u001b[0m files_to_copy \u001b[38;5;241m=\u001b[39m _fetch_files_to_copy(\n\u001b[0;32m 3773\u001b[0m copies\u001b[38;5;241m=\u001b[39mcopies,\n\u001b[0;32m 3774\u001b[0m repo_type\u001b[38;5;241m=\u001b[39mrepo_type,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 3778\u001b[0m endpoint\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mendpoint,\n\u001b[0;32m 3779\u001b[0m )\n\u001b[0;32m 3780\u001b[0m commit_payload \u001b[38;5;241m=\u001b[39m _prepare_commit_payload(\n\u001b[0;32m 3781\u001b[0m operations\u001b[38;5;241m=\u001b[39moperations,\n\u001b[0;32m 3782\u001b[0m files_to_copy\u001b[38;5;241m=\u001b[39mfiles_to_copy,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 3785\u001b[0m parent_commit\u001b[38;5;241m=\u001b[39mparent_commit,\n\u001b[0;32m 3786\u001b[0m )\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\hf_api.py:4262\u001b[0m, in \u001b[0;36mHfApi.preupload_lfs_files\u001b[1;34m(self, repo_id, additions, token, repo_type, revision, create_pr, num_threads, free_memory, gitignore_content)\u001b[0m\n\u001b[0;32m 4256\u001b[0m logger\u001b[38;5;241m.\u001b[39minfo(\n\u001b[0;32m 4257\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSkipped upload for \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(new_lfs_additions)\u001b[38;5;250m \u001b[39m\u001b[38;5;241m-\u001b[39m\u001b[38;5;250m \u001b[39m\u001b[38;5;28mlen\u001b[39m(new_lfs_additions_to_upload)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m LFS file(s) \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 4258\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m(ignored by gitignore file).\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m 4259\u001b[0m )\n\u001b[0;32m 4261\u001b[0m \u001b[38;5;66;03m# Upload new LFS files\u001b[39;00m\n\u001b[1;32m-> 4262\u001b[0m \u001b[43m_upload_lfs_files\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 4263\u001b[0m \u001b[43m \u001b[49m\u001b[43madditions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mnew_lfs_additions_to_upload\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 4264\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 4265\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 4266\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 4267\u001b[0m \u001b[43m \u001b[49m\u001b[43mendpoint\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mendpoint\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 4268\u001b[0m \u001b[43m \u001b[49m\u001b[43mnum_threads\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mnum_threads\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 4269\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# If `create_pr`, we don't want to check user permission on the revision as users with read permission\u001b[39;49;00m\n\u001b[0;32m 4270\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# should still be able to create PRs even if they don't have write permission on the target branch of the\u001b[39;49;00m\n\u001b[0;32m 4271\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;66;43;03m# PR (i.e. `revision`).\u001b[39;49;00m\n\u001b[0;32m 4272\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mnot\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mcreate_pr\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[0;32m 4273\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 4274\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m addition \u001b[38;5;129;01min\u001b[39;00m new_lfs_additions_to_upload:\n\u001b[0;32m 4275\u001b[0m addition\u001b[38;5;241m.\u001b[39m_is_uploaded \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\utils\\_validators.py:118\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.<locals>._inner_fn\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 115\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m check_use_auth_token:\n\u001b[0;32m 116\u001b[0m kwargs \u001b[38;5;241m=\u001b[39m smoothly_deprecate_use_auth_token(fn_name\u001b[38;5;241m=\u001b[39mfn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m, has_token\u001b[38;5;241m=\u001b[39mhas_token, kwargs\u001b[38;5;241m=\u001b[39mkwargs)\n\u001b[1;32m--> 118\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m fn(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\_commit_api.py:360\u001b[0m, in \u001b[0;36m_upload_lfs_files\u001b[1;34m(additions, repo_type, repo_id, token, endpoint, num_threads, revision)\u001b[0m\n\u001b[0;32m 358\u001b[0m batch_actions: List[Dict] \u001b[38;5;241m=\u001b[39m []\n\u001b[0;32m 359\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m chunk \u001b[38;5;129;01min\u001b[39;00m chunk_iterable(additions, chunk_size\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m256\u001b[39m):\n\u001b[1;32m--> 360\u001b[0m batch_actions_chunk, batch_errors_chunk \u001b[38;5;241m=\u001b[39m \u001b[43mpost_lfs_batch_info\u001b[49m\u001b[43m(\u001b[49m\n\u001b[0;32m 361\u001b[0m \u001b[43m \u001b[49m\u001b[43mupload_infos\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mop\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mupload_info\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mop\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mchunk\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 362\u001b[0m \u001b[43m \u001b[49m\u001b[43mtoken\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mtoken\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 363\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 364\u001b[0m \u001b[43m \u001b[49m\u001b[43mrepo_type\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrepo_type\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 365\u001b[0m \u001b[43m \u001b[49m\u001b[43mrevision\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrevision\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 366\u001b[0m \u001b[43m \u001b[49m\u001b[43mendpoint\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mendpoint\u001b[49m\u001b[43m,\u001b[49m\n\u001b[0;32m 367\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 369\u001b[0m \u001b[38;5;66;03m# If at least 1 error, we do not retrieve information for other chunks\u001b[39;00m\n\u001b[0;32m 370\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m batch_errors_chunk:\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\utils\\_validators.py:118\u001b[0m, in \u001b[0;36mvalidate_hf_hub_args.<locals>._inner_fn\u001b[1;34m(*args, **kwargs)\u001b[0m\n\u001b[0;32m 115\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m check_use_auth_token:\n\u001b[0;32m 116\u001b[0m kwargs \u001b[38;5;241m=\u001b[39m smoothly_deprecate_use_auth_token(fn_name\u001b[38;5;241m=\u001b[39mfn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m, has_token\u001b[38;5;241m=\u001b[39mhas_token, kwargs\u001b[38;5;241m=\u001b[39mkwargs)\n\u001b[1;32m--> 118\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m fn(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\lfs.py:159\u001b[0m, in \u001b[0;36mpost_lfs_batch_info\u001b[1;34m(upload_infos, token, repo_type, repo_id, revision, endpoint)\u001b[0m\n\u001b[0;32m 157\u001b[0m headers \u001b[38;5;241m=\u001b[39m {\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mLFS_HEADERS, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mbuild_hf_headers(token\u001b[38;5;241m=\u001b[39mtoken \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m)} \u001b[38;5;66;03m# Token must be provided or retrieved\u001b[39;00m\n\u001b[0;32m 158\u001b[0m resp \u001b[38;5;241m=\u001b[39m get_session()\u001b[38;5;241m.\u001b[39mpost(batch_url, headers\u001b[38;5;241m=\u001b[39mheaders, json\u001b[38;5;241m=\u001b[39mpayload)\n\u001b[1;32m--> 159\u001b[0m \u001b[43mhf_raise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresp\u001b[49m\u001b[43m)\u001b[49m\n\u001b[0;32m 160\u001b[0m batch_info \u001b[38;5;241m=\u001b[39m resp\u001b[38;5;241m.\u001b[39mjson()\n\u001b[0;32m 162\u001b[0m objects \u001b[38;5;241m=\u001b[39m batch_info\u001b[38;5;241m.\u001b[39mget(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mobjects\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m)\n",
"File \u001b[1;32mD:\\software\\Anaconda\\envs\\Work1\\lib\\site-packages\\huggingface_hub\\utils\\_errors.py:362\u001b[0m, in \u001b[0;36mhf_raise_for_status\u001b[1;34m(response, endpoint_name)\u001b[0m\n\u001b[0;32m 358\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m BadRequestError(message, response\u001b[38;5;241m=\u001b[39mresponse) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n\u001b[0;32m 360\u001b[0m \u001b[38;5;66;03m# Convert `HTTPError` into a `HfHubHTTPError` to display request information\u001b[39;00m\n\u001b[0;32m 361\u001b[0m \u001b[38;5;66;03m# as well (request id and/or server error message)\u001b[39;00m\n\u001b[1;32m--> 362\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HfHubHTTPError(\u001b[38;5;28mstr\u001b[39m(e), response\u001b[38;5;241m=\u001b[39mresponse) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01me\u001b[39;00m\n",
"\u001b[1;31mHfHubHTTPError\u001b[0m: 403 Client Error: Forbidden for url: https://huggingface.co/shawhin/distilbert-base-uncased-lora-text-classification.git/info/lfs/objects/batch (Request ID: Root=1-65f44b6d-3a7059390bd0f46b3618a6e6;b93e4a6f-c6a2-4179-8d62-ec4b3235048e)\n\nAuthorization error."
]
}
],
"source": [
"model.push_to_hub(model_id) # save model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f487331a-8552-4fb2-867f-985b8fe1d1ab",
"metadata": {},
"outputs": [],
"source": [
"trainer.push_to_hub(model_id) # save trainer"
]
},
{
"cell_type": "markdown",
"id": "00e7feaa-b70e-4b1d-a118-23c616d14639",
"metadata": {},
"source": [
"### Optional: load peft model"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "19cffa01-25a4-4c86-a7fa-a84353b8caae",
"metadata": {},
"outputs": [],
"source": [
"# how to load peft model from hub for inference\n",
"config = PeftConfig.from_pretrained(model_id)\n",
"inference_model = AutoModelForSequenceClassification.from_pretrained(\n",
" config.base_model_name_or_path, num_labels=2, id2label=id2label, label2id=label2id\n",
")\n",
"tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)\n",
"model = PeftModel.from_pretrained(inference_model, model_id)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "77c6ed42-8ec3-4343-9e42-405feac052ba",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Work1",
"language": "python",
"name": "work1"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|