{"cells":[{"cell_type":"markdown","source":["# **미세튜닝된 어댑터 업로드한 ID: yelin1001**"],"metadata":{"id":"8WVANRh09Lmi"},"id":"8WVANRh09Lmi"},{"cell_type":"markdown","metadata":{"id":"28e4c4d1-a73f-437b-a1bd-c2cc3874924a"},"source":["# 강의 11주차: llama2-food-order-understanding\n","\n","1. llama-2-7b-chat-hf 를 주문 문장 이해에 미세 튜닝\n","\n","- food-order-understanding-small-3200.json (학습)\n","- food-order-understanding-small-800.json (검증)\n","\n","\n","종속적인 필요 내용\n","- huggingface 계정 설정 및 llama-2 사용 승인\n","- 로깅을 위한 wandb"],"id":"28e4c4d1-a73f-437b-a1bd-c2cc3874924a"},{"cell_type":"markdown","source":["**종속성 설치**"],"metadata":{"id":"bw2BFoFLTKUm"},"id":"bw2BFoFLTKUm"},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":8705,"status":"ok","timestamp":1702196144558,"user":{"displayName":"장예람","userId":"15119681621712404836"},"user_tz":-540},"id":"nDZe_wqKU6J3","colab":{"base_uri":"https://localhost:8080/"},"outputId":"e6dd1dd9-debb-4436-f7bc-56bbcb8f92aa"},"outputs":[{"output_type":"stream","name":"stdout","text":["Requirement already satisfied: transformers in /usr/local/lib/python3.10/dist-packages (4.35.2)\n","Requirement already satisfied: peft in /usr/local/lib/python3.10/dist-packages (0.7.0)\n","Requirement already satisfied: accelerate in /usr/local/lib/python3.10/dist-packages (0.25.0)\n","Requirement already satisfied: optimum in /usr/local/lib/python3.10/dist-packages (1.15.0)\n","Requirement already satisfied: bitsandbytes in /usr/local/lib/python3.10/dist-packages (0.41.3)\n","Requirement already satisfied: trl in /usr/local/lib/python3.10/dist-packages (0.7.4)\n","Requirement already satisfied: wandb in /usr/local/lib/python3.10/dist-packages (0.16.1)\n","Requirement 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","Requirement already satisfied: coloredlogs in /usr/local/lib/python3.10/dist-packages (from optimum) (15.0.1)\n","Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from optimum) (1.12)\n","Requirement already satisfied: datasets in /usr/local/lib/python3.10/dist-packages (from optimum) (2.15.0)\n","Requirement already satisfied: tyro>=0.5.11 in /usr/local/lib/python3.10/dist-packages (from trl) (0.6.0)\n","Requirement already satisfied: Click!=8.0.0,>=7.1 in /usr/local/lib/python3.10/dist-packages (from wandb) (8.1.7)\n","Requirement already satisfied: GitPython!=3.1.29,>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from wandb) (3.1.40)\n","Requirement already satisfied: sentry-sdk>=1.0.0 in /usr/local/lib/python3.10/dist-packages (from wandb) (1.38.0)\n","Requirement already satisfied: docker-pycreds>=0.4.0 in /usr/local/lib/python3.10/dist-packages (from wandb) (0.4.0)\n","Requirement already satisfied: setproctitle in /usr/local/lib/python3.10/dist-packages (from wandb) (1.3.3)\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","Requirement already satisfied: gitdb<5,>=4.0.1 in /usr/local/lib/python3.10/dist-packages (from GitPython!=3.1.29,>=1.0.0->wandb) (4.0.11)\n","Requirement 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","Requirement already satisfied: sentencepiece!=0.1.92,>=0.1.91 in /usr/local/lib/python3.10/dist-packages (from transformers) (0.1.99)\n","Requirement already satisfied: docstring-parser>=0.14.1 in /usr/local/lib/python3.10/dist-packages (from tyro>=0.5.11->trl) (0.15)\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","Requirement already satisfied: shtab>=1.5.6 in /usr/local/lib/python3.10/dist-packages (from tyro>=0.5.11->trl) (1.6.5)\n","Requirement already satisfied: humanfriendly>=9.1 in /usr/local/lib/python3.10/dist-packages (from coloredlogs->optimum) (10.0)\n","Requirement already satisfied: pyarrow>=8.0.0 in /usr/local/lib/python3.10/dist-packages (from datasets->optimum) (9.0.0)\n","Requirement already satisfied: pyarrow-hotfix in /usr/local/lib/python3.10/dist-packages (from datasets->optimum) (0.6)\n","Requirement already satisfied: dill<0.3.8,>=0.3.0 in /usr/local/lib/python3.10/dist-packages (from datasets->optimum) (0.3.7)\n","Requirement 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","Requirement already satisfied: multiprocess in /usr/local/lib/python3.10/dist-packages (from datasets->optimum) (0.70.15)\n","Requirement 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","Requirement already satisfied: smmap<6,>=3.0.1 in /usr/local/lib/python3.10/dist-packages (from gitdb<5,>=4.0.1->GitPython!=3.1.29,>=1.0.0->wandb) (5.0.1)\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"]}],"source":["pip install transformers peft accelerate optimum bitsandbytes trl wandb"],"id":"nDZe_wqKU6J3"},{"cell_type":"markdown","source":["**라이브러리 및 모듈 임포트**\n","\n","> 데이터 처리, 모델 학습, 추론에 사용되는 라이브러리 및 모듈을 포함함."],"metadata":{"id":"4_gMKrh6TjGX"},"id":"4_gMKrh6TjGX"},{"cell_type":"code","execution_count":null,"metadata":{"executionInfo":{"elapsed":14335,"status":"ok","timestamp":1702196162680,"user":{"displayName":"장예람","userId":"15119681621712404836"},"user_tz":-540},"id":"51eb00d7-2928-41ad-9ae9-7f0da7d64d6d","colab":{"base_uri":"https://localhost:8080/"},"outputId":"3e9731bf-0785-48a2-c214-c90fabfe2975"},"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":145,"referenced_widgets":["6c3c61b83cd64895937672390d7d23ee","0211c736ccd34d1a883453a0c7a75968","63e98766b62245e49b008692b300f2a5","922e92f8c4aa4b04a5d4eb2553460b16","9f75bb819f944448a1c48692de4264b3","9db00cec03a04177a27ee68e7b3e8566","d0e2410a804e434590702b7ebf22ac6a","93089b782db543d6bcd698360c6e7cce","4bb9b8b910244b6ba45acf68e8c17fbf","a74143e64e6540358b32ec393fd6ca8a","6afdf54c14144c5695af1c986482d5f4","ae18cd14111e4c2ca8cec89c57d343c4","fa5f74b5f8c440ae81008a623fea9d8f","3d62a99998234c99ae900f564c9a53e3","74c090ffb4f641acba87a8b8e73bf9a2","7bba3d2cf45c48ad92c5377cd6932541","6a085462311f4fbaa1eac0225e55e1f4","b9186db9ec4040eb9cb1c5831994a9f3","e69eea3a02594848b2a34c23b4a0a319","77865c0d156d4da28694b7351d8516b2","52eacf6b63324c8c8b137939034459f0","fffaee2b18324f3294b77466d8bcfd3f","f178f4ef9e8a426a924ef7370da16a29","66ff6848ba0b46d3b34055af04ca8d5d","fd62b22cb52d4d59aba9965853674b1a","c15cf50dafcf4d05a81ac7e3b778372d","bfbf4f2c9d0e41f0a9daf904ff728722","40c2db0f041a41e4a2852cb2a7ad70ba","47b5041a893747d1b51577bc4140ccfa","4d205bfdf7b2463aba5dd9aeabd4b1b9","639699879c444db5a1a877f052690e75","9ca13b8c658a43379330ccfaee4b1853"]},"executionInfo":{"elapsed":512,"status":"ok","timestamp":1702196168826,"user":{"displayName":"장예람","userId":"15119681621712404836"},"user_tz":-540},"id":"tX7gYxZaVhYL","outputId":"65a19ba6-4b9a-4847-ab45-4bf56358a2df"},"outputs":[{"output_type":"display_data","data":{"text/plain":["VBox(children=(HTML(value='
/content/wandb/run-20231210_062710-fuf9tsl1
"],"text/plain":["Step | \n","Training Loss | \n","
---|---|
100 | \n","0.999800 | \n","
200 | \n","0.662000 | \n","
300 | \n","0.635600 | \n","
"],"text/plain":[" "]},"metadata":{}},{"output_type":"execute_result","data":{"text/plain":["TrainOutput(global_step=1600, training_loss=0.6306900310516358, metrics={'train_runtime': 6006.4783, 'train_samples_per_second': 0.533, 'train_steps_per_second': 0.266, 'total_flos': 6.53458201903104e+16, 'train_loss': 0.6306900310516358, 'epoch': 0.02})"]},"metadata":{},"execution_count":26}],"source":["trainer.train()\n"],"id":"tFeEyBJnCi0C"},{"cell_type":"markdown","metadata":{"id":"76sRe172fGlm"},"source":["구글 코랩 T-4 GPU: 1:37:34 예상시간\n","- 총 1,600 스텝 필요\n","- 하지만 이보다 일찍 종료됨 약 900번 미만 스텝에서 종료됨\n","\n","시퀀스 길이 512의 경우\n","- 14.4 G / 15.0 G 사용\n","- 메모리 오버플로우 발생시 512보다 줄일 것"],"id":"76sRe172fGlm"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":35},"executionInfo":{"elapsed":516,"status":"ok","timestamp":1702196105028,"user":{"displayName":"장예람","userId":"15119681621712404836"},"user_tz":-540},"id":"3Y4FQSyRghQt","outputId":"f715a831-ca2d-4db2-dd28-5d03feaad25c"},"outputs":[{"output_type":"execute_result","data":{"text/plain":["'/gdrive/MyDrive/nlp/lora-llama-2-7b-nsmc'"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"}},"metadata":{},"execution_count":27}],"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):\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,\n"," use_cache=True,\n"," max_new_tokens=float('inf'),\n"," temperature=0.5\n"," )\n"," decoded_text = tokenizer.batch_decode(pred, skip_special_tokens=True)\n"," return (decoded_text[0][len(input_prompt):])"],"id":"1919cf1f-482e-4185-9d06-e3cea1918416"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":1537373,"status":"ok","timestamp":1701343970452,"user":{"displayName":"장예린","userId":"14823696343626614035"},"user_tz":-540},"id":"Lxptc3T1kDSO","outputId":"1604b12c-871e-4d41-f739-b7e54ad03091"},"outputs":[{"name":"stdout","output_type":"stream","text":[";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n"]}],"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)}"],"id":"Lxptc3T1kDSO"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"elapsed":398,"status":"ok","timestamp":1701265256607,"user":{"displayName":"장예린","userId":"08326973126538993419"},"user_tz":-540},"id":"fefd04ba-2ed8-4f84-bdd0-86d52b3f39f6","outputId":"642ae37a-4c0c-4385-f75c-20a8f0957242"},"outputs":[{"name":"stdout","output_type":"stream","text":["부정\n"]}],"source":["print(eval_dic[8])"],"id":"fefd04ba-2ed8-4f84-bdd0-86d52b3f39f6"},{"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":"U5AcpecYRRlC"},"id":"U5AcpecYRRlC","execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**프롬프트 정의**\n","\n","\n","> 사용자 영화 리뷰의 예측을 생성하기 위한 프롬프트 정의함.\n","\n"],"metadata":{"id":"qVr_AMtJa-AF"},"id":"qVr_AMtJa-AF"},{"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":"nzxPwBS5RY1P"},"id":"nzxPwBS5RY1P","execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**wrapper_generate 함수**\n","\n","\n","> 사용자 영화 리뷰에 대한 입력 프롬프트 생성.\n","\n"],"metadata":{"id":"zkWiQd1NaFZz"},"id":"zkWiQd1NaFZz"},{"cell_type":"code","source":["def wrapper_generate(model, input_prompt):\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,\n"," use_cache=True,\n"," max_new_tokens=float('inf'),\n"," temperature=0.5\n"," )\n"," decoded_text = tokenizer.batch_decode(pred, skip_special_tokens=True)\n"," return (decoded_text[0][len(input_prompt):])"],"metadata":{"id":"nPNJNH5TRcYQ"},"id":"nPNJNH5TRcYQ","execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**양자화 설정**\n","\n","\n","> 4비트로 양자화함.\n","\n"],"metadata":{"id":"XwPG_xZ3aXaL"},"id":"XwPG_xZ3aXaL"},{"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비트로 양자화하여 저장된 모델을 가져옴.\n","\n"],"metadata":{"id":"m2z3YCa1aoP4"},"id":"m2z3YCa1aoP4"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":49,"referenced_widgets":["1bf559f3bca24143856ad4ae301fbbdd","f9a6930ee95e47ffb42bdc3b261ca9ee","d92a0b5ae4eb41f1b0c979bdbdd4d9e8","5006257fb8974369b5f5d7f06618714c","a1b69b152643481b809dbabd0d861bfe","910cd365fa204024b95359cdda0733a4","acbb7b75e0a14062ae38ebd027277479","117d8169a35d412f8ae31bf1f605f878","484c97490f354e21a62b6e29423c3f30","88b0fd56cdeb4ab39ba32c88a751461c","ffd4cb3c6fcf456695c7acf1f667564b"]},"id":"39db2ee4-23c8-471f-89b2-bca34964bf81","outputId":"e0853c55-6dc7-4683-865e-ab4869bdd232","executionInfo":{"status":"ok","timestamp":1702196292221,"user_tz":-540,"elapsed":76093,"user":{"displayName":"장예람","userId":"15119681621712404836"}}},"outputs":[{"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":"1bf559f3bca24143856ad4ae301fbbdd"}},"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",")"],"id":"39db2ee4-23c8-471f-89b2-bca34964bf81"},{"cell_type":"markdown","source":["**토크나이저 초기화**\n","\n","\n","> 지정된 모델에서 미리 훈련된 토크나이저를 로드함.\n","\n"],"metadata":{"id":"aZSH97Zmb3fK"},"id":"aZSH97Zmb3fK"},{"cell_type":"code","execution_count":null,"metadata":{"id":"b0b75ca4-730d-4bde-88bb-a86462a76d52"},"outputs":[],"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","trained_model.config.pad_token_id = tokenizer.pad_token_id"],"id":"b0b75ca4-730d-4bde-88bb-a86462a76d52"},{"cell_type":"markdown","source":["**데이터셋 생성**\n","\n","\n","> 추론에 사용할 테스트 데이터 생성함.\n","\n"],"metadata":{"id":"qHcNDlVObcoa"},"id":"qHcNDlVObcoa"},{"cell_type":"code","execution_count":null,"metadata":{"id":"b5O6GD2BVr67"},"outputs":[],"source":["valid_dataset = create_valid_datasets(tokenizer, script_args)"],"id":"b5O6GD2BVr67"},{"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/"},"executionInfo":{"elapsed":1899461,"status":"ok","timestamp":1702198228843,"user":{"displayName":"장예람","userId":"15119681621712404836"},"user_tz":-540},"id":"e374555b-9f8a-4617-8ea7-c1e6ee1b2999","outputId":"8abff7db-796b-4a9c-8720-b366eb0134aa"},"outputs":[{"output_type":"stream","name":"stdout","text":[";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";긍정\n",";부정\n",";부정\n",";부정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";긍정\n",";부정\n",";부정\n",";긍정\n",";부정\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","> 정확도 및 혼돈 행렬로 모델을 평가함.\n","\n"],"metadata":{"id":"PtEfo3dHg6b2"},"id":"PtEfo3dHg6b2"},{"cell_type":"code","execution_count":null,"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":525},"executionInfo":{"elapsed":647,"status":"ok","timestamp":1702199173554,"user":{"displayName":"장예람","userId":"15119681621712404836"},"user_tz":-540},"id":"zGWyzs1tbSJH","outputId":"aea1608e-5bb1-4329-faed-4c6a9f582816"},"outputs":[{"output_type":"stream","name":"stdout","text":["Accuracy: 0.874\n"]},{"output_type":"display_data","data":{"text/plain":["\n"," \n","
\n"," \n"," \n"," \n"," Step \n"," Training Loss \n"," \n"," \n"," 100 \n"," 0.999800 \n"," \n"," \n"," 200 \n"," 0.662000 \n"," \n"," \n"," 300 \n"," 0.635600 \n"," \n"," \n"," 400 \n"," 0.624600 \n"," \n"," \n"," 500 \n"," 0.612100 \n"," \n"," \n"," 600 \n"," 0.590900 \n"," \n"," \n"," 700 \n"," 0.623500 \n"," \n"," \n"," 800 \n"," 0.575600 \n"," \n"," \n"," 900 \n"," 0.605100 \n"," \n"," \n"," 1000 \n"," 0.594400 \n"," \n"," \n"," 1100 \n"," 0.600400 \n"," \n"," \n"," 1200 \n"," 0.608700 \n"," \n"," \n"," 1300 \n"," 0.591000 \n"," \n"," \n"," 1400 \n"," 0.581000 \n"," \n"," \n"," 1500 \n"," 0.599200 \n"," \n"," \n"," \n","1600 \n"," 0.587100 \n","