mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
349 lines
9.0 KiB
Python
349 lines
9.0 KiB
Python
|
{
|
|||
|
"cells": [
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"toc": "true"
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"# Table of Contents\n",
|
|||
|
" <p><div class=\"lev1\"><a href=\"#What-does-TensorFlow-do?\"><span class=\"toc-item-num\">1 </span>What does TensorFlow do?</a></div><div class=\"lev1\"><a href=\"#Exercises\"><span class=\"toc-item-num\">2 </span>Exercises</a></div><div class=\"lev2\"><a href=\"#Exercise-1\"><span class=\"toc-item-num\">2.1 </span>Exercise 1</a></div><div class=\"lev2\"><a href=\"#Exercise-2\"><span class=\"toc-item-num\">2.2 </span>Exercise 2</a></div><div class=\"lev2\"><a href=\"#Exercise-3\"><span class=\"toc-item-num\">2.3 </span>Exercise 3</a></div>"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"# What does TensorFlow do?"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"- http://learningtensorflow.com/lesson2/"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"TensorFlow is a way of representing computation without actually performing it until asked. In this sense, it is a form of lazy computing, and it allows for some great improvements to the running of code:\n",
|
|||
|
"\n",
|
|||
|
"- Faster computation of complex variables\n",
|
|||
|
"- Distributed computation across multiple systems, including GPUs.\n",
|
|||
|
"- Reduced redundency in some computations"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"Let’s have a look at this in action. First, a very basic python script:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 1,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"40\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"x = 35\n",
|
|||
|
"y = x + 5\n",
|
|||
|
"print(y)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"This script basically just says “create a variable x with value 35, set the value of a new variable y to that plus 5, which is currently 40, and print it out”. The value 40 will print out when you run this program."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 2,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"<tensorflow.python.ops.variables.Variable object at 0x10ae81890>\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import tensorflow as tf\n",
|
|||
|
"\n",
|
|||
|
"x = tf.constant(35, name='x')\n",
|
|||
|
"y = tf.Variable(x + 5, name='y')\n",
|
|||
|
"\n",
|
|||
|
"print(y)"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"After running this, you’ll get quite a funny output, something like ```<tensorflow.python.ops.variables.Variable object at 0x7f074bfd9ef0>```. This is clearly not the value 40.\n",
|
|||
|
"\n",
|
|||
|
"The reason why, is that our program actually does something quite different to the previous one. The code here does the following:\n",
|
|||
|
"\n",
|
|||
|
"- Import the tensorflow module and call it tf\n",
|
|||
|
"- Create a constant value called x, and give it the numerical value 35\n",
|
|||
|
"- Create a Variable called y, and define it as being the equation x + 5\n",
|
|||
|
"- Print out the equation object for y\n",
|
|||
|
"\n",
|
|||
|
"The subtle difference is that y isn’t given “the current value of x + 5” as in our previous program. Instead, it is effectively an equation that means “when this variable is computed, take the value of x (as it is then) and add 5 to it”. The computation of the value of y is never actually performed in the above program.\n",
|
|||
|
"\n"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 3,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"40\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import tensorflow as tf\n",
|
|||
|
"\n",
|
|||
|
"x = tf.constant(35, name='x')\n",
|
|||
|
"y = tf.Variable(x + 5, name='y')\n",
|
|||
|
"\n",
|
|||
|
"model = tf.initialize_all_variables()\n",
|
|||
|
"\n",
|
|||
|
"with tf.Session() as session:\n",
|
|||
|
" session.run(model)\n",
|
|||
|
" print(session.run(y))"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"We have removed the print(y) statement, and instead we have code that creates a session, and actually computes the value of y. This is quite a bit of boilerplate, but it works like this:\n",
|
|||
|
"\n",
|
|||
|
"1. Import the tensorflow module and call it tf\n",
|
|||
|
"2. Create a constant value called x, and give it the numerical value 35\n",
|
|||
|
"3. Create a Variable called y, and define it as being the equation x + 5\n",
|
|||
|
"4. Initialize the variables with initialize_all_variables (we will go into more detail on this)\n",
|
|||
|
"5. Create a session for computing the values\n",
|
|||
|
"6. Run the model created in 4\n",
|
|||
|
"7. Run just the variable y and print out its current value\n",
|
|||
|
"\n",
|
|||
|
"The step 4 above is where some magic happens. In this step, a graph is created of the dependencies between the variables. In this case, the variable y depends on the variable x, and that value is transformed by adding 5 to it. Keep in mind that this value isn’t computed until step 7, as up until then, only equations and relations are computed."
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"# Exercises"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Exercise 1\n",
|
|||
|
"\n",
|
|||
|
"- Constants can also be arrays. Predict what this code will do, then run it to confirm:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"[40 45 50]\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import tensorflow as tf\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"x = tf.constant([35, 40, 45], name='x')\n",
|
|||
|
"y = tf.Variable(x + 5, name='y')\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"model = tf.initialize_all_variables()\n",
|
|||
|
"\n",
|
|||
|
"with tf.Session() as session:\n",
|
|||
|
" session.run(model)\n",
|
|||
|
" print(session.run(y))"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"## Exercise 2\n",
|
|||
|
"- Generate a NumPy array of 10,000 random numbers (called x) and create a Variable storing the equation"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {},
|
|||
|
"source": [
|
|||
|
"$$y = 5x^2 - 3x + 15$$\n",
|
|||
|
"\n",
|
|||
|
"You can generate the NumPy array using the following code:"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 5,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"data": {
|
|||
|
"text/plain": [
|
|||
|
"array([136, 612, 947, ..., 205, 238, 803])"
|
|||
|
]
|
|||
|
},
|
|||
|
"execution_count": 5,
|
|||
|
"metadata": {},
|
|||
|
"output_type": "execute_result"
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import numpy as np\n",
|
|||
|
"data = np.random.randint(1000, size=10000)\n",
|
|||
|
"data"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 6,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"[ 92087 1870899 4481219 ..., 209525 282521 3221651]\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import tensorflow as tf\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"x = tf.constant(data, name='x')\n",
|
|||
|
"y = tf.Variable(5*(x**2) - (3*x) + 15, name='y')\n",
|
|||
|
"\n",
|
|||
|
"\n",
|
|||
|
"model = tf.initialize_all_variables()\n",
|
|||
|
"\n",
|
|||
|
"with tf.Session() as session:\n",
|
|||
|
" session.run(model)\n",
|
|||
|
" print(session.run(y))"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "markdown",
|
|||
|
"metadata": {
|
|||
|
"collapsed": true
|
|||
|
},
|
|||
|
"source": [
|
|||
|
"## Exercise 3\n",
|
|||
|
"- You can also update variables in loops, which we will use later for machine learning. Take a look at this code, and predict what it will do (then run it to check):"
|
|||
|
]
|
|||
|
},
|
|||
|
{
|
|||
|
"cell_type": "code",
|
|||
|
"execution_count": 4,
|
|||
|
"metadata": {
|
|||
|
"collapsed": false
|
|||
|
},
|
|||
|
"outputs": [
|
|||
|
{
|
|||
|
"name": "stdout",
|
|||
|
"output_type": "stream",
|
|||
|
"text": [
|
|||
|
"1\n",
|
|||
|
"2\n",
|
|||
|
"3\n",
|
|||
|
"4\n",
|
|||
|
"5\n"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"source": [
|
|||
|
"import tensorflow as tf\n",
|
|||
|
"\n",
|
|||
|
"x = tf.Variable(0, name='x')\n",
|
|||
|
"\n",
|
|||
|
"model = tf.initialize_all_variables()\n",
|
|||
|
"\n",
|
|||
|
"with tf.Session() as session:\n",
|
|||
|
" for i in range(5):\n",
|
|||
|
" session.run(model)\n",
|
|||
|
" x = x + 1\n",
|
|||
|
" print(session.run(x))"
|
|||
|
]
|
|||
|
}
|
|||
|
],
|
|||
|
"metadata": {
|
|||
|
"kernelspec": {
|
|||
|
"display_name": "Python 2",
|
|||
|
"language": "python",
|
|||
|
"name": "python2"
|
|||
|
},
|
|||
|
"language_info": {
|
|||
|
"codemirror_mode": {
|
|||
|
"name": "ipython",
|
|||
|
"version": 2
|
|||
|
},
|
|||
|
"file_extension": ".py",
|
|||
|
"mimetype": "text/x-python",
|
|||
|
"name": "python",
|
|||
|
"nbconvert_exporter": "python",
|
|||
|
"pygments_lexer": "ipython2",
|
|||
|
"version": "2.7.11"
|
|||
|
},
|
|||
|
"toc": {
|
|||
|
"toc_cell": true,
|
|||
|
"toc_number_sections": true,
|
|||
|
"toc_section_display": "none",
|
|||
|
"toc_threshold": "8",
|
|||
|
"toc_window_display": true
|
|||
|
}
|
|||
|
},
|
|||
|
"nbformat": 4,
|
|||
|
"nbformat_minor": 0
|
|||
|
}
|