|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# 01 TensorFlow Basics" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": { |
| 14 | + "collapsed": true |
| 15 | + }, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "# import\n", |
| 19 | + "import tensorflow as tf" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "cell_type": "markdown", |
| 24 | + "metadata": {}, |
| 25 | + "source": [ |
| 26 | + "### Tensors\n", |
| 27 | + "```\n", |
| 28 | + "Tensor is an n-dimensional matrix\n", |
| 29 | + "0-d tensor: scalar (number)\n", |
| 30 | + "1-d tensor: vector\n", |
| 31 | + "2-d tensor: matrix\n", |
| 32 | + "...\n", |
| 33 | + "```" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "markdown", |
| 38 | + "metadata": {}, |
| 39 | + "source": [ |
| 40 | + "### Tensor's Rank\n", |
| 41 | + "```\n", |
| 42 | + "The number of dimensions in a tensor.\n", |
| 43 | + "[]: a rank 0 tensor\n", |
| 44 | + "[1,2,3]: a rank 1 tensor - a vector with shape [3]\n", |
| 45 | + "[[1,2,3], [4,5,6]]: a rank 2 tensor- matrix with shape [2,3]\n", |
| 46 | + "[[[1,2,3]], [[7,8,9]]]: a rank 3 tensor shape [2,1,3]\n", |
| 47 | + "```" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "metadata": {}, |
| 53 | + "source": [ |
| 54 | + "### Computational Graph\n", |
| 55 | + "``` \n", |
| 56 | + "Series of tensorflow operations arranged into graph of nodes. To actually evaluate the nodes, we must run the computational graph within a session. A session encapsulates the control and state of the TensorFlow runtime.```" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": 2, |
| 62 | + "metadata": { |
| 63 | + "collapsed": false |
| 64 | + }, |
| 65 | + "outputs": [ |
| 66 | + { |
| 67 | + "name": "stdout", |
| 68 | + "output_type": "stream", |
| 69 | + "text": [ |
| 70 | + "b'TensorFlow Playground'\n" |
| 71 | + ] |
| 72 | + } |
| 73 | + ], |
| 74 | + "source": [ |
| 75 | + "#add a constant to the graph\n", |
| 76 | + "hello = tf.constant(\"TensorFlow Playground\")\n", |
| 77 | + "\n", |
| 78 | + "#create tf session\n", |
| 79 | + "sess = tf.Session()\n", |
| 80 | + "\n", |
| 81 | + "#run the session\n", |
| 82 | + "print(sess.run(hello))" |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + "cell_type": "code", |
| 87 | + "execution_count": 3, |
| 88 | + "metadata": { |
| 89 | + "collapsed": false |
| 90 | + }, |
| 91 | + "outputs": [ |
| 92 | + { |
| 93 | + "name": "stdout", |
| 94 | + "output_type": "stream", |
| 95 | + "text": [ |
| 96 | + "3.0\n", |
| 97 | + "5 <dtype: 'int32'>\n" |
| 98 | + ] |
| 99 | + } |
| 100 | + ], |
| 101 | + "source": [ |
| 102 | + "#tf.constant\n", |
| 103 | + "a = tf.constant(3.0, tf.float32) #to specify a constant right away\n", |
| 104 | + "b = tf.constant(5)\n", |
| 105 | + "\n", |
| 106 | + "sess = tf.Session()\n", |
| 107 | + "\n", |
| 108 | + "print(sess.run(a))\n", |
| 109 | + "print(sess.run(b), b.dtype)" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": 4, |
| 115 | + "metadata": { |
| 116 | + "collapsed": false |
| 117 | + }, |
| 118 | + "outputs": [ |
| 119 | + { |
| 120 | + "name": "stdout", |
| 121 | + "output_type": "stream", |
| 122 | + "text": [ |
| 123 | + "mat1 shape: (1, 2)\n", |
| 124 | + "mat2 shape: (2, 1)\n", |
| 125 | + "[[-3.]]\n", |
| 126 | + "finally shape (1, 1)\n" |
| 127 | + ] |
| 128 | + } |
| 129 | + ], |
| 130 | + "source": [ |
| 131 | + "#tf.constant for matrix multiplications\n", |
| 132 | + "mat1 = tf.constant([[6., 0.]])\n", |
| 133 | + "print(\"mat1 shape:\", mat1.shape)\n", |
| 134 | + "\n", |
| 135 | + "mat2 = tf.constant([[-0.5], [9]])\n", |
| 136 | + "print(\"mat2 shape:\", mat2.shape)\n", |
| 137 | + "\n", |
| 138 | + "with tf.Session() as sess:\n", |
| 139 | + " prod = tf.matmul(mat1, mat2)\n", |
| 140 | + " print(sess.run(prod))\n", |
| 141 | + " print(\"finally shape\", prod.shape)" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": 5, |
| 147 | + "metadata": { |
| 148 | + "collapsed": false |
| 149 | + }, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "name": "stdout", |
| 153 | + "output_type": "stream", |
| 154 | + "text": [ |
| 155 | + "8.0\n", |
| 156 | + "30.0\n" |
| 157 | + ] |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "#tf.placeholder\n", |
| 162 | + "#to specify a placeholder and value will be provided later\n", |
| 163 | + "c = tf.placeholder(tf.float32) \n", |
| 164 | + "d = tf.placeholder(tf.float32)\n", |
| 165 | + "\n", |
| 166 | + "#operation \n", |
| 167 | + "addition = tf.add(c,d)\n", |
| 168 | + "product = tf.multiply(c,d)\n", |
| 169 | + "\n", |
| 170 | + "sess = tf.Session()\n", |
| 171 | + "\n", |
| 172 | + "print(sess.run(addition, feed_dict={c: 10, d: -2}))\n", |
| 173 | + "print(sess.run(product, {c: 25, d: 1.2}))" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": 6, |
| 179 | + "metadata": { |
| 180 | + "collapsed": false |
| 181 | + }, |
| 182 | + "outputs": [ |
| 183 | + { |
| 184 | + "name": "stdout", |
| 185 | + "output_type": "stream", |
| 186 | + "text": [ |
| 187 | + "[ 0. 0.30000001 0.60000002 0.90000004]\n", |
| 188 | + "23.66\n" |
| 189 | + ] |
| 190 | + } |
| 191 | + ], |
| 192 | + "source": [ |
| 193 | + "#tf.Variables allow us to add trainable parameters to a graph. \n", |
| 194 | + "#They are constructed with a type and initial value:\n", |
| 195 | + "w = tf.Variable([.3], tf.float32)\n", |
| 196 | + "b = tf.Variable([-.3], tf.float32)\n", |
| 197 | + "x = tf.placeholder(tf.float32)\n", |
| 198 | + "\n", |
| 199 | + "model = w*x + b\n", |
| 200 | + "\n", |
| 201 | + "#To initialize all the variables in a TensorFlow program, you must explicitly call a special operation as follows:\n", |
| 202 | + "init = tf.global_variables_initializer()\n", |
| 203 | + "sess = tf.Session()\n", |
| 204 | + "sess.run(init)\n", |
| 205 | + "\n", |
| 206 | + "#Since x is a placeholder, we can evaluate linear_model for several values of x simultaneously as follows:\n", |
| 207 | + "print(sess.run(model, {x: [1,2,3,4]}))\n", |
| 208 | + "\n", |
| 209 | + "#We've created a model, but we don't know how good it is yet. \n", |
| 210 | + "#To evaluate the model on training data, we need a y placeholder to provide the desired values, \n", |
| 211 | + "#and we need to write a loss function.\n", |
| 212 | + "y = tf.placeholder(tf.float32)\n", |
| 213 | + "\n", |
| 214 | + "#squaring the error\n", |
| 215 | + "squared_deltas = tf.square(model - y)\n", |
| 216 | + "\n", |
| 217 | + "#sum all the sqaured errors\n", |
| 218 | + "loss = tf.reduce_sum(squared_deltas)\n", |
| 219 | + "\n", |
| 220 | + "print(sess.run(loss, {x:[1,2,3,4], y:[0,-1,-2,-3]}))\n", |
| 221 | + "\n", |
| 222 | + "sess.close()" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "execution_count": 7, |
| 228 | + "metadata": { |
| 229 | + "collapsed": false |
| 230 | + }, |
| 231 | + "outputs": [ |
| 232 | + { |
| 233 | + "name": "stdout", |
| 234 | + "output_type": "stream", |
| 235 | + "text": [ |
| 236 | + "0.0\n" |
| 237 | + ] |
| 238 | + } |
| 239 | + ], |
| 240 | + "source": [ |
| 241 | + "#after getting the optimal parameteres we can assign the final optimal values to our tf.Variable using tf.assign\n", |
| 242 | + "fixw = tf.assign(w, [-1])\n", |
| 243 | + "fixb = tf.assign(b, [1])\n", |
| 244 | + "\n", |
| 245 | + "sess = tf.Session()\n", |
| 246 | + "sess.run([fixw, fixb])\n", |
| 247 | + "\n", |
| 248 | + "print(sess.run(loss,{x:[1,2,3,4], y:[0,-1,-2,-3]}))\n", |
| 249 | + "\n", |
| 250 | + "sess.close()" |
| 251 | + ] |
| 252 | + }, |
| 253 | + { |
| 254 | + "cell_type": "markdown", |
| 255 | + "metadata": {}, |
| 256 | + "source": [ |
| 257 | + "### complete program" |
| 258 | + ] |
| 259 | + }, |
| 260 | + { |
| 261 | + "cell_type": "code", |
| 262 | + "execution_count": 8, |
| 263 | + "metadata": { |
| 264 | + "collapsed": false |
| 265 | + }, |
| 266 | + "outputs": [ |
| 267 | + { |
| 268 | + "name": "stdout", |
| 269 | + "output_type": "stream", |
| 270 | + "text": [ |
| 271 | + "w: [-0.99999791] b: [ 0.99999392] loss: 2.52847e-11\n" |
| 272 | + ] |
| 273 | + } |
| 274 | + ], |
| 275 | + "source": [ |
| 276 | + "#imports\n", |
| 277 | + "import numpy as np\n", |
| 278 | + "import tensorflow as tf\n", |
| 279 | + "\n", |
| 280 | + "#model parameters\n", |
| 281 | + "w = tf.Variable([.3], tf.float32)\n", |
| 282 | + "b = tf.Variable([.3], tf.float32)\n", |
| 283 | + "\n", |
| 284 | + "#model input and output\n", |
| 285 | + "x = tf.placeholder(tf.float32)\n", |
| 286 | + "model = w * x + b\n", |
| 287 | + "y = tf.placeholder(tf.float32)\n", |
| 288 | + "\n", |
| 289 | + "#loss\n", |
| 290 | + "loss = tf.reduce_sum(tf.square(model-y))\n", |
| 291 | + "\n", |
| 292 | + "#optimiser\n", |
| 293 | + "optimiser = tf.train.GradientDescentOptimizer(0.01)\n", |
| 294 | + "train = optimiser.minimize(loss)\n", |
| 295 | + "\n", |
| 296 | + "#trainings data\n", |
| 297 | + "x_train = [1,2,3,4]\n", |
| 298 | + "y_train = [0,-1,-2,-3]\n", |
| 299 | + "\n", |
| 300 | + "#initialise the variables\n", |
| 301 | + "init = tf.global_variables_initializer()\n", |
| 302 | + "sess = tf.Session()\n", |
| 303 | + "sess.run(init)\n", |
| 304 | + "\n", |
| 305 | + "#training loop\n", |
| 306 | + "for i in range(1000):\n", |
| 307 | + " sess.run(train, {x:x_train, y:y_train})\n", |
| 308 | + "\n", |
| 309 | + "#accuracy\n", |
| 310 | + "final_w, final_b, final_loss = sess.run([w,b,loss], {x:x_train, y:y_train})\n", |
| 311 | + "print(\"w: %s b: %s loss: %s\" %(final_w, final_b, final_loss))\n", |
| 312 | + "sess.close()" |
| 313 | + ] |
| 314 | + } |
| 315 | + ], |
| 316 | + "metadata": { |
| 317 | + "kernelspec": { |
| 318 | + "display_name": "Python 3", |
| 319 | + "language": "python", |
| 320 | + "name": "python3" |
| 321 | + }, |
| 322 | + "language_info": { |
| 323 | + "codemirror_mode": { |
| 324 | + "name": "ipython", |
| 325 | + "version": 3 |
| 326 | + }, |
| 327 | + "file_extension": ".py", |
| 328 | + "mimetype": "text/x-python", |
| 329 | + "name": "python", |
| 330 | + "nbconvert_exporter": "python", |
| 331 | + "pygments_lexer": "ipython3", |
| 332 | + "version": "3.4.5" |
| 333 | + } |
| 334 | + }, |
| 335 | + "nbformat": 4, |
| 336 | + "nbformat_minor": 2 |
| 337 | +} |
0 commit comments