Last active
May 11, 2018 07:19
-
-
Save RenSys/4b6c4b0abda338d6580e1aca6946c183 to your computer and use it in GitHub Desktop.
TensorFlow - Basic Minimisation with Gradient Descent
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "import numpy as np\nimport tensorflow as tf", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "/home/karl/anaconda2/envs/py36-test/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n from ._conv import register_converters as _register_converters\n", | |
"name": "stderr" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### Using Gradient Descent, find the value of w when\nW**2 - 10*W + 25 = 0" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "w = tf.Variable(0, dtype=tf.float32)", | |
"execution_count": 25, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "cost = tf.add( tf.add(w**2, tf.multiply(-10.0, w )), 25)\ntrain = tf.train.GradientDescentOptimizer(0.01).minimize(cost)\ninit = tf.global_variables_initializer()\nsession = tf.Session()\nsession.run(init)\nprint(session.run(w))\n", | |
"execution_count": 46, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "0.0\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### Perform 1 round of Gradient Descent" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "session.run(train)\nprint(session.run(w))", | |
"execution_count": 47, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "0.099999994\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### Perform 1000 round of Gradient Descent" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "for i in range(1_000):\n session.run(train)\nprint(session.run(w))", | |
"execution_count": 48, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "4.9999886\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### Test" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "5**2 - 10*5 +25", | |
"execution_count": 53, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 53, | |
"data": { | |
"text/plain": "0" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "### Overload Version" | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "x = tf.Variable(0, dtype=tf.float32)\ncost = w**2 - 10*w + 25\ntrain = tf.train.GradientDescentOptimizer(0.01).minimize(cost)\ninit = tf.global_variables_initializer()\nsession = tf.Session()\nsession.run(init)\nprint(session.run(w))", | |
"execution_count": 56, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "0.0\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "for i in range(1_000):\n session.run(train)\nprint(session.run(w))", | |
"execution_count": 57, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "4.9999886\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "py36-test", | |
"display_name": "py36-test", | |
"language": "python" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": "block", | |
"toc_window_display": false | |
}, | |
"language_info": { | |
"name": "python", | |
"version": "3.6.4", | |
"mimetype": "text/x-python", | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"pygments_lexer": "ipython3", | |
"nbconvert_exporter": "python", | |
"file_extension": ".py" | |
}, | |
"gist": { | |
"id": "4b6c4b0abda338d6580e1aca6946c183", | |
"data": { | |
"description": "TensorFlow - Basic Minimisation with Gradient Descent", | |
"public": true | |
} | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/4b6c4b0abda338d6580e1aca6946c183" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment