data-science-ipython-notebooks/deep-learning/tensor-flow-tutorials/3_Placeholders.ipynb

203 lines
170 KiB
Python
Raw Normal View History

2016-05-15 12:07:33 +08:00
{
"cells": [
2016-07-06 12:09:44 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> 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": [
"# Placeholders"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"So far we have used **Variables** to manage our data, but there is a more basic structure, the **placeholder**. A **placeholder** is simply a variable that we will assign data to at a later date. It allows us to create our operations and build our computation graph, without needing the data. In **TensorFlow** terminology, we then feed data into the graph through these placeholders.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 2. 4. 6.]\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"\n",
"x = tf.placeholder(\"float\", 3)\n",
"y = x * 2\n",
"\n",
"with tf.Session() as session:\n",
" result = session.run(y, feed_dict={x: [1, 2, 3]})\n",
" print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This example works a little differently from our previous ones, lets break it down.\n",
"\n",
"First, we import **tensorflow** as normal. Then we create a **placeholder** called x, i.e. a place in memory where we will store value later on. The 3 on this line denotes that we will store three values in this placeholder.\n",
"\n",
"Then, we create a Tensor called, which is the operation of multiplying **x** by 2. Note that we havent defined any initial values for x yet.\n",
"\n",
"We now have an operation (**y**) defined, and can now run it in a session. We create a session object, and then run just the y variable. Note that this means, that if we defined a much larger graph of operations, we can run just a small segment of the graph. This subgraph evaluation is actually a bit selling point of TensorFlow, and one that isnt present in many other libraries that do similar things.\n",
"\n",
"Running **y** requires knowledge about the values of **x**. We define these inside the **feed_dict** argument to run. We state here that the values of x are **[1, 2, 3]**. We run y, giving us the result of **[2, 4, 6]**.\n",
"\n",
"Placeholders do not need to be statically sized. Lets update our program to allow x to take on any length. Change the definition of x to be:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"x = tf.placeholder(\"float\", None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now, when we define the values of **x** in the **feed_dict** we can have any number of values. The code should still work, and give the same answer, but now it will also work with any number of values in **feed_dict**.\n",
"\n",
"Placeholders can also have multiple dimensions, allowing for storing arrays. In the following example, we create a 3 by 2 matrix, and store some numbers in it. We then use the same operation as before to do element-wise doubling of the numbers.\n",
"\n"
]
},
{
"cell_type": "code",
2016-07-06 12:09:44 +08:00
"execution_count": 3,
2016-05-15 12:07:33 +08:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 2. 4. 6.]\n",
" [ 8. 10. 12.]]\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"\n",
"x = tf.placeholder(\"float\", [None, 3])\n",
"y = x * 2\n",
"\n",
"with tf.Session() as session:\n",
" x_data = [[1, 2, 3],\n",
" [4, 5, 6],]\n",
" result = session.run(y, feed_dict={x: x_data})\n",
" print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first dimension of the placeholder is **None**, meaning we can have any number of rows. The second dimension is fixed at 3, meaning each row needs to have three columns of data.\n",
"\n",
"We can extend this to take an arbitrary number of **None** dimensions. In this example, we load up the image from our last lesson, then create a placeholder that stores a slice of that image. The slice is a 2D segment of the image, but each “pixel” has three components (red, green, blue). Therefore, we need **None** for the first two dimensions, but need **3** (or **None** would work) for the last dimension. We then use TensorFlows **slice** method to take a subsegment out of the image to operate on."
]
},
{
"cell_type": "code",
2016-07-06 12:09:44 +08:00
"execution_count": 4,
2016-05-15 12:07:33 +08:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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gdmhraOCcR\nEQ8uKFUN3in1oCIEz6DyOBzeVwjg3ABfnSR3t1PXyziW8GHA8VMrOBVyB+DJnaX+XaRsOA/OE7MS\nFWLOls6nTKeQxTHbOIiIcOXQTa7INbbmN4HIgX0rSO0RlM31Dba3J1zYusyb/8o3c+LB48zWh2wk\npQotHFXGJw4wuXGe2bRjpTpAIjOLcy5fvojkDe4/cZID9UE++anPo6MVjr1ulbBZ052b0TXXWbtl\nwvhPneDInUc45EdcvaAc2n+cN/7Ym7jrxAFWl4/yDX/+LRy5tJ8uJlKKpKw4J9SVZ1A5c3jBAghk\nKq+GyEW5+75DaP9PM0nLa2iiyx1djDQ5kjWScjSnJpDFnF3KRlf0yxvYtf4sfbXE09ZaxoKOsrP2\nsmB4T5WEsv/Y0PZSvy777VP2Vc5qzjPnr3CKmgXNSs7gfGBQ1dShwntP5TxSMp4UBcmBWuxhwdqZ\nMxUh50gXW9puTtvO6GLD/fcdZjabMplssbG9zeZ0TtcpXdYFfeOcA+dwYgH7j7JnHfH9p5oTj3Me\n76oSvQpQV7eA6SjkrKRs6YJD0KSFgwBEzSFBD9FICpFsG1ClIDCLrnGXU0tqTq5HzK96mf0u5fJV\nIUX7HiDrABVHVodFX09KZXGQCtRXuhwRakrcteeqFMdoz+8RHmWRsVhkfeRW1AkHbq0BO19BUM04\n8WUBGUqzY8soDqcZnAWD7HJBjRbdBaGqoXbCIHijGoLDhYQLjkqNr0p5mdXmr/Km8EruueUA4aBn\n4/qclSMjti92+NcEmjjnymDG7159N2v5X4NuolmJEXxPQyAkdXQZ5hlCLsemypf/xQq3LK9wtZkz\nPSiw3hC6bW5qYGW0jzB
"text/plain": [
2016-07-06 12:09:44 +08:00
"<matplotlib.figure.Figure at 0x10f37e550>"
2016-05-15 12:07:33 +08:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%matplotlib inline\n",
"import tensorflow as tf\n",
"import matplotlib.image as mpimg\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# First, load the image again\n",
"filename = \"images/MarshOrchid.jpg\"\n",
"raw_image_data = mpimg.imread(filename)\n",
"\n",
"image = tf.placeholder(\"uint8\", [None, None, 3])\n",
"slice = tf.slice(image, [1000, 0, 0], [3000, -1, -1])\n",
"\n",
"with tf.Session() as session:\n",
" result = session.run(slice, feed_dict={image: raw_image_data})\n",
" print(result.shape)\n",
"\n",
"plt.imshow(result)\n",
"plt.show()"
]
}
],
"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": false,
"toc_number_sections": false,
"toc_threshold": "8",
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 0
}