Skip to content

Instantly share code, notes, and snippets.

@butsugiri
Created March 20, 2019 05:52
Show Gist options
  • Save butsugiri/a9f003e1ceaa8955ce3faeb7dfa6f71a to your computer and use it in GitHub Desktop.
Save butsugiri/a9f003e1ceaa8955ce3faeb7dfa6f71a to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import numpy.random as rnd\n",
"import numpy as np\n",
"import matplotlib\n",
"import seaborn as sns\n",
"import numpy as np\n",
"import chainer\n",
"from chainer import cuda, Variable\n",
"from chainer import Chain\n",
"import chainer.functions as F\n",
"import chainer.links as L\n",
"import chainer.optimizers as O"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"logit = Variable(np.random.randn(5, 3))\n",
"label1 = np.asarray([0,1,2,1,2], 'i')\n",
"label2 = np.asarray([0,-1, -1, -1, -1], 'i')\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"variable(1.62515638)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"F.softmax_cross_entropy(x=logit, t=label1, normalize=False)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"variable(0.81395807)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"F.softmax_cross_entropy(x=logit, t=label2, normalize=False, ignore_label=-1)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"variable(4.06979035)"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"F.softmax_cross_entropy(x=logit, t=label2, normalize=True, ignore_label=-1)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"variable(4.06979035)"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"-F.log(F.softmax(logit)[0])[0]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"variable([4.06979035, 0. , 0. , 0. , 0. ])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"F.softmax_cross_entropy(x=logit, t=label2, normalize=False, ignore_label=-1, reduce='no')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment