Skip to content

Instantly share code, notes, and snippets.

@kldarek
Last active November 1, 2020 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kldarek/18229d27134dc2e5059ec279ea947897 to your computer and use it in GitHub Desktop.
Save kldarek/18229d27134dc2e5059ec279ea947897 to your computer and use it in GitHub Desktop.
NLLLoss.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "NLLLoss.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyPNbbN5m3BcJNXChLQy58Ln",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/kldarek/18229d27134dc2e5059ec279ea947897/nllloss.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "6O5plo8xR0W7"
},
"source": [
"import torch\n",
"import torch.nn.functional as F\n",
"from torch import tensor"
],
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GLfr3cldR7pY",
"outputId": "c56734b4-9cd7-4889-ea19-73c90843a911",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"source": [
"sm_acts = tensor([[0.7189, 0.1380, 0.1431],\n",
"[0.0443, 0.1951, 0.7606],\n",
"[0.5092, 0.3590, 0.1318],\n",
"[0.1392, 0.1421, 0.7187],\n",
"[0.0353, 0.6423, 0.3224],\n",
"[0.2209, 0.1252, 0.6540]])\n",
"sm_acts"
],
"execution_count": 10,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[0.7189, 0.1380, 0.1431],\n",
" [0.0443, 0.1951, 0.7606],\n",
" [0.5092, 0.3590, 0.1318],\n",
" [0.1392, 0.1421, 0.7187],\n",
" [0.0353, 0.6423, 0.3224],\n",
" [0.2209, 0.1252, 0.6540]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 10
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "TNqmjMAHSq6z",
"outputId": "00026101-5612-444b-b85a-ecbb20a2f4c8",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"source": [
"log = torch.log(sm_acts)\n",
"log"
],
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([[-0.3300, -1.9805, -1.9442],\n",
" [-3.1168, -1.6342, -0.2736],\n",
" [-0.6749, -1.0244, -2.0265],\n",
" [-1.9718, -1.9512, -0.3303],\n",
" [-3.3439, -0.4427, -1.1320],\n",
" [-1.5100, -2.0778, -0.4246]])"
]
},
"metadata": {
"tags": []
},
"execution_count": 18
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "x62029iUSHFd",
"outputId": "c95ca8fb-4c2b-42f2-e89f-6a0e4036bb2f",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"source": [
"targ = tensor([0, 0, 1, 1, 2, 2])\n",
"F.nll_loss(log, targ, reduction='none')"
],
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"tensor([0.3300, 3.1168, 1.0244, 1.9512, 1.1320, 0.4246])"
]
},
"metadata": {
"tags": []
},
"execution_count": 19
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "1fGAraInSO8f"
},
"source": [
"F.nll_loss??"
],
"execution_count": 12,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "U0yPn5AeSb8t"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment