Created
May 23, 2024 12:50
-
-
Save dsaint31x/e631a1584f04703d428e3c034e6c63b1 to your computer and use it in GitHub Desktop.
dl_classification_loss_activation.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyNfWqHsTiUaC9SiATfQckFu", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/dsaint31x/e631a1584f04703d428e3c034e6c63b1/dl_classification_loss_activation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"\n", | |
"torch.set_printoptions(precision=2)" | |
], | |
"metadata": { | |
"id": "8Dk3a9k9VCe2" | |
}, | |
"execution_count": 29, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "lmh9W4ypRv-n", | |
"outputId": "12f20f62-9021-4523-cbc6-ec172a6322d7" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Sigmoid outputs: tensor([0.88, 0.27, 0.62])\n" | |
] | |
} | |
], | |
"source": [ | |
"import torch\n", | |
"# import torch.nn.functional as F\n", | |
"\n", | |
"# 원시 출력값 (logits)\n", | |
"logits = torch.tensor([2.0, -1.0, 0.5])\n", | |
"\n", | |
"# Sigmoid 적용\n", | |
"sigmoid_outputs = torch.sigmoid(logits)\n", | |
"\n", | |
"# Print Output\n", | |
"print('Sigmoid outputs:', sigmoid_outputs)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"import torch.nn.functional as F\n", | |
"\n", | |
"\n", | |
"# 원시 출력값 (logits)\n", | |
"logits = torch.tensor([2.0, 1.0, 0.1])\n", | |
"\n", | |
"# Softmax 적용\n", | |
"softmax_outputs = F.softmax(logits, dim=0)\n", | |
"print(f'{softmax_outputs=}')" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "FA4lK-wyRzRo", | |
"outputId": "1a56c26d-f97d-4542-a557-9a468ecf5e89" | |
}, | |
"execution_count": 31, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"softmax_outputs=tensor([0.66, 0.24, 0.10])\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"# Softmax 모듈 초기화 (dim=-1은 텐서의 마지막 차원을 기준으로 softmax를 적용)\n", | |
"softmax = torch.nn.Softmax(dim=-1)\n", | |
"\n", | |
"\n", | |
"# 원시 출력값 (logits)\n", | |
"logits = torch.tensor([2.0, 1.0, 0.1])\n", | |
"\n", | |
"# Softmax 적용.\n", | |
"softmax_outputs = softmax(logits)\n", | |
"print(f'{softmax_outputs=}')" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "bqb0gA26UfbE", | |
"outputId": "1e048dce-6b56-4a3a-eb09-51033e8a73a4" | |
}, | |
"execution_count": 32, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"softmax_outputs=tensor([0.66, 0.24, 0.10])\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch.nn as nn\n", | |
"\n", | |
"# 예측값과 실제값\n", | |
"outputs = torch.tensor([0.7, 0.2, 0.9], requires_grad=True).float()\n", | |
"targets = torch.tensor([1.0, 0.0, 1.0]).float()\n", | |
"\n", | |
"# BCELoss 선언\n", | |
"criterion = nn.BCELoss()\n", | |
"\n", | |
"# 시그모이드 활성화 적용\n", | |
"outputs = torch.sigmoid(outputs)\n", | |
"\n", | |
"# 손실 계산\n", | |
"loss = criterion(outputs, targets)\n", | |
"print('BCELoss:', loss.item())" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "5kJYII7gU9Lt", | |
"outputId": "9e71592d-5c7a-47ba-91d9-0001a5ab388e" | |
}, | |
"execution_count": 33, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"BCELoss: 0.514159619808197\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"import torch.nn as nn\n", | |
"\n", | |
"# 원시 출력값 (시그모이드 활성화 적용 전 raw score)\n", | |
"raw_outputs = torch.tensor([0.5, -1.0, 2.0], requires_grad=True)\n", | |
"targets = torch.tensor([1.0, 0.0, 1.0])\n", | |
"\n", | |
"# BCEWithLogitsLoss 선언\n", | |
"criterion = nn.BCEWithLogitsLoss()\n", | |
"\n", | |
"# 손실 계산 (시그모이드 내부 적용)\n", | |
"loss = criterion(raw_outputs, targets)\n", | |
"print(f'BCEWithLogitsLoss: {loss.item():.4f}')" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "GIrTV8P_ZfAN", | |
"outputId": "bb8c69b8-5b6d-4d0e-c951-c03babab1c5d" | |
}, | |
"execution_count": 34, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"BCEWithLogitsLoss: 0.3048\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# 원시 출력값 (softmax 활성화 적용 전)\n", | |
"raw_outputs = torch.tensor(\n", | |
"\t[[1.0, 2.0, 3.0],\n", | |
" [1.0, 2.0, 0.0],\n", | |
" [0.0, 2.0, 1.0]],\n", | |
" requires_grad=True).float()\n", | |
"targets = torch.tensor([2, 0, 1]).long() # 각 샘플의 클래스 인덱스\n", | |
"print(targets.dtype)\n", | |
"# CrossEntropyLoss 선언\n", | |
"criterion = nn.CrossEntropyLoss()\n", | |
"\n", | |
"# 손실 계산 (softmax 내부 적용)\n", | |
"loss = criterion(raw_outputs, targets)\n", | |
"print(f'CrossEntropyLoss: {loss.item():.2f}')" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "-UYH_zVka0t3", | |
"outputId": "1dbb2a0a-0326-41b6-b375-7963eb59c263" | |
}, | |
"execution_count": 35, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"torch.int64\n", | |
"CrossEntropyLoss: 0.74\n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import torch\n", | |
"import torch.nn\n", | |
"\n", | |
"# 모델 출력\n", | |
"logits = torch.tensor(\n", | |
"\t[[0.1, 0.2, 0.7], # max idx 2\n", | |
" \t [0.8, 0.1, 0.1], # max idx 0\n", | |
" \t [0.3, 0.5, 0.2]], # max idx 1\n", | |
" \trequires_grad=True).float()\n", | |
"\n", | |
"# 타겟 클래스 인덱스\n", | |
"targets = torch.tensor([2, 0, 1]).long()\n", | |
"\n", | |
"# 로그 소프트맥스 적용\n", | |
"log_softmax = F.log_softmax(logits, dim=1)\n", | |
"\n", | |
"# NLLLoss 선언\n", | |
"criterion = nn.NLLLoss()\n", | |
"loss = criterion(log_softmax, targets)\n", | |
"print(f'NLLLoss with LogSoftmax: {loss.item():.2f}')" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "EJZExLiBbsyC", | |
"outputId": "c4e878a9-e60e-4aa5-c48b-64959243b80c" | |
}, | |
"execution_count": 36, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"NLLLoss with LogSoftmax: 0.80\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment