Add files via upload

patch-1
Artem Nikonorov 2 years ago committed by GitHub
parent b428ebc380
commit 627c363ef3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,176 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 27,
"id": "753d746a",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "a0449fde",
"metadata": {},
"outputs": [],
"source": [
"def softmax(s, y):\n",
" l_i = -np.log(np.exp(s[y])/sum(np.exp(s)))\n",
" return l_i\n",
"\n",
"\n",
"\n",
"def svm(s, y):\n",
" m = np.maximum(np.zeros_like(s), s - s[y] + 1)\n",
" m[y] = 0 \n",
" l_i = np.sum(m)\n",
" return l_i\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "7531b4de",
"metadata": {},
"outputs": [],
"source": [
"s = np.array([[10, -2, 3],\n",
" [10, 9, 9],\n",
" [10, -100, -100]])\n",
"\n",
"y = 0\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "c1bf4f3b",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"softmax:\n",
"\n",
"0.0009176050495943237\n",
"0.5514447139320511\n",
"-0.0\n",
"\n",
"SVM:\n",
"\n",
"0\n",
"0\n",
"0\n"
]
}
],
"source": [
"print(\"softmax:\\n\")\n",
"for i in range(s.shape[0]):\n",
" print(softmax(s[i,:], y))\n",
"\n",
"print(\"\\nSVM:\\n\")\n",
"for i in range(s.shape[0]):\n",
" print(svm(s[i,:], y))\n"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "49662904",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 20, -2, 3],\n",
" [ 10, 9, 9],\n",
" [ 10, -100, -100]])"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s[0,0] += 10\n",
"s"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "3c85dfef",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"softmax:\n",
"\n",
"4.167832299541146e-08\n",
"0.5514447139320511\n",
"-0.0\n",
"\n",
"SVM:\n",
"\n",
"0\n",
"0\n",
"0\n"
]
}
],
"source": [
"print(\"softmax:\\n\")\n",
"for i in range(s.shape[0]):\n",
" print(softmax(s[i,:], y))\n",
"\n",
"print(\"\\nSVM:\\n\")\n",
"for i in range(s.shape[0]):\n",
" print(svm(s[i,:], y))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9bac1fcb",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading…
Cancel
Save