Skip to content

Instantly share code, notes, and snippets.

@karaage0703
Created December 14, 2017 22:24
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 karaage0703/35d87e52e1650b691d8a70ad22f67437 to your computer and use it in GitHub Desktop.
Save karaage0703/35d87e52e1650b691d8a70ad22f67437 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3グループをx[0],x[1],x[2]と定義"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = [9376,3600,11442]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"x[0]がx[1]にa[0]貰う、x[0]がx[2]にa[1]貰う、x[1]がx[2]にa[2]貰うと定義"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"a = [0,0,0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"関数をf(a)と定義"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def f(a):\n",
" x_a = [0,0,0]\n",
" x_a[0] = x[0] - a[0] - a[1]\n",
" x_a[1] = x[1] + a[0] - a[2]\n",
" x_a[2] = x[2] + a[1] + a[2]\n",
" return abs(x_a[0] - x_a[1]) + abs(x_a[1] - x_a[2]) + abs(x_a[0] - x_a[2])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"学習係数を eta"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"eta = [-0.01, -0.01, -0.01]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"勾配法による勾配計算"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def numerical_gradient(f, x):\n",
" h = 1e-4 # 0.0001\n",
" grad = [0,0,0]\n",
"\n",
" for idx in range(len(x)):\n",
" tmp_val = x[idx]\n",
" x[idx] = tmp_val + h\n",
" fxh1 = f(x)\n",
"\n",
" x[idx] = tmp_val - h \n",
" fxh2 = f(x)\n",
"\n",
" grad[idx] = (fxh1 - fxh2) / (2*h)\n",
" x[idx] = tmp_val\n",
"\n",
" return [x * y for (x, y) in zip(eta, grad)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"勾配法による学習"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1923.7000044771776, -688.6599949750234, -2612.359998543616]\n"
]
}
],
"source": [
"while f(a) > 10:\n",
" tmp = numerical_gradient(f, a)\n",
" a = [x + y for (x, y) in zip(tmp, a)]\n",
"print(a)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8140.959990497846\n",
"8136.060003020793\n",
"8140.980006481361\n"
]
}
],
"source": [
"print(x[0] - a[0] - a[1])\n",
"print(x[1] + a[0] - a[2])\n",
"print(x[2] + a[1] + a[2])"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment