├── CatsOrDogsClassification.ipynb ├── Tensorflow101.ipynb └── Tensorflow2.0.ipynb /Tensorflow101.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Tensorflow101.ipynb", 7 | "version": "0.3.2", 8 | "provenance": [] 9 | }, 10 | "kernelspec": { 11 | "display_name": "Python 3", 12 | "language": "python", 13 | "name": "python3" 14 | } 15 | }, 16 | "cells": [ 17 | { 18 | "metadata": { 19 | "id": "dsMHujL4WeNP", 20 | "colab_type": "code", 21 | "colab": {}, 22 | "outputId": "af8879d1-5071-44c0-d51f-6b12327f55fe" 23 | }, 24 | "cell_type": "code", 25 | "source": [ 26 | "import numpy as np\n", 27 | "import random\n", 28 | "import matplotlib.pyplot as plt\n", 29 | "import tensorflow as tf" 30 | ], 31 | "execution_count": 0, 32 | "outputs": [ 33 | { 34 | "output_type": "stream", 35 | "text": [ 36 | "/anaconda3/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", 37 | " from ._conv import register_converters as _register_converters\n" 38 | ], 39 | "name": "stderr" 40 | } 41 | ] 42 | }, 43 | { 44 | "metadata": { 45 | "scrolled": false, 46 | "id": "w4nTZthhWeNX", 47 | "colab_type": "code", 48 | "colab": {}, 49 | "outputId": "b2d5c1d0-37f6-4b21-d589-56da60be98bc" 50 | }, 51 | "cell_type": "code", 52 | "source": [ 53 | "%matplotlib notebook\n", 54 | "random.seed(9)\n", 55 | "\n", 56 | "x_data = np.array([-3,-1,1,3])\n", 57 | "t_data = np.array([3,4.5,4.5,5.5])\n", 58 | "\n", 59 | "m_init = random.uniform(-1,1)\n", 60 | "c_init = 0\n", 61 | "\n", 62 | "x_input = tf.placeholder(dtype=tf.float32, shape=[None])\n", 63 | "m = tf.Variable(m_init, dtype=tf.float32, name=\"m\")\n", 64 | "c = tf.Variable(c_init, dtype=tf.float32, name=\"c\")\n", 65 | "\n", 66 | "print(x_input)\n", 67 | "print(m)\n", 68 | "print(c)\n", 69 | "\n", 70 | "mx = tf.multiply(m,x_input)\n", 71 | "y_model = tf.add(mx,c)\n", 72 | "\n", 73 | "with tf.Session() as sess:\n", 74 | " sess.run(tf.global_variables_initializer())\n", 75 | " output = sess.run(y_model, feed_dict={x_input: x_data})\n", 76 | "print(output)\n", 77 | "\n", 78 | "y_init = m_init*x_data + c_init\n", 79 | "print(y_init)\n", 80 | "\n", 81 | "fig = plt.figure()\n", 82 | "plt.scatter(x_data, t_data)\n", 83 | "plt.plot(x_data, y_init)\n", 84 | "fig.canvas.draw()\n", 85 | "\n", 86 | "target = tf.placeholder(dtype=tf.float32, shape=[None])\n", 87 | "error = tf.square(tf.subtract(y_model, target))\n", 88 | "loss = tf.reduce_sum(error)\n", 89 | "\n", 90 | "optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)\n", 91 | "train_step = optimizer.minimize(loss)\n", 92 | "\n", 93 | "iterations = 100\n", 94 | "with tf.Session() as sess:\n", 95 | " sess.run(tf.global_variables_initializer())\n", 96 | " for i in range(iterations):\n", 97 | " _, y_next = sess.run([train_step, y_model], feed_dict={x_input: x_data, target: t_data})\n", 98 | "\n", 99 | "# y_next = sess.run(y_model, feed_dict={x_input: x_data, target: t_data})\n", 100 | "\n", 101 | " line, = plt.plot(x_data, y_next)\n", 102 | " fig.canvas.draw()\n", 103 | " line.remove()\n", 104 | " \n", 105 | "plt.plot(x_data, y_next)\n", 106 | "fig.canvas.draw()\n" 107 | ], 108 | "execution_count": 0, 109 | "outputs": [ 110 | { 111 | "output_type": "stream", 112 | "text": [ 113 | "Tensor(\"Placeholder:0\", shape=(?,), dtype=float32)\n", 114 | "\n", 115 | "\n", 116 | "[ 0.22195587 0.07398529 -0.07398529 -0.22195587]\n", 117 | "[ 0.22195585 0.07398528 -0.07398528 -0.22195585]\n" 118 | ], 119 | "name": "stdout" 120 | }, 121 | { 122 | "output_type": "display_data", 123 | "data": { 124 | "application/javascript": [ 125 | "/* Put everything inside the global mpl namespace */\n", 126 | "window.mpl = {};\n", 127 | "\n", 128 | "\n", 129 | "mpl.get_websocket_type = function() {\n", 130 | " if (typeof(WebSocket) !== 'undefined') {\n", 131 | " return WebSocket;\n", 132 | " } else if (typeof(MozWebSocket) !== 'undefined') {\n", 133 | " return MozWebSocket;\n", 134 | " } else {\n", 135 | " alert('Your browser does not have WebSocket support.' +\n", 136 | " 'Please try Chrome, Safari or Firefox ≥ 6. ' +\n", 137 | " 'Firefox 4 and 5 are also supported but you ' +\n", 138 | " 'have to enable WebSockets in about:config.');\n", 139 | " };\n", 140 | "}\n", 141 | "\n", 142 | "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n", 143 | " this.id = figure_id;\n", 144 | "\n", 145 | " this.ws = websocket;\n", 146 | "\n", 147 | " this.supports_binary = (this.ws.binaryType != undefined);\n", 148 | "\n", 149 | " if (!this.supports_binary) {\n", 150 | " var warnings = document.getElementById(\"mpl-warnings\");\n", 151 | " if (warnings) {\n", 152 | " warnings.style.display = 'block';\n", 153 | " warnings.textContent = (\n", 154 | " \"This browser does not support binary websocket messages. \" +\n", 155 | " \"Performance may be slow.\");\n", 156 | " }\n", 157 | " }\n", 158 | "\n", 159 | " this.imageObj = new Image();\n", 160 | "\n", 161 | " this.context = undefined;\n", 162 | " this.message = undefined;\n", 163 | " this.canvas = undefined;\n", 164 | " this.rubberband_canvas = undefined;\n", 165 | " this.rubberband_context = undefined;\n", 166 | " this.format_dropdown = undefined;\n", 167 | "\n", 168 | " this.image_mode = 'full';\n", 169 | "\n", 170 | " this.root = $('
');\n", 171 | " this._root_extra_style(this.root)\n", 172 | " this.root.attr('style', 'display: inline-block');\n", 173 | "\n", 174 | " $(parent_element).append(this.root);\n", 175 | "\n", 176 | " this._init_header(this);\n", 177 | " this._init_canvas(this);\n", 178 | " this._init_toolbar(this);\n", 179 | "\n", 180 | " var fig = this;\n", 181 | "\n", 182 | " this.waiting = false;\n", 183 | "\n", 184 | " this.ws.onopen = function () {\n", 185 | " fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n", 186 | " fig.send_message(\"send_image_mode\", {});\n", 187 | " if (mpl.ratio != 1) {\n", 188 | " fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n", 189 | " }\n", 190 | " fig.send_message(\"refresh\", {});\n", 191 | " }\n", 192 | "\n", 193 | " this.imageObj.onload = function() {\n", 194 | " if (fig.image_mode == 'full') {\n", 195 | " // Full images could contain transparency (where diff images\n", 196 | " // almost always do), so we need to clear the canvas so that\n", 197 | " // there is no ghosting.\n", 198 | " fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n", 199 | " }\n", 200 | " fig.context.drawImage(fig.imageObj, 0, 0);\n", 201 | " };\n", 202 | "\n", 203 | " this.imageObj.onunload = function() {\n", 204 | " fig.ws.close();\n", 205 | " }\n", 206 | "\n", 207 | " this.ws.onmessage = this._make_on_message_function(this);\n", 208 | "\n", 209 | " this.ondownload = ondownload;\n", 210 | "}\n", 211 | "\n", 212 | "mpl.figure.prototype._init_header = function() {\n", 213 | " var titlebar = $(\n", 214 | " '
');\n", 216 | " var titletext = $(\n", 217 | " '
');\n", 219 | " titlebar.append(titletext)\n", 220 | " this.root.append(titlebar);\n", 221 | " this.header = titletext[0];\n", 222 | "}\n", 223 | "\n", 224 | "\n", 225 | "\n", 226 | "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n", 227 | "\n", 228 | "}\n", 229 | "\n", 230 | "\n", 231 | "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n", 232 | "\n", 233 | "}\n", 234 | "\n", 235 | "mpl.figure.prototype._init_canvas = function() {\n", 236 | " var fig = this;\n", 237 | "\n", 238 | " var canvas_div = $('
');\n", 239 | "\n", 240 | " canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n", 241 | "\n", 242 | " function canvas_keyboard_event(event) {\n", 243 | " return fig.key_event(event, event['data']);\n", 244 | " }\n", 245 | "\n", 246 | " canvas_div.keydown('key_press', canvas_keyboard_event);\n", 247 | " canvas_div.keyup('key_release', canvas_keyboard_event);\n", 248 | " this.canvas_div = canvas_div\n", 249 | " this._canvas_extra_style(canvas_div)\n", 250 | " this.root.append(canvas_div);\n", 251 | "\n", 252 | " var canvas = $('');\n", 253 | " canvas.addClass('mpl-canvas');\n", 254 | " canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n", 255 | "\n", 256 | " this.canvas = canvas[0];\n", 257 | " this.context = canvas[0].getContext(\"2d\");\n", 258 | "\n", 259 | " var backingStore = this.context.backingStorePixelRatio ||\n", 260 | "\tthis.context.webkitBackingStorePixelRatio ||\n", 261 | "\tthis.context.mozBackingStorePixelRatio ||\n", 262 | "\tthis.context.msBackingStorePixelRatio ||\n", 263 | "\tthis.context.oBackingStorePixelRatio ||\n", 264 | "\tthis.context.backingStorePixelRatio || 1;\n", 265 | "\n", 266 | " mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n", 267 | "\n", 268 | " var rubberband = $('');\n", 269 | " rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n", 270 | "\n", 271 | " var pass_mouse_events = true;\n", 272 | "\n", 273 | " canvas_div.resizable({\n", 274 | " start: function(event, ui) {\n", 275 | " pass_mouse_events = false;\n", 276 | " },\n", 277 | " resize: function(event, ui) {\n", 278 | " fig.request_resize(ui.size.width, ui.size.height);\n", 279 | " },\n", 280 | " stop: function(event, ui) {\n", 281 | " pass_mouse_events = true;\n", 282 | " fig.request_resize(ui.size.width, ui.size.height);\n", 283 | " },\n", 284 | " });\n", 285 | "\n", 286 | " function mouse_event_fn(event) {\n", 287 | " if (pass_mouse_events)\n", 288 | " return fig.mouse_event(event, event['data']);\n", 289 | " }\n", 290 | "\n", 291 | " rubberband.mousedown('button_press', mouse_event_fn);\n", 292 | " rubberband.mouseup('button_release', mouse_event_fn);\n", 293 | " // Throttle sequential mouse events to 1 every 20ms.\n", 294 | " rubberband.mousemove('motion_notify', mouse_event_fn);\n", 295 | "\n", 296 | " rubberband.mouseenter('figure_enter', mouse_event_fn);\n", 297 | " rubberband.mouseleave('figure_leave', mouse_event_fn);\n", 298 | "\n", 299 | " canvas_div.on(\"wheel\", function (event) {\n", 300 | " event = event.originalEvent;\n", 301 | " event['data'] = 'scroll'\n", 302 | " if (event.deltaY < 0) {\n", 303 | " event.step = 1;\n", 304 | " } else {\n", 305 | " event.step = -1;\n", 306 | " }\n", 307 | " mouse_event_fn(event);\n", 308 | " });\n", 309 | "\n", 310 | " canvas_div.append(canvas);\n", 311 | " canvas_div.append(rubberband);\n", 312 | "\n", 313 | " this.rubberband = rubberband;\n", 314 | " this.rubberband_canvas = rubberband[0];\n", 315 | " this.rubberband_context = rubberband[0].getContext(\"2d\");\n", 316 | " this.rubberband_context.strokeStyle = \"#000000\";\n", 317 | "\n", 318 | " this._resize_canvas = function(width, height) {\n", 319 | " // Keep the size of the canvas, canvas container, and rubber band\n", 320 | " // canvas in synch.\n", 321 | " canvas_div.css('width', width)\n", 322 | " canvas_div.css('height', height)\n", 323 | "\n", 324 | " canvas.attr('width', width * mpl.ratio);\n", 325 | " canvas.attr('height', height * mpl.ratio);\n", 326 | " canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n", 327 | "\n", 328 | " rubberband.attr('width', width);\n", 329 | " rubberband.attr('height', height);\n", 330 | " }\n", 331 | "\n", 332 | " // Set the figure to an initial 600x600px, this will subsequently be updated\n", 333 | " // upon first draw.\n", 334 | " this._resize_canvas(600, 600);\n", 335 | "\n", 336 | " // Disable right mouse context menu.\n", 337 | " $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n", 338 | " return false;\n", 339 | " });\n", 340 | "\n", 341 | " function set_focus () {\n", 342 | " canvas.focus();\n", 343 | " canvas_div.focus();\n", 344 | " }\n", 345 | "\n", 346 | " window.setTimeout(set_focus, 100);\n", 347 | "}\n", 348 | "\n", 349 | "mpl.figure.prototype._init_toolbar = function() {\n", 350 | " var fig = this;\n", 351 | "\n", 352 | " var nav_element = $('
')\n", 353 | " nav_element.attr('style', 'width: 100%');\n", 354 | " this.root.append(nav_element);\n", 355 | "\n", 356 | " // Define a callback function for later on.\n", 357 | " function toolbar_event(event) {\n", 358 | " return fig.toolbar_button_onclick(event['data']);\n", 359 | " }\n", 360 | " function toolbar_mouse_event(event) {\n", 361 | " return fig.toolbar_button_onmouseover(event['data']);\n", 362 | " }\n", 363 | "\n", 364 | " for(var toolbar_ind in mpl.toolbar_items) {\n", 365 | " var name = mpl.toolbar_items[toolbar_ind][0];\n", 366 | " var tooltip = mpl.toolbar_items[toolbar_ind][1];\n", 367 | " var image = mpl.toolbar_items[toolbar_ind][2];\n", 368 | " var method_name = mpl.toolbar_items[toolbar_ind][3];\n", 369 | "\n", 370 | " if (!name) {\n", 371 | " // put a spacer in here.\n", 372 | " continue;\n", 373 | " }\n", 374 | " var button = $('