data-science-ipython-notebooks/deep-learning/tensor-flow-tutorials/1_tensorflow_basic.ipynb

206 lines
6.2 KiB
Python
Raw Normal View History

2016-05-15 12:07:33 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-07-06 12:09:44 +08:00
"> This work comes from [LearningTensorFlow.com](http://learningtensorflow.com/), developed by [dataPipeline](http://datapipeline.co.au/), with whom the copyright remains. \n",
"For more tutorials and extended exercises, please see the website."
2016-05-15 12:07:33 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2016-07-06 12:09:44 +08:00
"# What does TensorFlow do?"
2016-05-15 12:07:33 +08:00
]
},
{
"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": [
"Lets 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": [
2016-07-06 12:09:44 +08:00
"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. If you arent familiar with python, create a new text file called `basic_script.py`, and copy that code in. Save it on your computer and run it with:\n",
"\n",
"```bash\n",
"python basic_script.py\n",
"```\n",
"\n",
"Note that the path (i.e. `basic_script.py`) must reference the file, so if it is in the Code folder, you use:\n",
"\n",
"```\n",
"python Code/basic_script.py\n",
"```\n",
"\n",
"Also, make sure you have activated the Anaconda virtual environment. On Linux, this will make your prompt look something like:\n",
"\n",
"```\n",
"(tensorenv)username@computername:~$\n",
"```\n",
"\n",
"If that is working, lets convert it to a TensorFlow equivalent."
2016-05-15 12:07:33 +08:00
]
},
{
"cell_type": "code",
2016-07-06 12:09:44 +08:00
"execution_count": 1,
2016-05-15 12:07:33 +08:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2016-07-06 12:09:44 +08:00
"<tensorflow.python.ops.variables.Variable object at 0x10f2fe850>\n"
2016-05-15 12:07:33 +08:00
]
}
],
"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": [
2016-07-06 12:09:44 +08:00
"After running this, youll get quite a funny output, something like ```<tensorflow.python.ops.variables.Variable object at 0x10f2fe850>```. This is clearly not the value 40.\n",
2016-05-15 12:07:33 +08:00
"\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",
2016-07-06 12:09:44 +08:00
"- Import the tensorflow module and call it `tf`\n",
2016-05-15 12:07:33 +08:00
"- 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 isnt 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",
2016-07-06 12:09:44 +08:00
"\n",
"Lets fix that:"
2016-05-15 12:07:33 +08:00
]
},
{
"cell_type": "code",
2016-07-06 12:09:44 +08:00
"execution_count": 2,
2016-05-15 12:07:33 +08:00
"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": [
2016-07-06 12:09:44 +08:00
"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",
2016-05-15 12:07:33 +08:00
"\n",
2016-07-06 12:09:44 +08:00
"1. Import the tensorflow module and call it `tf`\n",
2016-05-15 12:07:33 +08:00
"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",
2016-07-06 12:09:44 +08:00
"4. Initialize the variables with `initialize_all_variables` (we will go into more detail on this)\n",
2016-05-15 12:07:33 +08:00
"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 isnt computed until step 7, as up until then, only equations and relations are computed."
]
}
],
"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": {
2016-07-06 12:09:44 +08:00
"toc_cell": false,
"toc_number_sections": false,
2016-05-15 12:07:33 +08:00
"toc_section_display": "none",
"toc_threshold": "8",
"toc_window_display": true
2016-07-06 12:09:44 +08:00
},
"toc_position": {
"height": "86px",
"left": "1331.45px",
"right": "20px",
"top": "120px",
"width": "164px"
2016-05-15 12:07:33 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 0
}