diff --git "a/lecture_midm_7B_food_order_understanding_v1_2.ipynb" "b/lecture_midm_7B_food_order_understanding_v1_2.ipynb" new file mode 100644--- /dev/null +++ "b/lecture_midm_7B_food_order_understanding_v1_2.ipynb" @@ -0,0 +1 @@ +{"cells":[{"cell_type":"markdown","source":["# **미세튜닝된 어댑터 업로드한 ID: yelin1001**"],"metadata":{"id":"3vV0Eyh39Q5U"},"id":"3vV0Eyh39Q5U"},{"cell_type":"markdown","metadata":{"id":"28e4c4d1-a73f-437b-a1bd-c2cc3874924a"},"source":["# 강의 11주차: midm-food-order-understanding\n","\n","1. KT-AI/midm-bitext-S-7B-inst-v1 를 주문 문장 이해에 미세 튜닝\n","\n","- food-order-understanding-small-3200.json (학습)\n","- food-order-understanding-small-800.json (검증)\n","\n","\n","종속적인 필요 내용\n","- huggingface 계정 설정 및 llama-2 사용 승인\n","- 로깅을 위한 wandb\n","\n","\n","history\n","\n","v1.2\n","- KT-AI/midm-bitext-S-7B-inst-v1 에 safetensors 포맷이 올라왔기에, 해당 리포에서 받도록 설정 변경\n","- 전체 과정 재검증"],"id":"28e4c4d1-a73f-437b-a1bd-c2cc3874924a"},{"cell_type":"markdown","source":["**종속성 설치**"],"metadata":{"id":"QGv6ld9q1XHv"},"id":"QGv6ld9q1XHv"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"nDZe_wqKU6J3","outputId":"ba12f714-4a8e-4ede-d2ef-30ae5684aca4","executionInfo":{"status":"ok","timestamp":1702034765617,"user_tz":-540,"elapsed":28982,"user":{"displayName":"장예린","userId":"10756148590164405752"}}},"outputs":[{"output_type":"stream","name":"stdout","text":["Requirement already satisfied: transformers in /usr/local/lib/python3.10/dist-packages (4.35.2)\n","Collecting peft\n"," Downloading peft-0.7.0-py3-none-any.whl (168 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m168.3/168.3 kB\u001b[0m \u001b[31m3.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting accelerate\n"," Downloading accelerate-0.25.0-py3-none-any.whl (265 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m265.7/265.7 kB\u001b[0m \u001b[31m7.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting optimum\n"," Downloading optimum-1.15.0-py3-none-any.whl (400 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m400.9/400.9 kB\u001b[0m \u001b[31m9.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting bitsandbytes\n"," Downloading bitsandbytes-0.41.3-py3-none-any.whl (92.6 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m92.6/92.6 MB\u001b[0m \u001b[31m10.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting trl\n"," Downloading trl-0.7.4-py3-none-any.whl (133 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m133.9/133.9 kB\u001b[0m \u001b[31m16.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting wandb\n"," Downloading wandb-0.16.1-py3-none-any.whl (2.1 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.1/2.1 MB\u001b[0m \u001b[31m74.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting einops\n"," Downloading einops-0.7.0-py3-none-any.whl (44 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m44.6/44.6 kB\u001b[0m \u001b[31m6.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from transformers) (3.13.1)\n","Requirement already satisfied: huggingface-hub<1.0,>=0.16.4 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.19.4)\n","Requirement already satisfied: numpy>=1.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (1.23.5)\n","Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from transformers) (23.2)\n","Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (6.0.1)\n","Requirement already satisfied: regex!=2019.12.17 in /usr/local/lib/python3.10/dist-packages (from transformers) (2023.6.3)\n","Requirement already satisfied: requests in /usr/local/lib/python3.10/dist-packages (from transformers) (2.31.0)\n","Requirement already satisfied: tokenizers<0.19,>=0.14 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.15.0)\n","Requirement already satisfied: safetensors>=0.3.1 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.4.1)\n","Requirement already satisfied: tqdm>=4.27 in /usr/local/lib/python3.10/dist-packages (from transformers) (4.66.1)\n","Requirement already satisfied: psutil in /usr/local/lib/python3.10/dist-packages (from peft) (5.9.5)\n","Requirement already satisfied: torch>=1.13.0 in /usr/local/lib/python3.10/dist-packages (from peft) (2.1.0+cu118)\n","Collecting coloredlogs (from optimum)\n"," Downloading coloredlogs-15.0.1-py2.py3-none-any.whl (46 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.0/46.0 kB\u001b[0m \u001b[31m5.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from optimum) (1.12)\n","Collecting datasets (from optimum)\n"," Downloading datasets-2.15.0-py3-none-any.whl (521 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m521.2/521.2 kB\u001b[0m \u001b[31m41.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting tyro>=0.5.11 (from trl)\n"," Downloading tyro-0.6.0-py3-none-any.whl (100 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m100.9/100.9 kB\u001b[0m \u001b[31m12.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: Click!=8.0.0,>=7.1 in /usr/local/lib/python3.10/dist-packages (from wandb) (8.1.7)\n","Collecting GitPython!=3.1.29,>=1.0.0 (from wandb)\n"," Downloading GitPython-3.1.40-py3-none-any.whl (190 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m190.6/190.6 kB\u001b[0m \u001b[31m23.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting sentry-sdk>=1.0.0 (from wandb)\n"," Downloading sentry_sdk-1.38.0-py2.py3-none-any.whl (252 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m252.8/252.8 kB\u001b[0m \u001b[31m28.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting docker-pycreds>=0.4.0 (from wandb)\n"," Downloading docker_pycreds-0.4.0-py2.py3-none-any.whl (9.0 kB)\n","Collecting setproctitle (from wandb)\n"," Downloading setproctitle-1.3.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30 kB)\n","Requirement already satisfied: setuptools in /usr/local/lib/python3.10/dist-packages (from wandb) (67.7.2)\n","Requirement already satisfied: appdirs>=1.4.3 in /usr/local/lib/python3.10/dist-packages (from wandb) (1.4.4)\n","Requirement already satisfied: protobuf!=4.21.0,<5,>=3.19.0 in /usr/local/lib/python3.10/dist-packages (from wandb) (3.20.3)\n","Requirement already satisfied: six>=1.4.0 in /usr/local/lib/python3.10/dist-packages (from docker-pycreds>=0.4.0->wandb) (1.16.0)\n","Collecting gitdb<5,>=4.0.1 (from GitPython!=3.1.29,>=1.0.0->wandb)\n"," Downloading gitdb-4.0.11-py3-none-any.whl (62 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.7/62.7 kB\u001b[0m \u001b[31m8.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: fsspec>=2023.5.0 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<1.0,>=0.16.4->transformers) (2023.6.0)\n","Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.10/dist-packages (from huggingface-hub<1.0,>=0.16.4->transformers) (4.5.0)\n","Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.3.2)\n","Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (3.6)\n","Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2.0.7)\n","Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests->transformers) (2023.11.17)\n","Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (3.2.1)\n","Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (3.1.2)\n","Requirement already satisfied: triton==2.1.0 in /usr/local/lib/python3.10/dist-packages (from torch>=1.13.0->peft) (2.1.0)\n","Collecting sentencepiece!=0.1.92,>=0.1.91 (from transformers)\n"," Downloading sentencepiece-0.1.99-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m78.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hCollecting docstring-parser>=0.14.1 (from tyro>=0.5.11->trl)\n"," Downloading docstring_parser-0.15-py3-none-any.whl (36 kB)\n","Requirement already satisfied: rich>=11.1.0 in /usr/local/lib/python3.10/dist-packages (from tyro>=0.5.11->trl) (13.7.0)\n","Collecting shtab>=1.5.6 (from tyro>=0.5.11->trl)\n"," Downloading shtab-1.6.5-py3-none-any.whl (13 kB)\n","Collecting humanfriendly>=9.1 (from coloredlogs->optimum)\n"," Downloading humanfriendly-10.0-py2.py3-none-any.whl (86 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m86.8/86.8 kB\u001b[0m \u001b[31m12.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: pyarrow>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from datasets->optimum) (9.0.0)\n","Collecting pyarrow-hotfix (from datasets->optimum)\n"," Downloading pyarrow_hotfix-0.6-py3-none-any.whl (7.9 kB)\n","Collecting dill<0.3.8,>=0.3.0 (from datasets->optimum)\n"," Downloading dill-0.3.7-py3-none-any.whl (115 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m115.3/115.3 kB\u001b[0m \u001b[31m14.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from datasets->optimum) (1.5.3)\n","Requirement already satisfied: xxhash in /usr/local/lib/python3.10/dist-packages (from datasets->optimum) (3.4.1)\n","Collecting multiprocess (from datasets->optimum)\n"," Downloading multiprocess-0.70.15-py310-none-any.whl (134 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m134.8/134.8 kB\u001b[0m \u001b[31m17.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hRequirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from datasets->optimum) (3.9.1)\n","Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->optimum) (1.3.0)\n","Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets->optimum) (23.1.0)\n","Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets->optimum) (6.0.4)\n","Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets->optimum) (1.9.3)\n","Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets->optimum) (1.4.0)\n","Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets->optimum) (1.3.1)\n","Requirement already satisfied: async-timeout<5.0,>=4.0 in /usr/local/lib/python3.10/dist-packages (from aiohttp->datasets->optimum) (4.0.3)\n","Collecting smmap<6,>=3.0.1 (from gitdb<5,>=4.0.1->GitPython!=3.1.29,>=1.0.0->wandb)\n"," Downloading smmap-5.0.1-py3-none-any.whl (24 kB)\n","Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.10/dist-packages (from rich>=11.1.0->tyro>=0.5.11->trl) (3.0.0)\n","Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.10/dist-packages (from rich>=11.1.0->tyro>=0.5.11->trl) (2.16.1)\n","Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch>=1.13.0->peft) (2.1.3)\n","Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets->optimum) (2.8.2)\n","Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->datasets->optimum) (2023.3.post1)\n","Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.10/dist-packages (from markdown-it-py>=2.2.0->rich>=11.1.0->tyro>=0.5.11->trl) (0.1.2)\n","Installing collected packages: sentencepiece, bitsandbytes, smmap, shtab, setproctitle, sentry-sdk, pyarrow-hotfix, humanfriendly, einops, docstring-parser, docker-pycreds, dill, multiprocess, gitdb, coloredlogs, tyro, GitPython, accelerate, wandb, datasets, trl, peft, optimum\n","Successfully installed GitPython-3.1.40 accelerate-0.25.0 bitsandbytes-0.41.3 coloredlogs-15.0.1 datasets-2.15.0 dill-0.3.7 docker-pycreds-0.4.0 docstring-parser-0.15 einops-0.7.0 gitdb-4.0.11 humanfriendly-10.0 multiprocess-0.70.15 optimum-1.15.0 peft-0.7.0 pyarrow-hotfix-0.6 sentencepiece-0.1.99 sentry-sdk-1.38.0 setproctitle-1.3.3 shtab-1.6.5 smmap-5.0.1 trl-0.7.4 tyro-0.6.0 wandb-0.16.1\n"]}],"source":["pip install transformers peft accelerate optimum bitsandbytes trl wandb einops"],"id":"nDZe_wqKU6J3"},{"cell_type":"markdown","source":["**라이브러리 및 모듈 임포트**\n","\n","> 데이터 처리, 모델 학습, 추론에 사용되는 라이브러리 및 모듈을 포함함."],"metadata":{"id":"mxXOIhar1c_2"},"id":"mxXOIhar1c_2"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"51eb00d7-2928-41ad-9ae9-7f0da7d64d6d","outputId":"cdd1ead9-5511-4ac9-d491-81745915c552","executionInfo":{"status":"ok","timestamp":1702034791587,"user_tz":-540,"elapsed":21068,"user":{"displayName":"장예린","userId":"10756148590164405752"}}},"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.10/dist-packages/trl/trainer/ppo_config.py:141: UserWarning: The `optimize_cuda_cache` arguement will be deprecated soon, please use `optimize_device_cache` instead.\n"," warnings.warn(\n"]}],"source":["import os\n","from dataclasses import dataclass, field\n","from typing import Optional\n","import re\n","\n","import torch\n","import tyro\n","from accelerate import Accelerator\n","from datasets import load_dataset, Dataset\n","from peft import AutoPeftModelForCausalLM, LoraConfig\n","from tqdm import tqdm\n","from transformers import (\n"," AutoModelForCausalLM,\n"," AutoTokenizer,\n"," BitsAndBytesConfig,\n"," TrainingArguments,\n",")\n","\n","from trl import SFTTrainer\n","\n","from trl.trainer import ConstantLengthDataset"],"id":"51eb00d7-2928-41ad-9ae9-7f0da7d64d6d"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":162,"referenced_widgets":["c9ce3698b3394296b6798cbbf8ddc724","232b14f9c2c24d288da416db01805a63","ac386ff03b65403baec9d51fd4974751","f8ea2806c9f04fba9405ef67d88b5e1d","e4cab837d16a4c469c34b5d3a10ad66a","65e9cdbbcd07433e9e16ded92090932f","7165fc33c43e4a7e8f9b543ac75c7ab2","6845b38b57a342798208d1ec003d4fae","aa6ac3b0818e436b80e3d2e1de20aa4c","4409ed630ac54adda1447851dc83e406","a064832a02fd4619b15f7dc33e393a2a","737d67665a4d460fa66a5476450b7b26","a32f94b7399046c3bc372cb47789c0c7","e6c60a658df047c68655aa19679f953e","385aaaa585ca416b8d47209926d90fff","458a7a274b4048fbabf247e7bcf0311d","fc1720a332f8456fafa28201ef15c7b4","5f50951016d0433cb713a635919701b2","2dde2a96536941ce9be7b515449007d4","9323cb30144a4456958e5c6e4ee1ad32","8af34a10528d42dbac7bc7d8cd8b7b38","56767534afe44dc8805f3691f25c7d6a","c5d8b5766bb74bd288bad8519659bba8","1e4a1cb769e84cd9922e792ec64e894b","6449f00cea404eac9d47ef9c66bcf947","d65da8e94de941b98f5b924a75b5cfbe","117c506e02ec4955ad5ed3f1bff101e1","6010cf9a274f48f2b7ea6be4a39f1036","d4919d97690540f9b0435401b1991399","e853c95344f845f398c01ffd4398f078","ea29c770c607482d8e4307ca108e0c94","b602bf9ab61a4b608cc281f5ba085e55"]},"id":"tX7gYxZaVhYL","outputId":"e19a087a-9f52-4715-f429-d9b6016f5f0f","executionInfo":{"status":"ok","timestamp":1702034804376,"user_tz":-540,"elapsed":367,"user":{"displayName":"장예린","userId":"10756148590164405752"}}},"outputs":[{"output_type":"display_data","data":{"text/plain":["VBox(children=(HTML(value='
/content/wandb/run-20231128_113144-sxco8kez
"]},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Step | \n","Training Loss | \n","
---|---|
50 | \n","1.698200 | \n","
100 | \n","1.034100 | \n","
150 | \n","1.001700 | \n","
200 | \n","1.013100 | \n","
250 | \n","0.948000 | \n","
300 | \n","1.010000 | \n","
350 | \n","0.972000 | \n","
400 | \n","1.057900 | \n","
450 | \n","1.020900 | \n","
500 | \n","0.986600 | \n","
550 | \n","0.998400 | \n","
600 | \n","1.005900 | \n","
650 | \n","0.916100 | \n","
700 | \n","0.919100 | \n","
750 | \n","0.908800 | \n","
800 | \n","0.944000 | \n","
"]},"metadata":{}},{"output_type":"execute_result","data":{"text/plain":["TrainOutput(global_step=800, training_loss=1.027174186706543, metrics={'train_runtime': 2297.0794, 'train_samples_per_second': 0.697, 'train_steps_per_second': 0.348, 'total_flos': 2.48413559980032e+16, 'train_loss': 1.027174186706543, 'epoch': 0.32})"]},"metadata":{},"execution_count":28}],"source":["trainer.train()"],"id":"14019fa9-0c6f-4729-ac99-0d407af375b8"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":35},"id":"3Y4FQSyRghQt","outputId":"15c3e43a-e930-429e-f1d7-f9f05bb10730","executionInfo":{"status":"ok","timestamp":1701173403220,"user_tz":-540,"elapsed":450,"user":{"displayName":"장예린","userId":"08326973126538993419"}}},"outputs":[{"output_type":"execute_result","data":{"text/plain":["'/gdrive/MyDrive/nlp/lora-midm-7b-nsmc-v1.2'"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"}},"metadata":{},"execution_count":29}],"source":["script_args.training_args.output_dir"],"id":"3Y4FQSyRghQt"},{"cell_type":"code","execution_count":null,"metadata":{"id":"49f05450-da2a-4edd-9db2-63836a0ec73a"},"outputs":[],"source":["trainer.save_model(script_args.training_args.output_dir)"],"id":"49f05450-da2a-4edd-9db2-63836a0ec73a"},{"cell_type":"markdown","metadata":{"id":"652f307e-e1d7-43ae-b083-dba2d94c2296"},"source":["# 추론 테스트"],"id":"652f307e-e1d7-43ae-b083-dba2d94c2296"},{"cell_type":"code","execution_count":null,"metadata":{"id":"ea8a1fea-7499-4386-9dea-0509110f61af"},"outputs":[],"source":["from transformers import pipeline, TextStreamer"],"id":"ea8a1fea-7499-4386-9dea-0509110f61af"},{"cell_type":"code","execution_count":null,"metadata":{"id":"52626888-1f6e-46b6-a8dd-836622149ff5"},"outputs":[],"source":["instruction_prompt_template = \"\"\"###System;다음은 네이버 영화 리뷰들을 모아놓은 문장이다. 이를 분석하여 사용자가 작성한 영화 리뷰의 감정을 긍정 또는 부정으로 예측하라.\n","\n","### 리뷰 내용: {0} ### 분석 결과:\n","\"\"\"\n","\n","prompt_template = \"\"\"###System;{System}\n","###User;{User}\n","###Midm;\"\"\"\n","\n","default_system_msg = (\n"," \"너는 먼저 사용자가 작성한 영화 리뷰의 감정을 분석하는 에이전트이다. 이로부터 주어진 영화 리뷰에 대한 긍정 또는 부정을 추출해야 한다.\"\n",")"],"id":"52626888-1f6e-46b6-a8dd-836622149ff5"},{"cell_type":"code","execution_count":null,"metadata":{"id":"1919cf1f-482e-4185-9d06-e3cea1918416"},"outputs":[],"source":["def wrapper_generate(model, input_prompt, do_stream=False):\n"," data = tokenizer(input_prompt, return_tensors=\"pt\")\n"," streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)\n"," input_ids = data.input_ids[..., :-1]\n"," with torch.no_grad():\n"," pred = model.generate(\n"," #input_ids=input_ids.cuda(),\n"," input_ids = input_ids.to('cuda'),\n"," streamer=streamer if do_stream else None,\n"," use_cache=True,\n"," max_new_tokens=float('inf'),\n"," do_sample=False\n"," )\n"," decoded_text = tokenizer.batch_decode(pred, skip_special_tokens=True)\n"," decoded_text = decoded_text[0].replace(\"<[!newline]>\", \"\\n\")\n"," return (decoded_text[len(input_prompt):])"],"id":"1919cf1f-482e-4185-9d06-e3cea1918416"},{"cell_type":"code","source":["eval_dic = {i: wrapper_generate(model=base_model, input_prompt=prompt_template.format(System=default_system_msg, User=example[\"document\"]))for i, example in enumerate(valid_dataset)}"],"metadata":{"id":"ab-3Q0ZnZHeg"},"id":"ab-3Q0ZnZHeg","execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"3f471e3a-723b-4df5-aa72-46f571f6bab6"},"source":["# 미세튜닝된 모델 로딩 후 테스트"],"id":"3f471e3a-723b-4df5-aa72-46f571f6bab6"},{"cell_type":"code","source":["from transformers import pipeline, TextStreamer"],"metadata":{"id":"XMU2ydLG2k3Z"},"id":"XMU2ydLG2k3Z","execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**프롬프트 정의**\n","\n","\n","> 사용자 영화 리뷰의 예측을 생성하기 위한 프롬프트 정의함."],"metadata":{"id":"e-SkE9mf2sWx"},"id":"e-SkE9mf2sWx"},{"cell_type":"code","source":["instruction_prompt_template = \"\"\"###System;다음은 네이버 영화 리뷰들을 모아놓은 문장이다. 이를 분석하여 사용자가 작성한 영화 리뷰의 감정을 긍정 또는 부정으로 예측하라.\n","\n","### 리뷰 내용: {0} ### 분석 결과:\n","\"\"\"\n","\n","prompt_template = \"\"\"###System;{System}\n","###User;{User}\n","###Midm;\"\"\"\n","\n","default_system_msg = (\n"," \"너는 사용자가 작성한 리뷰의 긍정 또는 부정을 판단해야 한다.\"\n",")"],"metadata":{"id":"XUvbhk6t2pXs"},"id":"XUvbhk6t2pXs","execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**wrapper_generate 함수**\n","\n","\n","> 사용자 영화 리뷰에 대한 입력 프롬프트 생성."],"metadata":{"id":"p4J4p1b62yAM"},"id":"p4J4p1b62yAM"},{"cell_type":"code","source":["def wrapper_generate(model, input_prompt, do_stream=False):\n"," data = tokenizer(input_prompt, return_tensors=\"pt\")\n"," streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)\n"," input_ids = data.input_ids[..., :-1]\n"," with torch.no_grad():\n"," pred = model.generate(\n"," input_ids=input_ids.cuda(),\n"," streamer=streamer if do_stream else None,\n"," use_cache=True,\n"," max_new_tokens=float('inf'),\n"," do_sample=False\n"," )\n"," decoded_text = tokenizer.batch_decode(pred, skip_special_tokens=True)\n"," decoded_text = decoded_text[0].replace(\"<[!newline]>\", \"\\n\")\n"," return (decoded_text[len(input_prompt):])"],"metadata":{"id":"iov1rlPJ21K-"},"id":"iov1rlPJ21K-","execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**양자화 설정**\n","\n","\n","> 4비트로 양자화함.\n"],"metadata":{"id":"Vs2CLd2D24ha"},"id":"Vs2CLd2D24ha"},{"cell_type":"code","execution_count":null,"metadata":{"id":"a43bdd07-7555-42b2-9888-a614afec892f"},"outputs":[],"source":["bnb_config = BitsAndBytesConfig(\n"," load_in_4bit=True,\n"," bnb_4bit_quant_type=\"nf4\",\n"," bnb_4bit_compute_dtype=torch.bfloat16,\n",")"],"id":"a43bdd07-7555-42b2-9888-a614afec892f"},{"cell_type":"markdown","source":["**모델 불러오기**\n","\n","> 미세튜닝된 모델을 4비트로 양자화하여 저장된 모델을 가져옴."],"metadata":{"id":"q6n3Tr4L27kR"},"id":"q6n3Tr4L27kR"},{"cell_type":"code","execution_count":null,"metadata":{"id":"39db2ee4-23c8-471f-89b2-bca34964bf81","colab":{"base_uri":"https://localhost:8080/","height":539,"referenced_widgets":["c2fd65a3f6d941e7b2ee311abd843de1","4d27ca442602458183365f64069d8060","ec252d6aba434310963f90ce0ed38d31","ad24ffb33db14ead93d17d7604118c09","9ed48cf506e04feab20197b3ce72de2e","b7088a7d4ff44d99952c4f93ea6703d6","3a644ec4a7e24f3695007a4bc0517b1a","cfad46ba0e3348a999b704bb884e53e0","412183eef1d44071bf5a5c711ec4b0c3","23e478d6e4b04b408b3c09530acad811","97ae019cd979462b9849183b154363ce","f89669afd8344e78af7cb65c2193388d","d1652fd6364a4ae4977f3d8fa4c30bf4","17a1eb7be7f9438fbc0a442e75867a00","0ed614bcbb984736b4d76489b242dabd","9b473abde4fe4e048260a4d6cc261fcc","16bec1ee9a714a58a4f8cc2c73df3054","5c6a90593db44479a00a17d08f2f372f","83f14d6621c74bffaa7636a1bc6fca52","efc891bb308b4954ad7d2a159d8d432e","fea05935ee3c48d9bd8e323e3727c444","218c579528a54c32a3a11360e241c2b7","4e0e55a1fc304d419cd039309a4ad772","854102a7dcd5407ca9594d8a29b85034","340e4ae8dd484e43a368ede33bd6018f","9fdb56aa70e34571954452534169da92","9d74e809eeb14cb2b49b93e7f1cba670","17df0fe7f3824fd8ac5bee2564a147ea","7da31d43b1bb450187abdf03bc84169e","69f1fcc5c6034926a18b13e0fa7ff851","7dc6c47f66ff4d029ee5497c6549bfb8","56032c34d6914226bdcb2e7640da4381","737ce0b9d9a84b15aad21bf13ebd1136","96c28d9784844a38bd43f17fcf341526","d54f28a82077491d85890c3133f8bb17","5a8f94f9001a4bb0b7666381ec44787f","e16f931caca74f708aed43c6a3dc10cc","49bd70185c124c68bb4ef315560ddaec","0562521df5e0458cb5d48ce7d241933f","6bf0eb9b098e4f18807904fbfbd4b20b","1a9be19bc6794718b8bae064f98f5ff8","f913fa5302b04842958e3ee710879d6e","9cabaac5c8484bc4863e19da135763b6","0d1a0c77a7d544f4b5e3142ce8d48081","7789f290df334e30a1ad829853e31e15","75b9922909dd4103b188778aff457311","083013e09b9f49579358736c66e9d032","88a9d66d6c3e4fe38fe0c2a26e6402da","b6d3b77208104d50936526089a204548","b55a75c9d89b45c5825256f5296eeebb","605580e4e53145c9b4a6061857ce6185","938745b20e1a49c194327e18e39e2916","30d86f839ddd4f418b58473d66e0f2b7","53c838b5c07940f2be0dd6a6ab02b3cc","f46eaf640feb4cfe8d3e8adaead28209","f12a7f89f6d2435f9dfdf05fb7c5daaa","32ecc05b86984b8997ad43e27dd63512","36bae218caf4460796435b10a1aaa6b3","29b37cf59b564ee6bc1ff6948b33ee24","55dcc8edeb474e2ba7793509dff51879","2ff04b51e24747018b6903beb2948a65","e7392eade55846f6b3a603c38bc85dab","b9b96ac6dc29485cb04f1b1bbcd8cb7e","b6d262184a1541b296f94a7d9eab9128","918cf5037bed4fc6b86cec27bbbd8df0","e92c4ec7873d4b31b696c6f5a098dbcc","c88098d28e11421f87463e83e81d3e17","441ccd3214244c44b6d239e3c164a49d","d848184f52cc4f27939b02683ebebfef","316c740ea79e4b4bac22aeaefabea537","1278f290c4c1441c9903b229d5f74a33","5b21b6a5a4f74b119ce3aa4b47518c04","696f283b47854e0b8b3b650d48fc4c7c","7b7ba03b3a0243a29e09127af03d1a9d","a8d344bd1c87403899d15d45c803dab7","74c15cae3a6d4dc0af178f7ea775759f","0b8dabc9628043f2b9f459ed8b17d148","bab910c60b044b9e972fe84b3860189f","65ad4b9159c642a4b0b7c13d4999f229","e29409a04a204ea995aaefb5079d460f","4fbdec2cd7e94b1fa4811dc89b8f0620","5c96f6fb78be4c7c991e52373853338f","76fa12d3ba99455181b2df000d34173f","9c7846adcf934a8aa89ccb8fc7f9a55c","45af7599bc5143a4b8d2046340389a63","b83005e296fd48628f479e049d19dea9","3e41896793364e539163b8fb229e1313","d2f1f59d26724bd48e91ce0ab339627b","204a944a12884c9d8eff0f0891fe5c23","aed8d7b38e724749803a816e7ec8d0c4","fdc5ad3fdbd74ecea5851e807ccd5665","062027b01a76428ba755e1aca4d477e1","bf39c3fdcab54c3fb99234568daf7166","45b03f0ec28f4b0e8a0dcc1005df2953","85fafa11386a468fb016374080a023c7","c9cb72bc82a54193bc360b5b869a3534","960264073f5b4338b7f8c7f21356816a","84b012ffcd644e02bd262196a8b979ad","7f6964afc9c34b3caf901d2b8bb977fa","915fd3620fe84b8cb8cef82704adf868","dba36473c9fe4c208fd31bd00d7268fd","11bcd762a6b44b02be9157945ea1e7f1","2a229f6c77fb49ed8a2c4cb8287301a2","3f5d388ceb5c4df2bea0e241292cde7d","7092f348997e4d85a6741a08a47eabfc","a66a74a482d74f80ad15f1d70304d450","10c08106c76c4ac68728f2e3f0359aa0","e2d94819ed0f4d3b84ad70f959cd0502","c3163dfcf853426bae55293c9f600fcf","fb805a205ad8403dbf74fe56d2b935b6"]},"executionInfo":{"status":"ok","timestamp":1702035314460,"user_tz":-540,"elapsed":200285,"user":{"displayName":"장예린","userId":"10756148590164405752"}},"outputId":"0a76048a-2303-4827-f841-55c47eb3b103"},"outputs":[{"output_type":"display_data","data":{"text/plain":["config.json: 0%| | 0.00/1.14k [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"c2fd65a3f6d941e7b2ee311abd843de1"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["configuration_midm.py: 0%| | 0.00/831 [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"f89669afd8344e78af7cb65c2193388d"}},"metadata":{}},{"output_type":"stream","name":"stderr","text":["A new version of the following files was downloaded from https://huggingface.co/KT-AI/midm-bitext-S-7B-inst-v1:\n","- configuration_midm.py\n",". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n"]},{"output_type":"display_data","data":{"text/plain":["modeling_midm.py: 0%| | 0.00/61.1k [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"4e0e55a1fc304d419cd039309a4ad772"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["rotary_position_embedding.py: 0%| | 0.00/4.06k [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"96c28d9784844a38bd43f17fcf341526"}},"metadata":{}},{"output_type":"stream","name":"stderr","text":["A new version of the following files was downloaded from https://huggingface.co/KT-AI/midm-bitext-S-7B-inst-v1:\n","- rotary_position_embedding.py\n",". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n","A new version of the following files was downloaded from https://huggingface.co/KT-AI/midm-bitext-S-7B-inst-v1:\n","- modeling_midm.py\n","- rotary_position_embedding.py\n",". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n"]},{"output_type":"display_data","data":{"text/plain":["model.safetensors.index.json: 0%| | 0.00/25.4k [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"7789f290df334e30a1ad829853e31e15"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Downloading shards: 0%| | 0/2 [00:00, ?it/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"f12a7f89f6d2435f9dfdf05fb7c5daaa"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["model-00001-of-00002.safetensors: 0%| | 0.00/9.97G [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"c88098d28e11421f87463e83e81d3e17"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["model-00002-of-00002.safetensors: 0%| | 0.00/6.22G [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"bab910c60b044b9e972fe84b3860189f"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Loading checkpoint shards: 0%| | 0/2 [00:00, ?it/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"204a944a12884c9d8eff0f0891fe5c23"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["generation_config.json: 0%| | 0.00/132 [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"915fd3620fe84b8cb8cef82704adf868"}},"metadata":{}}],"source":["trained_model = AutoPeftModelForCausalLM.from_pretrained(\n"," script_args.training_args.output_dir,\n"," quantization_config=bnb_config,\n"," device_map=\"auto\",\n"," cache_dir=script_args.cache_dir,\n"," trust_remote_code=True,\n",")"],"id":"39db2ee4-23c8-471f-89b2-bca34964bf81"},{"cell_type":"markdown","source":["**토크나이저 초기화**\n","\n","\n","> 지정된 모델에서 미리 훈련된 토크나이저를 로드함."],"metadata":{"id":"blIuzrDC2-Mr"},"id":"blIuzrDC2-Mr"},{"cell_type":"code","execution_count":null,"metadata":{"id":"b0b75ca4-730d-4bde-88bb-a86462a76d52","colab":{"base_uri":"https://localhost:8080/","height":187,"referenced_widgets":["81a15629dac64a6faa9bcfb5410f3588","e807dda9a7de4d4b86e5c47d1fd951a1","55baa7e325984defbd9a5d53fdb25bda","5264a42c14894af29c70ca4a00562cda","34010f6a2efa4d65b8961817fba44656","1a0306813d514b35afcde99fc7deaee6","6089cbb639724d4898f252199f7933d6","da0e74aa4f3a4905a9f4c6d63a8a489d","38a23ddce9c941a4aa8907125f304ca4","9f26a083a2e7465d94bb0f358001933c","81df7de0293e4ee3953133d479133945","4bfa73ff754548518b55f1e461290b11","2bcabfdad3f34f758de776f8c3d2814d","4a1f25c9fc07474994ec722f58f1bd6a","fe9fcfca5b3a46ccba2bca9620137322","f3c3a96d493a4d70a22ab7d40424d321","286caa64806d4116873d9ca69191e3d2","4942e858401d4e128de58ba58b77f0bd","3ef40a7706fa4cabb1e39bbdcc86d4cf","86ec597fdbb848d3aa9ed7c41faa3523","a2d671fa9905436c91fe32d3c3d59922","0b2ea01d783648f78b57e7677e46dfc4","abd68d68d5c84ecb8ce8daefd486300e","b7d1367a61404abf8a13a17556a72fe5","151d4d95f3954950baa69567abd1a0dc","078fffe629014e678fe30df577468de3","656c87b1edb94a40982da06783c46f61","c07657a02dce4c198c36e1d57211ea9c","e1ce68c3ab6b40b19c3fc9bd3ba236b7","4c5eb15964554c50ad99ff9ad7b83262","7480a95b4d414955a458aa84a461622f","4581e8a6eb4249b780e9773968f4a3d8","a3e657a9b87d4e06a2f8f73625efb049"]},"executionInfo":{"status":"ok","timestamp":1702035338823,"user_tz":-540,"elapsed":1990,"user":{"displayName":"장예린","userId":"10756148590164405752"}},"outputId":"e404a961-4323-4ee7-d8c4-1fe2c98051df"},"outputs":[{"output_type":"display_data","data":{"text/plain":["tokenizer_config.json: 0%| | 0.00/349 [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"81a15629dac64a6faa9bcfb5410f3588"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["midm_bitext_tokenization.py: 0%| | 0.00/12.8k [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"4bfa73ff754548518b55f1e461290b11"}},"metadata":{}},{"output_type":"stream","name":"stderr","text":["A new version of the following files was downloaded from https://huggingface.co/KT-AI/midm-bitext-S-7B-inst-v1:\n","- midm_bitext_tokenization.py\n",". Make sure to double-check they do not contain any added malicious code. To avoid downloading new versions of the code file, you can pin a revision.\n"]},{"output_type":"display_data","data":{"text/plain":["midm_bitext_tokenizer.model: 0%| | 0.00/1.46M [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"abd68d68d5c84ecb8ce8daefd486300e"}},"metadata":{}}],"source":["tokenizer = AutoTokenizer.from_pretrained(\n"," script_args.model_name,\n"," trust_remote_code=True,\n"," cache_dir=script_args.cache_dir,\n",")\n","\n","if getattr(tokenizer, \"pad_token\", None) is None:\n"," tokenizer.pad_token = tokenizer.eos_token\n","tokenizer.padding_side = \"right\" # Fix weird overflow issue with fp16 training\n","\n","tokenizer.add_special_tokens(dict(bos_token=''))\n","\n","trained_model.config.pad_token_id = tokenizer.pad_token_id\n","trained_model.config.bos_token_id = tokenizer.bos_token_id"],"id":"b0b75ca4-730d-4bde-88bb-a86462a76d52"},{"cell_type":"markdown","source":["**데이터셋 생성**\n","\n","\n","> 추론에 사용할 테스트 데이터 생성함.\n"],"metadata":{"id":"Wi_2M_tE3A58"},"id":"Wi_2M_tE3A58"},{"cell_type":"code","source":["valid_dataset = create_valid_datasets(tokenizer, script_args)"],"metadata":{"id":"a_uxsZ4U3EHP","colab":{"base_uri":"https://localhost:8080/","height":305,"referenced_widgets":["2eaf7829ffc140a1b6d3208809466334","f4bf440fc087498e9732285b4cda952e","0794089ac98d4b71b9f4ab72a722b551","96f0f0e093024a028e68cfe0e7d12bfc","fa577ddf15034af8a3b18bc6e2c9ac2d","a110536755b1423c93f71859f593ca3e","d3a728e157aa48d8a8587525f1ceda3d","3e45464565d643c6b32b8102f1c7f209","f79554438f914040b9ec0be3cb7535a6","1c8cc175720a489d97839be5a87f4c16","914ac7f77beb4448b7f68814a5280514","e4343745ea8e47dc9bd457e59c03efa6","493074e2841c4cdeb05b2a978d1c27aa","3c3dc3032edd40b2b993dc3b9823302a","6842f78ad395443fb8e5d4bd7dcf90b2","a2516650762b4f8e900373721236a9b7","bbee9f26f99e43ec97a0e4a93657b8f0","68e04dea8b2d4c88b48f2acb90b639a4","eabc245366b54f5a896e54e8aa00706b","ad20749f4eb54ea897796946b8b26b70","bf57773cdc124e1ea9d0dc5555c2a7bf","dfd4783b4aae4eccaa58f5f638563c91","5de1cc1c663242ca905071a18fc132f6","bbab88a07d324036a35fd28f0601aa72","6b1dcf85dc954777bfb1a074cfcfb74e","1d6890f9b08c40aea9ac9e7f188a0d70","3510db1d4bac45b595b00eea59a561c7","e32eaad3f9a94797ba356760980ccf0f","daa09d2068dc449eb6f7dc5edb6d6d5e","169da7ad74bf4cafbb6e14077883035e","6ce52c88931b4dd2885712da6c795f0b","52bf3328cb2f4f938914037bfb2bc016","9cbf69c354214248a482de8ba3009761","84af4a82759c458ea43b18aa2e3fa78d","05b9b25d31ae40d88e3c3910cd6d05e8","d886251450e142299460d00b6a83478d","7f08f4e2f7794353a706c40a2d6fe16b","a593214ec31441dbbc33840b9818fc6e","54fdc654bb894cf7aa22fee3dc1db9e4","b6b7176d0c9741c389a4fc127ea3935e","a43bdfcfac814d17bd9fb02576761930","ebfcf61e0010469d8c2e9445944c25e5","c3806079349646d3b1260c14b42c24fc","dd91e810468a4ef09cc81746d3b3f002","84c548f8cace4bbda2a2dab1f78c3e29","294323d127564772a6654e8c21008ede","8e7d0b4ccee74857ae10136970199e72","9e554e0ba8a84a3baa50079f4b755cca","7cc4edc38c4c4a8c9236b5a880ba434d","a9e88671e5bb4809b36571ae4998c11a","092ea00b9f37476ea910287ca9fd594b","c52af5c452bf4dd4a1e5aaac5df2692b","41c17bf9195540fc8c78ea45429c9cd9","88fcfdcdd3c2433e8efee0a7c52df212","efc33584391e4ec99eacdff33e67bdc4","ec009d083c414688a22d8245eba8299b","5b56720078114f0b9a138d9f58c8e694","e0a18abaf0de475ebdda661a416a5296","81ef9f81ee7d4b9da630928f58fb3552","03740b16841242faaadf636e86fec671","eb160d88e7584c12b447d690613881cd","7348a1af040b4809b622997df68bcb5e","d00a686c6bf24a818247bb5e80f9094a","53c8bfb3ea384bfca527ba0ca9ee7a00","79fa46263201423a824d9eac5dea9517","85e8cf64b3c24ca3932d76fad2df34cf","4bbefa59b4964625bf344febe6cd498e","dafb892db7b949579011b3e4a9a8eada","e4a6f2ee44314785b071f33e8bca7346","cdc16a985df94125b233ec4f5935697f","75c19385910346fe956f391ffd281e7c","023fbf8f348645d9bafc51d7c8e057e8","f659cb7caff047d69802b09385c461de","bab379e08b3c44279e75c2686762e562","0d00703b1efb480dadb681bc119082c6","d591a8a4fa3f4dd1bb491d73fb326035","a0a13112dcab4ca6baf044e4df6ab098","8c4aae732a1146b680f3b2a41d167a08","61033c650eac4ba3941efb4e60b9e860","0e3e6dd4655d4eafbaa40307fc389168","b4d596da34d6463a92c8312e908ea6bc","5a00b40b55d14bf1b5b50879f218a5cc","256d0f9209f84f54961ddd871d0533b1","9ea42fea8c974593a4088863dc5f0b7a","064ec33c77cb499da2b5c95532cc1998","38f480b370744b11b941d645e6b8397a","1a9e4bba634f4f2b8c0e0fbe1f474c64","a03145c757ef4010ba961153df8ba0a1","27a4ae327f794d1ca4c76b2da68136e8","9d36060760c04bf9ac29aac2d4dad7a1","5876b33eba414869a43069e21de4d221","86f39bcada2c4fa091e350c0ccc368c7","b955712e65424f8dbd7f52764a434af8","a953b9b5ddac44c9b1490dc7b8de8862","83177f7df428445fb22a087f6122016d","e1866f1e20c94bd4beb6917dc4d25495","0ba40c87ac5c4406aa386d7c24216fef","9c5235da4ef748789d631e908b65181b","2d7f004eff654b3ca89fea1f2c2aa574"]},"executionInfo":{"status":"ok","timestamp":1702035353099,"user_tz":-540,"elapsed":12783,"user":{"displayName":"장예린","userId":"10756148590164405752"}},"outputId":"02b1ed9f-c670-4732-b2dd-f4a7f9e93652"},"id":"a_uxsZ4U3EHP","execution_count":null,"outputs":[{"output_type":"display_data","data":{"text/plain":["Downloading builder script: 0%| | 0.00/3.18k [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"2eaf7829ffc140a1b6d3208809466334"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Downloading metadata: 0%| | 0.00/1.67k [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"e4343745ea8e47dc9bd457e59c03efa6"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Downloading readme: 0%| | 0.00/3.74k [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"5de1cc1c663242ca905071a18fc132f6"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Downloading data files: 0%| | 0/2 [00:00, ?it/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"84af4a82759c458ea43b18aa2e3fa78d"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Downloading data: 0%| | 0.00/6.33M [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"84c548f8cace4bbda2a2dab1f78c3e29"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Downloading data: 0%| | 0.00/2.12M [00:00, ?B/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"ec009d083c414688a22d8245eba8299b"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Extracting data files: 0%| | 0/2 [00:00, ?it/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"4bbefa59b4964625bf344febe6cd498e"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Generating train split: 0%| | 0/150000 [00:00, ? examples/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"8c4aae732a1146b680f3b2a41d167a08"}},"metadata":{}},{"output_type":"display_data","data":{"text/plain":["Generating test split: 0%| | 0/50000 [00:00, ? examples/s]"],"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"27a4ae327f794d1ca4c76b2da68136e8"}},"metadata":{}}]},{"cell_type":"markdown","metadata":{"id":"X1tRCa4EiYXp"},"source":["추론 과정에서는 GPU 메모리를 약 5.5 GB 활용"],"id":"X1tRCa4EiYXp"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"e374555b-9f8a-4617-8ea7-c1e6ee1b2999","executionInfo":{"status":"ok","timestamp":1702037381306,"user_tz":-540,"elapsed":2026493,"user":{"displayName":"장예린","userId":"10756148590164405752"}},"outputId":"99642683-e38d-4f19-d277-464b755858aa"},"outputs":[{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.10/dist-packages/transformers/generation/utils.py:1473: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use and modify the model generation configuration (see https://huggingface.co/docs/transformers/generation_strategies#default-text-generation-configuration )\n"," warnings.warn(\n"]}],"source":["eval_dic = {i: wrapper_generate(model=trained_model, input_prompt=prompt_template.format(System=default_system_msg, User=example[\"document\"]))for i, example in enumerate(valid_dataset)}"],"id":"e374555b-9f8a-4617-8ea7-c1e6ee1b2999"},{"cell_type":"markdown","source":["**모델 평가 및 결과**\n","\n","\n","> 정확도 및 혼돈 행렬로 모델을 평가함."],"metadata":{"id":"O50xByhv3RoQ"},"id":"O50xByhv3RoQ"},{"cell_type":"code","source":["import seaborn as sns\n","import matplotlib.pyplot as plt\n","import pandas as pd\n","from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score, f1_score, classification_report\n","\n","true_labels = [example[\"label\"] for example in valid_dataset]\n","\n","predicted_labels = [1 if \"긍정\" in eval_dic[i] else 0 for i in range(len(valid_dataset))]\n","\n","conf_matrix = confusion_matrix(true_labels, predicted_labels)\n","\n","accuracy = accuracy_score(true_labels, predicted_labels)\n","\n","TN = conf_matrix[0, 0] if len(conf_matrix) > 1 else 0\n","TP = conf_matrix[1, 1] if len(conf_matrix) > 1 else 0\n","FP = conf_matrix[0, 1] if len(conf_matrix) > 1 else 0\n","FN = conf_matrix[1, 0] if len(conf_matrix) > 1 else 0\n","\n","# 강의자료 표처럼 만들기 위해 TP, TN 위치 바꿈\n","conf_matrix[0, 0] = TP\n","conf_matrix[1, 1] = TN\n","\n","# PP, PN\n","data = {\n"," 'True Positive (TP)': [TP, FN],\n"," 'True Negative (TN)': [FP, TN],\n","}\n","\n","df_2x2 = pd.DataFrame(data, index=['Predicted Positive (PP)', 'Predicted Negative (PN)'])\n","\n","print(\"Accuracy:\", accuracy)\n","#print(df_2x2)\n","\n","# Create a heatmap of the confusion matrix\n","plt.figure(figsize=(4,2))\n","sns.set(font_scale=1.4) # 폰트 크기 조절\n","\n","# 히트맵 생성\n","sns.heatmap(conf_matrix, annot=True, fmt=\"d\", cmap=\"Blues\", linewidths=.5, annot_kws={\"size\": 12})\n","plt.xticks(ticks=[0.5, 1.5], labels=['TP', 'TN'])\n","plt.yticks(ticks=[0.5, 1.5], labels=['PP', 'PN'])\n","plt.title('Confusion Matrix')\n","plt.show()\n","\n","# 정밀도, 재현율, F1 점수 계산\n","precision = precision_score(true_labels, predicted_labels)\n","recall = recall_score(true_labels, predicted_labels)\n","f1 = f1_score(true_labels, predicted_labels)\n","\n","print(\"Precision:\", precision)\n","print(\"Recall:\", recall)\n","print(\"F1 Score:\", f1, \"\\n\")\n","\n","# 분류 리포트 출력\n","class_report = classification_report(true_labels, predicted_labels, target_names=['Negative', 'Positive'])\n","print(\"Classification Report:\\n\", class_report)"],"metadata":{"id":"TL7zOjZD3T14","colab":{"base_uri":"https://localhost:8080/","height":522},"executionInfo":{"status":"ok","timestamp":1702037385497,"user_tz":-540,"elapsed":1004,"user":{"displayName":"장예린","userId":"10756148590164405752"}},"outputId":"104717f4-2d79-48f3-a62c-b34e358cd527"},"id":"TL7zOjZD3T14","execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Accuracy: 0.91\n"]},{"output_type":"display_data","data":{"text/plain":["