{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# 알아두면 쓸데있는 신비한 패키지 사전 📒\n\n제 3회 AIKUTHON에 오신 걸 환영합니다 🎉\n\n본 노트북에서는 이번 대회에서 추가적으로 활용할 수 있는 패키지를 안내합니다.\n\n대회를 참여하고 수행하는 데에 도움이 되시길 바랍니다.\n\n<img width=\"640px\" height=\"320px\" src=\"https://image.tving.com/upload/cms/caip/CAIP0200/P000379768.jpg/dims/resize/1280\" />\n\n> 본 노트북은 구글 코랩 기준으로 만들어졌습니다. 몇몇 기능을 동작하지 않을 수 있으니 그런 경우에는 이 문서를 참고해주세요. [링크](https://colab.research.google.com/drive/1Fb_TlEIz-LGnqOkCui95sV2SnCNwDvjz?usp=sharing)","metadata":{"id":"e56izjfYwXXJ"}},{"cell_type":"markdown","source":"## 목차\n\n1.   Pandas로 캐글 대회 참여하기\n2.   Pyplot으로 데이터 시각화하기\n3.   Einops로 자유자재로 텐서 다루기\n4.   Scikit-learn Metrics로 모델 다양하게 평가하기\n5.   Tensorboard로 훈련 과정 모니터링하기\n\n","metadata":{"id":"v7czmpqqn1re"}},{"cell_type":"markdown","source":"## 1. Pandas로 캐글 대회 참여하기\n\n*   [Pandas Website](https://pandas.pydata.org/)\n*   [Pandas Docs](https://pandas.pydata.org/docs/)","metadata":{"id":"3h5aYpSnw5cP"}},{"cell_type":"markdown","source":"먼저 훈련 데이터 파일에 접근해봅시다.","metadata":{"id":"M5qUVUK41WQ0"}},{"cell_type":"code","source":"# 배부 받은 폴더의 train.csv 파일까지의 경로\nimport os\n\ntrain_filepath = \"/kaggle/input/3-aikuthon/train.csv\"\nassert os.path.exists(train_filepath), \"File does not exist\"","metadata":{"id":"_NhkspCaxfiK","execution":{"iopub.status.busy":"2023-08-25T02:01:15.182033Z","iopub.execute_input":"2023-08-25T02:01:15.182534Z","iopub.status.idle":"2023-08-25T02:01:15.225710Z","shell.execute_reply.started":"2023-08-25T02:01:15.182498Z","shell.execute_reply":"2023-08-25T02:01:15.223624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pandas 라이브러리를 활용하여 train.csv 파일을 데이터프레임 형태로 열기\nimport pandas as pd\n\ntrain_df = pd.read_csv(train_filepath)\ntrain_df = train_df.rename(columns={train_df.columns[0]: \"id\"})\ntrain_df.shape  # 데이터프레임의 크기. 135,792개의 데이터가 있습니다.","metadata":{"id":"oBqds7s-yOPa","outputId":"663057f2-7ad5-4aa5-e394-9f1d26b54fe8","execution":{"iopub.status.busy":"2023-08-25T02:01:15.229320Z","iopub.execute_input":"2023-08-25T02:01:15.229806Z","iopub.status.idle":"2023-08-25T02:01:20.522506Z","shell.execute_reply.started":"2023-08-25T02:01:15.229766Z","shell.execute_reply":"2023-08-25T02:01:20.520438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 데이터프레임의 첫 5행만 확인하기\n# 총 8개의 행이 존재합니다.\n# (id: 곡 식별자, song_name: 곡 제목, genre: 장르 식별자, lyrics: 가사, ...)\ntrain_df.head()","metadata":{"id":"rsHYkj2nymYe","outputId":"cc6b061f-f088-4f65-bf7f-b69958f683e1","execution":{"iopub.status.busy":"2023-08-25T02:01:20.524621Z","iopub.execute_input":"2023-08-25T02:01:20.525006Z","iopub.status.idle":"2023-08-25T02:01:20.546389Z","shell.execute_reply.started":"2023-08-25T02:01:20.524975Z","shell.execute_reply":"2023-08-25T02:01:20.544638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 행 이름과 인덱스를 통해 값에 접근하기\ntrain_df[\"id\"][0]","metadata":{"id":"YUEs10kNzEFF","outputId":"924cccc9-7f26-46f8-e961-92094ce8ebe0","execution":{"iopub.status.busy":"2023-08-25T02:01:20.548426Z","iopub.execute_input":"2023-08-25T02:01:20.548763Z","iopub.status.idle":"2023-08-25T02:01:20.570257Z","shell.execute_reply.started":"2023-08-25T02:01:20.548739Z","shell.execute_reply":"2023-08-25T02:01:20.568207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"song_name\"][10]","metadata":{"id":"Lylfp9IhzKtG","outputId":"6c1f0262-1fe7-4587-c8dd-12f12e0c8c38","execution":{"iopub.status.busy":"2023-08-25T02:01:20.574285Z","iopub.execute_input":"2023-08-25T02:01:20.574688Z","iopub.status.idle":"2023-08-25T02:01:20.586985Z","shell.execute_reply.started":"2023-08-25T02:01:20.574657Z","shell.execute_reply":"2023-08-25T02:01:20.585216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df[\"lyrics\"][100]","metadata":{"id":"94aAGkggzO93","outputId":"32d92e47-d265-45b9-cee3-04525b1b1a53","execution":{"iopub.status.busy":"2023-08-25T02:01:20.588903Z","iopub.execute_input":"2023-08-25T02:01:20.589247Z","iopub.status.idle":"2023-08-25T02:01:20.608008Z","shell.execute_reply.started":"2023-08-25T02:01:20.589217Z","shell.execute_reply":"2023-08-25T02:01:20.606559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"장르 파일도 비슷한 방식으로 활용할 수 있습니다.","metadata":{"id":"NHAiYk_v1aox"}},{"cell_type":"code","source":"# 배부 받은 폴더의 genres.csv 파일까지의 경로\ngenres_filepath = \"/kaggle/input/3-aikuthon/genres.csv\"\nassert os.path.exists(genres_filepath)","metadata":{"id":"kkQcPln_zhB8","execution":{"iopub.status.busy":"2023-08-25T02:01:20.610095Z","iopub.execute_input":"2023-08-25T02:01:20.610483Z","iopub.status.idle":"2023-08-25T02:01:20.623146Z","shell.execute_reply.started":"2023-08-25T02:01:20.610454Z","shell.execute_reply":"2023-08-25T02:01:20.621506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# genres.csv 파일을 데이터프레임 형태로 열기\ngenres_df = pd.read_csv(genres_filepath, index_col=0)\ngenres_df.shape  # 총 52개의 장르가 있습니다.","metadata":{"id":"yKWvqUuezYWH","outputId":"755fcd28-662e-4b63-f8e0-c308fdd7f109","execution":{"iopub.status.busy":"2023-08-25T02:01:20.625666Z","iopub.execute_input":"2023-08-25T02:01:20.626117Z","iopub.status.idle":"2023-08-25T02:01:20.654132Z","shell.execute_reply.started":"2023-08-25T02:01:20.626083Z","shell.execute_reply":"2023-08-25T02:01:20.653086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"genres_df","metadata":{"id":"xRlfDfzZzvY9","outputId":"97ca9b8a-74a9-4a1a-9022-67b8d3ba05c8","execution":{"iopub.status.busy":"2023-08-25T02:01:20.655602Z","iopub.execute_input":"2023-08-25T02:01:20.655969Z","iopub.status.idle":"2023-08-25T02:01:20.670389Z","shell.execute_reply.started":"2023-08-25T02:01:20.655937Z","shell.execute_reply":"2023-08-25T02:01:20.668312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 특정한 행에 있는 곡의 장르를 알 수 있습니다\ngenre_id = int(train_df[\"genre\"][0])\ngenres_df[\"genre\"][genre_id]","metadata":{"id":"SG85t_KfzzqT","outputId":"488123cb-e542-4564-908d-7658b52940b0","execution":{"iopub.status.busy":"2023-08-25T02:01:20.672513Z","iopub.execute_input":"2023-08-25T02:01:20.673173Z","iopub.status.idle":"2023-08-25T02:01:20.685669Z","shell.execute_reply.started":"2023-08-25T02:01:20.673130Z","shell.execute_reply":"2023-08-25T02:01:20.684856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 몇몇 행의 여러 개의 장르에 동시에 속합니다\nprint(train_df[\"genre\"][4])\ngenre_ids = map(int, train_df[\"genre\"][4].split(\",\"))\n[genres_df[\"genre\"][i] for i in genre_ids]","metadata":{"id":"bDoaKu07z_uc","outputId":"a26976ee-91f0-4070-c203-a1c7e9cc5e3e","execution":{"iopub.status.busy":"2023-08-25T02:01:20.686832Z","iopub.execute_input":"2023-08-25T02:01:20.687128Z","iopub.status.idle":"2023-08-25T02:01:20.705222Z","shell.execute_reply.started":"2023-08-25T02:01:20.687104Z","shell.execute_reply":"2023-08-25T02:01:20.703964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"테스트 파일을 읽고 제출용 `.csv` 파일을 만들어봅시다.","metadata":{"id":"AMqeaqZ41hiz"}},{"cell_type":"code","source":"# 배부 받은 폴더의 test.csv 파일까지의 경로\ntest_filepath = \"/kaggle/input/3-aikuthon/test.csv\"\nassert os.path.exists(test_filepath)","metadata":{"id":"CpKGBcdU1mIk","execution":{"iopub.status.busy":"2023-08-25T02:01:20.706387Z","iopub.execute_input":"2023-08-25T02:01:20.706797Z","iopub.status.idle":"2023-08-25T02:01:20.718598Z","shell.execute_reply.started":"2023-08-25T02:01:20.706761Z","shell.execute_reply":"2023-08-25T02:01:20.716455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test.csv 파일을 데이터프레임 형태로 열기\ntest_df = pd.read_csv(test_filepath)\ntest_df = test_df.rename(columns={test_df.columns[0]: \"id\"})\ntest_df.shape","metadata":{"id":"Ct96Eb-81tEz","outputId":"0797c69e-4d4f-4740-f589-ebb9e3434750","execution":{"iopub.status.busy":"2023-08-25T02:01:20.720295Z","iopub.execute_input":"2023-08-25T02:01:20.720662Z","iopub.status.idle":"2023-08-25T02:01:21.092811Z","shell.execute_reply.started":"2023-08-25T02:01:20.720632Z","shell.execute_reply":"2023-08-25T02:01:21.090619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"id":"uFcKEm9n1zVw","outputId":"8b18daab-393c-4f91-a393-8b0f9ef6cec8","execution":{"iopub.status.busy":"2023-08-25T02:01:21.098320Z","iopub.execute_input":"2023-08-25T02:01:21.098669Z","iopub.status.idle":"2023-08-25T02:01:21.110757Z","shell.execute_reply.started":"2023-08-25T02:01:21.098641Z","shell.execute_reply":"2023-08-25T02:01:21.109464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 여러분이 만든 모델이 각각의 테스트 데이터에 대해 장르 식별자를 예측했다고 합시다.\n# 그리고 예측한 데이터가 아래와 같이 길이가 10,500인 배열에 저장되어 있다고 합시다.\nfrom random import randint\n\npredictions = [randint(0, 52) for _ in range(10_500)]","metadata":{"id":"0Bk0H4u_2CC7","execution":{"iopub.status.busy":"2023-08-25T02:01:21.112272Z","iopub.execute_input":"2023-08-25T02:01:21.112563Z","iopub.status.idle":"2023-08-25T02:01:21.136188Z","shell.execute_reply.started":"2023-08-25T02:01:21.112539Z","shell.execute_reply":"2023-08-25T02:01:21.134214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 제출용 .csv 파일은 두 개의 행 (id, genre) 만을 포함해야 합니다.\n# id 행은 테스트 데이터의 id 행과 동일하고, genre 행은 모델이 예측한 장르 식별자 값이어야 합니다.\nsubmission = {\"id\": test_df[\"id\"], \"genre\": predictions}\nsubmission_df = pd.DataFrame.from_dict(submission)\nsubmission_df.shape","metadata":{"id":"mItmVdqz2oM7","outputId":"e96404e7-f0e6-4fd2-fa3e-737f06c5761a","execution":{"iopub.status.busy":"2023-08-25T02:01:21.138242Z","iopub.execute_input":"2023-08-25T02:01:21.138670Z","iopub.status.idle":"2023-08-25T02:01:21.163696Z","shell.execute_reply.started":"2023-08-25T02:01:21.138641Z","shell.execute_reply":"2023-08-25T02:01:21.161857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.head()","metadata":{"id":"5iQb4QAG3HyB","outputId":"2b8afbd9-2c5a-458d-cdf4-2229d62f5370","execution":{"iopub.status.busy":"2023-08-25T02:01:21.165271Z","iopub.execute_input":"2023-08-25T02:01:21.166418Z","iopub.status.idle":"2023-08-25T02:01:21.178844Z","shell.execute_reply.started":"2023-08-25T02:01:21.166370Z","shell.execute_reply":"2023-08-25T02:01:21.177533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 원하는 경로에 .csv 파일을 저장합니다\nsubmission_filepath = \"/kaggle/working/submission.csv\"\nsubmission_df.to_csv(submission_filepath, index=False)","metadata":{"id":"A0zyCB7B3J6T","execution":{"iopub.status.busy":"2023-08-25T02:01:21.180527Z","iopub.execute_input":"2023-08-25T02:01:21.181462Z","iopub.status.idle":"2023-08-25T02:01:21.208580Z","shell.execute_reply.started":"2023-08-25T02:01:21.181415Z","shell.execute_reply":"2023-08-25T02:01:21.207173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 2. Pyplot으로 데이터 시각화하기\n\n*   [Pyplot Tutorial](https://matplotlib.org/stable/tutorials/introductory/pyplot.html)","metadata":{"id":"oDPpco8t3eeR"}},{"cell_type":"code","source":"# Pyplot 갤러리에서 그래프 형태를 찾아 데이터를 시각화해보세요\nfrom IPython.display import IFrame\n\nIFrame(src=\"https://matplotlib.org/stable/plot_types/index.html\", width=900, height=600)","metadata":{"id":"BNCgp8j1nYhD","outputId":"accab043-60f9-4235-f39c-29accb53f3ee","execution":{"iopub.status.busy":"2023-08-25T02:01:21.210492Z","iopub.execute_input":"2023-08-25T02:01:21.211007Z","iopub.status.idle":"2023-08-25T02:01:21.220642Z","shell.execute_reply.started":"2023-08-25T02:01:21.210968Z","shell.execute_reply":"2023-08-25T02:01:21.219338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scatter\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\nplt.style.use('_mpl-gallery')\n\n# make the data\nnp.random.seed(3)\nx = 4 + np.random.normal(0, 2, 24)\ny = 4 + np.random.normal(0, 2, len(x))\n# size and color:\nsizes = np.random.uniform(15, 80, len(x))\ncolors = np.random.uniform(15, 80, len(x))\n\n# plot\nfig, ax = plt.subplots()\n\nax.scatter(x, y, s=sizes, c=colors, vmin=0, vmax=100)\n\nax.set(xlim=(0, 8), xticks=np.arange(1, 8),\n       ylim=(0, 8), yticks=np.arange(1, 8))\n\nplt.show()","metadata":{"id":"iIxCgYEingN6","outputId":"d992197f-cbe5-44cf-855b-23abc7f2e2eb","execution":{"iopub.status.busy":"2023-08-25T02:01:21.222033Z","iopub.execute_input":"2023-08-25T02:01:21.222753Z","iopub.status.idle":"2023-08-25T02:01:21.402254Z","shell.execute_reply.started":"2023-08-25T02:01:21.222719Z","shell.execute_reply":"2023-08-25T02:01:21.401431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pie\nplt.style.use('_mpl-gallery-nogrid')\n\n\n# make data\nx = [1, 2, 3, 4]\ncolors = plt.get_cmap('Blues')(np.linspace(0.2, 0.7, len(x)))\n\n# plot\nfig, ax = plt.subplots()\nax.pie(x, colors=colors, radius=3, center=(4, 4),\n       wedgeprops={\"linewidth\": 1, \"edgecolor\": \"white\"}, frame=True)\n\nax.set(xlim=(0, 8), xticks=np.arange(1, 8),\n       ylim=(0, 8), yticks=np.arange(1, 8))\n\nplt.show()","metadata":{"id":"7O4s2Cq8nx7r","outputId":"1f0e2cd4-78da-42a5-9dad-7058e35d144c","execution":{"iopub.status.busy":"2023-08-25T02:01:21.403369Z","iopub.execute_input":"2023-08-25T02:01:21.404296Z","iopub.status.idle":"2023-08-25T02:01:21.576258Z","shell.execute_reply.started":"2023-08-25T02:01:21.404263Z","shell.execute_reply":"2023-08-25T02:01:21.575466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 3. Scikit-learn Metrics로 모델 다양하게 평가하기\n\n\n\n*   [Scikit-learn Metrics Docs](https://scikit-learn.org/stable/modules/classes.html#module-sklearn.metrics)\n","metadata":{"id":"tok_YUOb4IC2"}},{"cell_type":"markdown","source":"5개의 클래스를 분류하는 문제를 다루고 있다고 가정해봅시다.","metadata":{"id":"M1mgk_t65n0y"}},{"cell_type":"code","source":"!pip install scipy","metadata":{"execution":{"iopub.status.busy":"2023-08-25T02:01:21.577346Z","iopub.execute_input":"2023-08-25T02:01:21.578691Z","iopub.status.idle":"2023-08-25T02:01:33.711878Z","shell.execute_reply.started":"2023-08-25T02:01:21.578636Z","shell.execute_reply":"2023-08-25T02:01:33.710569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 100개의 데이터 샘플에 대해 정답이 다음과 같을 때\nfrom random import randint\n\ntargets = [randint(0, 5) for _ in range(100)]","metadata":{"id":"aet0F2hr5b7y","execution":{"iopub.status.busy":"2023-08-25T02:01:33.713229Z","iopub.execute_input":"2023-08-25T02:01:33.713554Z","iopub.status.idle":"2023-08-25T02:01:33.720378Z","shell.execute_reply.started":"2023-08-25T02:01:33.713523Z","shell.execute_reply":"2023-08-25T02:01:33.718508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 여러분의 모델이 다음과 같이 예측했다고 합시다.\npreds = [randint(0, 5) for _ in range(100)]","metadata":{"id":"MSdx4XAK56RS","execution":{"iopub.status.busy":"2023-08-25T02:01:33.722004Z","iopub.execute_input":"2023-08-25T02:01:33.722449Z","iopub.status.idle":"2023-08-25T02:01:33.740645Z","shell.execute_reply.started":"2023-08-25T02:01:33.722420Z","shell.execute_reply":"2023-08-25T02:01:33.739307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scikit-learn이 제공하는 함수를 이용하면 정확도를 쉽게 계산할 수 있습니다.\nfrom sklearn.metrics import accuracy_score\n\naccuracy = accuracy_score(targets, preds)\naccuracy","metadata":{"id":"oSP9QfrJ5-js","outputId":"520fa0a0-6b05-47c7-9b19-8c953d597f8a","execution":{"iopub.status.busy":"2023-08-25T02:01:33.742460Z","iopub.execute_input":"2023-08-25T02:01:33.742807Z","iopub.status.idle":"2023-08-25T02:01:34.347643Z","shell.execute_reply.started":"2023-08-25T02:01:33.742781Z","shell.execute_reply":"2023-08-25T02:01:34.346648Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Confusion matrix를 계산하고\nfrom sklearn.metrics import confusion_matrix\n\ncmat = confusion_matrix(targets, preds, labels=range(5))\ncmat.shape","metadata":{"id":"uuVQaXQD68c3","outputId":"4fce5d6c-7d6b-4813-e207-668dd19899e3","execution":{"iopub.status.busy":"2023-08-25T02:01:34.348894Z","iopub.execute_input":"2023-08-25T02:01:34.349205Z","iopub.status.idle":"2023-08-25T02:01:34.361023Z","shell.execute_reply.started":"2023-08-25T02:01:34.349180Z","shell.execute_reply":"2023-08-25T02:01:34.359900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 쉽게 표현할 수 있습니다. 어떤 클래스를 잘 틀리는지 확인하기 수월하겠죠?\nfrom sklearn.metrics import ConfusionMatrixDisplay\n\ncm_display = ConfusionMatrixDisplay(cmat, display_labels=range(5))\ncm_display.plot()","metadata":{"id":"kNctVXHN7Fnv","outputId":"aa34f6c1-0fe0-4dde-ec78-1f4a0c2ee767","execution":{"iopub.status.busy":"2023-08-25T02:01:34.362635Z","iopub.execute_input":"2023-08-25T02:01:34.363051Z","iopub.status.idle":"2023-08-25T02:01:34.642108Z","shell.execute_reply.started":"2023-08-25T02:01:34.363016Z","shell.execute_reply":"2023-08-25T02:01:34.640480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 4. Tensorboard로 훈련 과정 모니터링하기\n\n*   [PyTorch SummaryWriter](v)\n","metadata":{"id":"FHy8K0pn9-rc"}},{"cell_type":"code","source":"# 패키지 설치하기\n# !pip install jupyter-tensorboard","metadata":{"id":"YniWpqBzoKnT","outputId":"3b5ce017-c6b0-4405-ee08-35dd41983819","execution":{"iopub.status.busy":"2023-08-25T02:01:34.644181Z","iopub.execute_input":"2023-08-25T02:01:34.644938Z","iopub.status.idle":"2023-08-25T02:01:34.649263Z","shell.execute_reply.started":"2023-08-25T02:01:34.644900Z","shell.execute_reply":"2023-08-25T02:01:34.648121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 익스텐션 로드하기\n%load_ext tensorboard","metadata":{"id":"7sq8HoqzoYQg","outputId":"2f99544a-2633-4c8c-d3a5-884ae1c7954d","execution":{"iopub.status.busy":"2023-08-25T02:01:34.650950Z","iopub.execute_input":"2023-08-25T02:01:34.651374Z","iopub.status.idle":"2023-08-25T02:01:34.690364Z","shell.execute_reply.started":"2023-08-25T02:01:34.651340Z","shell.execute_reply":"2023-08-25T02:01:34.688526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 텐서보드 실행하기\n%tensorboard --logdir ./runs/","metadata":{"id":"tmPbWlfPoh8C","outputId":"54d5c228-e068-472c-8dd2-78cbfe4820d1","execution":{"iopub.status.busy":"2023-08-25T02:01:34.720012Z","iopub.execute_input":"2023-08-25T02:01:34.720516Z","iopub.status.idle":"2023-08-25T02:01:47.251195Z","shell.execute_reply.started":"2023-08-25T02:01:34.720483Z","shell.execute_reply":"2023-08-25T02:01:47.249932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 텐서보드 활용 예시\nimport math\n# PyTorch에서 텐서보드에 기록하기 위한 클래스\nfrom torch.utils.tensorboard import SummaryWriter\n\nnum_epochs = 100\nnum_samples = 30\n\n# 다음과 같이 어떤 모델을 훈련하여 train/valid loss를 얻는다고 가정해봅시다\n# add_scalar 함수를 활용하여 값을 기록할 수 있습니다\nwriter = SummaryWriter()\n\nfor epoch in range(num_epochs):\n    print(\"Epoch %d\" % (epoch + 1))\n    num_steps = (epoch + 1) * num_samples\n\n    train_loss = 1 / (epoch + 1)\n    print(\"    Training loss: %.4f\" % train_loss)\n    writer.add_scalar(\"train/loss\", train_loss, num_steps)\n\n    valid_loss = 1 / (epoch + 1) + 0.5\n    print(\"    Validation loss: %.4f\" % valid_loss)\n    writer.add_scalar(\"valid/loss\", valid_loss, num_steps)\n\nwriter.close()","metadata":{"id":"AfHIVuIdoePv","outputId":"510ebd1c-8d8d-45ea-bcf9-cd35a6574bba","execution":{"iopub.status.busy":"2023-08-25T02:01:47.252330Z","iopub.execute_input":"2023-08-25T02:01:47.252592Z","iopub.status.idle":"2023-08-25T02:01:53.156023Z","shell.execute_reply.started":"2023-08-25T02:01:47.252570Z","shell.execute_reply":"2023-08-25T02:01:53.155061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 5. 그 외 패키지\n\n\n\n*   Einops\n*   Weights & Biases\n*   PyTorch Lightning\n*   PyTorch Ignite\n*   Torchmetrics\n*   HuggingFace (transformers, evaluate, etc.)\n\n","metadata":{"id":"IZVTzAbNPBox"}}]}